Pr b python api#43
Open
shayankashif123 wants to merge 2 commits intoc2siorg:mainfrom
Open
Conversation
- FastAPI app with GET /health and POST /validate endpoints - In-process regex rule engine (6 rules, 24+ patterns) - Pre-filter + sidecar two-layer enforcement flow - Pydantic request/response contracts - 82 tests — all passing, no sidecar required - SDK zero-dependency contract preserved
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Python FastAPI facade and rules engine (Phase 1)
This PR was split out from the original mixed PR following the review comment that flagged the
api/directory as a separateask from the core TypeScript SDK work. The reviewer correctly identified that this is a different proposal — a Python FastAPI
facade with its own rules engine — that should go through its own design discussion before merging.
The TypeScript SDK work that closes #34 has been separated into
PR A and can be reviewed independently.
What this is
An HTTP API wrapper around the ACF Python SDK and Go sidecar. It is not the hot path — the authoritative enforcement path
remains SDK → UDS → sidecar. This layer exists for tooling, demos, and integration testing.
Every inbound HTTP request passes through two layers:
HTTP client
↓
Layer 1: RuleEngine — in-process regex pre-filter fast rejection, no sidecar round-trip needed
↓
Layer 2: ACF Python SDK → UDS → Go sidecar authoritative OPA/Rego policy evaluation
↓
ValidateResponse (decision + signals + score)
What changed and why
api/main.py— FastAPI application (264 lines)Two endpoints:
GET /health— liveness check, returns sidecar connectivityand rules loaded count
POST /validate— runs payload through RuleEngine first, forwards to sidecar via Python SDK if not hard-blockedapi/rules/engine.py— rule-based pre-filter (175 lines)In-process regex engine evaluated before every sidecar call. Scoring is non-additive —
RuleResult.scoreis the highestsingle signal weight seen, never a sum. This prevents noise rules from aggregating into false blocks.
Severity weights:
critical = 0.95 → hard_block = True, never reaches sidecar
high = 0.80
medium = 0.55
low = 0.25
api/models.py— Pydantic request/response modelsType-validated models for
/validate. Accepts all four v1 hook types:prompt,context,tool_call,memory.api/config/rules.yaml— rule definitionsLoaded at startup. Hot-reloadable without restart — pattern library updates never require a deploy.
docs/api.md— API guide (352 lines)Architecture diagram, endpoint reference, hook types, rule authoring guide, and Phase 2 roadmap.
tests/api/— API test suitetest_health.py— liveness and rule count assertionstest_rules_engine.py— scoring, hard_block, signaldeduplication
test_validate.py— all four hook types end-to-endOpen design questions for review
These are the questions @tharindupr flagged as needing
architectural discussion before this merges:
rules.yamland what is the process for adding new patterns?Testing
Phase 2 scope (not in this PR)
/validate