Skip to content

RED-217728 Allow pre-command hooks to abort JSON commands on error, make pre-command hook optional - #1653

Merged
AvivDavid23 merged 3 commits into
masterfrom
RED-217728-precommand-may-error
Sep 28, 2026
Merged

AvivDavid23 merged 3 commits into
masterfrom
RED-217728-precommand-may-error

Conversation

@AvivDavid23

@AvivDavid23 AvivDavid23 commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Note

Medium Risk
Changes macro contracts and command dispatch for all JSON commands; LLAPI behavior intentionally differs (hook errors ignored), which downstream embedders must understand when migrating hooks from () to Ok(()).

Overview
Makes the pre_command_function hook optional on redis_json_module_create! / redis_json_module_export_shared_api! and changes it to return RedisResult<()> so JSON command wrappers can abort before manager dispatch when the hook errors. OSS drops its no-op hook; run_on_manager! still runs an optional hook for LLAPI setup but discards errors so shared C API reads are not blocked.

Adds as-library unit tests for success/error hook paths and documents the contract in AGENTS.md. CI, Makefile, and workflows now run cargo test -p redis_json --features as-library alongside the default test suite.

Reviewed by Cursor Bugbot for commit d5ab0a7. Bugbot is set up for automated code reviews on this repo. Configure here.

@codecov

codecov Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.84%. Comparing base (ac573d8) to head (d5ab0a7).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1653      +/-   ##
==========================================
- Coverage   85.97%   85.84%   -0.14%     
==========================================
  Files          15       15              
  Lines        5306     5278      -28     
==========================================
- Hits         4562     4531      -31     
- Misses        744      747       +3     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 8c00e22. Configure here.

Comment thread redis_json/src/lib.rs Resolved
@AvivDavid23 AvivDavid23 changed the title RED-217728 Allow pre-command hooks to abort JSON commands on error RED-217728 Allow pre-command hooks to abort JSON commands on error, make pre-command hook optional Sep 28, 2026

@gabsow gabsow left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewed at 3205244. Built and ran both feature configurations locally and checked the downstream RedisJsonHdt consumer.

What looks right: the hook still runs before manager selection on both the command and shared-API paths, so HDT's set_curr_ctx ordering is preserved; json_forget → json_del still fires the hook exactly once; all ~26 JSONAPI_* sites were updated consistently; and no LLAPI version/struct bump is needed since no field was added. Default-feature cargo test -p redis_json passes.

Main concerns (details inline):

  1. Two of the three new hook tests never run in CI. pre_command_error_skips_command and pre_command_success_runs_command are gated on --features as-library, which no CI job enables. The PR's headline semantics are therefore unverified by CI, and this is a likely contributor to the failing codecov/project check.
  2. pre_command_success_runs_command passes for an accidental reason. It only sees Ok(()) because non-hygienic macro_rules item resolution lets the function-local pre_command shadow the module-level erroring one. Verified: renaming the local helper makes the test fail. A thread_local! switch would make the intent explicit and also let all three cases run under default features, fixing (1) at the same time.
  3. Swallowing the hook error in run_on_manager! deserves a log line and a doc entry. Keeping LLAPI behavior unchanged is defensible, but a fully discarded error in a RediSearch-driven path is hard to diagnose, and the command/LLAPI divergence should be documented in AGENTS.md's LLAPI section rather than only in a macro comment.
  4. Making pre_command_function optional weakens the contract for the consumer that needs it. HDT's hook is mandatory setup; omitting the key now compiles and fails at runtime instead of at build time. Passing an explicit no-op at the OSS site would drop the no-op function without loosening the macro.
  5. Coordinate the downstream break. HDT's hook returns () and won't compile after this merges — worth linking the paired PR.

Smaller items not worth inline comments:

  • command_without_pre_command_runs is cfg(not(as-library)) while the others are cfg(as-library), so no single build exercises both branches of the new $(...)? optionality. Same root cause as (1)/(2).
  • Commit 2's message, "make pre command hook optionaly", has a typo and drops the RED-217728 prefix that commit 1 carries — worth squashing or rewording.
  • No flow-test coverage of the new behavior. Understandable now that OSS has no hook, but a smoke test on the HDT side (command rejected while the LLAPI still reads the same key) would be the real end-to-end guard for concern (3).

Nothing here is a blocking correctness bug in the production path; (1) and (2) are the ones I'd want resolved before merge, since as it stands the new behavior has no CI coverage.


Generated by Claude Code

Comment thread redis_json/src/lib.rs Resolved
Comment thread redis_json/src/lib.rs Resolved
Comment thread redis_json/src/lib.rs Resolved
Comment thread redis_json/src/lib.rs Resolved
Comment thread redis_json/src/lib.rs
macro_rules! json_command {
($cmd:ident) => {
|ctx: &Context, args: Vec<RedisString>| -> RedisResult {
$($pre_command_function_expr(ctx, &args)?;)?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is a breaking change for the downstream consumer — worth linking the paired PR in the description so the two land together.

RedisJsonHdt/src/lib.rs:867 currently declares fn pre_command(ctx: &Context, _args: &Vec<RedisString>) returning (). Once ? is applied here, that crate won't compile until the signature becomes RedisResult<()>. (The &args → &Vec<RedisString> coercion is fine, so the signature return type is the only required change.)

Ordering is preserved correctly, for what it's worth: the hook still runs before manager selection, so HDT's set_curr_ctx happens before is_hdt_db() is evaluated — and json_forget → json_del still fires the hook exactly once.


Generated by Claude Code

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Leaving this one open — it's the only item from my review still outstanding, and I'd rather it stay visible than get resolved on a documentation-only fix.

The AGENTS.md migration note ("Existing hooks returning () must return Ok(()) after setup") helps, but I checked and there's no paired RedisJsonHdt PR queued. On the blast radius: HDT declares redis_json = { git = "...", branch = "master", features = ["as-library"] } but commits a Cargo.lock pinned to rev 1c74f65, so this won't break the moment it merges — it breaks on the next routine pin bump, with a fairly opaque "? cannot be applied to ()" error at the macro expansion site. Queuing the one-line HDT change now seems better than leaving that trap for whoever runs the next cargo update.

One note for whoever writes it: per the new contract, setup has to complete before any Err return, since the LLAPI path ignores the error and proceeds. set_curr_ctx is unconditional today so HDT satisfies this, but a guard added to that same hook later must not early-return ahead of it.


Generated by Claude Code

Comment thread redis_json/src/lib.rs Resolved
Comment thread redis_json/src/lib.rs Resolved

gabsow commented Sep 28, 2026

Copy link
Copy Markdown
Collaborator

Re-reviewed at d5ab0a7. Everything from my earlier pass is addressed — thanks for the quick turnaround. I re-ran both feature configurations locally rather than relying on the diff:

  • cargo test -p redis_json → 17 tests, green.
  • cargo test -p redis_json --features as-library → 18 tests, green, and order-independent under --test-threads=1.
  • The self::pre_command + HOOK_FAILS rework means the abort path is now genuinely covered: the error test sets the flag and command_handler would panic! if the hook had returned Ok.
  • The two warnings under --features as-library (unused command-impl imports, dead version()) pre-exist on master, so the new CI leg isn't adding noise.

I've resolved six of my seven threads. The one I left open is the downstream coordination: there's no paired RedisJsonHdt PR queued yet, and because HDT tracks branch = "master" behind a committed Cargo.lock (pinned at 1c74f65), the break surfaces on the next pin bump rather than at merge — details in that thread.

Otherwise this looks good to me. Everything completed in CI is green (clippy, linter, sanitizer, coverage, memory-regression, Bugbot); only codecov/project is red on the project-coverage threshold while codecov/patch passes, and the platform builds are still running.


Generated by Claude Code

@gabsow gabsow left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Approved at d5ab0a7. The CI coverage gap is addressed by running the as-library tests, hook success/error selection is explicit, and the command/LLAPI contract is documented. No remaining blocking code issue found in this review.

Please keep the HDT migration follow-up visible: its hook must return RedisResult<()> with Ok(()) after setup when updating the RedisJSON dependency. The codecov/project threshold failure also remains to be resolved or accepted through the normal maintainer process; codecov/patch and the build/test checks pass.

@AvivDavid23
AvivDavid23 merged commit 2c01c80 into master Sep 28, 2026
59 of 60 checks passed
@AvivDavid23
AvivDavid23 deleted the RED-217728-precommand-may-error branch September 28, 2026 15:12
AvivDavid23 added a commit that referenced this pull request Sep 29, 2026
AvivDavid23 added a commit that referenced this pull request Sep 29, 2026
AvivDavid23 added a commit that referenced this pull request Sep 29, 2026
AvivDavid23 added a commit that referenced this pull request Sep 29, 2026
AvivDavid23 added a commit that referenced this pull request Sep 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants