Skip to content

[FEAT] - Guardrails core (HEN-139, slice 1) - #97

Draft
Henrik-3 wants to merge 1 commit into
masterfrom
feat/guardrails-core
Draft

Henrik-3 wants to merge 1 commit into
masterfrom
feat/guardrails-core

Conversation

@Henrik-3

@Henrik-3 Henrik-3 commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Slice 1 of HEN-139 (Guardrails). This is the first PR of a stack: UI, test suite, reroute and gateway checks build on it.

What it does

Prompts are checked against admin-defined rules before they reach a model.

  • Data: guard_rules, guard_rule_assignments (everyone, team, user or key) and guard_events, all in the initial schema. Also the admin.guard.view|edit|test|events.view permissions and en/de translations.
  • Checks (src/types/guard/classifier.rs): the check model's type picks the strategy. Decision models get a predicate, or a choice over the policy's categories plus none. Chat models answer with a strict JSON schema (matched, category, confidence, reason). The prompt is sent apart from the instructions, which tell the model to treat it as data only. Checks are billed to the user as guard, or to the admin as guard_test.
  • Evaluation (evaluate.rs): rules run in parallel, each with its own timeout. Any trigger at or above its threshold makes the rule match. Block beats log-only, then priority decides. Monitor rules never act, and new rules start in monitor mode. Block rules fail closed by default.
  • Middleware: GuardMiddleware is registered in build_engine before ProviderRoutingMiddleware. An enforced block answers guard_blocked (403) with the rule's message or a default, plus the reason unless the rule hides it. guard, guard_test and title requests are skipped, and tool steps aren't checked again.
  • Events: one row per checked rule holding score, label, a reason cut to 300 characters, and latency. Never prompt text.
  • API under /api/v1/admin/guard: rules (CRUD, update is PUT with the whole rule), rules/{id}/assignments, and POST test, which runs all rules for a simulated subject, chosen ids or an unsaved draft.
  • Generated TS types and an AGENTS.md section.

Still planned for this PR

  • SystemOne as a check strategy: like decision, with positive and negative examples in if_true / if_false. ProviderKind::SystemOne comes in a separate provider PR outside this stack.
  • Rules apply to web chat only in v1; gateway checks become their own later slice.
  • Replace fit_prompt, which cuts long prompts, with checks over up to 8 chunks where the worst score wins. Anything larger counts as a failed check, so the rule's on_error decides.

Not in this slice

  • GET /events, cost per event and a rate limit on /test (slice 2)
  • Bundling several rules on one decision model into one request
  • The trusted-provider restriction for check models (slice 4)
  • Checks for service-account keys (no user) are recorded but not reserved on a budget.

Tests

src/tests/guard.rs: conflict resolution, monitor mode, on_error, timeouts, verdict parsing, prompt selection, validation and prompt fitting all pass locally. The two DB tests (rule selection per subject, and the middleware blocking end to end with events that hold no prompt text) haven't run locally because Docker isn't running; CI runs them.

Slice 1 of HEN-139: guard rules with assignments and audit, checks through
decision models or structured chat output, GuardMiddleware ahead of provider
routing (guard_blocked on enforced block rules), prompt-free guard events and
an admin test endpoint that runs the same evaluation without acting on it.
@coderabbitai

coderabbitai Bot commented Nov 29, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. 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

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
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

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.

1 participant