Skip to content

refactor(stack-encrypt): move the JsonDocument EQL simulators into eql_simulator - #1758

Merged
coderdan merged 2 commits into
nextfrom
refactor/stack-encrypt-eql-simulator
Nov 23, 2026
Merged

coderdan merged 2 commits into
nextfrom
refactor/stack-encrypt-eql-simulator

Conversation

@coderdan

@coderdan coderdan commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Stack Encrypt's JsonDocument loses four public methods. Each one copies, in Rust, what the database (EQL) does when your app reads part of an encrypted JSON document. The same code is now in a new module, stack_encrypt::eql_simulator, behind a new Cargo feature, eql-simulator. The feature is off by default. Production code never needed these methods, so the move only affects tests and examples that read a stored document without PostgreSQL.

Changes

The four methods moved to functions in stack_encrypt::eql_simulator. The names follow the EQL SQL function each one copies, where a public one exists.

Removed method New function What it copies
doc.children(token) json_tree_children(&doc, token) eql_v4.json_tree_children
doc.subtree(tokens) json_tree_subtree(&doc, tokens) eql_v4.json_tree_subtree(doc, tokens, 0)
doc.find(token) find_by_token(&doc, token) How the EQL functions find the entry a token names
doc.node(slot) entry_row(&doc, slot) eql_v4_internal.json_tree_graft, the row for one entry

Library code:

  • The code of the four read helpers now lives in stack_encrypt::eql_simulator itself: json_tree_children, json_tree_subtree (with a private walk), find_by_token, and entry_row.
  • JsonDocument keeps only a crate-private find, because decrypting a whole document uses it. find_by_token calls it.
  • Each behaviour is documented once, on the public function.
  • The module compiles with the off-by-default eql-simulator feature, or in this crate's unit tests. The public docs show it only when the feature is on.
  • To reach the tag helpers, derive::parent_tag and derive::hit_token are now pub(crate).
  • The module docs start with three facts. The module copies EQL. Production code does not use it. The feature exists so tests and examples run without PostgreSQL. Each function's doc names the SQL function it copies.
  • The crate docs, in the section on testing, mention the feature in plain text, with no link. A build without the feature does not link to the module.

Callers:

  • stack-encrypt's integration tests and doctests turn the feature on through a dev-dependency on the crate itself. That dependency turns default features off, so --no-default-features test builds still leave out http.
  • The EQL encryption test crate (packages/eql/tests/encryption) turns the feature on in its Cargo.toml.
  • No fuzz crate, bench, or Go guest used the methods.
  • Ten test files call the new functions. The comments in those files name eql_simulator::json_tree_subtree instead of JsonDocument::subtree.
  • The two doc examples that read one level of a document (JsonSubtreeKey::open_child and JsonChildPlaintext) call the module. Each one has one comment that shows the SQL query the call stands in for.

Documentation:

  • The stack-encrypt CHANGELOG has a "Breaking" entry under Unreleased with the upgrade steps.
  • AGENTS.md and the JSON tree design plan name the new module.

Verification

I ran these on this branch. Each one passed unless the line says otherwise.

  • cargo fmt --check for stack-encrypt and for the EQL workspace.
  • cargo clippy -p stack-encrypt -- -D warnings with every feature, with --no-default-features, and with default features. Each ran with --all-targets, and again on the library alone, where the new feature stays off.
  • cargo build -p stack-encrypt with default features and with --no-default-features, so the library builds without eql-simulator.
  • cargo test -p stack-encrypt --all-features: 1,655 tests passed and 1 was ignored. The count includes the tests/ui snapshots, and they did not change. With --no-default-features, 1,052 tests passed. The module's doc example and the two updated doc examples ran in both builds.
  • cargo doc --no-deps -p stack-encrypt with RUSTDOCFLAGS='-D warnings', with default features, with --no-default-features, and with every feature. No page in the builds without the feature links to eql_simulator.
  • The EQL workspace: cargo test -p eql-bindings -p eql-codegen -p eql-encryption-tests --all-features passed 622 tests, with 65 ignored, because those tests need PostgreSQL. I did not run the PostgreSQL suite. Clippy passed.
  • cargo doc for eql-bindings fails with 10 rustdoc errors, such as unresolved link to super::all. This branch does not change eql-bindings, and an earlier branch in this stack showed the same 10 errors.
  • I rebuilt the four encrypt guests and the auth guest without the feature. go test ./... passed in languages/golang, and go generate ./... changed no file.
  • cargo mutants --in-diff on this branch's diff found 15 mutants. Tests caught 4. The other 11 did not compile. No mutant survived.
  • I did not run cargo udeps. That check needs a pinned nightly toolchain that is not installed here.

Related

Closes #1755

Follows #1751, now merged. That pull request added JsonSubtreeKey::open_child and the doc examples this branch updates.

Review notes

  • Names. EQL has public SQL functions for two of the four, so those two take the SQL names. find_by_token has no SQL function of its own: the EQL functions look the token's self tag up in the map that eql_v4_internal.json_tree_index builds. entry_row copies eql_v4_internal.json_tree_graft. That function is internal and its name says little to a reader, so the Rust name says what the function returns.
  • Signatures are unchanged. Pass the document first, then the method's argument. Each function returns what its method returned. A function that returns rows with their depth already set, as the SQL does, would remove a JSON round trip that several test files repeat. I left that out to keep this change a move.
  • docs.rs. Cargo.toml already builds docs.rs pages with every feature on, so the module shows on docs.rs. I kept that, because test authors need to find the module. The module's first lines say what it is and that it needs the feature.
  • The self dev-dependency is path-only, so cargo publish drops it. The dependency adds stack-encrypt to its own dependency list in Cargo.lock.
  • No stored byte changes.

@coderdan
coderdan requested a review from a team as a code owner November 23, 2026 01:03
@changeset-bot

changeset-bot Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 0d3ce6d

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI; Enforced overrides: Organization
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 648af389-b310-4e1e-a1ff-394aebb9d361

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain for the change separating database-read simulation from the production JSON API.

No open issues 🎉

What changed in this PR

Separates database-read simulation from Stack Encrypt’s production JSON API, addressing #1755 and updating the child-read examples added in #1751. Stored bytes and production decryption behavior remain unchanged.

Changes:

  • Moves four public JsonDocument helpers into eql_simulator, behind an off-by-default feature, while retaining private lookup for whole-document decryption.
  • Enables the feature for tests and migrates callers and examples to the new functions.
  • Documents the database’s role and provides breaking-change migration steps.
File Description
packages/​stack-encrypt/​tests/​json_tree_vectors.rs Uses simulator token lookup for test vectors.
packages/​stack-encrypt/​tests/​json_read_checks.rs Migrates read-authentication test helpers.
packages/​stack-encrypt/​tests/​json_properties.rs Migrates property tests and updates explanatory comments.
packages/​stack-encrypt/​tests/​json_owned.rs Migrates owned-document read helpers.
packages/​stack-encrypt/​tests/​json_operators.rs Uses simulator functions to construct operator test rows.
packages/​stack-encrypt/​tests/​json_leakage/​mod.rs Updates simulator naming in comments only.
packages/​stack-encrypt/​tests/​json_leakage/​distinguishers.rs Updates walk descriptions in comments only.
packages/​stack-encrypt/​tests/​json_leakage/​chain.rs Updates the simulator reference in a comment only.
packages/​stack-encrypt/​tests/​json_leakage/​calibration.rs Compares the leakage harness with the relocated simulator.
packages/​stack-encrypt/​tests/​json_index.rs Migrates JSON index tests to simulator functions.
packages/​stack-encrypt/​tests/​json_field_references.rs Migrates token lookup and child-read tests.
packages/​stack-encrypt/​tests/​heap_hygiene.rs Uses simulator functions to prepare memory-hygiene test inputs.
packages/​stack-encrypt/​src/​lib.rs Exports the feature-gated module and documents test usage.
packages/​stack-encrypt/​src/​json/​query.rs Updates the child-opening example and its SQL explanation.
packages/​stack-encrypt/​src/​json/​plaintext.rs Updates the child-plaintext example and its SQL explanation.
packages/​stack-encrypt/​src/​json/​mod.rs Makes helpers crate-private and gates simulation-only methods.
packages/​stack-encrypt/​src/​eql_simulator.rs Adds documented wrappers around the existing read helpers.
packages/​stack-encrypt/​CHANGELOG.md Records the breaking change and migration steps.
packages/​stack-encrypt/​Cargo.toml Adds the feature and self dev-dependency for tests.
packages/​eql/​tests/​encryption/​tests/​json_tree_field_references.rs Migrates simulated child reads in EQL tests.
packages/​eql/​tests/​encryption/​tests/​json_tree_data_plan.rs Migrates simulated subtree and extraction rows.
packages/​eql/​tests/​encryption/​Cargo.toml Enables the simulator for EQL encryption tests.
docs/​plans/​2026-10-17-json-tree-index.md Documents the helpers’ new location.
Cargo.lock Records the self dev-dependency.
AGENTS.md Documents simulator usage and SQL-parity expectations.

Review effort: Balanced

@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Mutation testing (cargo-mutants, --in-diff, stack-auth + stack-encrypt + stack-encrypt-jsonpath)

caught missed unviable timeout
14 0 14 0

Every mutant in the changed lines was caught by a test.

@cipherstash-agent cipherstash-agent left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review attestation

Reviewed 63fa97c3 · posted from Claude Code (scheduled routine) · claude-opus-5-5 · effort medium
Sources: code-review · claude-opus-5-5; mattpocock-skills:code-review · claude-opus-5-5; rust-security agent · claude-opus-5-5 (subagents ran with no model override)
Skills: code-review, mattpocock-skills:code-review, company-skills:rust-security, company-skills:review-cross-check, company-skills:review-post

Findings (4): 1 major, 3 minor. 3 inline, 1 below. rust-security found nothing: eql-simulator stays off in every shipped build (root workspace normal edges, the Go guests, eql-bindings, protect-ffi), and the moved functions handle no key or plaintext. Two other candidates were refuted at head: the mismatched cfg gate on node, and feature unification through the self dev-dependency. No current caller triggers either.

  • packages/stack-encrypt/src/json/mod.rs:549-640 minor Issue #1755 item 1 asks to move the four methods into the module, and item 5 keeps only find crate-private. Here children, subtree, walk and node still live in impl JsonDocument, with comments such as "as the server enumerates them". The public surface moved and the code did not. Moving the bodies into eql_simulator.rs is feasible: they need public header()/entries(), and derive::self_tag/parent_tag/hit_token raised to pub(crate). Gate the module any(test, feature) for the unit test at mod.rs:1161. That also removes the duplicated docs.

Generated by Claude Code

Comment thread packages/stack-encrypt/CHANGELOG.md Resolved

@coderdan coderdan left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Human review. LGTM assuming agent findings are addressed.

…l_simulator

`JsonDocument` had four public methods: `find`, `children`, `subtree`,
and `node`. Each one copies, in Rust, part of what the database (EQL)
does when an app reads a stored JSON document. Production code never
calls them, because the database does that work. Tests and doc examples
call them so that they run without PostgreSQL. As public methods, they
looked like part of the normal read, and any change to them was a
breaking change.

The four methods are now functions in a new module,
`stack_encrypt::eql_simulator`. The module is behind a new feature,
`eql-simulator`. The feature is off by default.

- `json_tree_children(&doc, token)` copies `eql_v4.json_tree_children`.
- `json_tree_subtree(&doc, tokens)` copies `eql_v4.json_tree_subtree`
  with a budget of 0.
- `find_by_token(&doc, token)` copies how the EQL functions find the
  entry a token names. EQL has no public function for that step.
- `entry_row(&doc, slot)` copies `eql_v4_internal.json_tree_graft`, the
  row the database returns for one entry. That SQL function is internal,
  so the Rust name says what it does instead.

The new functions call crate-private `JsonDocument` methods, so the code
is not copied. Decrypting a whole document uses `find`, so `find` is
compiled in every build. The other three methods and the private `walk`
compile only with the feature or in unit tests.

stack-encrypt's integration tests and doctests turn the feature on
through a path-only dev-dependency on the crate itself. That dependency
turns default features off, so `--no-default-features` test builds stay
free of `http`. The EQL encryption test crate turns the feature on in its
`Cargo.toml`. No fuzz crate, bench, or Go guest used the methods. The Go
guests build without the feature.

The doc examples call the module and show the SQL each call stands in
for. The CHANGELOG has a Breaking entry with the upgrade steps. No stored
byte changes.

Closes #1755
The `eql_simulator` functions called crate-private `JsonDocument`
methods. So each behaviour was documented twice, once on the method and
once on the function, and nothing kept the two in step.

The code of `children`, `subtree`, `walk`, and `node` is now in
`src/eql_simulator.rs`, as the functions themselves. The docs for each
behaviour exist once. `JsonDocument::find` stays crate-private on `JsonDocument`,
because decrypting a whole document uses it. `find_by_token` calls it,
and has the full doc.

The module reads the document through the public `header()` and
`entries()`. The module also calls `derive::parent_tag` and `derive::hit_token`.
Those two functions are now `pub(crate)`. The `json::derive` module is
`pub(crate)` too. A new crate-private `JsonNode::new` builds the row
that `entry_row` returns.

The module compiles with the `eql-simulator` feature or in unit tests.
So the unit test that walks a long forged chain calls
`eql_simulator::json_tree_subtree`. The public API still shows the
module only when the feature is on. The functions return the same rows,
in the same order, with the same stopping rules.

Also from the review:

- The upgrade line in the CHANGELOG and in the module docs now names
  `version = "0.4"`. The Breaking entry is released as 0.4.0. Version
  0.3.x has no `eql-simulator` feature.
- The doc examples on `JsonSubtreeKey::open_child` and
  `JsonChildPlaintext` say, on the import line, that `eql_simulator`
  needs the `eql-simulator` feature.
@coderdan
coderdan force-pushed the refactor/stack-encrypt-eql-simulator branch from d4ef7cc to 0d3ce6d Compare November 23, 2026 02:09
@coderdan

Copy link
Copy Markdown
Contributor Author

Review 6015579923, "move the bodies": done in 0d3ce6d. The code of the four read helpers is now in stack_encrypt::eql_simulator itself, so JsonDocument keeps only the crate-private find that whole-document decryption needs. Rows, order and stopping rules are unchanged, and the existing tests pass with one call site changed. The branch is also rebased onto next after #1753 merged; the only conflict was the CHANGELOG, where both Breaking entries are kept.

@coderdan coderdan left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Human pass. LGTM.

@coderdan
coderdan merged commit 0e6d604 into next Nov 23, 2026
60 checks passed
@coderdan
coderdan deleted the refactor/stack-encrypt-eql-simulator branch November 23, 2026 03:55

@coderdan coderdan left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Human pass. LGTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

stack-encrypt: move the JsonDocument EQL simulators (find, children, subtree, node) out of the public API

3 participants