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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions .claude/board/LATEST_STATE.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
## 2026-11-12 — two characterisations (tests only; in a draft PR)
## 2026-11-12 — ABI minor 14: a mask belongs to one resource (D-LGJ-MASK-PARENT-1, in a PR)

Operator decision 2026-11-12: apply the same-parent rule to the nine mask
arguments that checked only the row count.

- **Changed:** `lgj_op_eq_u32`, `lgj_op_gt_i32`, `lgj_plan_eval`,
`lgj_reduce_sum_i32`, `lgj_reduce_i32`, `lgj_op_eq_classid`,
`lgj_op_ternary_match` and both masks of `lgj_hop` now compare the mask's
parent with the resource by registry identity (`Arc::ptr_eq`). The status is
the existing `MASK_LENGTH_MISMATCH`. No new symbol, no registry change.
- **Contract:** `docs/abi.md` §3.1 is new and normative. The change narrows
accepted input, so it is not additive. The major is not bumped; the minor is
(13 to 14), and §3.1 records that as a deliberate exception to §2 with its
reason. Reversing that choice is one constant on each side.
- **Tests:** `provenance.rs` re-pinned. All twelve exports refuse a sibling
mask and a mask of the other resource kind; every refusal leaves the mask and
all other outputs unchanged; `lgj_hop` is checked per mask; the earlier
relabelling hazard is now a refusal.
- **Supersedes** the "No rule was changed" line of the entry below.

## 2026-11-12 — two characterisations (tests only; merged in #95)

**The two lowerings emit the same `Program`** (`lowering_convergence.rs`,
`both_lowerings_emit_the_same_program`). For the 56 plans with at least one AND
Expand Down Expand Up @@ -29,9 +49,9 @@ against five mask origins, asserted as a table:
(617), the four consumer mains (153) and the TinkerPop harness (80) all pass.
`bench/` was not run. No rule was changed.

## 2026-11-12 — ABI minor 13: a plan folded to one scalar, with no mask (D-LGJ-SCALAR-1, in a draft PR)
## 2026-11-12 — ABI minor 13: a plan folded to one scalar, with no mask (D-LGJ-SCALAR-1, merged in #95)

Status: implemented and measured; in a draft PR on top of the merged P0 fix (#94).
Status: merged in #95 (`74cda93`), on top of the P0 fix (#94).

- **Added:** `lgj_plan_scalar` (`docs/abi.md` §21) with `LGJ_FOLD_COUNT` and
`LGJ_FOLD_SUM_I32`. `plan_lower::lower_scalar` reuses `lower_plan`'s ops and
Expand Down
3 changes: 2 additions & 1 deletion .claude/board/STATUS_BOARD.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,6 @@ same lowering: lance-graph #1257 (SAP CATS, merged).
|---|---|---|
| D-LGJ-FOLD-4 | ABI minor 12 `lgj_plan_group_sum_i32` + `View.sumByGroup` / `sumByGroupVia` + `GroupTotals`: a `GROUP BY … SUM` as ONE program, one crossing, no selection (`docs/abi.md` §20) | **DONE 2026-09-22** — native 191 tests, Java 612 checks (JDK 28), 1 crossing measured beside the 32-crossing path, six disable arms red-then-green |
| D-LGJ-FOLD-5 | Migrate `consumers/bricks` `sumBy()` from the 32-crossing per-group path onto `sumByGroup` (re-pin BricksAuthTest's crossing arithmetic 32 → 1) | **DONE 2026-09-22** — one fused call; **1 crossing at 1,000 and 64,000 rows AND at 1, 16 and 64 groups**. The group-count arm is new and load-bearing: the public `sumBy()` always asks for `Orders.REGIONS` groups, so nothing previously reachable varied the group count and "one crossing whatever the group count" was unfalsifiable; a package-private `sumByGroupCount` exists for that arm alone (invisible to `getMethods()`, so the aggregate-egress guard still audits exactly the public surface). Disable arm A (revert to the per-group loop) measured the old cost formula exactly — **2 / 32 / 128 at 1 / 16 / 64 groups, identical at both row counts, i.e. `2 × groups`** — with parity GREEN throughout, so the two paths agree on the answer and differ only in cost. `Orders.REGIONS` replaces three literal 16s with one named source, a MIRROR of the native generator's classid cardinality (the manifest does not report it) pinned two-sided against the fixture rather than against a copied literal; arms B (20) and C (8) each fire a DIFFERENT half, so neither half alone catches both directions. BricksAuthTest **70/70** (was 62) |
| D-LGJ-SCALAR-1 | ABI minor 13 `lgj_plan_scalar`: count and sum folded straight from the plan with no mask (`docs/abi.md` §21); `lgj_plan_eval` unchanged | **Implemented 2026-11-12; in a draft PR.** Native 205 tests, Java 617 checks, consumers unchanged; `sumOf()` 2 crossings to 1; mask write removed; native latency: count unchanged, sum 3–25% faster |
| D-LGJ-SCALAR-1 | ABI minor 13 `lgj_plan_scalar`: count and sum folded straight from the plan with no mask (`docs/abi.md` §21); `lgj_plan_eval` unchanged | **DONE 2026-11-12, merged in #95.** Native 205 tests, Java 617 checks, consumers unchanged; `sumOf()` 2 crossings to 1; mask write removed; native latency: count unchanged, sum 3–25% faster |
| D-LGJ-MASK-PARENT-1 | ABI minor 14: every export that takes a mask beside a resource requires the mask's parent to be that resource (`docs/abi.md` §3.1); nine arguments previously checked only the row count | **In a PR, 2026-11-12.** `provenance.rs` re-pinned (12 exports × 5 origins); refusals leave outputs unchanged |

77 changes: 67 additions & 10 deletions docs/abi.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ semantic API (see `architecture.md`). Therefore:

```
LGJ_ABI_MAJOR = 0 // incompatible change ⇒ bump; Java refuses to load
LGJ_ABI_MINOR = 12 // additive change ⇒ bump; older Java may still load
LGJ_ABI_MINOR = 14 // additive change ⇒ bump; older Java may still load
LGJ_MAGIC = 0x4C_47_4A_5F_41_42_49_00 // "LGJ_ABI\0" big-endian-read
```

Expand Down Expand Up @@ -216,6 +216,10 @@ required — a gate that rejected everything would satisfy a rejection-only test
- **Minor 13** (2026-11-12) — `lgj_plan_scalar` (§21): the fused plan folded
straight into a count or a sum for a caller that asked for no mask.
`lgj_plan_eval` is unchanged. No new status, no manifest growth.
- **Minor 14** (2026-11-12) — no new symbol. A mask is accepted only against
the resource it was created over, in every export that takes one (§3.1).
Nine mask arguments that checked only the row count now check the parent.
This narrows accepted input; §3.1 states the compatibility effect.

## 3. Status codes

Expand All @@ -230,7 +234,7 @@ are no error strings across the membrane and no `errno` dependence.
| `-3` | `WRONG_RESOURCE_KIND` | e.g. a mask handle passed where a pattern was required |
| `-4` | `INVALID_LANE` | `lane_id` out of range for this resource |
| `-5` | `LANE_KIND_MISMATCH` | op's element type ≠ lane's element type |
| `-6` | `MASK_LENGTH_MISMATCH` | mask row-count ≠ resource row-count |
| `-6` | `MASK_LENGTH_MISMATCH` | the mask does not belong with the resource or masks it is used with: another parent, or another row count (§3.1) |
| `-7` | `PARENT_CLOSED` | child (mask) outlived its parent resource |
| `-8` | `VERSION_MISMATCH` | caller's ABI version incompatible |
| `-9` | `LENGTH_OVERFLOW` | requested size overflows `usize`/allocation limit |
Expand All @@ -247,6 +251,61 @@ are no error strings across the membrane and no `errno` dependence.
`INVALID_HANDLE` is deliberately the response to *use-after-close*, not a crash.
See §4.

### 3.1 Mask provenance (ABI minor ≥ 14)

**A mask may be read or written only against the resource it was created
over.** Every export that takes a mask beside a resource compares the mask's
parent with that resource by registry identity. Equal row count, equal
resource kind and equal contents are not identity: two resources opened from
the same `(rows, seed)` are two populations.

The checks, in order, for each mask argument:

1. the handle is live and is a mask (`INVALID_HANDLE`, `WRONG_RESOURCE_KIND`);
2. its parent is still open (`PARENT_CLOSED`);
3. its parent is the resource of this call, and its row count matches
(`MASK_LENGTH_MISMATCH`).

A refusal happens before any output is written: the destination mask and every
scalar or buffer output hold what they held before the call.

`MASK_LENGTH_MISMATCH` is kept for the third check. The mask algebra (§7) and
the register sweeps (§14 to §16) already report a foreign parent with this
code, and it is the one status whose meaning is "these do not belong
together". No status is added.

**What changed at minor 14.** Until then nine mask arguments were checked for
row count only: `lgj_op_eq_u32`, `lgj_op_gt_i32`, `lgj_plan_eval`,
`lgj_reduce_sum_i32`, `lgj_reduce_i32`, `lgj_op_eq_classid`,
`lgj_op_ternary_match`, and the source and the destination of `lgj_hop`. This
document stated that reading as deliberate. It allowed a predicate evaluated on
resource B to land in a mask belonging to resource A, and A's values to be
summed over it, with status `OK` and a number that answered neither.

**Compatibility.** This narrows accepted input; it is not an additive change.

- A caller that only ever pairs a mask with the resource it was created over
sees no difference. Every Java build in this repository is such a caller:
the facade creates a mask from its owner and uses it with that owner only.
With the rule applied, `AllTests`, the four consumer mains and the TinkerPop
harness pass unchanged.
- A raw-ABI caller that used a mask across two equally sized resources now
receives `MASK_LENGTH_MISMATCH` where it received `OK`.
- §2 reserves the major for incompatible change. The major is not bumped
here: doing so would make every existing Java build refuse to load a library
that serves it correctly, for the sake of a pairing no shipped caller makes.
The minor is bumped instead, so the stricter rule is visible in the manifest
and a caller can test for it. This is a deliberate exception to the letter
of §2, recorded here rather than left implicit.

What the rule does not restrict: several masks over one resource, one mask
used with several lanes or facets of its resource, a mask aliased as source
and destination, and any number of schemas or class readings over one
resource. Those all share one parent.

Pinned by `native/lgj-abi/src/exports/tests/provenance.rs`: twelve exports
against five mask origins.

## 4. Ownership, lifetime, and the generation-checked handle

This is the part of the design that Java cannot get from Rust for free. Inside
Expand Down Expand Up @@ -854,9 +913,9 @@ i32 lgj_hop(u64 store, u32 edge_classid, u64 facet_mask, u32 decode_mode,
```

`store` must be a `LGJ_RESOURCE_ROWSTORE` (§11); `src_mask` and `dst_mask`
must be row-count-compatible with it — the same row-count-only reading
`lgj_op_eq_classid` already uses (a mask from an equally sized but
distinct resource is accepted; `MASK_LENGTH_MISMATCH` otherwise).
must each belong to it (§3.1): a mask over any other resource, equally
sized or not, is `MASK_LENGTH_MISMATCH`. The two masks are checked
separately. Until minor 14 only the row count was checked.

**Semantics.** `dst_mask` is OVERWRITTEN with the one-hop reachable set:
for every row `r` set in `src_mask`, for every facet `f` in the *effective
Expand Down Expand Up @@ -1535,11 +1594,9 @@ so the answer is `UNSUPPORTED_LAYOUT` (`-18`) — the same deferral-stated-as-a-
status the register-sweep family already uses. Checked BEFORE the mask is
resolved, so `dst_mask` is provably untouched on a refusal.

**Mask compatibility is row-count only**, matching `lgj_op_eq_classid` — the
closest sibling, a predicate that writes a mask — rather than the register
sweeps' stricter parent-identity check. abi.md names only the row-count
condition for a predicate's destination (§3), and inventing a second reading for
one new predicate is exactly the drift this membrane exists to prevent.
**The mask must belong to this store** (§3.1), as for every export that takes a
mask beside a resource. Until minor 14 this predicate checked only the row
count, matching `lgj_op_eq_classid` as it then was.

**Kernel.** `ndarray::simd::ternary_match_strided_to_mask`, which lowers the
test to one `ternlog::<XOR_AND>` per 16 records plus a compare-against-zero —
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public enum Status {
WRONG_RESOURCE_KIND(-3, "a handle of the wrong resource kind was passed"),
INVALID_LANE(-4, "lane_id out of range for this resource"),
LANE_KIND_MISMATCH(-5, "the operation's element type does not match the lane's"),
MASK_LENGTH_MISMATCH(-6, "mask row-count does not match resource row-count"),
MASK_LENGTH_MISMATCH(-6, "mask does not belong to this resource (another parent, or another row count)"),
PARENT_CLOSED(-7, "the mask outlived its parent resource"),
VERSION_MISMATCH(-8, "caller's ABI version is incompatible"),
LENGTH_OVERFLOW(-9, "requested size overflows the allocation limit"),
Expand Down
14 changes: 12 additions & 2 deletions native/lgj-abi/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,16 @@ pub const LGJ_ABI_MAJOR: u32 = 0;
/// `lgj_plan_eval` is unchanged: it still lands the mask and returns its
/// count, for callers that demand the mask. No new status and no manifest
/// growth, so a minor-12 Java loads and sees none of it.
pub const LGJ_ABI_MINOR: u32 = 13;
///
/// **Minor 14** (docs/abi.md §3.1): no new symbol. A mask is accepted only
/// against the resource it was created over, in every export that takes a
/// mask beside a resource. Nine exports checked only the row count until now
/// (`lgj_op_eq_u32`, `lgj_op_gt_i32`, `lgj_plan_eval`, `lgj_reduce_sum_i32`,
/// `lgj_reduce_i32`, `lgj_op_eq_classid`, `lgj_op_ternary_match` and both
/// masks of `lgj_hop`). This NARROWS what those exports accept; it is not
/// additive. The minor is bumped so the change is visible in the manifest;
/// §3.1 states the compatibility effect.
pub const LGJ_ABI_MINOR: u32 = 14;

/// `"LGJ_ABI\0"` read big-endian.
///
Expand All @@ -127,7 +136,8 @@ pub const LGJ_ERR_WRONG_RESOURCE_KIND: i32 = -3;
pub const LGJ_ERR_INVALID_LANE: i32 = -4;
/// The op's element type ≠ the lane's element type.
pub const LGJ_ERR_LANE_KIND_MISMATCH: i32 = -5;
/// Mask row-count ≠ resource row-count.
/// The mask does not belong with the resource or masks it is used with:
/// another parent, or another row count (docs/abi.md §3.1).
pub const LGJ_ERR_MASK_LENGTH_MISMATCH: i32 = -6;
/// A child (mask) outlived its parent resource.
pub const LGJ_ERR_PARENT_CLOSED: i32 = -7;
Expand Down
47 changes: 25 additions & 22 deletions native/lgj-abi/src/exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -896,12 +896,12 @@ fn resolve_pattern_and_mask(
dst_mask: u64,
) -> Result<(std::sync::Arc<ResourceEntry>, std::sync::Arc<ResourceEntry>), i32> {
let pattern = registry::resolve_kind(res, LGJ_RESOURCE_PATTERN)?;
let (mask, _parent) = registry::resolve_mask_with_parent(dst_mask)?;
// abi.md names only the row-count condition here (§3 MASK_LENGTH_MISMATCH),
// so a mask from a *different but equally sized* pattern is accepted. That
// is a deliberate reading: the kernels care about length, and the Java layer
// never constructs such a pairing.
if mask.n_rows != pattern.n_rows {
let (mask, parent) = registry::resolve_mask_with_parent(dst_mask)?;
// The mask must belong to THIS pattern (abi.md §3.1, minor 14): its parent
// is compared by registry identity, never by row count, kind or contents.
// Until minor 14 only the row count was checked, so a predicate evaluated
// on one pattern could land in a mask belonging to another.
if !std::sync::Arc::ptr_eq(&parent, &pattern) || mask.n_rows != pattern.n_rows {
return Err(LGJ_ERR_MASK_LENGTH_MISMATCH);
}
Ok((pattern, mask))
Expand Down Expand Up @@ -1012,11 +1012,12 @@ pub extern "C" fn lgj_op_eq_classid(res: u64, facet: u32, needle: u32, dst_mask:
if facet >= crate::rowstore::ROW_FACETS {
return LGJ_ERR_INVALID_LANE;
}
let (mask, _parent) = match registry::resolve_mask_with_parent(dst_mask) {
let (mask, parent) = match registry::resolve_mask_with_parent(dst_mask) {
Ok(t) => t,
Err(e) => return e,
};
if mask.n_rows != store_entry.n_rows {
// The mask must belong to THIS store (abi.md §3.1, minor 14).
if !std::sync::Arc::ptr_eq(&parent, &store_entry) || mask.n_rows != store_entry.n_rows {
return LGJ_ERR_MASK_LENGTH_MISMATCH;
}
let mut g = match mask.write_mask() {
Expand Down Expand Up @@ -1072,11 +1073,9 @@ pub extern "C" fn lgj_op_eq_classid(res: u64, facet: u32, needle: u32, dst_mask:
///
/// # Mask compatibility
///
/// Row-count only, matching [`lgj_op_eq_classid`] — the closest sibling, a
/// predicate that WRITES a mask — rather than the register sweeps' stricter
/// parent-identity check. abi.md names only the row-count condition for a
/// predicate's destination, and inventing a second reading for one new
/// predicate would be the drift this membrane is built to prevent.
/// The mask must belong to this store (abi.md §3.1, minor 14), as for
/// [`lgj_op_eq_classid`] and every other export that takes a mask beside a
/// resource.
///
/// # Bulk (§6)
///
Expand Down Expand Up @@ -1116,11 +1115,12 @@ pub unsafe extern "C" fn lgj_op_ternary_match(
if facet >= crate::rowstore::ROW_FACETS {
return LGJ_ERR_INVALID_LANE;
}
let (mask, _parent) = match registry::resolve_mask_with_parent(dst_mask) {
let (mask, parent) = match registry::resolve_mask_with_parent(dst_mask) {
Ok(t) => t,
Err(e) => return e,
};
if mask.n_rows != store_entry.n_rows {
// The mask must belong to THIS store (abi.md §3.1, minor 14).
if !std::sync::Arc::ptr_eq(&parent, &store_entry) || mask.n_rows != store_entry.n_rows {
return LGJ_ERR_MASK_LENGTH_MISMATCH;
}

Expand Down Expand Up @@ -2625,10 +2625,9 @@ pub unsafe extern "C" fn lgj_plan_group_sum_i32(
// ───────────────────────────────────────────────────────────────────────────

/// Resolve `(rowstore, src_mask, dst_mask)` for `lgj_hop`, checking that
/// both masks are row-count-compatible with the store — the same
/// row-count-only reading `resolve_pattern_and_mask` already uses (a mask
/// from a different but equally sized resource is accepted; abi.md names
/// only the row-count condition).
/// both masks belong to the store (abi.md §3.1, minor 14): each mask's
/// parent is compared with the store by registry identity, separately for
/// the source and for the destination.
#[allow(clippy::type_complexity)]
fn resolve_rowstore_and_hop_masks(
store: u64,
Expand All @@ -2643,9 +2642,13 @@ fn resolve_rowstore_and_hop_masks(
i32,
> {
let store_entry = registry::resolve_kind(store, LGJ_RESOURCE_ROWSTORE)?;
let (src, _src_parent) = registry::resolve_mask_with_parent(src_mask)?;
let (dst, _dst_parent) = registry::resolve_mask_with_parent(dst_mask)?;
if src.n_rows != store_entry.n_rows || dst.n_rows != store_entry.n_rows {
let (src, src_parent) = registry::resolve_mask_with_parent(src_mask)?;
let (dst, dst_parent) = registry::resolve_mask_with_parent(dst_mask)?;
if !std::sync::Arc::ptr_eq(&src_parent, &store_entry)
|| !std::sync::Arc::ptr_eq(&dst_parent, &store_entry)
|| src.n_rows != store_entry.n_rows
|| dst.n_rows != store_entry.n_rows
{
return Err(LGJ_ERR_MASK_LENGTH_MISMATCH);
}
Ok((store_entry, src, dst))
Expand Down
Loading
Loading