Repository navigation
Description
Task
T-022· Milestone M2 — Scenario + kinematic2d · Size S · Priority P1 · Epic #2Implements FR-SCN-004 · NFR-SEC-003
Branch
feat/t-022-constraint-evaluator-restricted-ast-whitelist— one task = one branch = one PR (doc 32 §6)
Summary
Implement scenario/constraints.py: parse constraint expressions (e.g. robot.max_speed * comms.cmd_delay_ms <= 0.1) into a whitelisted AST and evaluate them safely; rejection sampling with max attempts.
Context / motivation
Constraints must never become an eval injection vector (TM-03). The Specification pattern (doc 34 §4) makes them composable.
Scope
In scope
- Allowed nodes: numeric literals, parameter names (dotted ids),
+ - * / **, comparisons,and/or/not, parentheses,abs/min/max. - Everything else (attribute access on non-params, calls, subscripts, lambdas, dunder names) rejected at load time with a precise error.
Constraint.evaluate(values) -> boolvectorised over(n,d)batches.rejection_sample(sampler, space, n, rng, max_attempts)raisingValidationErrorafter max attempts (default 1000 → settings key).
Implementation guidance
- Parse with
ast.parse(expr, mode='eval'), walk with an explicitNodeVisitorwhitelist, compile to a closure over NumPy operations — never calleval/compileon user input. - Standards: respect the layering contracts (doc 16 §2) and conventions in doc 34; no hard-coded tunables — thresholds/defaults come from typed settings (doc 27); no magic numbers (ruff
PLR2004); public APIs typed (mypy strict) and documented.
Configuration keys
From the configuration reference (doc 27). Keys marked (new) must be added to the typed settings models and appear in the generated reference (T-003c).
- scenario.constraint_max_attempts (new key, default 1000 — add to settings + doc 27)
Acceptance criteria
-
P1 * P6 <= 0.1never yields a violating scenario in 10 000 samples. - Expressions with
__import__,().__class__,open(...), lambdas are rejected at load time. - Fuzz test (Hypothesis-generated expressions) never raises anything but
ValidationError. - Tests listed below added and green in CI; coverage of new code meets the gate in doc 18 §3.
- Docs, config reference and CHANGELOG
Unreleasedupdated where behaviour, keys or interfaces changed. - Definition of Done (doc 32 §3) satisfied.
Tests required
tests/unit/scenario/test_constraints.py,tests/property/scenario/test_constraints_fuzz.py.
Dependencies
Blocked by: #33 (T-020b)
Blocks: #84 (T-050a), #161 (T-109)
Parallelization notes
Files / paths owned by this task — avoid editing other paths; shared files (pyproject.toml entry points, cli/main.py, api/routers/__init__.py, router.tsx, docker/compose.yaml, ci.yml) are append-only:
backend/src/roboprobe/scenario/constraints.py- backend/src/roboprobe/core/settings/ (one new field)
Parallel with T-021/T-023/T-024.
Definition of Done
Per doc 32 §3 and AGENTS.md §5: lint, format, mypy strict and import-linter green; tests added and passing; docs updated; PR template completed with Closes #<this issue>; Conventional Commit messages referencing T-022 (doc 16 §6); commits authored only by the repository owner identity (AGENTS.md §4); CI green; reviewer checklist (doc 32 §5.1) satisfied.
References
Design docs: doc 06 §5 · doc 02 §3.1 · doc 31 §3 · doc 21 §1 · doc 34 §4
Requirements: FR-SCN-004 · NFR-SEC-003 (traceability: doc 28)
External:
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsDone
Activity
warehouse_v1.yaml#33