From 248f6bcff74893500dd666be6d34ca5d81c68691 Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Sun, 15 Nov 2026 10:38:48 -0800 Subject: [PATCH 1/5] feat(review): add versioned JSON comparison reports --- Cargo.toml | 1 + README.md | 14 +- docs/diff-report-v1.schema.json | 206 +++++++++++ docs/diff-reports.md | 185 ++++++++++ src/bin/klamath.rs | 146 +++++++- src/review.rs | 144 +++++++- src/review/changes.rs | 9 + src/review/report.rs | 385 ++++++++++++++++++++ src/review/selection.rs | 24 +- tests/golden/report-equal.json | 78 ++++ tests/golden/report-failed.json | 64 ++++ tests/golden/report-geometry.json | 360 ++++++++++++++++++ tests/golden/report-incomplete.json | 105 ++++++ tests/golden/report-metadata.json | 87 +++++ tests/review.rs | 2 + tests/review/reports.rs | 545 ++++++++++++++++++++++++++++ 16 files changed, 2312 insertions(+), 43 deletions(-) create mode 100644 docs/diff-report-v1.schema.json create mode 100644 docs/diff-reports.md create mode 100644 src/review/report.rs create mode 100644 tests/golden/report-equal.json create mode 100644 tests/golden/report-failed.json create mode 100644 tests/golden/report-geometry.json create mode 100644 tests/golden/report-incomplete.json create mode 100644 tests/golden/report-metadata.json create mode 100644 tests/review/reports.rs diff --git a/Cargo.toml b/Cargo.toml index e201109..b08e0a5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ crate-type = ["cdylib", "rlib"] [dependencies] byteorder = "^1" arrow = {version = "^54", features = ["ffi"]} +serde_json = "1" [profile.release] diff --git a/README.md b/README.md index 2eb673a..937b41c 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ schema, offset definitions, and ID assignment. ## Layout review CLI -The review CLI uses the existing Rust dependencies: +The review CLI is a standalone Rust executable: ```sh cargo build --bin klamath @@ -67,6 +67,8 @@ target/debug/klamath diff old.gds new.gds --hierarchy mermaid --max-changes 20 target/debug/klamath hierarchy chip.gds --format mermaid --depth 4 target/debug/klamath diff old.gds new.gds --top CHIP --layer 1/0 target/debug/klamath diff old.gds new.gds --show-layer 1/0 +target/debug/klamath diff old.gds new.gds --format json +target/debug/klamath diff old.gds new.gds --format json --hierarchy graph target/debug/klamath name-layers chip.gds named.gds --cell CHIP --carrier 900/7 --name 1:0:WG ``` @@ -98,6 +100,16 @@ and **2** for invalid input or incomplete comparison. `--max-changes` limits the number of object details displayed, not the comparison. Source offsets are included in changed-object reports for tracing changes back to record dumps. +`diff --format json` emits a versioned report with typed before/after evidence, +structured diagnostics, and independent comparison status and completeness. +JSON includes all changes by default. Explicit display filters and `--max-changes` +record omitted evidence without changing totals or exit status. `--hierarchy graph` +adds the complete scoped reference graph, with symbolic array multiplicities; +`--depth` applies to text output only. Timestamp-only changes still return 0. +Input and argument errors also produce JSON when requested. See the +[report contract](docs/diff-reports.md) and +[version 1 JSON Schema](docs/diff-report-v1.schema.json). + Unmatched objects are paired for explanation only when candidates are unique by type/layer/bounding box, then by type/layer among the remaining objects. References additionally match by target. Ambiguous groups stay additions and diff --git a/docs/diff-report-v1.schema.json b/docs/diff-report-v1.schema.json new file mode 100644 index 0000000..c47d031 --- /dev/null +++ b/docs/diff-report-v1.schema.json @@ -0,0 +1,206 @@ +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "Klamath diff report version 1", + "description": "Object-profile comparison evidence, not a layout serialization. Unknown additional fields are permitted for compatible extensions.", + "type": "object", + "required": ["schema_version", "kind", "producer", "comparison_profile", "inputs", "scope", "coordinates", "status", "result", "exit_code", "summary", "report_selection", "layer_legend", "library_changes", "layer_name_changes", "cell_changes", "hierarchy", "diagnostics"], + "properties": { + "schema_version": {"const": 1}, + "kind": {"const": "diff"}, + "producer": { + "type": "object", "required": ["name", "version"], + "properties": {"name": {"const": "klamath"}, "version": {"type": "string"}} + }, + "comparison_profile": {"type": "string"}, + "inputs": { + "type": "object", "required": ["old", "new"], + "properties": {"old": {"$ref": "#/$defs/input"}, "new": {"$ref": "#/$defs/input"}} + }, + "scope": { + "type": "object", "required": ["top", "cell", "layer"], + "properties": {"top": {"type": ["string", "null"]}, "cell": {"type": ["string", "null"]}, "layer": {"$ref": "#/$defs/nullableLayer"}} + }, + "coordinates": { + "type": "object", "required": ["unit", "frame", "bounds"], + "properties": {"unit": {"const": "database_unit"}, "frame": {"const": "cell_local"}, "bounds": {"const": "coordinate_envelope"}} + }, + "status": {"enum": ["complete", "incomplete", "failed"]}, + "result": {"enum": ["equal", "different", "unknown"]}, + "exit_code": {"enum": [0, 1, 2]}, + "summary": {"oneOf": [{"type": "null"}, {"$ref": "#/$defs/summary"}]}, + "report_selection": {"$ref": "#/$defs/selection"}, + "layer_legend": { + "type": "array", "items": { + "type": "object", "required": ["layer", "name"], + "properties": {"layer": {"$ref": "#/$defs/layer"}, "name": {"type": "string"}} + } + }, + "library_changes": {"type": "array", "items": {"$ref": "#/$defs/libraryChange"}}, + "layer_name_changes": { + "type": "array", "items": { + "type": "object", "required": ["layer", "before", "after"], + "properties": {"layer": {"$ref": "#/$defs/layer"}, "before": {"type": ["string", "null"]}, "after": {"type": ["string", "null"]}} + } + }, + "cell_changes": {"type": "array", "items": {"$ref": "#/$defs/cellChange"}}, + "hierarchy": {"oneOf": [{"type": "null"}, {"$ref": "#/$defs/graph"}]}, + "diagnostics": {"type": "array", "items": {"$ref": "#/$defs/diagnostic"}} + }, + "allOf": [ + { + "if": {"properties": {"status": {"const": "complete"}}}, + "then": { + "properties": {"result": {"enum": ["equal", "different"]}, "summary": {"$ref": "#/$defs/summary", "properties": {"counts": {"const": "exact"}}}, "diagnostics": {"maxItems": 0}}, + "if": {"properties": {"result": {"const": "equal"}}}, + "then": {"properties": {"exit_code": {"const": 0}}}, + "else": {"properties": {"exit_code": {"const": 1}}} + }, + "else": {"properties": {"exit_code": {"const": 2}, "diagnostics": {"minItems": 1}}} + }, + { + "if": {"properties": {"status": {"const": "incomplete"}}}, + "then": {"properties": {"result": {"enum": ["different", "unknown"]}, "summary": {"$ref": "#/$defs/summary", "properties": {"counts": {"const": "observed"}}}}} + }, + { + "if": {"properties": {"status": {"const": "failed"}}}, + "then": {"properties": {"result": {"const": "unknown"}, "summary": {"type": "null"}, "library_changes": {"maxItems": 0}, "layer_name_changes": {"maxItems": 0}, "cell_changes": {"maxItems": 0}, "hierarchy": {"type": "null"}}} + } + ], + "$defs": { + "decimal": {"type": "string", "pattern": "^(0|[1-9][0-9]*)$"}, + "hex": {"type": "string", "pattern": "^0x([0-9a-f]{2})*$"}, + "uint16": {"type": "integer", "minimum": 0, "maximum": 65535}, + "int32": {"type": "integer", "minimum": -2147483648, "maximum": 2147483647}, + "layer": { + "type": "object", "required": ["number", "purpose"], + "properties": {"number": {"$ref": "#/$defs/uint16"}, "purpose": {"$ref": "#/$defs/uint16"}} + }, + "nullableLayer": {"oneOf": [{"type": "null"}, {"$ref": "#/$defs/layer"}]}, + "point": {"type": "array", "minItems": 2, "maxItems": 2, "items": {"$ref": "#/$defs/int32"}}, + "fields": {"type": "array", "items": {"$ref": "#/$defs/field"}}, + "field": { + "type": "object", "required": ["tag", "name", "encoding", "value", "payload_hex"], + "properties": { + "tag": {"$ref": "#/$defs/uint16"}, "name": {"type": "string"}, + "encoding": {"enum": ["xy", "uint16", "int16", "int32", "bits", "real8", "ascii", "none", "bytes"]}, + "payload_hex": {"$ref": "#/$defs/hex"}, + "value": {"type": ["array", "string", "null"]} + }, + "allOf": [ + {"if": {"properties": {"encoding": {"const": "xy"}}}, "then": {"properties": {"value": {"type": "array", "items": {"$ref": "#/$defs/point"}}}}}, + {"if": {"properties": {"encoding": {"enum": ["uint16", "bits"]}}}, "then": {"properties": {"value": {"type": "array", "items": {"$ref": "#/$defs/uint16"}}}}}, + {"if": {"properties": {"encoding": {"const": "int16"}}}, "then": {"properties": {"value": {"type": "array", "items": {"type": "integer", "minimum": -32768, "maximum": 32767}}}}}, + {"if": {"properties": {"encoding": {"const": "int32"}}}, "then": {"properties": {"value": {"type": "array", "items": {"$ref": "#/$defs/int32"}}}}}, + {"if": {"properties": {"encoding": {"const": "real8"}}}, "then": {"properties": {"value": {"type": "array", "items": {"type": ["number", "null"]}}}}}, + {"if": {"properties": {"encoding": {"const": "ascii"}}}, "then": {"properties": {"value": {"type": ["string", "null"]}}}}, + {"if": {"properties": {"encoding": {"enum": ["none", "bytes"]}}}, "then": {"properties": {"value": {"type": "null"}}}} + ] + }, + "input": { + "type": "object", "required": ["path", "path_encoded_hex", "present", "units"], + "properties": { + "path": {"type": ["string", "null"]}, + "path_encoded_hex": {"oneOf": [{"type": "null"}, {"$ref": "#/$defs/hex"}]}, + "present": {"type": ["boolean", "null"]}, + "units": {"oneOf": [{"type": "null"}, {"$ref": "#/$defs/field"}]} + } + }, + "summary": { + "type": "object", "required": ["counts", "changed_cells", "removed_objects", "added_objects", "layer_name_changes", "library_field_changes"], + "properties": { + "counts": {"enum": ["exact", "observed"]}, + "changed_cells": {"$ref": "#/$defs/decimal"}, "removed_objects": {"$ref": "#/$defs/decimal"}, "added_objects": {"$ref": "#/$defs/decimal"}, + "layer_name_changes": {"$ref": "#/$defs/decimal"}, "library_field_changes": {"$ref": "#/$defs/decimal"} + } + }, + "selection": { + "type": "object", "required": ["cell", "layer", "max_objects", "truncated", "filtered_cells", "filtered_objects", "limited_objects", "emitted_objects"], + "properties": { + "cell": {"type": ["string", "null"]}, "layer": {"$ref": "#/$defs/nullableLayer"}, + "max_objects": {"oneOf": [{"type": "null"}, {"$ref": "#/$defs/decimal"}]}, "truncated": {"type": "boolean"}, + "filtered_cells": {"$ref": "#/$defs/decimal"}, "filtered_objects": {"$ref": "#/$defs/decimal"}, + "limited_objects": {"$ref": "#/$defs/decimal"}, "emitted_objects": {"$ref": "#/$defs/decimal"} + } + }, + "libraryChange": { + "oneOf": [ + { + "type": "object", "required": ["field", "tag", "before", "after"], + "properties": {"field": {"const": "presence"}, "tag": {"type": "null"}, "before": {"type": "boolean"}, "after": {"type": "boolean"}} + }, + { + "type": "object", "required": ["field", "tag", "before", "after"], + "properties": {"field": {"type": "string"}, "tag": {"$ref": "#/$defs/uint16"}, "before": {"$ref": "#/$defs/fields"}, "after": {"$ref": "#/$defs/fields"}} + } + ] + }, + "source": { + "type": "object", "required": ["side", "byte_offset", "end_offset"], + "properties": {"side": {"enum": ["old", "new"]}, "byte_offset": {"$ref": "#/$defs/decimal"}, "end_offset": {"$ref": "#/$defs/decimal"}} + }, + "property": { + "type": "object", "required": ["attribute", "value", "payload_hex"], + "properties": {"attribute": {"$ref": "#/$defs/uint16"}, "value": {"type": ["string", "null"]}, "payload_hex": {"$ref": "#/$defs/hex"}} + }, + "object": { + "type": "object", "required": ["id", "kind", "tag", "source", "layer", "bounds", "target", "origin", "width", "counts", "fields", "properties"], + "properties": { + "id": {"type": "string"}, "kind": {"type": "string"}, "tag": {"$ref": "#/$defs/uint16"}, "source": {"$ref": "#/$defs/source"}, + "layer": {"$ref": "#/$defs/nullableLayer"}, + "bounds": {"oneOf": [{"type": "null"}, {"type": "array", "minItems": 4, "maxItems": 4, "items": {"$ref": "#/$defs/int32"}}]}, + "target": {"type": ["string", "null"]}, "origin": {"oneOf": [{"type": "null"}, {"$ref": "#/$defs/point"}]}, + "width": {"oneOf": [{"type": "null"}, {"$ref": "#/$defs/int32"}]}, + "counts": {"oneOf": [{"type": "null"}, {"type": "array", "minItems": 2, "maxItems": 2, "items": {"type": "integer", "minimum": 1, "maximum": 32767}}]}, + "fields": {"$ref": "#/$defs/fields"}, "properties": {"type": "array", "items": {"$ref": "#/$defs/property"}} + } + }, + "replacement": { + "type": "object", "required": ["removed", "added", "match_rule", "explanation", "field_changes", "properties_changed"], + "properties": { + "removed": {"type": "string"}, "added": {"type": "string"}, "match_rule": {"type": "string"}, "explanation": {"type": "string"}, "properties_changed": {"type": "boolean"}, + "field_changes": {"type": "array", "items": { + "type": "object", "required": ["tag", "before", "after"], + "properties": {"tag": {"$ref": "#/$defs/uint16"}, "before": {"oneOf": [{"type": "null"}, {"$ref": "#/$defs/field"}]}, "after": {"oneOf": [{"type": "null"}, {"$ref": "#/$defs/field"}]}} + }} + } + }, + "cellSide": { + "oneOf": [{"type": "null"}, { + "type": "object", "required": ["byte_offset", "attributes"], + "properties": {"byte_offset": {"$ref": "#/$defs/decimal"}, "attributes": {"$ref": "#/$defs/fields"}} + }] + }, + "cellChange": { + "type": "object", "required": ["name", "before", "after", "attributes_changed", "removed", "added", "replacements"], + "properties": { + "name": {"type": "string"}, "before": {"$ref": "#/$defs/cellSide"}, "after": {"$ref": "#/$defs/cellSide"}, "attributes_changed": {"type": "boolean"}, + "removed": {"type": "array", "items": {"$ref": "#/$defs/object"}}, "added": {"type": "array", "items": {"$ref": "#/$defs/object"}}, + "replacements": {"type": "array", "items": {"$ref": "#/$defs/replacement"}} + } + }, + "diagnostic": { + "type": "object", "required": ["code", "severity", "message", "side", "byte_offset", "cell", "layer", "target"], + "properties": { + "code": {"type": "string"}, "severity": {"const": "error"}, "message": {"type": "string"}, "side": {"enum": ["old", "new", null]}, + "byte_offset": {"oneOf": [{"type": "null"}, {"$ref": "#/$defs/decimal"}]}, "cell": {"type": ["string", "null"]}, + "layer": {"$ref": "#/$defs/nullableLayer"}, "target": {"type": ["string", "null"]} + } + }, + "graph": { + "type": "object", "required": ["nodes", "edges"], + "properties": { + "nodes": {"type": "array", "items": { + "type": "object", "required": ["cell", "before", "after", "change"], + "properties": { + "cell": {"type": "string"}, "before": {"enum": ["included", "outside_scope", "unresolved", "absent"]}, "after": {"enum": ["included", "outside_scope", "unresolved", "absent"]}, + "change": {"enum": ["direct", "affected", "unchanged"]} + } + }}, + "edges": {"type": "array", "items": { + "type": "object", "required": ["parent", "target", "before_count", "after_count", "reference_changed"], + "properties": {"parent": {"type": "string"}, "target": {"type": "string"}, "before_count": {"$ref": "#/$defs/decimal"}, "after_count": {"$ref": "#/$defs/decimal"}, "reference_changed": {"type": "boolean"}} + }} + } + } + } +} diff --git a/docs/diff-reports.md b/docs/diff-reports.md new file mode 100644 index 0000000..1557109 --- /dev/null +++ b/docs/diff-reports.md @@ -0,0 +1,185 @@ +# Machine-readable diff reports + +```sh +klamath diff before.gds after.gds --format json > report.json +klamath diff before.gds after.gds --format json --hierarchy graph +klamath diff before.gds after.gds --format json --top CHIP --layer 1/0 +``` + +The command writes one UTF-8 JSON document followed by a newline to stdout. +Comparison diagnostics belong to that document. Output write failures are +reported on stderr and return 2; interrupted output is not a valid report. +JSON output currently belongs to `diff`; `dump`, `hierarchy`, and the Git +external driver retain their existing text interfaces. No external process is +used to produce a report. + +The [version 1 JSON Schema](diff-report-v1.schema.json) defines the structure. +[Golden reports](../tests/golden/report-geometry.json) illustrate complete +evidence; other `report-*.json` goldens cover equality, metadata, incomplete +comparison, and failure. Their producer version is replaced with `test` so +package version changes do not alter the fixtures. + +## Versions and determinism + +`schema_version` describes the report structure. `comparison_profile` names the +equality rules, currently `klamath-object-v2`. `producer.version` identifies the +implementation. These versions are independent. Consumers interpreting equality +must understand the comparison profile as well as the schema. + +Compatible additions may introduce optional fields. Consumers should ignore +unfamiliar additional fields and handle unfamiliar diagnostic codes and matching +rules without treating them as equality. Removed fields or changed meanings +require a new schema version. The schema intentionally permits additional fields. + +For the same inputs, paths, options, and producer version, serialization is +deterministic. Cells, layer pairs, fields, properties, and graph edges are sorted. +Duplicate objects and properties remain separate occurrences. Reports contain +no generation timestamp or required content fingerprint. + +## Result and exit status + +| `status` | `result` | Exit code | Interpretation | +| --- | --- | --- | --- | +| `complete` | `equal` | 0 | Equal under the recorded profile and scope. | +| `complete` | `different` | 1 | Differences found. | +| `incomplete` | `different` | 2 | Definite differences found, with comparison limitations. | +| `incomplete` | `unknown` | 2 | Equality cannot be established. | +| `failed` | `unknown` | 2 | No comparison result is available. | + +Unsupported records, future metadata versions, unresolved references, and +hierarchy cycles make the comparison incomplete. Malformed input, read errors, +invalid scopes, and invalid command arguments produce failed reports. CI must +check completeness as well as differences. An exit code of 1 is a normal +comparison outcome, including when stdout is redirected to a report file. + +Library and cell timestamps are excluded from the comparison profile. A valid, +complete timestamp-only comparison returns 0. Raw record dumps remain available +for examining those timestamps. + +`summary` counts the entire scoped comparison, before display selection. Its +`counts` is `exact` for a complete comparison and `observed` for an incomplete +one. Failed reports have a null summary and empty change arrays. Replacement +suggestions and affected ancestors never add to change counts. + +## Scope, selection, and numbers + +`scope` records the actual `top`, `cell`, and numeric `layer` selectors. Null +means unrestricted. The complete file is parsed before applying scope, so an +invalid record outside the selected subtree still fails the command. + +`report_selection` records display-only `cell`, `layer`, and `max_objects`. +JSON has no default object limit. Explicit `--max-changes N` limits the total +number of emitted object occurrences across both sides. Removals precede +additions within each cell. `filtered_cells`, `filtered_objects`, +`limited_objects`, and `emitted_objects` explain evidence selection. `truncated` +is true if filtering or a limit omits any changed cell or object. Library and +layer-name changes, diagnostics, summaries, and the optional graph are retained. +A replacement is emitted only when both linked objects are included. + +All aggregate counts, limits, and byte offsets are unsigned decimal **strings**. +This avoids loss of precision in JSON consumers using binary64 numbers. +Coordinates, numeric tags, layer/type pairs, widths, and array dimensions are +bounded integers represented as JSON numbers. Null means absent or unavailable; +zero and empty arrays do not imply absence. + +`inputs.old` and `inputs.new` contain input paths, presence, and UNITS fields. +Presence is distinct from an empty library. The Rust API also supports absent +Git revisions using `Document::empty_like`; no units are invented for absent +inputs. Failed reports may have null presence and units. Argument failures can +lack input/selection context because the command could not be parsed. + +Paths are UTF-8 when representable. Otherwise `path` is null and +`path_encoded_hex` preserves Rust's platform-native encoded path bytes, prefixed +with `0x`. This is platform-dependent evidence rather than a portable path name. + +## Change evidence + +`library_changes` records changed header fields as typed before/after arrays. +Library presence changes use the special field `presence`, a null tag, and +boolean before/after values. Units are not rescaled to a common grid. + +`layer_name_changes` records numeric pairs with before/after names. A layer +pair is `{ "number": 1, "purpose": 0 }`, where purpose is DATATYPE, TEXTTYPE, +BOXTYPE, or NODETYPE according to the object kind. `layer_legend` contains +unchanged names available in the scope. Embedded annotation labels are excluded +from ordinary object changes. A metadata rename appears once. + +Each `cell_changes` entry has its name, nullable before/after cell records, +attribute evidence, removed and added objects, and replacement suggestions. +A null cell side means the cell is absent in that scope. An existing empty +cell has a byte offset and an empty attribute array. + +Each object retains: + +- Its kind and numeric tag, numeric layer pair, target cell, and coordinate envelope. +- Its original source side and byte range `[byte_offset, end_offset)`. +- Typed field records and the sorted property multiset, including duplicates. +- Convenience fields for text/reference origin, path/text width, and reference + array dimensions. SREF dimensions are `[1, 1]`; the kind and retained fields + distinguish an SREF from a one-element AREF and explicit fields from omission. + +Coordinates are cell-local integer database units. `bounds` contains +`[xmin, ymin, xmax, ymax]` of stored coordinates. It excludes path stroke width, +rendered text dimensions, and recursively transformed target geometry. +Equal envelopes do not establish geometric equality. Unit changes must be +considered before deriving physical movements or dimensions. + +A retained field has a numeric `tag`, convenience `name`, `encoding`, decoded +`value`, and `payload_hex`. The numeric tag is authoritative; names are display +aids. XY values are arrays of `[x, y]` pairs. Other numeric fields are arrays +of their declared type. Unsupported encodings retain bytes with a null value. +String and property values are null if their bytes are not valid UTF-8; +`payload_hex` always preserves them without replacement characters. + +Payloads follow the comparison profile: boundary/box rings use canonical start +and winding, string padding is removed, and fields/properties are sorted. They +are comparison evidence, not a verbatim serialization of the source records. +REAL8 payload encodings are retained exactly alongside their decoded values. +Explicit defaults and omitted fields remain distinct. Source ranges allow raw +record inspection when original encoding or order is needed. + +Replacement suggestions link report-local removed/added IDs, with a `match_rule`, +typed `field_changes`, and `properties_changed`. Their explanation is optional +for consumers to display and must not be parsed. Current matching rules are +`unique_kind_layer_bounds`, `unique_kind_layer`, and `unique_kind_target`. +These are explanatory matches, not persistent identity or shape equivalence. +Ambiguous candidates stay independent removals and additions. IDs are unique +within a report and have no meaning across reports. + +## Diagnostics and hierarchy + +Diagnostics have `code`, `severity`, `message`, input `side`, original byte +offset, cell, layer, and target context. Unknown context is null. Messages are +for display; codes and context support automation. Current limitation codes are +`unsupported_record`, `unsupported_metadata_version`, `unresolved_reference`, +`hierarchy_cycle`, and `scope_mismatch`. Failure codes are `invalid_input`, +`io_error`, `invalid_scope`, and `command_error`. Fatal parser errors currently +retain their location in the human message; their structured byte offset is +null. Consumers must not depend on message wording. + +`--hierarchy graph` includes the full scoped union graph of both revisions. +Otherwise `hierarchy` is null. Nodes are keyed by cell name, with per-side +states `included`, `outside_scope`, `unresolved`, or `absent`. Their `change` +is `direct`, `affected` through a descendant, or `unchanged`. + +Edges record parent, target, before/after direct placement counts, and whether +the underlying reference records changed. AREF columns times rows contribute +symbolically; arrays are never expanded. Counts are direct edge multiplicities, +not recursively accumulated placements. Shared cells occur once. Removed edges +remain visible, and ancestors are computed over the union of both revisions. +Graph content is independent of display filters and object limits. Text depth +limits and ASCII/Mermaid choices do not apply to JSON graphs. + +## Development + +`cargo test --test review` checks report semantics, CLI behavior, and versioned +goldens. To intentionally regenerate report goldens after reviewing a contract +change, run: + +```sh +KLAMATH_UPDATE_REPORT_GOLDENS=1 cargo test --test review machine_reports_match_versioned_goldens +``` + +Schema validators can validate the goldens or actual CLI output against +`diff-report-v1.schema.json`. Schema validation does not replace semantic checks +such as replacement links, summary totals, or status/exit-code agreement. diff --git a/src/bin/klamath.rs b/src/bin/klamath.rs index 5dc0c21..37e0a02 100644 --- a/src/bin/klamath.rs +++ b/src/bin/klamath.rs @@ -1,3 +1,4 @@ +use klamath_rs_ext::review::report::{ReportInputs, ReportOptions, failure_report}; use klamath_rs_ext::review::{ Comparison, DisplayFilter, Document, Scope, dump_records, hierarchy, parse_layer, parse_layer_name, write_layer_names, @@ -7,12 +8,13 @@ use std::{ ffi::OsString, fs, io::{self, Write}, + path::PathBuf, process::ExitCode, }; const HELP: &str = "Usage: klamath dump FILE [--records] - klamath diff OLD NEW [--top NAME] [--cell NAME] [--layer L/T]\n [--show-cell NAME] [--show-layer L/T] [--hierarchy ascii|mermaid|none]\n [--max-changes N] [--depth N] + klamath diff OLD NEW [--top NAME] [--cell NAME] [--layer L/T]\n [--show-cell NAME] [--show-layer L/T] [--hierarchy ascii|mermaid|none|graph]\n [--max-changes N] [--depth N] [--format text|json] klamath hierarchy FILE [--format ascii|mermaid] [--depth N] [--top NAME]\n klamath git-diff [OPTIONS] -- PATH OLD OLD_OID OLD_MODE NEW NEW_OID NEW_MODE klamath name-layers INPUT OUTPUT --cell NAME --carrier L/T --name L:T:NAME ... @@ -85,8 +87,57 @@ fn stdout(output: &str) -> Result<(), String> { .map_err(|e| e.to_string()) } -fn run() -> Result { - let args: Vec<_> = std::env::args_os().skip(1).collect(); +fn json_stdout(report: &serde_json::Value) -> Result<(), String> { + let mut output = io::stdout().lock(); + serde_json::to_writer_pretty(&mut output, report).map_err(|e| e.to_string())?; + output.write_all(b"\n").map_err(|e| e.to_string()) +} + +fn json_diff(paths: &[OsString], scope: &Scope, options: &ReportOptions) -> Result { + let inputs = ReportInputs { + old: Some(PathBuf::from(&paths[0])), + new: Some(PathBuf::from(&paths[1])), + }; + let comparison = || { + let read = |index, side| { + fs::read(&paths[index]) + .map_err(|e| ("io_error", format!("{:?}: {e}", paths[index]), Some(side))) + }; + let old_bytes = read(0, "old")?; + let new_bytes = read(1, "new")?; + let old = Document::parse(&old_bytes).map_err(|e| ("invalid_input", e, Some("old")))?; + let new = Document::parse(&new_bytes).map_err(|e| ("invalid_input", e, Some("new")))?; + scope + .validate_pair(&old, &new) + .map_err(|e| ("invalid_scope", e, None))?; + let old = old + .selected(scope, true) + .map_err(|e| ("invalid_scope", e, Some("old")))?; + let new = new + .selected(scope, true) + .map_err(|e| ("invalid_scope", e, Some("new")))?; + let diff = Comparison::between(&old, &new); + Ok::<_, (&str, String, Option<&str>)>(( + diff.json_report(&old, &new, &inputs, options), + diff.exit_code(), + )) + }; + let (report, status) = match comparison() { + Ok(result) => result, + Err((code, message, side)) => ( + failure_report(&inputs, scope, options, code, &message, side), + 2, + ), + }; + if let Err(error) = json_stdout(&report) { + // Once output starts, never append a second report on a write failure. + eprintln!("klamath: {error}"); + return Ok(2); + } + Ok(status) +} + +fn run(args: &[OsString]) -> Result { if args.is_empty() || args[0] == "--help" || args[0] == "-h" { stdout(HELP)?; return Ok(0); @@ -111,7 +162,10 @@ fn run() -> Result { "ascii".to_string() }; let mut max_changes = 100; + let mut explicit_max = None; + let mut output_format = "text".to_string(); let mut depth = 8; + let mut explicit_depth = false; let mut paths = Vec::new(); let mut literal = false; let mut i = 1; @@ -128,22 +182,25 @@ fn run() -> Result { return Ok(0); } Some("--records") if command == "dump" => raw = true, - Some("--top") => scope.top = Some(value(&args, &mut i)?), - Some("--cell") => scope.cell = Some(value(&args, &mut i)?), - Some("--layer") => scope.layer = Some(parse_layer(&value(&args, &mut i)?)?), - Some("--show-cell") if compare => show.cell = Some(value(&args, &mut i)?), + Some("--top") => scope.top = Some(value(args, &mut i)?), + Some("--cell") => scope.cell = Some(value(args, &mut i)?), + Some("--layer") => scope.layer = Some(parse_layer(&value(args, &mut i)?)?), + Some("--show-cell") if compare => show.cell = Some(value(args, &mut i)?), Some("--show-layer") if compare => { - show.layer = Some(parse_layer(&value(&args, &mut i)?)?) + show.layer = Some(parse_layer(&value(args, &mut i)?)?) } - Some("--hierarchy") if compare => format = value(&args, &mut i)?, - Some("--format") if command == "hierarchy" => format = value(&args, &mut i)?, + Some("--hierarchy") if compare => format = value(args, &mut i)?, + Some("--format") if command == "hierarchy" => format = value(args, &mut i)?, + Some("--format") if command == "diff" => output_format = value(args, &mut i)?, Some("--max-changes") if compare => { - max_changes = value(&args, &mut i)? + max_changes = value(args, &mut i)? .parse() - .map_err(|_| "invalid change limit")? + .map_err(|_| "invalid change limit")?; + explicit_max = Some(max_changes); } Some("--depth") if command != "dump" => { - depth = value(&args, &mut i)?.parse().map_err(|_| "invalid depth")? + depth = value(args, &mut i)?.parse().map_err(|_| "invalid depth")?; + explicit_depth = true; } Some(s) if s.starts_with('-') => return Err(format!("unknown option {s:?}")), _ => paths.push(args[i].clone()), @@ -153,6 +210,25 @@ fn run() -> Result { if paths.len() != expected && !(command == "git-diff" && paths.len() == 9) { return Err(HELP.to_string()); } + if !matches!(output_format.as_str(), "text" | "json") { + return Err("diff format must be text or json".to_string()); + } + if output_format == "json" { + if !matches!(format.as_str(), "none" | "graph") || explicit_depth { + return Err( + "JSON accepts --hierarchy graph|none; --depth applies to text output".to_string(), + ); + } + return json_diff( + &paths, + &scope, + &ReportOptions { + filter: show, + max_objects: explicit_max, + hierarchy: format == "graph", + }, + ); + } if !matches!(format.as_str(), "ascii" | "mermaid" | "none") || (command == "hierarchy" && format == "none") { @@ -248,11 +324,51 @@ fn run() -> Result { }) } +fn wants_json(args: &[OsString]) -> bool { + if args.first().is_none_or(|arg| arg != "diff") { + return false; + } + let mut format = None; + let mut i = 1; + while i < args.len() { + match args[i].to_str() { + Some("--") => break, + Some("--format") => { + format = args.get(i + 1).and_then(|v| v.to_str()); + i += 1; + } + Some( + "--top" | "--cell" | "--layer" | "--show-cell" | "--show-layer" | "--hierarchy" + | "--max-changes" | "--depth", + ) => i += 1, + _ => (), + } + i += 1; + } + format == Some("json") +} + fn main() -> ExitCode { - match run() { + let args: Vec<_> = std::env::args_os().skip(1).collect(); + let json = wants_json(&args); + match run(&args) { Ok(status) => ExitCode::from(status), Err(message) => { - eprintln!("klamath: {message}"); + if json { + let report = failure_report( + &ReportInputs::default(), + &Scope::default(), + &ReportOptions::default(), + "command_error", + &message, + None, + ); + if let Err(error) = json_stdout(&report) { + eprintln!("klamath: {error}"); + } + } else { + eprintln!("klamath: {message}"); + } ExitCode::from(2) } } diff --git a/src/review.rs b/src/review.rs index 4ee01fd..05763b2 100644 --- a/src/review.rs +++ b/src/review.rs @@ -14,6 +14,31 @@ mod selection; pub use selection::{DisplayFilter, Scope}; mod changes; pub use changes::Replacement; +pub mod report; + +/// A comparison limitation with source context independent of its wording. +#[derive(Clone, Debug)] +pub struct Diagnostic { + pub code: &'static str, + pub message: String, + pub offset: Option, + pub cell: Option, + pub layer: Option, + pub target: Option, +} + +impl Diagnostic { + fn at(code: &'static str, offset: usize, message: String) -> Self { + Self { + code, + message: error(offset, message), + offset: Some(offset), + cell: None, + layer: None, + target: None, + } + } +} pub const PROFILE: &str = "klamath-object-v2"; pub const LAYER_SENTINEL: &str = "KLAMATH_LAYER_NAMES_V1"; @@ -45,7 +70,7 @@ pub struct Object<'a> { pub multiplicity: u64, key: Key<'a>, metadata: bool, - issues: Vec, + issues: Vec, } impl Object<'_> { @@ -76,7 +101,7 @@ pub struct Cell<'a> { pub end_offset: usize, pub objects: Vec>, attributes: Vec<(u16, &'a [u8])>, - issues: Vec, + issues: Vec, } #[derive(Debug)] @@ -86,8 +111,9 @@ pub struct Document<'a> { pub layer_names: BTreeMap, /// Unsupported features or invalid reference graphs prevent an equality claim. pub issues: Vec, + pub diagnostics: Vec, header: Vec<(u16, &'a [u8])>, - global_issues: Vec, + global_issues: Vec, known_cells: BTreeSet, pub scope: Scope, present: bool, @@ -246,7 +272,7 @@ fn allowed_field(kind: u16, tag: u16) -> bool { } } -fn object<'a>(records: &[Record<'a>], issues: &mut Vec) -> Result, String> { +fn object<'a>(records: &[Record<'a>], issues: &mut Vec) -> Result, String> { let first = &records[0]; let mut fields = Vec::new(); let mut properties = Vec::new(); @@ -264,7 +290,8 @@ fn object<'a>(records: &[Record<'a>], issues: &mut Vec) -> Result Document<'a> { cells: BTreeMap::new(), layer_names: BTreeMap::new(), issues: Vec::new(), + diagnostics: Vec::new(), header: Vec::new(), global_issues: Vec::new(), known_cells: BTreeSet::new(), @@ -461,7 +489,8 @@ impl<'a> Document<'a> { | RTAG_SRFNAME | RTAG_LIBSECUR ) { - doc.global_issues.push(error( + doc.global_issues.push(Diagnostic::at( + "unsupported_record", record.offset, format!("unsupported library record 0x{:04x}", record.tag), )); @@ -534,7 +563,8 @@ impl<'a> Document<'a> { return Err(error(record.offset, "missing ENDSTR")); } if record.tag != RTAG_STRCLASS { - cell.issues.push(error( + cell.issues.push(Diagnostic::at( + "unsupported_record", record.offset, format!("unsupported cell record 0x{:04x}", record.tag), )); @@ -571,9 +601,10 @@ impl<'a> Document<'a> { for object in cell.objects.iter_mut().filter(|o| o.kind == RTAG_TEXT) { let text = object.field(RTAG_STRING).unwrap(); if text.starts_with(b"KLAMATH_LAYER_NAMES_") && text != LAYER_SENTINEL.as_bytes() { - object.issues.push(error( + object.issues.push(Diagnostic::at( + "unsupported_metadata_version", object.offset, - "unsupported layer-name metadata version", + "unsupported layer-name metadata version".to_string(), )); } } @@ -613,13 +644,26 @@ impl<'a> Document<'a> { } fn check_graph(&mut self) { - let edges = self.edges(); - for (parent, target) in edges.keys() { - if !self.known_cells.contains(target) { - self.issues - .push(format!("cell {parent:?}: unresolved reference {target:?}")); + let mut unresolved = BTreeMap::new(); + for (parent, cell) in &self.cells { + for object in &cell.objects { + if let Some(target) = &object.target + && !self.known_cells.contains(target) + { + unresolved.entry((parent, target)).or_insert(object.offset); + } } } + for ((parent, target), offset) in unresolved { + self.diagnostics.push(Diagnostic { + code: "unresolved_reference", + message: format!("cell {parent:?}: unresolved reference {target:?}"), + offset: Some(offset), + cell: Some(parent.clone()), + layer: None, + target: Some(target.clone()), + }); + } let mut state = BTreeMap::<&str, u8>::new(); for root in self.cells.keys() { let mut stack = vec![(root.as_str(), false)]; @@ -631,8 +675,14 @@ impl<'a> Document<'a> { match state.get(name) { Some(2) => continue, Some(1) => { - self.issues - .push(format!("cyclic hierarchy involving {name:?}")); + self.diagnostics.push(Diagnostic { + code: "hierarchy_cycle", + message: format!("cyclic hierarchy involving {name:?}"), + offset: self.cells.get(name).map(|c| c.offset), + cell: Some(name.to_string()), + layer: None, + target: None, + }); continue; } _ => (), @@ -723,9 +773,28 @@ fn format_records(records: &[Record<'_>]) -> String { out } +/// A retained comparison field. Polygon coordinates and string padding follow +/// the comparison profile; REAL encodings and unsupported payloads are exact. +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct FieldValue { + pub tag: u16, + pub data: Vec, +} + +fn owned_fields<'a>(fields: impl IntoIterator) -> Vec { + fields + .into_iter() + .map(|(tag, data)| FieldValue { + tag, + data: data.to_vec(), + }) + .collect() +} + #[derive(Debug)] pub struct ObjectChange { pub offset: usize, + pub end_offset: usize, pub kind: u16, pub layer: Option, pub bounds: Option, @@ -734,12 +803,15 @@ pub struct ObjectChange { pub width: Option, pub counts: Option<[u16; 2]>, pub description: String, + pub fields: Vec, + pub properties: Vec<(u16, Vec)>, } impl From<&Object<'_>> for ObjectChange { fn from(object: &Object<'_>) -> Self { Self { offset: object.offset, + end_offset: object.end_offset, kind: object.kind, layer: object.layer, bounds: object.bounds, @@ -754,12 +826,27 @@ impl From<&Object<'_>> for ObjectChange { .field(RTAG_WIDTH) .map(|data| i32::from_be_bytes(data.try_into().unwrap())), counts: object.target.as_ref().map(|_| { - object - .field(RTAG_COLROW) - .map(|data| [integer(&data[..2]), integer(&data[2..])]) - .unwrap_or([1, 1]) + if object.kind == RTAG_AREF { + let data = object.field(RTAG_COLROW).unwrap(); + [integer(&data[..2]), integer(&data[2..])] + } else { + [1, 1] + } }), description: object.description(), + fields: owned_fields( + object + .key + .fields + .iter() + .map(|(tag, data)| (*tag, data.as_ref())), + ), + properties: object + .key + .properties + .iter() + .map(|(a, v)| (*a, v.to_vec())) + .collect(), } } } @@ -770,6 +857,8 @@ pub struct CellChange { pub old_offset: Option, pub new_offset: Option, pub attributes_changed: bool, + pub old_attributes: Vec, + pub new_attributes: Vec, pub removed: Vec, pub added: Vec, pub replacements: Vec, @@ -785,6 +874,9 @@ pub struct LayerNameChange { #[derive(Debug)] pub struct LibraryFieldChange { pub field: String, + pub tag: Option, + pub old_fields: Vec, + pub new_fields: Vec, pub old: Vec, pub new: Vec, } @@ -821,6 +913,9 @@ impl Comparison { if old.present != new.present { result.library_fields.push(LibraryFieldChange { field: "presence".to_string(), + tag: None, + old_fields: Vec::new(), + new_fields: Vec::new(), old: vec![if old.present { "present" } else { "absent" }.to_string()], new: vec![if new.present { "present" } else { "absent" }.to_string()], }); @@ -852,6 +947,9 @@ impl Comparison { if a != b { result.library_fields.push(LibraryFieldChange { field: tag_name(tag), + tag: Some(tag), + old_fields: owned_fields(a.iter().map(|data| (tag, *data))), + new_fields: owned_fields(b.iter().map(|data| (tag, *data))), old: a.iter().map(|data| format_data(tag, data)).collect(), new: b.iter().map(|data| format_data(tag, data)).collect(), }); @@ -881,6 +979,12 @@ impl Comparison { old_offset: a.map(|c| c.offset), new_offset: b.map(|c| c.offset), attributes_changed: a.map(|c| &c.attributes) != b.map(|c| &c.attributes), + old_attributes: owned_fields( + a.into_iter().flat_map(|c| c.attributes.iter().copied()), + ), + new_attributes: owned_fields( + b.into_iter().flat_map(|c| c.attributes.iter().copied()), + ), removed: Vec::new(), added: Vec::new(), replacements: Vec::new(), diff --git a/src/review/changes.rs b/src/review/changes.rs index c962766..22e202a 100644 --- a/src/review/changes.rs +++ b/src/review/changes.rs @@ -5,6 +5,8 @@ pub struct Replacement { pub removed: usize, pub added: usize, pub explanation: String, + /// Explanation matching is never object identity or geometric equivalence. + pub match_rule: &'static str, } type Group = (u16, Option, Option, Option); @@ -54,6 +56,13 @@ pub(super) fn replacements(old: &[ObjectChange], new: &[ObjectChange]) -> Vec, + pub new: Option, +} + +/// Limits affect evidence only. The default includes every changed object. +#[derive(Clone, Debug, Default)] +pub struct ReportOptions { + pub filter: DisplayFilter, + pub max_objects: Option, + pub hierarchy: bool, +} + +fn layer(pair: Layer) -> Value { + json!({"number": pair.0, "purpose": pair.1}) +} + +fn scope(value: &Scope) -> Value { + json!({"top": value.top, "cell": value.cell, "layer": value.layer.map(layer)}) +} + +fn input(path: Option<&Path>, doc: Option<&Document<'_>>) -> Value { + let units = doc + .filter(|d| d.present) + .and_then(|d| d.header.iter().find(|(tag, _)| *tag == RTAG_UNITS)) + .map(|(tag, data)| field(*tag, data)); + json!({ + "path": path.and_then(Path::to_str), + "path_encoded_hex": path.filter(|p| p.to_str().is_none()) + .map(|p| hex(p.as_os_str().as_encoded_bytes())), + "present": doc.map(|d| d.present), + "units": units + }) +} + +fn envelope(inputs: &ReportInputs, selection: &Scope, options: &ReportOptions) -> Value { + json!({ + "schema_version": SCHEMA_VERSION, + "kind": "diff", + "producer": {"name": "klamath", "version": env!("CARGO_PKG_VERSION")}, + "comparison_profile": PROFILE, + "inputs": {"old": input(inputs.old.as_deref(), None), "new": input(inputs.new.as_deref(), None)}, + "scope": scope(selection), + "coordinates": {"unit": "database_unit", "frame": "cell_local", "bounds": "coordinate_envelope"}, + "status": "failed", + "result": "unknown", + "exit_code": 2, + "summary": null, + "report_selection": { + "cell": options.filter.cell, + "layer": options.filter.layer.map(layer), + "max_objects": options.max_objects.map(|n| n.to_string()), + "truncated": false, + "filtered_cells": "0", "filtered_objects": "0", "limited_objects": "0", "emitted_objects": "0" + }, + "layer_legend": [], + "library_changes": [], "layer_name_changes": [], "cell_changes": [], + "hierarchy": null, + "diagnostics": [] + }) +} + +/// Input/selection failures still produce a valid report. Unknown locations are +/// null rather than inferred by parsing an error's human-readable message. +pub fn failure_report( + inputs: &ReportInputs, + selection: &Scope, + options: &ReportOptions, + code: &str, + message: &str, + side: Option<&str>, +) -> Value { + let mut report = envelope(inputs, selection, options); + report["diagnostics"] = json!([{ + "code": code, "severity": "error", "message": message, + "side": side, "byte_offset": null, "cell": null, "layer": null, "target": null + }]); + report +} + +fn field(tag: u16, data: &[u8]) -> Value { + let (encoding, value) = if tag == RTAG_XY && data.len().is_multiple_of(8) { + ("xy", json!(points(data))) + } else if matches!( + tag, + RTAG_LAYER | RTAG_DATATYPE | RTAG_TEXTTYPE | RTAG_NODETYPE | RTAG_BOXTYPE + ) && data.len().is_multiple_of(2) + { + ( + "uint16", + json!(data.chunks_exact(2).map(integer).collect::>()), + ) + } else { + match tag & 0xff { + 0 if data.is_empty() => ("none", Value::Null), + 1 if data.len().is_multiple_of(2) => ( + "bits", + json!(data.chunks_exact(2).map(integer).collect::>()), + ), + 2 if data.len().is_multiple_of(2) => ( + "int16", + json!( + data.chunks_exact(2) + .map(|v| i16::from_be_bytes(v.try_into().unwrap())) + .collect::>() + ), + ), + 3 if data.len().is_multiple_of(4) => ( + "int32", + json!( + data.chunks_exact(4) + .map(|v| i32::from_be_bytes(v.try_into().unwrap())) + .collect::>() + ), + ), + 5 if data.len().is_multiple_of(8) => ( + "real8", + json!( + data.chunks_exact(8) + .map(|v| decode_real8(u64::from_be_bytes(v.try_into().unwrap()))) + .collect::>() + ), + ), + 6 => ("ascii", json!(std::str::from_utf8(data).ok())), + _ => ("bytes", Value::Null), + } + }; + json!({"tag": tag, "name": tag_name(tag), "encoding": encoding, "value": value, "payload_hex": hex(data)}) +} + +fn fields(values: &[FieldValue]) -> Vec { + values.iter().map(|f| field(f.tag, &f.data)).collect() +} + +fn object(value: &ObjectChange, id: &str, side: &str) -> Value { + json!({ + "id": id, "kind": kind_name(value.kind), "tag": value.kind, + "source": {"side": side, "byte_offset": value.offset.to_string(), "end_offset": value.end_offset.to_string()}, + "layer": value.layer.map(layer), "bounds": value.bounds, "target": value.target, + "origin": value.origin.filter(|_| matches!(value.kind, RTAG_SREF | RTAG_AREF | RTAG_TEXT)), "width": value.width, "counts": value.counts, + "fields": fields(&value.fields), + "properties": value.properties.iter().map(|(attribute, bytes)| json!({ + "attribute": attribute, "value": std::str::from_utf8(bytes).ok(), "payload_hex": hex(bytes) + })).collect::>() + }) +} + +fn field_changes(old: &ObjectChange, new: &ObjectChange) -> Vec { + let a: BTreeMap<_, _> = old.fields.iter().map(|f| (f.tag, &f.data)).collect(); + let b: BTreeMap<_, _> = new.fields.iter().map(|f| (f.tag, &f.data)).collect(); + a.keys() + .chain(b.keys()) + .copied() + .collect::>() + .into_iter() + .filter(|tag| a.get(tag) != b.get(tag)) + .map(|tag| { + json!({ + "tag": tag, + "before": a.get(&tag).map(|data| field(tag, data)), + "after": b.get(&tag).map(|data| field(tag, data)) + }) + }) + .collect() +} + +impl Comparison { + /// Render evidence from the same documents used to construct this comparison. + /// IDs are local to this report, not persistent object identities. + pub fn json_report( + &self, + old: &Document<'_>, + new: &Document<'_>, + inputs: &ReportInputs, + options: &ReportOptions, + ) -> Value { + let mut report = envelope(inputs, &self.scope, options); + report["inputs"] = json!({"old": input(inputs.old.as_deref(), Some(old)), "new": input(inputs.new.as_deref(), Some(new))}); + let different = !self.cells.is_empty() + || !self.library_fields.is_empty() + || !self.layer_names.is_empty(); + report["status"] = json!(if self.issues.is_empty() { + "complete" + } else { + "incomplete" + }); + report["result"] = json!(if different { + "different" + } else if self.is_equal() { + "equal" + } else { + "unknown" + }); + report["exit_code"] = json!(self.exit_code()); + report["summary"] = json!({ + "counts": if self.issues.is_empty() { "exact" } else { "observed" }, + "changed_cells": self.cells.len().to_string(), + "removed_objects": self.cells.iter().map(|c| c.removed.len()).sum::().to_string(), + "added_objects": self.cells.iter().map(|c| c.added.len()).sum::().to_string(), + "layer_name_changes": self.layer_names.len().to_string(), + "library_field_changes": self.library_fields.len().to_string() + }); + report["layer_legend"] = json!( + self.layer_legend + .iter() + .map(|(pair, name)| json!({"layer": layer(*pair), "name": name})) + .collect::>() + ); + report["layer_name_changes"] = json!( + self.layer_names + .iter() + .map(|c| json!({ + "layer": layer(c.layer), "before": c.old, "after": c.new + })) + .collect::>() + ); + report["library_changes"] = json!(self.library_fields.iter().map(|c| { + if c.tag.is_none() { + json!({"field": "presence", "tag": null, "before": old.present, "after": new.present}) + } else { + json!({"field": c.field, "tag": c.tag, "before": fields(&c.old_fields), "after": fields(&c.new_fields)}) + } + }).collect::>()); + let (mut emitted, mut filtered_objects, mut limited, mut filtered_cells) = + (0_usize, 0_usize, 0_usize, 0_usize); + let mut cells = Vec::new(); + for (cell_index, cell) in self.cells.iter().enumerate() { + if options + .filter + .cell + .as_ref() + .is_some_and(|name| name != &cell.name) + { + filtered_cells += 1; + filtered_objects += cell.removed.len() + cell.added.len(); + continue; + } + let (mut removed, mut added) = (Vec::new(), Vec::new()); + let mut shown = BTreeSet::new(); + for (side, objects, output) in [ + ("old", &cell.removed, &mut removed), + ("new", &cell.added, &mut added), + ] { + for (index, change) in objects.iter().enumerate() { + if options + .filter + .layer + .is_some_and(|l| change.layer != Some(l)) + { + filtered_objects += 1; + } else if options.max_objects.is_some_and(|max| emitted >= max) { + limited += 1; + } else { + let id = format!("c{cell_index}:{side}:{index}"); + output.push(object(change, &id, side)); + shown.insert(id); + emitted += 1; + } + } + } + let replacements: Vec<_> = cell.replacements.iter().filter_map(|r| { + let a = format!("c{cell_index}:old:{}", r.removed); + let b = format!("c{cell_index}:new:{}", r.added); + (shown.contains(&a) && shown.contains(&b)).then(|| json!({ + "removed": a, "added": b, "match_rule": r.match_rule, + "explanation": r.explanation, + "field_changes": field_changes(&cell.removed[r.removed], &cell.added[r.added]), + "properties_changed": cell.removed[r.removed].properties != cell.added[r.added].properties + })) + }).collect(); + cells.push(json!({ + "name": cell.name, + "before": cell.old_offset.map(|offset| json!({"byte_offset": offset.to_string(), "attributes": fields(&cell.old_attributes)})), + "after": cell.new_offset.map(|offset| json!({"byte_offset": offset.to_string(), "attributes": fields(&cell.new_attributes)})), + "attributes_changed": cell.attributes_changed, + "removed": removed, "added": added, "replacements": replacements + })); + } + report["cell_changes"] = json!(cells); + report["report_selection"]["truncated"] = + json!(filtered_cells > 0 || filtered_objects > 0 || limited > 0); + for (key, count) in [ + ("filtered_cells", filtered_cells), + ("filtered_objects", filtered_objects), + ("limited_objects", limited), + ("emitted_objects", emitted), + ] { + report["report_selection"][key] = json!(count.to_string()); + } + let mut diagnostics: Vec<_> = [("old", old), ("new", new)] + .into_iter() + .flat_map(|(side, doc)| { + doc.diagnostics.iter().map(move |d| { + json!({ + "code": d.code, "severity": "error", "message": d.message, "side": side, + "byte_offset": d.offset.map(|n| n.to_string()), "cell": d.cell, + "layer": d.layer.map(layer), "target": d.target + }) + }) + }) + .collect(); + if old.scope != new.scope { + diagnostics.push(json!({"code": "scope_mismatch", "severity": "error", "message": "comparison scopes differ", "side": null, "byte_offset": null, "cell": null, "layer": null, "target": null})); + } + report["diagnostics"] = json!(diagnostics); + if options.hierarchy { + report["hierarchy"] = graph(old, new, self); + } + report + } +} + +fn graph(old: &Document<'_>, new: &Document<'_>, diff: &Comparison) -> Value { + let a = old.edges(); + let b = new.edges(); + let edges: BTreeSet<_> = a.keys().chain(b.keys()).cloned().collect(); + let names: BTreeSet<_> = old + .cells + .keys() + .chain(new.cells.keys()) + .cloned() + .chain( + edges + .iter() + .flat_map(|(parent, child)| [parent.clone(), child.clone()]), + ) + .collect(); + let direct: BTreeSet<_> = diff.cells.iter().map(|c| c.name.clone()).collect(); + let mut affected = direct.clone(); + let mut parents = BTreeMap::<&str, Vec<&str>>::new(); + for (parent, child) in &edges { + parents.entry(child).or_default().push(parent); + } + let mut pending: Vec<_> = direct.iter().cloned().collect(); + while let Some(child) = pending.pop() { + for parent in parents.get(child.as_str()).into_iter().flatten() { + if affected.insert((*parent).to_string()) { + pending.push((*parent).to_string()); + } + } + } + let changed_refs: BTreeSet<_> = diff + .cells + .iter() + .flat_map(|c| { + c.removed + .iter() + .chain(&c.added) + .filter_map(|o| o.target.as_ref().map(|t| (c.name.clone(), t.clone()))) + }) + .collect(); + let a_targets: BTreeSet<_> = a.keys().map(|(_, t)| t.as_str()).collect(); + let b_targets: BTreeSet<_> = b.keys().map(|(_, t)| t.as_str()).collect(); + let state = |doc: &Document<'_>, targets: &BTreeSet<&str>, name: &String| { + if doc.cells.contains_key(name) { + "included" + } else if doc.known_cells.contains(name) { + "outside_scope" + } else if targets.contains(name.as_str()) { + "unresolved" + } else { + "absent" + } + }; + json!({ + "nodes": names.iter().map(|name| json!({ + "cell": name, "before": state(old, &a_targets, name), "after": state(new, &b_targets, name), + "change": if direct.contains(name) { "direct" } else if affected.contains(name) { "affected" } else { "unchanged" } + })).collect::>(), + "edges": edges.iter().map(|edge| json!({ + "parent": edge.0, "target": edge.1, + "before_count": a.get(edge).copied().unwrap_or(0).to_string(), + "after_count": b.get(edge).copied().unwrap_or(0).to_string(), + "reference_changed": changed_refs.contains(edge) + })).collect::>() + }) +} diff --git a/src/review/selection.rs b/src/review/selection.rs index 42e09f2..bab79a1 100644 --- a/src/review/selection.rs +++ b/src/review/selection.rs @@ -63,6 +63,7 @@ impl<'a> Document<'a> { cells: BTreeMap::new(), layer_names: BTreeMap::new(), issues: Vec::new(), + diagnostics: Vec::new(), header: self.header.clone(), global_issues: Vec::new(), known_cells: BTreeSet::new(), @@ -84,17 +85,26 @@ impl<'a> Document<'a> { } pub(super) fn refresh_issues(&mut self) { - self.issues = self.global_issues.clone(); + self.diagnostics = self.global_issues.clone(); for (name, cell) in &self.cells { - for issue in cell - .issues - .iter() - .chain(cell.objects.iter().flat_map(|o| &o.issues)) - { - self.issues.push(format!("cell {name:?}: {issue}")); + for issue in &cell.issues { + let mut issue = issue.clone(); + issue.cell = Some(name.clone()); + issue.message = format!("cell {name:?}: {}", issue.message); + self.diagnostics.push(issue); + } + for object in &cell.objects { + for issue in &object.issues { + let mut issue = issue.clone(); + issue.cell = Some(name.clone()); + issue.layer = object.layer; + issue.message = format!("cell {name:?}: {}", issue.message); + self.diagnostics.push(issue); + } } } self.check_graph(); + self.issues = self.diagnostics.iter().map(|d| d.message.clone()).collect(); } /// Project a parsed document. Missing selectors are allowed only for one diff --git a/tests/golden/report-equal.json b/tests/golden/report-equal.json new file mode 100644 index 0000000..cc02059 --- /dev/null +++ b/tests/golden/report-equal.json @@ -0,0 +1,78 @@ +{ + "cell_changes": [], + "comparison_profile": "klamath-object-v2", + "coordinates": { + "bounds": "coordinate_envelope", + "frame": "cell_local", + "unit": "database_unit" + }, + "diagnostics": [], + "exit_code": 0, + "hierarchy": null, + "inputs": { + "new": { + "path": "after.gds", + "path_encoded_hex": null, + "present": true, + "units": { + "encoding": "real8", + "name": "UNITS", + "payload_hex": "0x3f04189374bc6a7f3a044b82fa09b5a5", + "tag": 773, + "value": [ + 0.001, + 9.999999999999999e-10 + ] + } + }, + "old": { + "path": "before.gds", + "path_encoded_hex": null, + "present": true, + "units": { + "encoding": "real8", + "name": "UNITS", + "payload_hex": "0x3f04189374bc6a7f3a044b82fa09b5a5", + "tag": 773, + "value": [ + 0.001, + 9.999999999999999e-10 + ] + } + } + }, + "kind": "diff", + "layer_legend": [], + "layer_name_changes": [], + "library_changes": [], + "producer": { + "name": "klamath", + "version": "test" + }, + "report_selection": { + "cell": null, + "emitted_objects": "0", + "filtered_cells": "0", + "filtered_objects": "0", + "layer": null, + "limited_objects": "0", + "max_objects": null, + "truncated": false + }, + "result": "equal", + "schema_version": 1, + "scope": { + "cell": null, + "layer": null, + "top": null + }, + "status": "complete", + "summary": { + "added_objects": "0", + "changed_cells": "0", + "counts": "exact", + "layer_name_changes": "0", + "library_field_changes": "0", + "removed_objects": "0" + } +} diff --git a/tests/golden/report-failed.json b/tests/golden/report-failed.json new file mode 100644 index 0000000..3b08309 --- /dev/null +++ b/tests/golden/report-failed.json @@ -0,0 +1,64 @@ +{ + "cell_changes": [], + "comparison_profile": "klamath-object-v2", + "coordinates": { + "bounds": "coordinate_envelope", + "frame": "cell_local", + "unit": "database_unit" + }, + "diagnostics": [ + { + "byte_offset": null, + "cell": null, + "code": "invalid_input", + "layer": null, + "message": "truncated record", + "severity": "error", + "side": "new", + "target": null + } + ], + "exit_code": 2, + "hierarchy": null, + "inputs": { + "new": { + "path": "after.gds", + "path_encoded_hex": null, + "present": null, + "units": null + }, + "old": { + "path": "before.gds", + "path_encoded_hex": null, + "present": null, + "units": null + } + }, + "kind": "diff", + "layer_legend": [], + "layer_name_changes": [], + "library_changes": [], + "producer": { + "name": "klamath", + "version": "test" + }, + "report_selection": { + "cell": null, + "emitted_objects": "0", + "filtered_cells": "0", + "filtered_objects": "0", + "layer": null, + "limited_objects": "0", + "max_objects": null, + "truncated": false + }, + "result": "unknown", + "schema_version": 1, + "scope": { + "cell": null, + "layer": null, + "top": null + }, + "status": "failed", + "summary": null +} diff --git a/tests/golden/report-geometry.json b/tests/golden/report-geometry.json new file mode 100644 index 0000000..9e03987 --- /dev/null +++ b/tests/golden/report-geometry.json @@ -0,0 +1,360 @@ +{ + "cell_changes": [ + { + "added": [ + { + "bounds": [ + 0, + 0, + 10, + 10 + ], + "counts": null, + "fields": [ + { + "encoding": "uint16", + "name": "layer", + "payload_hex": "0x0001", + "tag": 3330, + "value": [ + 1 + ] + }, + { + "encoding": "uint16", + "name": "datatype", + "payload_hex": "0x0000", + "tag": 3586, + "value": [ + 0 + ] + }, + { + "encoding": "xy", + "name": "xy", + "payload_hex": "0x0000000000000000000000000000000a0000000a000000000000000000000000", + "tag": 4099, + "value": [ + [ + 0, + 0 + ], + [ + 0, + 10 + ], + [ + 10, + 0 + ], + [ + 0, + 0 + ] + ] + } + ], + "id": "c0:new:0", + "kind": "BOUNDARY", + "layer": { + "number": 1, + "purpose": 0 + }, + "origin": null, + "properties": [], + "source": { + "byte_offset": "382", + "end_offset": "438", + "side": "new" + }, + "tag": 2048, + "target": null, + "width": null + } + ], + "after": { + "attributes": [], + "byte_offset": "346" + }, + "attributes_changed": false, + "before": { + "attributes": [], + "byte_offset": "346" + }, + "name": "leaf", + "removed": [ + { + "bounds": [ + 0, + 0, + 10, + 10 + ], + "counts": null, + "fields": [ + { + "encoding": "uint16", + "name": "layer", + "payload_hex": "0x0001", + "tag": 3330, + "value": [ + 1 + ] + }, + { + "encoding": "uint16", + "name": "datatype", + "payload_hex": "0x0000", + "tag": 3586, + "value": [ + 0 + ] + }, + { + "encoding": "xy", + "name": "xy", + "payload_hex": "0x0000000000000000000000000000000a0000000a0000000a0000000a000000000000000000000000", + "tag": 4099, + "value": [ + [ + 0, + 0 + ], + [ + 0, + 10 + ], + [ + 10, + 10 + ], + [ + 10, + 0 + ], + [ + 0, + 0 + ] + ] + } + ], + "id": "c0:old:0", + "kind": "BOUNDARY", + "layer": { + "number": 1, + "purpose": 0 + }, + "origin": null, + "properties": [], + "source": { + "byte_offset": "382", + "end_offset": "446", + "side": "old" + }, + "tag": 2048, + "target": null, + "width": null + } + ], + "replacements": [ + { + "added": "c0:new:0", + "explanation": "BOUNDARY modified; coordinate bounds unchanged", + "field_changes": [ + { + "after": { + "encoding": "xy", + "name": "xy", + "payload_hex": "0x0000000000000000000000000000000a0000000a000000000000000000000000", + "tag": 4099, + "value": [ + [ + 0, + 0 + ], + [ + 0, + 10 + ], + [ + 10, + 0 + ], + [ + 0, + 0 + ] + ] + }, + "before": { + "encoding": "xy", + "name": "xy", + "payload_hex": "0x0000000000000000000000000000000a0000000a0000000a0000000a000000000000000000000000", + "tag": 4099, + "value": [ + [ + 0, + 0 + ], + [ + 0, + 10 + ], + [ + 10, + 10 + ], + [ + 10, + 0 + ], + [ + 0, + 0 + ] + ] + }, + "tag": 4099 + } + ], + "match_rule": "unique_kind_layer_bounds", + "properties_changed": false, + "removed": "c0:old:0" + } + ] + } + ], + "comparison_profile": "klamath-object-v2", + "coordinates": { + "bounds": "coordinate_envelope", + "frame": "cell_local", + "unit": "database_unit" + }, + "diagnostics": [], + "exit_code": 1, + "hierarchy": { + "edges": [ + { + "after_count": "1", + "before_count": "1", + "parent": "CHIP", + "reference_changed": false, + "target": "left" + }, + { + "after_count": "1", + "before_count": "1", + "parent": "CHIP", + "reference_changed": false, + "target": "right" + }, + { + "after_count": "2", + "before_count": "2", + "parent": "left", + "reference_changed": false, + "target": "leaf" + }, + { + "after_count": "3", + "before_count": "3", + "parent": "right", + "reference_changed": false, + "target": "leaf" + } + ], + "nodes": [ + { + "after": "included", + "before": "included", + "cell": "CHIP", + "change": "affected" + }, + { + "after": "included", + "before": "included", + "cell": "leaf", + "change": "direct" + }, + { + "after": "included", + "before": "included", + "cell": "left", + "change": "affected" + }, + { + "after": "included", + "before": "included", + "cell": "right", + "change": "affected" + } + ] + }, + "inputs": { + "new": { + "path": "after.gds", + "path_encoded_hex": null, + "present": true, + "units": { + "encoding": "real8", + "name": "UNITS", + "payload_hex": "0x3f04189374bc6a7f3a044b82fa09b5a5", + "tag": 773, + "value": [ + 0.001, + 9.999999999999999e-10 + ] + } + }, + "old": { + "path": "before.gds", + "path_encoded_hex": null, + "present": true, + "units": { + "encoding": "real8", + "name": "UNITS", + "payload_hex": "0x3f04189374bc6a7f3a044b82fa09b5a5", + "tag": 773, + "value": [ + 0.001, + 9.999999999999999e-10 + ] + } + } + }, + "kind": "diff", + "layer_legend": [], + "layer_name_changes": [], + "library_changes": [], + "producer": { + "name": "klamath", + "version": "test" + }, + "report_selection": { + "cell": null, + "emitted_objects": "2", + "filtered_cells": "0", + "filtered_objects": "0", + "layer": null, + "limited_objects": "0", + "max_objects": null, + "truncated": false + }, + "result": "different", + "schema_version": 1, + "scope": { + "cell": null, + "layer": null, + "top": null + }, + "status": "complete", + "summary": { + "added_objects": "1", + "changed_cells": "1", + "counts": "exact", + "layer_name_changes": "0", + "library_field_changes": "0", + "removed_objects": "1" + } +} diff --git a/tests/golden/report-incomplete.json b/tests/golden/report-incomplete.json new file mode 100644 index 0000000..8f0346b --- /dev/null +++ b/tests/golden/report-incomplete.json @@ -0,0 +1,105 @@ +{ + "cell_changes": [], + "comparison_profile": "klamath-object-v2", + "coordinates": { + "bounds": "coordinate_envelope", + "frame": "cell_local", + "unit": "database_unit" + }, + "diagnostics": [ + { + "byte_offset": "158", + "cell": "CHIP", + "code": "unsupported_record", + "layer": { + "number": 1, + "purpose": 0 + }, + "message": "cell \"CHIP\": at byte 158: unsupported record 0x7f02 in BOUNDARY", + "severity": "error", + "side": "old", + "target": null + }, + { + "byte_offset": "158", + "cell": "CHIP", + "code": "unsupported_record", + "layer": { + "number": 1, + "purpose": 0 + }, + "message": "cell \"CHIP\": at byte 158: unsupported record 0x7f02 in BOUNDARY", + "severity": "error", + "side": "new", + "target": null + } + ], + "exit_code": 2, + "hierarchy": null, + "inputs": { + "new": { + "path": "after.gds", + "path_encoded_hex": null, + "present": true, + "units": { + "encoding": "real8", + "name": "UNITS", + "payload_hex": "0x3f04189374bc6a7f3a044b82fa09b5a5", + "tag": 773, + "value": [ + 0.001, + 9.999999999999999e-10 + ] + } + }, + "old": { + "path": "before.gds", + "path_encoded_hex": null, + "present": true, + "units": { + "encoding": "real8", + "name": "UNITS", + "payload_hex": "0x3f04189374bc6a7f3a044b82fa09b5a5", + "tag": 773, + "value": [ + 0.001, + 9.999999999999999e-10 + ] + } + } + }, + "kind": "diff", + "layer_legend": [], + "layer_name_changes": [], + "library_changes": [], + "producer": { + "name": "klamath", + "version": "test" + }, + "report_selection": { + "cell": null, + "emitted_objects": "0", + "filtered_cells": "0", + "filtered_objects": "0", + "layer": null, + "limited_objects": "0", + "max_objects": null, + "truncated": false + }, + "result": "unknown", + "schema_version": 1, + "scope": { + "cell": null, + "layer": null, + "top": null + }, + "status": "incomplete", + "summary": { + "added_objects": "0", + "changed_cells": "0", + "counts": "observed", + "layer_name_changes": "0", + "library_field_changes": "0", + "removed_objects": "0" + } +} diff --git a/tests/golden/report-metadata.json b/tests/golden/report-metadata.json new file mode 100644 index 0000000..2c51187 --- /dev/null +++ b/tests/golden/report-metadata.json @@ -0,0 +1,87 @@ +{ + "cell_changes": [], + "comparison_profile": "klamath-object-v2", + "coordinates": { + "bounds": "coordinate_envelope", + "frame": "cell_local", + "unit": "database_unit" + }, + "diagnostics": [], + "exit_code": 1, + "hierarchy": null, + "inputs": { + "new": { + "path": "after.gds", + "path_encoded_hex": null, + "present": true, + "units": { + "encoding": "real8", + "name": "UNITS", + "payload_hex": "0x3f04189374bc6a7f3a044b82fa09b5a5", + "tag": 773, + "value": [ + 0.001, + 9.999999999999999e-10 + ] + } + }, + "old": { + "path": "before.gds", + "path_encoded_hex": null, + "present": true, + "units": { + "encoding": "real8", + "name": "UNITS", + "payload_hex": "0x3f04189374bc6a7f3a044b82fa09b5a5", + "tag": 773, + "value": [ + 0.001, + 9.999999999999999e-10 + ] + } + } + }, + "kind": "diff", + "layer_legend": [], + "layer_name_changes": [ + { + "after": "CORE:WG", + "before": "WG", + "layer": { + "number": 1, + "purpose": 0 + } + } + ], + "library_changes": [], + "producer": { + "name": "klamath", + "version": "test" + }, + "report_selection": { + "cell": null, + "emitted_objects": "0", + "filtered_cells": "0", + "filtered_objects": "0", + "layer": null, + "limited_objects": "0", + "max_objects": null, + "truncated": false + }, + "result": "different", + "schema_version": 1, + "scope": { + "cell": null, + "layer": null, + "top": null + }, + "status": "complete", + "summary": { + "added_objects": "0", + "changed_cells": "0", + "counts": "exact", + "layer_name_changes": "1", + "library_field_changes": "0", + "removed_objects": "0" + } +} diff --git a/tests/review.rs b/tests/review.rs index 829eeb5..cebe600 100644 --- a/tests/review.rs +++ b/tests/review.rs @@ -1,4 +1,6 @@ mod common; +#[path = "review/reports.rs"] +mod reports; use common::{library_header, record}; use klamath_rs_ext::{ diff --git a/tests/review/reports.rs b/tests/review/reports.rs new file mode 100644 index 0000000..af19169 --- /dev/null +++ b/tests/review/reports.rs @@ -0,0 +1,545 @@ +use super::*; +use klamath_rs_ext::review::{ + DisplayFilter, Scope, + report::{ReportInputs, ReportOptions, failure_report}, +}; +use serde_json::{Value, json}; + +fn inputs() -> ReportInputs { + ReportInputs { + old: Some("before.gds".into()), + new: Some("after.gds".into()), + } +} + +fn report(old: &[u8], new: &[u8], options: &ReportOptions) -> Value { + let a = Document::parse(old).unwrap(); + let b = Document::parse(new).unwrap(); + Comparison::between(&a, &b).json_report(&a, &b, &inputs(), options) +} + +fn unknown(geometry: Vec) -> Vec { + let mut object = geometry; + object.truncate(object.len() - 4); + record(&mut object, 0x7f02, &[0, 42]); + record(&mut object, RTAG_ENDEL, &[]); + layout(&[("CHIP", vec![object])]) +} + +#[test] +fn reports_preserve_fixture_results_totals_and_replacement_links() { + for pair in corpus() { + let r = report(&pair.old, &pair.new, &ReportOptions::default()); + assert_eq!(r["schema_version"], 1); + assert_eq!(r["comparison_profile"], "klamath-object-v2"); + assert_eq!(r["producer"]["version"], env!("CARGO_PKG_VERSION")); + assert_eq!(r["status"], "complete", "{}", pair.name); + assert_eq!( + r["exit_code"], + if pair.changed_cells + pair.metadata == 0 { + 0 + } else { + 1 + } + ); + assert_eq!( + r["summary"]["changed_cells"], + pair.changed_cells.to_string() + ); + assert_eq!(r["summary"]["added_objects"], pair.added.to_string()); + assert_eq!(r["summary"]["removed_objects"], pair.removed.to_string()); + assert_eq!( + r["summary"]["layer_name_changes"], + pair.metadata.to_string() + ); + assert_eq!(r["report_selection"]["truncated"], false); + for cell in r["cell_changes"].as_array().unwrap() { + for replacement in cell["replacements"].as_array().unwrap() { + assert!( + cell["removed"] + .as_array() + .unwrap() + .iter() + .any(|o| o["id"] == replacement["removed"]) + ); + assert!( + cell["added"] + .as_array() + .unwrap() + .iter() + .any(|o| o["id"] == replacement["added"]) + ); + } + } + assert_eq!(r, report(&pair.old, &pair.new, &ReportOptions::default())); + } +} + +#[test] +fn incomplete_reports_distinguish_known_differences_and_include_structured_locations() { + let a = unknown(square()); + let same = report(&a, &a, &ReportOptions::default()); + assert_eq!(same["status"], "incomplete"); + assert_eq!(same["result"], "unknown"); + assert_eq!(same["exit_code"], 2); + assert_eq!(same["summary"]["counts"], "observed"); + let diagnostic = &same["diagnostics"][0]; + assert_eq!(diagnostic["code"], "unsupported_record"); + assert_eq!(diagnostic["side"], "old"); + assert_eq!(diagnostic["cell"], "CHIP"); + assert_eq!(diagnostic["layer"], json!({"number":1,"purpose":0})); + let offset: usize = diagnostic["byte_offset"].as_str().unwrap().parse().unwrap(); + assert_eq!(&a[offset + 2..offset + 4], &0x7f02_u16.to_be_bytes()); + let changed = report(&a, &unknown(triangle()), &ReportOptions::default()); + assert_eq!(changed["status"], "incomplete"); + assert_eq!(changed["result"], "different"); + assert_eq!(changed["exit_code"], 2); + assert_eq!( + changed["cell_changes"][0]["removed"][0]["fields"] + .as_array() + .unwrap() + .last() + .unwrap()["payload_hex"], + "0x002a" + ); +} + +#[test] +fn json_is_unbounded_by_default_and_explicit_filters_do_not_change_totals() { + let a = layout(&[("CHIP", vec![])]); + let b = layout(&[("CHIP", vec![square(); 120])]); + let full = report(&a, &b, &ReportOptions::default()); + assert_eq!( + full["cell_changes"][0]["added"].as_array().unwrap().len(), + 120 + ); + let limited = report( + &a, + &b, + &ReportOptions { + max_objects: Some(1), + ..Default::default() + }, + ); + assert_eq!(limited["summary"], full["summary"]); + assert_eq!(limited["exit_code"], 1); + assert_eq!(limited["report_selection"]["truncated"], true); + assert_eq!(limited["report_selection"]["limited_objects"], "119"); + assert_eq!(limited["report_selection"]["emitted_objects"], "1"); + let filtered = report( + &a, + &b, + &ReportOptions { + filter: DisplayFilter { + cell: Some("absent".into()), + layer: None, + }, + ..Default::default() + }, + ); + assert_eq!(filtered["summary"], full["summary"]); + assert_eq!(filtered["cell_changes"], json!([])); + assert_eq!(filtered["report_selection"]["filtered_cells"], "1"); + assert_eq!(filtered["report_selection"]["filtered_objects"], "120"); + let filtered = report( + &a, + &b, + &ReportOptions { + filter: DisplayFilter { + cell: None, + layer: Some((2, 0)), + }, + ..Default::default() + }, + ); + assert_eq!(filtered["summary"], full["summary"]); + assert_eq!(filtered["report_selection"]["filtered_objects"], "120"); + assert_eq!(filtered["cell_changes"][0]["added"], json!([])); +} + +#[test] +fn typed_evidence_preserves_defaults_properties_bytes_and_real_encodings() { + let mut a = reference("leaf", 0, 1); + a.truncate(a.len() - 4); + record(&mut a, RTAG_MAG, &0x4110000000000000_u64.to_be_bytes()); + record(&mut a, RTAG_PROPATTR, &17_u16.to_be_bytes()); + record(&mut a, RTAG_PROPVALUE, &[0xff, 0xfe]); + record(&mut a, RTAG_PROPATTR, &17_u16.to_be_bytes()); + record(&mut a, RTAG_PROPVALUE, &[0xff, 0xfe]); + record(&mut a, RTAG_ENDEL, &[]); + let mut b = a.clone(); + let mag = Document::parse(&layout(&[("CHIP", vec![a.clone()]), ("leaf", vec![])])) + .unwrap() + .records + .into_iter() + .find(|r| r.tag == RTAG_MAG) + .unwrap() + .offset; + // Replace the REAL8 payload within the object, retaining the same decoded value. + let within = a + .windows(8) + .position(|v| v == 0x4110000000000000_u64.to_be_bytes()) + .unwrap(); + b[within..within + 8].copy_from_slice(&0x4201000000000000_u64.to_be_bytes()); + let a = layout(&[("CHIP", vec![a]), ("leaf", vec![])]); + let b = layout(&[("CHIP", vec![b]), ("leaf", vec![])]); + let r = report(&a, &b, &ReportOptions::default()); + let replacement = &r["cell_changes"][0]["replacements"][0]; + assert_eq!(replacement["match_rule"], "unique_kind_target"); + let change = &replacement["field_changes"][0]; + assert_eq!(change["tag"], RTAG_MAG); + assert_eq!(change["before"]["value"], change["after"]["value"]); + assert_ne!( + change["before"]["payload_hex"], + change["after"]["payload_hex"] + ); + let removed = &r["cell_changes"][0]["removed"][0]; + assert_eq!(removed["properties"].as_array().unwrap().len(), 2); + assert_eq!(removed["properties"][0]["attribute"], 17); + assert_eq!(removed["properties"][0]["value"], Value::Null); + assert_eq!(removed["properties"][0]["payload_hex"], "0xfffe"); + let offset: usize = removed["source"]["byte_offset"] + .as_str() + .unwrap() + .parse() + .unwrap(); + assert!(offset < mag); + let end: usize = removed["source"]["end_offset"] + .as_str() + .unwrap() + .parse() + .unwrap(); + assert_eq!(&a[end - 4..end], &[0, 4, 17, 0]); + + let mut path = square(); + path[2..4].copy_from_slice(&RTAG_PATH.to_be_bytes()); + let mut width = path.clone(); + width.truncate(width.len() - 4); + record(&mut width, RTAG_WIDTH, &0_i32.to_be_bytes()); + record(&mut width, RTAG_ENDEL, &[]); + let r = report( + &layout(&[("CHIP", vec![path])]), + &layout(&[("CHIP", vec![width])]), + &ReportOptions::default(), + ); + let change = &r["cell_changes"][0]["replacements"][0]["field_changes"][0]; + assert_eq!(change["tag"], RTAG_WIDTH); + assert_eq!(change["before"], Value::Null); + assert_eq!(change["after"]["value"], json!([0])); +} + +#[test] +fn hierarchy_reports_shared_ancestors_and_symbolic_arrays() { + let options = ReportOptions { + hierarchy: true, + ..Default::default() + }; + let r = report(&shared(square()), &shared(triangle()), &options); + let graph = &r["hierarchy"]; + assert_eq!(graph["nodes"].as_array().unwrap().len(), 4); + assert_eq!( + graph["nodes"] + .as_array() + .unwrap() + .iter() + .find(|n| n["cell"] == "CHIP") + .unwrap()["change"], + "affected" + ); + assert_eq!( + graph["nodes"] + .as_array() + .unwrap() + .iter() + .find(|n| n["cell"] == "leaf") + .unwrap()["change"], + "direct" + ); + let edge = graph["edges"] + .as_array() + .unwrap() + .iter() + .find(|e| e["parent"] == "right") + .unwrap(); + assert_eq!(edge["before_count"], "3"); + assert_eq!(edge["reference_changed"], false); + let bytes = shared(square()); + let scope = Scope { + cell: Some("CHIP".into()), + ..Default::default() + }; + let doc = Document::parse(&bytes) + .unwrap() + .selected(&scope, false) + .unwrap(); + let r = Comparison::between(&doc, &doc).json_report(&doc, &doc, &inputs(), &options); + assert_eq!(r["scope"]["cell"], "CHIP"); + assert_eq!( + r["hierarchy"]["nodes"] + .as_array() + .unwrap() + .iter() + .find(|n| n["cell"] == "left") + .unwrap()["after"], + "outside_scope" + ); + let dangling = layout(&[("CHIP", vec![reference("missing", 0, 1)])]); + let r = report(&dangling, &dangling, &options); + assert_eq!(r["diagnostics"][0]["code"], "unresolved_reference"); + assert_eq!(r["diagnostics"][0]["target"], "missing"); + assert_eq!(r["hierarchy"]["nodes"][1]["after"], "unresolved"); + let cycle = layout(&[("CHIP", vec![reference("CHIP", 0, 1)])]); + assert_eq!( + report(&cycle, &cycle, &options)["diagnostics"][0]["code"], + "hierarchy_cycle" + ); +} + +#[test] +fn library_attributes_empty_cells_units_and_absence_are_explicit() { + let a = layout(&[]); + let b = layout(&[("empty", vec![])]); + let r = report(&a, &b, &ReportOptions::default()); + assert_eq!(r["cell_changes"][0]["before"], Value::Null); + assert_eq!(r["cell_changes"][0]["after"]["attributes"], json!([])); + let mut b = b; + let end = Document::parse(&b).unwrap().cells["empty"].end_offset; + let mut attribute = Vec::new(); + record(&mut attribute, RTAG_STRCLASS, &[0, 1]); + b.splice(end..end, attribute); + let r = report(&layout(&[("empty", vec![])]), &b, &ReportOptions::default()); + assert_eq!( + r["cell_changes"][0]["after"]["attributes"][0]["value"], + json!([1]) + ); + assert_eq!(r["inputs"]["old"]["units"]["encoding"], "real8"); + let doc = Document::parse(&a).unwrap(); + let absent = doc.empty_like(); + let r = Comparison::between(&absent, &doc).json_report( + &absent, + &doc, + &inputs(), + &ReportOptions::default(), + ); + assert_eq!(r["inputs"]["old"]["present"], false); + assert_eq!(r["inputs"]["old"]["units"], Value::Null); + assert_eq!( + r["library_changes"], + json!([{"field":"presence","tag":null,"before":false,"after":true}]) + ); + let mut b = a.clone(); + let units = doc + .records + .iter() + .find(|r| r.tag == RTAG_UNITS) + .unwrap() + .offset; + b[units + 12..units + 20].copy_from_slice( + &klamath_rs_ext::basic::encode_real8(2e-9) + .unwrap() + .to_be_bytes(), + ); + let r = report(&a, &b, &ReportOptions::default()); + assert_eq!(r["library_changes"][0]["tag"], RTAG_UNITS); + assert_ne!(r["inputs"]["old"]["units"], r["inputs"]["new"]["units"]); +} + +#[test] +fn reports_keep_extreme_coordinates_unsigned_layers_and_timestamp_equality() { + let a = layout(&[("quoted\"\\\n日本", vec![])]); + let mut shape = polygon(&[ + [i32::MIN, i32::MIN], + [i32::MAX, i32::MIN], + [i32::MAX, i32::MAX], + [i32::MIN, i32::MAX], + [i32::MIN, i32::MIN], + ]); + shape[8..10].copy_from_slice(&u16::MAX.to_be_bytes()); + let b = layout(&[("quoted\"\\\n日本", vec![shape])]); + let r = report(&a, &b, &ReportOptions::default()); + let object = &r["cell_changes"][0]["added"][0]; + assert_eq!(object["layer"]["number"], u16::MAX); + assert_eq!( + object["bounds"], + json!([i32::MIN, i32::MIN, i32::MAX, i32::MAX]) + ); + let encoded = serde_json::to_string(&r).unwrap(); + assert_eq!(serde_json::from_str::(&encoded).unwrap(), r); + let mut times = b.clone(); + let doc = Document::parse(&b).unwrap(); + for record in doc + .records + .iter() + .filter(|r| matches!(r.tag, RTAG_BGNLIB | RTAG_BGNSTR)) + { + times[record.offset + 4..record.offset + 28].fill(1); + } + let r = report(&b, ×, &ReportOptions::default()); + assert_eq!(r["result"], "equal"); + assert_eq!(r["exit_code"], 0); +} + +#[cfg(unix)] +#[test] +fn cli_preserves_non_utf8_path_bytes_and_literal_option_names() { + use std::os::unix::ffi::OsStringExt; + let directory = std::env::temp_dir().join(format!("klamath json paths {}", std::process::id())); + fs::create_dir_all(&directory).unwrap(); + struct Cleanup(PathBuf); + impl Drop for Cleanup { + fn drop(&mut self) { + let _ = fs::remove_dir_all(&self.0); + } + } + let _cleanup = Cleanup(directory.clone()); + let path = directory.join(std::ffi::OsString::from_vec(b"file\xff.gds".to_vec())); + fs::write(&path, layout(&[])).unwrap(); + let output = Command::new(env!("CARGO_BIN_EXE_klamath")) + .arg("diff") + .args(["--format", "json", "--"]) + .arg(&path) + .arg(&path) + .output() + .unwrap(); + assert_eq!(output.status.code(), Some(0)); + let r: Value = serde_json::from_slice(&output.stdout).unwrap(); + assert_eq!(r["inputs"]["old"]["path"], Value::Null); + let expected = "0x".to_string() + + &path + .as_os_str() + .as_encoded_bytes() + .iter() + .map(|b| format!("{b:02x}")) + .collect::(); + assert_eq!(r["inputs"]["old"]["path_encoded_hex"], expected); + for name in ["--format", "json"] { + fs::write(directory.join(name), layout(&[])).unwrap(); + } + let output = Command::new(env!("CARGO_BIN_EXE_klamath")) + .current_dir(&directory) + .args(["diff", "--", "--format", "json"]) + .output() + .unwrap(); + assert_eq!(output.status.code(), Some(0)); + assert!( + String::from_utf8(output.stdout) + .unwrap() + .starts_with("profile ") + ); +} + +#[test] +fn machine_reports_match_versioned_goldens() { + let options = ReportOptions { + hierarchy: true, + ..Default::default() + }; + let equal = layout(&[("CHIP", vec![square()])]); + let metadata = corpus() + .into_iter() + .find(|p| p.name == "layer name only") + .unwrap(); + let samples = [ + ("equal", report(&equal, &equal, &ReportOptions::default())), + ( + "geometry", + report(&shared(square()), &shared(triangle()), &options), + ), + ( + "metadata", + report(&metadata.old, &metadata.new, &ReportOptions::default()), + ), + ( + "incomplete", + report( + &unknown(square()), + &unknown(square()), + &ReportOptions::default(), + ), + ), + ( + "failed", + failure_report( + &inputs(), + &Scope::default(), + &ReportOptions::default(), + "invalid_input", + "truncated record", + Some("new"), + ), + ), + ]; + for (name, mut actual) in samples { + actual["producer"]["version"] = json!("test"); + let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join(format!("tests/golden/report-{name}.json")); + if std::env::var_os("KLAMATH_UPDATE_REPORT_GOLDENS").is_some() { + fs::write(&path, serde_json::to_string_pretty(&actual).unwrap() + "\n").unwrap(); + } + let expected: Value = serde_json::from_slice(&fs::read(&path).unwrap()).unwrap(); + assert_eq!(actual, expected, "{name}"); + } +} + +#[test] +fn cli_emits_json_for_success_input_errors_scope_errors_and_argument_errors() { + let directory = std::env::temp_dir().join(format!("klamath json {}", std::process::id())); + fs::create_dir_all(&directory).unwrap(); + struct Cleanup(PathBuf); + impl Drop for Cleanup { + fn drop(&mut self) { + let _ = fs::remove_dir_all(&self.0); + } + } + let _cleanup = Cleanup(directory.clone()); + let a = directory.join("before file.gds"); + let b = directory.join("after file.gds"); + fs::write(&a, shared(square())).unwrap(); + fs::write(&b, shared(triangle())).unwrap(); + let command = |extra: &[&str]| { + let output = Command::new(env!("CARGO_BIN_EXE_klamath")) + .arg("diff") + .arg(&a) + .arg(&b) + .args(["--format", "json"]) + .args(extra) + .output() + .unwrap(); + assert!(output.stderr.is_empty(), "{:?}", output.stderr); + let report: Value = serde_json::from_slice(&output.stdout).unwrap(); + assert_eq!( + output.status.code().unwrap(), + report["exit_code"].as_i64().unwrap() as i32 + ); + report + }; + let full = command(&["--hierarchy", "graph"]); + assert_eq!(full["result"], "different"); + assert!(full["hierarchy"].is_object()); + let limited = command(&["--max-changes", "0"]); + assert_eq!(limited["summary"], full["summary"]); + assert_eq!(limited["report_selection"]["emitted_objects"], "0"); + for extra in [ + &["--top", "missing"][..], + &["--unknown"], + &["--max-changes", "x"], + &["--hierarchy", "mermaid"], + &["--depth", "3"], + ] { + let failure = command(extra); + assert_eq!(failure["status"], "failed"); + assert_eq!(failure["result"], "unknown"); + assert_eq!(failure["summary"], Value::Null); + } + fs::write(&b, b"invalid").unwrap(); + let invalid = command(&[]); + assert_eq!(invalid["diagnostics"][0]["code"], "invalid_input"); + assert_eq!(invalid["diagnostics"][0]["side"], "new"); + assert_eq!(invalid["inputs"]["new"]["path"], b.to_str().unwrap()); + fs::remove_file(&b).unwrap(); + assert_eq!(command(&[])["diagnostics"][0]["code"], "io_error"); + fs::write(&b, fs::read(&a).unwrap()).unwrap(); + assert_eq!(command(&[])["result"], "equal"); + fs::write(&b, unknown(square())).unwrap(); + assert_eq!(command(&[])["status"], "incomplete"); +} From 0cbb2c0e2fa8494a75cc13f7fb5793abf63e7bfd Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Sun, 15 Nov 2026 19:46:52 -0800 Subject: [PATCH 2/5] fix(review): retain metadata version diagnostics across scopes --- README.md | 4 +- src/review.rs | 15 +++++-- src/review/names.rs | 4 +- tests/review/reports.rs | 88 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 104 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 937b41c..df3ea04 100644 --- a/README.md +++ b/README.md @@ -130,7 +130,9 @@ side is an addition/deletion. targets use that layer transitively. Cells with no relevant content are omitted. Unresolved references, cycles, and unsupported records with unknown layer effect remain incomplete results. Unsupported objects on another known layer can be -excluded. Library fields, including units, remain compared. Scoped dumps and +excluded. Library fields, including units, remain compared. Unsupported layer-name +version diagnostics are always retained, since those declarations can affect any +layer or cell in the library. Scoped dumps and hierarchy views accept the same selectors. Scope selection parses the complete input: malformed record grammar and conflicting metadata are still errors. diff --git a/src/review.rs b/src/review.rs index 05763b2..6f7f557 100644 --- a/src/review.rs +++ b/src/review.rs @@ -597,15 +597,22 @@ impl<'a> Document<'a> { } fn read_metadata(&mut self) -> Result<(), String> { - for cell in self.cells.values_mut() { - for object in cell.objects.iter_mut().filter(|o| o.kind == RTAG_TEXT) { + for (cell_name, cell) in &mut self.cells { + for object in cell.objects.iter().filter(|o| o.kind == RTAG_TEXT) { let text = object.field(RTAG_STRING).unwrap(); if text.starts_with(b"KLAMATH_LAYER_NAMES_") && text != LAYER_SENTINEL.as_bytes() { - object.issues.push(Diagnostic::at( + // Layer names apply across the library. An unknown version + // may describe layers used outside this carrier or cell, so + // geometry scope selection cannot establish its relevance. + let mut diagnostic = Diagnostic::at( "unsupported_metadata_version", object.offset, "unsupported layer-name metadata version".to_string(), - )); + ); + diagnostic.cell = Some(cell_name.clone()); + diagnostic.layer = object.layer; + diagnostic.message = format!("cell {cell_name:?}: {}", diagnostic.message); + self.global_issues.push(diagnostic); } } let carriers: BTreeSet<_> = cell diff --git a/src/review/names.rs b/src/review/names.rs index bea3228..956bbdc 100644 --- a/src/review/names.rs +++ b/src/review/names.rs @@ -63,9 +63,9 @@ pub fn write_layer_names( ) -> Result, String> { let doc = Document::parse(input)?; if doc - .issues + .diagnostics .iter() - .any(|i| i.contains("unsupported layer-name metadata version")) + .any(|i| i.code == "unsupported_metadata_version") { return Err("cannot rewrite an unsupported layer-name metadata version".to_string()); } diff --git a/tests/review/reports.rs b/tests/review/reports.rs index af19169..c428818 100644 --- a/tests/review/reports.rs +++ b/tests/review/reports.rs @@ -104,6 +104,73 @@ fn incomplete_reports_distinguish_known_differences_and_include_structured_locat ); } +#[test] +fn future_layer_metadata_remains_incomplete_under_geometry_scopes() { + for location in ["CHIP", "legend"] { + let bytes = if location == "CHIP" { + layout(&[( + "CHIP", + vec![square(), label((900, 7), "KLAMATH_LAYER_NAMES_V2")], + )]) + } else { + layout(&[ + ("CHIP", vec![square()]), + ("legend", vec![label((900, 7), "KLAMATH_LAYER_NAMES_V2")]), + ]) + }; + let source = Document::parse(&bytes).unwrap().diagnostics[0].offset; + for scope in [ + Scope::default(), + Scope { + layer: Some((1, 0)), + ..Default::default() + }, + Scope { + layer: Some((999, 0)), + ..Default::default() + }, + Scope { + top: Some("CHIP".into()), + ..Default::default() + }, + Scope { + cell: Some("CHIP".into()), + ..Default::default() + }, + Scope { + top: Some("CHIP".into()), + layer: Some((1, 0)), + ..Default::default() + }, + Scope { + cell: Some("CHIP".into()), + layer: Some((1, 0)), + ..Default::default() + }, + ] { + let doc = Document::parse(&bytes) + .unwrap() + .selected(&scope, false) + .unwrap(); + assert_eq!(doc.diagnostics.len(), 1, "{scope:?}"); + let diagnostic = &doc.diagnostics[0]; + assert_eq!(diagnostic.code, "unsupported_metadata_version"); + assert_eq!(diagnostic.cell.as_deref(), Some(location)); + assert_eq!(diagnostic.layer, Some((900, 7))); + assert_eq!(diagnostic.offset, source); + let diff = Comparison::between(&doc, &doc); + assert_eq!(diff.exit_code(), 2); + assert!(!diff.is_equal()); + assert!(diff.text(0).contains("INCOMPLETE")); + let json = diff.json_report(&doc, &doc, &inputs(), &ReportOptions::default()); + assert_eq!(json["status"], "incomplete"); + assert_eq!(json["result"], "unknown"); + assert_eq!(json["exit_code"], 2); + assert_eq!(json["diagnostics"].as_array().unwrap().len(), 2); + } + } +} + #[test] fn json_is_unbounded_by_default_and_explicit_filters_do_not_change_totals() { let a = layout(&[("CHIP", vec![])]); @@ -542,4 +609,25 @@ fn cli_emits_json_for_success_input_errors_scope_errors_and_argument_errors() { assert_eq!(command(&[])["result"], "equal"); fs::write(&b, unknown(square())).unwrap(); assert_eq!(command(&[])["status"], "incomplete"); + fs::write( + &b, + layout(&[ + ("CHIP", vec![square()]), + ("legend", vec![label((900, 7), "KLAMATH_LAYER_NAMES_V2")]), + ]), + ) + .unwrap(); + for flags in [ + &["--layer", "1/0"][..], + &["--top", "CHIP"], + &["--cell", "CHIP", "--layer", "1/0"], + ] { + let report = command(flags); + assert_eq!(report["status"], "incomplete"); + assert_eq!(report["exit_code"], 2); + assert_eq!( + report["diagnostics"][0]["code"], + "unsupported_metadata_version" + ); + } } From f38d88e3cfb13760bb2438a6451089c3eefa0c33 Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Sun, 15 Nov 2026 19:50:49 -0800 Subject: [PATCH 3/5] fix(arrow): reject fractional array pitches and avoid rectangle overflow --- ARROW_SCHEMA.md | 6 ++ src/elements.rs | 15 +++- tests/selective_reads.rs | 169 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 184 insertions(+), 6 deletions(-) diff --git a/ARROW_SCHEMA.md b/ARROW_SCHEMA.md index c36ef64..b916d67 100644 --- a/ARROW_SCHEMA.md +++ b/ARROW_SCHEMA.md @@ -93,6 +93,12 @@ References point to other cells in the library. - `xy1`: `UInt64` (Packed row basis vector increment) - `counts`: `UInt32` (Packed columns and rows) +Array increments must be exact signed 32-bit integer database-unit values. Full +reads, hierarchy scans, selected reads, and their FFI entry points reject arrays +whose endpoint/count division is fractional or outside that range. They never +round or truncate placements. The review CLI retains original endpoints and can +compare fractional arrays independently of this Arrow representation. + **SRefProp / ARefProp**: - Same as above, plus a `properties` field. diff --git a/src/elements.rs b/src/elements.rs index 9541b5d..dbf58e7 100644 --- a/src/elements.rs +++ b/src/elements.rs @@ -822,9 +822,9 @@ fn boundary_to_rect(vertices: &[i32]) -> Option<[i32; 4]> { let mut seen_corners: u8 = 0; for ii in 0..4 { let jj = (ii + 1) % 4; - let dx = xs[jj] - xs[ii]; - let dy = ys[jj] - ys[ii]; - if (dx == 0) == (dy == 0) { + let same_x = xs[jj] == xs[ii]; + let same_y = ys[jj] == ys[ii]; + if same_x == same_y { return None; } @@ -922,7 +922,14 @@ fn read_ref<'a>( } let pitch = |end: i32, origin: i32, count: i16| { - let value = (i64::from(end) - i64::from(origin)) / i64::from(count); + let delta = i64::from(end) - i64::from(origin); + let divisor = i64::from(count); + if delta % divisor != 0 { + return Err((input, crate::basic::ErrType::Failed( + "AREF pitch is not an integer database-unit increment; the Arrow schema cannot represent it exactly".to_string() + ))); + } + let value = delta / divisor; i32::try_from(value).map_err(|_| (input, crate::basic::ErrType::Failed( "AREF pitch exceeds signed 32-bit coordinate range".to_string() ))) diff --git a/tests/selective_reads.rs b/tests/selective_reads.rs index a6ebeaa..35abc2e 100644 --- a/tests/selective_reads.rs +++ b/tests/selective_reads.rs @@ -10,9 +10,12 @@ use klamath_rs_ext::{ basic::encode_real8, last_error_message, library::{read_library, read_selected_library, scan_library}, - read_cells_bytes, records, scan_bytes, + read_cells_bytes, read_path, records, scan_bytes, +}; +use std::{ + ffi::{CStr, CString}, + ptr, }; -use std::{ffi::CStr, ptr}; fn xy(values: &[i32]) -> Vec { values.iter().flat_map(|v| v.to_be_bytes()).collect() @@ -129,6 +132,168 @@ fn aref_invalid_counts_and_unrepresentable_pitches_return_errors() { assert!(scan_library(&bytes).is_err()); } +#[test] +fn fractional_aref_pitches_are_rejected_by_all_arrow_readers() { + for (column, row, counts) in [ + ([10, 0], [0, 20], [3, 2]), + ([-10, 0], [0, 20], [3, 2]), + ([0, 10], [20, 0], [3, 2]), + ([20, 0], [0, 10], [2, 3]), + ([20, 0], [-10, 0], [2, 3]), + ] { + let bytes = aref_fixture([0, 0], column, row, counts); + let range = [(library_header().len() as u64, bytes.len() as u64 - 4)]; + for error in [ + read_library(&bytes).unwrap_err().1, + scan_library(&bytes).unwrap_err().1, + read_selected_library(&bytes, &range).unwrap_err().1, + ] { + assert!(format!("{error:?}").contains("not an integer database-unit increment")); + } + } + + let bytes = aref_fixture([0, 0], [10, 0], [0, 20], [3, 2]); + let mut array = FFI_ArrowArray::empty(); + let mut schema = FFI_ArrowSchema::empty(); + assert_eq!( + unsafe { scan_bytes(bytes.as_ptr(), bytes.len(), &mut array, &mut schema) }, + -1 + ); + let range = [library_header().len() as u64, bytes.len() as u64 - 4]; + assert_eq!( + unsafe { + read_cells_bytes( + bytes.as_ptr(), + bytes.len(), + range.as_ptr(), + 1, + &mut array, + &mut schema, + ) + }, + -1 + ); + let path = std::env::temp_dir().join(format!( + "klamath-fractional-aref-{}.gds", + std::process::id() + )); + struct Cleanup(std::path::PathBuf); + impl Drop for Cleanup { + fn drop(&mut self) { + let _ = std::fs::remove_file(&self.0); + } + } + let _cleanup = Cleanup(path.clone()); + std::fs::write(&path, &bytes).unwrap(); + let path = CString::new(path.to_str().unwrap()).unwrap(); + assert_eq!( + unsafe { read_path(path.as_ptr(), &mut array, &mut schema) }, + -1 + ); + let message = unsafe { CStr::from_ptr(last_error_message()) } + .to_str() + .unwrap(); + assert!(message.contains("not an integer database-unit increment")); + assert!(array.is_released()); + + // The object reviewer keeps the exact endpoints, including fractional pitches. + let mut bytes = bytes; + bytes.truncate(bytes.len() - 4); + record(&mut bytes, records::RTAG_BGNSTR, &[0; 24]); + record(&mut bytes, records::RTAG_STRNAME, b"leaf"); + record(&mut bytes, records::RTAG_ENDSTR, &[]); + record(&mut bytes, records::RTAG_ENDLIB, &[]); + let doc = klamath_rs_ext::review::Document::parse(&bytes).unwrap(); + assert_eq!( + klamath_rs_ext::review::Comparison::between(&doc, &doc).exit_code(), + 0 + ); +} + +#[test] +fn rectangle_detection_handles_extreme_coordinates_and_preserves_other_polygons() { + let mut bytes = library_header(); + record(&mut bytes, records::RTAG_BGNSTR, &[0; 24]); + record(&mut bytes, records::RTAG_STRNAME, b"top\0"); + let wide = [ + i32::MIN, + i32::MIN, + i32::MAX, + i32::MIN, + i32::MAX, + i32::MAX, + i32::MIN, + i32::MAX, + i32::MIN, + i32::MIN, + ]; + let reversed = [ + i32::MIN, + i32::MIN, + i32::MIN, + i32::MAX, + i32::MAX, + i32::MAX, + i32::MAX, + i32::MIN, + i32::MIN, + i32::MIN, + ]; + let diagonal = [0, 0, 10, 10, 0, 10, 10, 0, 0, 0]; + let degenerate = [0, 0, 0, 0, 10, 0, 10, 0, 0, 0]; + for points in [&wide, &reversed, &diagonal, °enerate] { + record(&mut bytes, records::RTAG_BOUNDARY, &[]); + record(&mut bytes, records::RTAG_LAYER, &[0, 1]); + record(&mut bytes, records::RTAG_DATATYPE, &[0, 0]); + record(&mut bytes, records::RTAG_XY, &xy(points)); + record(&mut bytes, records::RTAG_ENDEL, &[]); + } + record(&mut bytes, records::RTAG_ENDSTR, &[]); + record(&mut bytes, records::RTAG_ENDLIB, &[]); + let full = read_library(&bytes).unwrap().1; + let scan = scan_library(&bytes).unwrap().1; + assert_eq!( + full, + read_selected_library(&bytes, &ranges(&scan)).unwrap().1 + ); + let cells = list_struct(&full, "cells", 0); + let rects = list_struct(&cells, "rect_batches", 0); + let values = rects + .column_by_name("rects") + .unwrap() + .as_any() + .downcast_ref::() + .unwrap() + .value(0); + let values = values.as_any().downcast_ref::().unwrap(); + assert_eq!( + values.values().as_ref(), + &[ + i32::MIN, + i32::MIN, + i32::MAX, + i32::MAX, + i32::MIN, + i32::MIN, + i32::MAX, + i32::MAX + ] + ); + let boundaries = list_struct(&cells, "boundary_batches", 0); + let values = boundaries + .column_by_name("vertices") + .unwrap() + .as_any() + .downcast_ref::() + .unwrap() + .value(0); + let values = values.as_any().downcast_ref::().unwrap(); + assert_eq!( + values.values().as_ref(), + [&diagonal[..8], °enerate[..8]].concat() + ); +} + #[test] fn paths_preserve_missing_explicit_zero_and_negative_widths() { let mut bytes = library_header(); From f081a808eaccdf12b102d00acf7b4626ce38516a Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Sun, 15 Nov 2026 19:51:13 -0800 Subject: [PATCH 4/5] fix(real8): normalize finite values and handle underflow consistently --- README.md | 4 ++ src/basic.rs | 91 ++++++++--------------------- tests/golden/report-equal.json | 8 +-- tests/golden/report-geometry.json | 8 +-- tests/golden/report-incomplete.json | 8 +-- tests/golden/report-metadata.json | 8 +-- tests/golden/review-canonical.txt | 2 +- tests/real8.rs | 75 ++++++++++++++++++++++++ 8 files changed, 120 insertions(+), 84 deletions(-) create mode 100644 tests/real8.rs diff --git a/README.md b/README.md index df3ea04..96b63e8 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,10 @@ A fast GDSII parser and writer in Rust, with Apache Arrow integration for effici - Optimized "batch" representations for rectangles and boundaries sharing the same layer. - C-compatible FFI for integration with other languages (e.g., Python). +REAL8 writing uses normalized GDS encodings and represents every in-range `f64` +exactly. Finite magnitudes below `2^-260` underflow to canonical zero; nonfinite +values and magnitudes at or above `2^252` are rejected by the encoder. + ## Usage `read_path` exports a library through the Arrow C Data Interface. Allocate diff --git a/src/basic.rs b/src/basic.rs index 828c282..b0b5057 100644 --- a/src/basic.rs +++ b/src/basic.rs @@ -166,79 +166,36 @@ impl fmt::Display for FloatTooBigError { } } -/// Convert from float64 to GDS REAL8 representation. +/// Convert float64 to a normalized GDS REAL8 representation. +/// Finite magnitudes below 2^-260 underflow to canonical zero, including -0.0. +/// Nonfinite values and magnitudes at or above 2^252 return an error. pub fn encode_real8(fnum: f64) -> Result { - // Split the ieee float bitfields let ieee = fnum.to_bits(); + let ieee_exp = ((ieee >> 52) & 0x7ff) as i32; + if ieee_exp == 0x7ff { + return Err(FloatTooBigError { float_value: fnum }); + } + + let exp2 = ieee_exp - 1023; + if exp2 < -260 { + // This also handles both signed zeros and every IEEE subnormal. + return Ok(0); + } + + let gds_exp = exp2.div_euclid(4) + 65; + if gds_exp > 0x7f { + return Err(FloatTooBigError { float_value: fnum }); + } + + // The 53-bit IEEE significand needs only 0..3 left shifts to become + // a normalized 56-bit GDS mantissa. Every in-range f64 is exact in REAL8. + let significand = (ieee & 0x000f_ffff_ffff_ffff) | (1_u64 << 52); + let mantissa = significand << exp2.rem_euclid(4); let sign = ieee & 0x8000_0000_0000_0000; - let ieee_exp = (ieee >> 52) as i32 & 0x7ff; - let ieee_mant = ieee & 0x000f_ffff_ffff_ffff; - - let subnorm = (ieee_exp == 0) & (ieee_mant != 0); - if (ieee_exp == 0) & (ieee_mant == 0) { - return Ok(0) - } - - // IEEE normal double is (1 + ieee_mant / 2^52) * 2^(ieee_exp - 1023) - // IEEE subnormal double is (ieee_mant / 2^52) * 2^(-1022) - // GDS real8 is (gds_mant / 2^(7*8)) * 16^(gds_exp - 64) - // = (gds_mant / 2^56) * 2^(4 * gds_exp - 256) - - // Convert exponent. - let exp2 = if subnorm { -1022 } else {ieee_exp + 1 - 1023}; // +1 is due to mantissa (1.xxxx in IEEE vs 0.xxxxx in GDSII) - let mut exp16 = exp2 / 4; - let rest = exp2 % 4; - - // Compensate for exponent coarseness - let comp = rest != 0; - let mut shift; - if comp { - exp16 += 1; - shift = 4 - rest; - } else { - shift = rest; - } - shift -= 3; // account for gds bit position - - // add leading one - let mut gds_mant_unshifted = ieee_mant; - if !subnorm { - gds_mant_unshifted += 0x10_0000_0000_0000; - } - - let mut gds_mant = if shift > 0 { - gds_mant_unshifted >> shift - } else { - gds_mant_unshifted << -shift - }; - - // add gds exponent bias - let mut gds_exp = exp16 + 64; - - if gds_exp < -14 { - // number is too small - return Ok(0) - } - - let neg_biased = gds_exp < 0; - if neg_biased { - gds_mant >>= gds_exp * 4; - gds_exp = 0; - } - - let too_big = (gds_exp > 0x7f) & !subnorm; - if too_big { - return Err(FloatTooBigError{float_value: fnum}); - } - - let gds_exp_bits = (gds_exp as u64) << 56; - - let real8 = sign | gds_exp_bits | gds_mant; - Ok(real8) + Ok(sign | ((gds_exp as u64) << 56) | mantissa) } - #[cfg(test)] mod tests { #[test] diff --git a/tests/golden/report-equal.json b/tests/golden/report-equal.json index cc02059..239f183 100644 --- a/tests/golden/report-equal.json +++ b/tests/golden/report-equal.json @@ -17,11 +17,11 @@ "units": { "encoding": "real8", "name": "UNITS", - "payload_hex": "0x3f04189374bc6a7f3a044b82fa09b5a5", + "payload_hex": "0x3e4189374bc6a7f03944b82fa09b5a54", "tag": 773, "value": [ 0.001, - 9.999999999999999e-10 + 1e-9 ] } }, @@ -32,11 +32,11 @@ "units": { "encoding": "real8", "name": "UNITS", - "payload_hex": "0x3f04189374bc6a7f3a044b82fa09b5a5", + "payload_hex": "0x3e4189374bc6a7f03944b82fa09b5a54", "tag": 773, "value": [ 0.001, - 9.999999999999999e-10 + 1e-9 ] } } diff --git a/tests/golden/report-geometry.json b/tests/golden/report-geometry.json index 9e03987..58ae71f 100644 --- a/tests/golden/report-geometry.json +++ b/tests/golden/report-geometry.json @@ -299,11 +299,11 @@ "units": { "encoding": "real8", "name": "UNITS", - "payload_hex": "0x3f04189374bc6a7f3a044b82fa09b5a5", + "payload_hex": "0x3e4189374bc6a7f03944b82fa09b5a54", "tag": 773, "value": [ 0.001, - 9.999999999999999e-10 + 1e-9 ] } }, @@ -314,11 +314,11 @@ "units": { "encoding": "real8", "name": "UNITS", - "payload_hex": "0x3f04189374bc6a7f3a044b82fa09b5a5", + "payload_hex": "0x3e4189374bc6a7f03944b82fa09b5a54", "tag": 773, "value": [ 0.001, - 9.999999999999999e-10 + 1e-9 ] } } diff --git a/tests/golden/report-incomplete.json b/tests/golden/report-incomplete.json index 8f0346b..09d4ad3 100644 --- a/tests/golden/report-incomplete.json +++ b/tests/golden/report-incomplete.json @@ -44,11 +44,11 @@ "units": { "encoding": "real8", "name": "UNITS", - "payload_hex": "0x3f04189374bc6a7f3a044b82fa09b5a5", + "payload_hex": "0x3e4189374bc6a7f03944b82fa09b5a54", "tag": 773, "value": [ 0.001, - 9.999999999999999e-10 + 1e-9 ] } }, @@ -59,11 +59,11 @@ "units": { "encoding": "real8", "name": "UNITS", - "payload_hex": "0x3f04189374bc6a7f3a044b82fa09b5a5", + "payload_hex": "0x3e4189374bc6a7f03944b82fa09b5a54", "tag": 773, "value": [ 0.001, - 9.999999999999999e-10 + 1e-9 ] } } diff --git a/tests/golden/report-metadata.json b/tests/golden/report-metadata.json index 2c51187..3a60532 100644 --- a/tests/golden/report-metadata.json +++ b/tests/golden/report-metadata.json @@ -17,11 +17,11 @@ "units": { "encoding": "real8", "name": "UNITS", - "payload_hex": "0x3f04189374bc6a7f3a044b82fa09b5a5", + "payload_hex": "0x3e4189374bc6a7f03944b82fa09b5a54", "tag": 773, "value": [ 0.001, - 9.999999999999999e-10 + 1e-9 ] } }, @@ -32,11 +32,11 @@ "units": { "encoding": "real8", "name": "UNITS", - "payload_hex": "0x3f04189374bc6a7f3a044b82fa09b5a5", + "payload_hex": "0x3e4189374bc6a7f03944b82fa09b5a54", "tag": 773, "value": [ 0.001, - 9.999999999999999e-10 + 1e-9 ] } } diff --git a/tests/golden/review-canonical.txt b/tests/golden/review-canonical.txt index 3cdc7ea..8811a25 100644 --- a/tests/golden/review-canonical.txt +++ b/tests/golden/review-canonical.txt @@ -3,7 +3,7 @@ scope top=all cell=all layer=all coordinates: integer database units; bounds: coordinate envelopes HEADER=[600] LIBNAME="test" -UNITS=[0.001, 9.999999999999999e-10] raw=0x3f04189374bc6a7f3a044b82fa09b5a5 +UNITS=[0.001, 1e-9] raw=0x3e4189374bc6a7f03944b82fa09b5a54 layer 1/0 name="WG" cell "CHIP" BOUNDARY layer=1 datatype=0 xy=[0, 0, 0, 10, 10, 10, 10, 0, 0, 0] diff --git a/tests/real8.rs b/tests/real8.rs new file mode 100644 index 0000000..63c4821 --- /dev/null +++ b/tests/real8.rs @@ -0,0 +1,75 @@ +use klamath_rs_ext::basic::{decode_real8, encode_real8, pack_real8}; + +#[test] +fn normalized_real8_has_known_encodings_and_exact_values() { + for (value, expected) in [ + (0.0, 0), + (-0.0, 0), + (1.0, 0x4110_0000_0000_0000), + (-2.0, 0xc120_0000_0000_0000), + (0.5, 0x4080_0000_0000_0000), + (0.001, 0x3e41_8937_4bc6_a7f0), + (1e-9, 0x3944_b82f_a09b_5a54), + (2_f64.powi(-260), 0x0010_0000_0000_0000), + ] { + let bits = encode_real8(value).unwrap(); + assert_eq!(bits, expected, "{value:e}"); + assert_eq!(decode_real8(bits), value); + } +} + +#[test] +fn real8_round_trips_each_supported_binary_exponent_and_mantissa_boundary() { + for exponent in -260..=251_i32 { + for mantissa in [ + 0, + 1, + 0x0008_0000_0000_0000, + 0x000f_ffff_ffff_fffe, + 0x000f_ffff_ffff_ffff, + ] { + for sign in [0, 1_u64 << 63] { + let ieee = sign | (((exponent + 1023) as u64) << 52) | mantissa; + let value = f64::from_bits(ieee); + let encoded = encode_real8(value).unwrap(); + assert_ne!(encoded & 0x00f0_0000_0000_0000, 0, "{value:e}"); + assert_eq!(decode_real8(encoded).to_bits(), ieee, "{value:e}"); + } + } + } +} + +#[test] +fn real8_underflow_is_canonical_and_overflow_is_an_error() { + let minimum = 2_f64.powi(-260); + let maximum_exclusive = 2_f64.powi(252); + for magnitude in [ + 0.0, + f64::from_bits(1), + f64::MIN_POSITIVE, + 1e-90, + 1e-80, + f64::from_bits(minimum.to_bits() - 1), + ] { + for value in [magnitude, -magnitude] { + assert_eq!(encode_real8(value).unwrap(), 0, "{value:e}"); + } + } + let largest = f64::from_bits(maximum_exclusive.to_bits() - 1); + assert_eq!(encode_real8(largest).unwrap(), 0x7fff_ffff_ffff_fff8); + assert_eq!(decode_real8(encode_real8(largest).unwrap()), largest); + for value in [ + maximum_exclusive, + -maximum_exclusive, + f64::MAX, + -f64::MAX, + f64::INFINITY, + f64::NEG_INFINITY, + f64::NAN, + ] { + assert!(encode_real8(value).is_err(), "{value}"); + let mut buffer = [0x55; 8]; + assert!(pack_real8(&mut buffer, value).is_err()); + assert_eq!(buffer, [0x55; 8]); + } +} From 912a267028f305b6e2210a2d3e32e2087be8283f Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Sun, 15 Nov 2026 20:01:26 -0800 Subject: [PATCH 5/5] fix(python): flatten packing buffers and package Cargo libraries --- .gitignore | 5 +++ MANIFEST.in | 6 +++ README.md | 45 +++++++++++++++++++ klamath_rs_ext/__init__.py | 2 +- klamath_rs_ext/_ffi.h | 50 +++++++++++++++++++++ klamath_rs_ext/basic.py | 4 +- klamath_rs_ext/klamath_rs_ext.py | 23 ++++++++++ pyproject.toml | 18 +++++--- setup.py | 76 ++++++++++++++++++++++++++++++++ tests/python/test_ffi.py | 69 +++++++++++++++++++++++++++++ tests/python/test_packing.py | 66 +++++++++++++++++++++++++++ 11 files changed, 355 insertions(+), 9 deletions(-) create mode 100644 MANIFEST.in create mode 100644 klamath_rs_ext/_ffi.h create mode 100644 klamath_rs_ext/klamath_rs_ext.py create mode 100644 setup.py create mode 100644 tests/python/test_ffi.py create mode 100644 tests/python/test_packing.py diff --git a/.gitignore b/.gitignore index 5d80173..ed58073 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,8 @@ Cargo.lock *.swp *.swo + +__pycache__/ +/build/ +/dist/ +/*.egg-info/ diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..9ba61f7 --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,6 @@ +include Cargo.toml Cargo.lock README.md ARROW_SCHEMA.md +recursive-include src *.rs +recursive-include klamath_rs_ext *.py *.h py.typed +recursive-include tests *.rs *.py *.json *.txt *.mmd +recursive-include docs *.md *.json +recursive-include contrib * diff --git a/README.md b/README.md index 96b63e8..2e51acf 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,51 @@ range values must be aligned. Exported Arrow data owns its storage independently of the input. See [Arrow Schema Documentation](ARROW_SCHEMA.md) for the scan schema, offset definitions, and ID assignment. +## Python package and validation + +The Python packing helpers require NumPy and CFFI, both declared as runtime +dependencies. Setuptools invokes `cargo build --release --lib` and bundles the +C ABI shared library in a platform-specific wheel. Its version comes from +`Cargo.toml`. The C declarations are in `klamath_rs_ext/_ffi.h`; the loader uses +CFFI ABI mode, so no Python extension compilation or Maturin is required. + +```sh +python3 -m pip install . +``` + +To build a source archive and a wheel with the `build` frontend and installed +Setuptools/Wheel tooling: + +```sh +python3 -m build --no-isolation +``` + +Source archives include a Cargo lockfile, and wheel builds use `--locked` so +building from an archive retains the resolved Rust dependency versions. Wheels +are tagged for the build platform and use the Python-independent `py3-none` ABI. +The CLI continues to build directly with Cargo and has no Python dependency. + +For source-tree development, point the CFFI loader at a Cargo-built library: + +```sh +cargo build --release --lib +KLAMATH_RS_LIBRARY="$PWD/target/release/libklamath_rs_ext.so" \ + python3 -m unittest discover -s tests/python -v +``` + +Use `libklamath_rs_ext.dylib` on macOS or `klamath_rs_ext.dll` on Windows. Without +the explicit override, the loader uses the library bundled inside the package. +After installing a wheel, run its tests outside the source checkout to exercise +the installed package: + +```sh +python3 -I -m unittest discover -s /absolute/path/to/klamath-rs/tests/python -v +``` + +The Python tests cover multidimensional and noncontiguous packing, input +preservation, range errors, and the C declarations for Arrow exports and release +callbacks. Run `cargo test` and `cargo test --release` for native validation. + ## Layout review CLI The review CLI is a standalone Rust executable: diff --git a/klamath_rs_ext/__init__.py b/klamath_rs_ext/__init__.py index d042d92..2916461 100644 --- a/klamath_rs_ext/__init__.py +++ b/klamath_rs_ext/__init__.py @@ -2,5 +2,5 @@ from .basic import pack_int2 as pack_int2 from .basic import pack_int4 as pack_int4 -__version__ = 0.2 +__version__ = "0.2.0" diff --git a/klamath_rs_ext/_ffi.h b/klamath_rs_ext/_ffi.h new file mode 100644 index 0000000..273067e --- /dev/null +++ b/klamath_rs_ext/_ffi.h @@ -0,0 +1,50 @@ +#ifndef KLAMATH_RS_EXT_H +#define KLAMATH_RS_EXT_H +#include +#include + +struct ArrowArray { + int64_t length; + int64_t null_count; + int64_t offset; + int64_t n_buffers; + int64_t n_children; + const void **buffers; + struct ArrowArray **children; + struct ArrowArray *dictionary; + void (*release)(struct ArrowArray *); + void *private_data; +}; + +struct ArrowSchema { + const char *format; + const char *name; + const char *metadata; + int64_t flags; + int64_t n_children; + struct ArrowSchema **children; + struct ArrowSchema *dictionary; + void (*release)(struct ArrowSchema *); + void *private_data; +}; + +int32_t read_path(const char *path, struct ArrowArray *array, struct ArrowSchema *schema); +int32_t scan_bytes(const uint8_t *data, size_t size, struct ArrowArray *array, struct ArrowSchema *schema); +int32_t read_cells_bytes(const uint8_t *data, size_t size, const uint64_t *ranges, size_t range_count, struct ArrowArray *array, struct ArrowSchema *schema); +const char *last_error_message(void); + +double f64_to_i16(double *data, size_t size); +double f64_to_i32(double *data, size_t size); +float f32_to_i16(float *data, size_t size); +float f32_to_i32(float *data, size_t size); +int64_t i64_to_i16(int64_t *data, size_t size); +int64_t i64_to_i32(int64_t *data, size_t size); +int32_t i32_to_i16(int32_t *data, size_t size); +int32_t i32_to_i32(int32_t *data, size_t size); +int16_t i16_to_i16(int16_t *data, size_t size); +uint64_t u64_to_i16(uint64_t *data, size_t size); +uint64_t u64_to_i32(uint64_t *data, size_t size); +uint32_t u32_to_i16(uint32_t *data, size_t size); +uint32_t u32_to_i32(uint32_t *data, size_t size); +uint16_t u16_to_i16(uint16_t *data, size_t size); +#endif diff --git a/klamath_rs_ext/basic.py b/klamath_rs_ext/basic.py index aabc246..c7bcd8e 100644 --- a/klamath_rs_ext/basic.py +++ b/klamath_rs_ext/basic.py @@ -49,7 +49,7 @@ def pack_int2(data: NDArray[numpy.integer] | Sequence[int] | int) -> bytes: if result != 0: raise ValueError(f'Invalid value for conversion to Int2: {result}') - i2arr = arr.view('>i2')[::arr.itemsize // 2] + i2arr = arr.reshape(-1).view('>i2')[::arr.itemsize // 2] return i2arr.tobytes() if arr.dtype == numpy.dtype('>i2'): @@ -79,7 +79,7 @@ def pack_int4(data: NDArray[numpy.integer] | Sequence[int] | int) -> bytes: if result != 0: raise ValueError(f'Invalid value for conversion to Int4: {result}') - i4arr = arr.view('>i4')[::arr.itemsize // 4] + i4arr = arr.reshape(-1).view('>i4')[::arr.itemsize // 4] return i4arr.tobytes() if arr.dtype == numpy.dtype('>i4'): diff --git a/klamath_rs_ext/klamath_rs_ext.py b/klamath_rs_ext/klamath_rs_ext.py new file mode 100644 index 0000000..0fc0ccd --- /dev/null +++ b/klamath_rs_ext/klamath_rs_ext.py @@ -0,0 +1,23 @@ +"""CFFI ABI access to the Cargo-built shared library.""" +import os +from pathlib import Path +import sys + +from cffi import FFI + + +ffi = FFI() +header = Path(__file__).with_name("_ffi.h").read_text() +ffi.cdef("\n".join(line for line in header.splitlines() if not line.lstrip().startswith("#"))) + +if sys.platform == "win32": + filename = "klamath_rs_ext.dll" +elif sys.platform == "darwin": + filename = "libklamath_rs_ext.dylib" +else: + filename = "libklamath_rs_ext.so" + +# An explicit override supports source-tree development without copying binaries +# into the package. Installed wheels load only their bundled native library. +library = Path(os.environ["KLAMATH_RS_LIBRARY"]) if "KLAMATH_RS_LIBRARY" in os.environ else Path(__file__).with_name(filename) +lib = ffi.dlopen(str(library.resolve())) diff --git a/pyproject.toml b/pyproject.toml index 06aa1af..7a14183 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [build-system] -requires = ["maturin>1.0,<2.0"] -build-backend = "maturin" +requires = ["setuptools>=64", "wheel"] +build-backend = "setuptools.build_meta" [project] name = "klamath_rs_ext" @@ -10,8 +10,6 @@ description = "Compiled extensions for klamath GDS library" authors = [ { name="Jan Petykiewicz", email="jan@mpxd.net" }, ] -homepage = "https://mpxd.net/code/jan/klamath-rs" -repository = "https://mpxd.net/code/jan/klamath-rs" classifiers = [ "Programming Language :: Python :: 3", "Development Status :: 4 - Beta", @@ -30,8 +28,16 @@ requires-python = ">=3.11" dynamic = ["version"] dependencies = [ "cffi", + "numpy", ] -[tool.maturin] -bindings = "cffi" +[project.urls] +Homepage = "https://mpxd.net/code/jan/klamath-rs" +Repository = "https://mpxd.net/code/jan/klamath-rs" + +[tool.setuptools] +packages = ["klamath_rs_ext"] + +[tool.setuptools.package-data] +klamath_rs_ext = ["py.typed", "_ffi.h"] diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..698a4dd --- /dev/null +++ b/setup.py @@ -0,0 +1,76 @@ +"""Bundle Cargo's C ABI library in a platform wheel, without a Python ABI tie.""" +import json +from pathlib import Path +import shutil +import subprocess +import sys +import tomllib + +from setuptools import Distribution, setup +from setuptools.command.build_py import build_py +from setuptools.command.sdist import sdist +from wheel.bdist_wheel import bdist_wheel + + +ROOT = Path(__file__).resolve().parent +VERSION = tomllib.loads((ROOT / "Cargo.toml").read_text())["package"]["version"] + + +def ensure_lockfile(): + if not (ROOT / "Cargo.lock").is_file(): + subprocess.run(["cargo", "generate-lockfile"], cwd=ROOT, check=True) + + +class CargoBuild(build_py): + def run(self): + ensure_lockfile() + super().run() + result = subprocess.run( + ["cargo", "build", "--release", "--lib", "--locked", "--message-format=json"], + cwd=ROOT, capture_output=True, text=True, + ) + sys.stderr.write(result.stderr) + library = None + for line in result.stdout.splitlines(): + try: + message = json.loads(line) + except json.JSONDecodeError: + print(line, file=sys.stderr) + continue + if message.get("reason") == "compiler-message": + sys.stderr.write(message["message"].get("rendered") or "") + if (message.get("reason") == "compiler-artifact" + and message["target"]["name"] == "klamath_rs_ext" + and "cdylib" in message["target"]["crate_types"]): + for filename in message["filenames"]: + if Path(filename).suffix in (".so", ".dylib", ".dll"): + library = Path(filename) + result.check_returncode() + if library is None: + raise RuntimeError("Cargo did not produce the klamath_rs_ext shared library") + shutil.copy2(library, Path(self.build_lib) / "klamath_rs_ext" / library.name) + + +class CargoSdist(sdist): + def run(self): + ensure_lockfile() + super().run() + + +class NativeDistribution(Distribution): + def has_ext_modules(self): + return True + + +class NativeWheel(bdist_wheel): + def get_tag(self): + # CFFI ABI mode needs the host platform, but no CPython extension ABI. + _, _, platform = super().get_tag() + return "py3", "none", platform + + +setup( + version=VERSION, + distclass=NativeDistribution, + cmdclass={"build_py": CargoBuild, "sdist": CargoSdist, "bdist_wheel": NativeWheel}, +) diff --git a/tests/python/test_ffi.py b/tests/python/test_ffi.py new file mode 100644 index 0000000..bf95534 --- /dev/null +++ b/tests/python/test_ffi.py @@ -0,0 +1,69 @@ +"""Exercise the explicit C ABI declarations against the actual shared library.""" +import struct +import tempfile +from pathlib import Path +import unittest + +from klamath_rs_ext.klamath_rs_ext import ffi, lib + + +def record(tag, data=b""): + return struct.pack(">HH", len(data) + 4, tag) + data + + +def layout(): + return b"".join([ + record(0x0002, struct.pack(">h", 600)), + record(0x0102, b"\0" * 24), + record(0x0206, b"test"), + record(0x0305, bytes.fromhex("3e4189374bc6a7f03944b82fa09b5a54")), + record(0x0502, b"\0" * 24), + record(0x0606, b"cell"), + record(0x0700), + record(0x0400), + ]) + + +class FfiTests(unittest.TestCase): + def check_export(self, call): + array = ffi.new("struct ArrowArray *") + schema = ffi.new("struct ArrowSchema *") + try: + self.assertEqual(call(array, schema), 0) + self.assertEqual(lib.last_error_message(), ffi.NULL) + self.assertEqual(array.length, 1) + self.assertEqual(ffi.string(schema.format), b"+s") + self.assertNotEqual(array.release, ffi.NULL) + self.assertNotEqual(schema.release, ffi.NULL) + self.assertGreaterEqual(array.n_children, 7) + self.assertEqual(array.n_children, schema.n_children) + self.assertEqual(ffi.string(schema.children[0].name), b"meters_per_db_unit") + finally: + if array.release != ffi.NULL: + array.release(array) + if schema.release != ffi.NULL: + schema.release(schema) + + def test_scan_selected_read_and_path_exports(self): + data = layout() + self.check_export(lambda a, s: lib.scan_bytes(data, len(data), a, s)) + # The library header is 62 bytes; the one cell ends just before ENDLIB. + ranges = ffi.new("uint64_t[]", [62, len(data) - 4]) + self.check_export(lambda a, s: lib.read_cells_bytes(data, len(data), ranges, 1, a, s)) + with tempfile.TemporaryDirectory() as directory: + path = Path(directory) / "file with spaces.gds" + path.write_bytes(data) + self.check_export(lambda a, s: lib.read_path(str(path).encode(), a, s)) + + def test_failures_leave_outputs_empty_and_expose_error_message(self): + array = ffi.new("struct ArrowArray *") + schema = ffi.new("struct ArrowSchema *") + self.assertEqual(lib.scan_bytes(ffi.NULL, 0, array, schema), -1) + self.assertNotEqual(lib.last_error_message(), ffi.NULL) + self.assertIn(b"scan_bytes", ffi.string(lib.last_error_message())) + self.assertEqual(array.release, ffi.NULL) + self.assertEqual(schema.release, ffi.NULL) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/python/test_packing.py b/tests/python/test_packing.py new file mode 100644 index 0000000..43f6487 --- /dev/null +++ b/tests/python/test_packing.py @@ -0,0 +1,66 @@ +import unittest + +import numpy as np + +from klamath_rs_ext import pack_int2, pack_int4 + + +class PackingTests(unittest.TestCase): + def test_scalars_lists_and_dimensions(self): + for pack, width in [(pack_int2, 2), (pack_int4, 4)]: + for dtype in [np.int64, np.int32, np.uint64, np.uint32, np.float64, np.float32]: + for shape in [(), (1,), (2, 2), (2, 2, 2)]: + count = int(np.prod(shape)) if shape else 1 + data = np.arange(count, dtype=dtype).reshape(shape) + before = data.copy() + with self.subTest(width=width, dtype=dtype, shape=shape): + self.assertEqual(pack(data), data.astype(f">i{width}").tobytes()) + np.testing.assert_array_equal(data, before) + for data in [5, [1, 2, 3], [[1, 2], [3, 4]], [], [[], []]]: + with self.subTest(width=width, data=data): + self.assertEqual(pack(data), np.asarray(data).astype(f">i{width}").tobytes()) + + def test_noncontiguous_readonly_and_endian_inputs(self): + original = np.arange(48, dtype=np.int64).reshape(6, 8) + readonly = original.copy() + readonly.setflags(write=False) + for data in [original.T, original[::-1, ::2], np.asfortranarray(original), readonly, + original.astype(">i8"), original.astype(">i2"), original.astype(">i4")]: + before = data.copy() + for pack, width in [(pack_int2, 2), (pack_int4, 4)]: + with self.subTest(width=width, strides=data.strides, dtype=data.dtype): + self.assertEqual(pack(data), data.astype(f">i{width}").tobytes(order="C")) + np.testing.assert_array_equal(data, before) + + def test_signed_limits_and_native_integer_widths(self): + for pack, width in [(pack_int2, 2), (pack_int4, 4)]: + low, high = -(1 << (8 * width - 1)), (1 << (8 * width - 1)) - 1 + data = np.array([[low, -1], [0, high]], dtype=np.int64) + self.assertEqual(pack(data), data.astype(f">i{width}").tobytes()) + for dtype in [np.int8, np.uint8, np.int16, np.uint16]: + data = np.array([[1, 2], [3, 4]], dtype=dtype) + self.assertEqual(pack_int2(data), data.astype(">i2").tobytes()) + self.assertEqual(pack_int4(data), data.astype(">i4").tobytes()) + + def test_failed_conversions_preserve_caller_data(self): + for pack, width in [(pack_int2, 2), (pack_int4, 4)]: + limit = 1 << (8 * width - 1) + for invalid in [limit, -limit - 1]: + data = np.array([[1, 2], [3, invalid]], dtype=np.int64) + before = data.copy() + with self.subTest(width=width, invalid=invalid): + with self.assertRaises(ValueError): + pack(data) + np.testing.assert_array_equal(data, before) + for dtype in [np.float32, np.float64]: + for invalid in [np.nan, np.inf, -np.inf]: + data = np.array([[1, 2], [3, invalid]], dtype=dtype) + before = data.copy() + with self.subTest(width=width, invalid=invalid, dtype=dtype): + with self.assertRaises(ValueError): + pack(data) + np.testing.assert_array_equal(data, before) + + +if __name__ == "__main__": + unittest.main()