feat(guardrails): fail-loud DB mode + seed/validate helpers + exclusivity warning (closes #166)#196
Merged
Conversation
…vity warning (closes #166) Three quiet behaviors in the BuildGuardrailChecker resolution ladder mismatched what operators expect from a production-grade guardrails deploy: a silent fall-back on Mongo unreachable, no application of DefaultStructuredGuardrails in DB mode (so an empty AgentConfig was strictly less protective than the no-config file path), and silent suppression of a checked-in guardrails.json when DB mode was active. This change closes all three from the operator surface. - FORGE_GUARDRAILS_DB_REQUIRED=true flips the silent fall-back to a startup-time error. BuildGuardrailChecker now returns (GuardrailChecker, error) so the runner can propagate the failure; the agent process refuses to serve. Default off for back-compat. - forge guardrails seed-defaults prints DefaultStructuredGuardrails as JSON suitable for piping into MongoDB. Round-trips through models.StructuredGuardrails so the output is library-consumable verbatim — operators have a one-liner baseline seed. - forge guardrails validate-db connects, fetches the agent's AgentConfig, and reports coverage. Warns when below baseline (fewer than 5 secret-pattern rules / missing PII / missing security thresholds / disabled core gates). Exits non-zero on missing document so CI hooks can fail rollout. - One-shot startup warning when both FORGE_GUARDRAILS_DB and a guardrails.json are present, pointing at the specific path being ignored. Through the ops logger only (not the audit stream). - NewDBGuardrailEngine connect timeout trimmed 10s → 3s. Production fails fast on misconfigured URIs; tests run in seconds, not tens of seconds. Pinned by TestBuildGuardrailChecker_{DBRequired_FailsLoudOnUnreachable, DBUnreachable_FallsBackByDefault,DBRequiredAcceptsForgivingParse, DBAndFile_WarnsOnce,DBOnly_NoFile_NoExclusivityWarn,FileOnly_NoWarn, HonorsCustomGuardrailsPath}, TestGuardrailsSeedDefaults_RoundTripsThroughLibraryModel, TestScoreAgentConfig_{FullDefaultsHaveNoWarnings,SnakeCaseCompat, EmptyDocFlagsEverything,FewerThan5SecretRulesWarns}, and TestExtractCustomRules_DefensiveOnShape. Library-side merge of defaults+DB config remains out of scope per the issue — Option B (operator seeds via the new subcommand) ships now; Option A waits on a stable library merge-semantics contract.
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.
Summary
Closes the three quiet behaviors in
BuildGuardrailChecker's DB-mode resolution ladder that made the feature unusable for production-grade guardrails deploys:FORGE_GUARDRAILS_DB_REQUIRED=truemakes an unreachable Mongo at startup a hard error instead of a silent downgrade.BuildGuardrailCheckernow returns(GuardrailChecker, error); the runner propagates the failure as a non-zero exit.forge guardrails seed-defaultsprintsDefaultStructuredGuardrailsas JSON;forge guardrails validate-dbinspects the agent'sAgentConfigand warns when below baseline. Closes the "DB mode bypasses built-in defaults" footgun without library-side coordination.FORGE_GUARDRAILS_DBis set AND aguardrails.jsonis present, pointing at the specific path being ignored.Why
DefaultStructuredGuardrailsships 11 vendor-secret patterns, four PII categories, and three threat-detection thresholds — none of which are applied in DB mode (the operator's MongoDBAgentConfigis the ONLY source). An empty or partial seed leaves the agent strictly less protected than a clean default deploy, and the silent fall-back path masked the failure. This PR makes both surfaces actionable.Operator-facing changes
Env vars
FORGE_GUARDRAILS_DB_REQUIREDfalsetrueandFORGE_GUARDRAILS_DBis set, unreachable DB at startup → runner returns non-nil error, agent refuses to serve.CLI
Connect timeout
Trimmed
NewDBGuardrailEngineMongo connect timeout from 10s to 3s. Production fails fast on misconfigured URIs; tests measurably faster too.Schema-shape compatibility
scoreAgentConfigtolerates both camelCase (the library struct tags — what production MongoDB carries) and snake_case (legacy seeds / hand-written docs from older guidance) without operator intervention. Pinned byTestScoreAgentConfig_SnakeCaseCompat.Out of scope (deferred)
Test plan
golangci-lint run ./forge-core/... ./forge-cli/... ./forge-plugins/...— 0 issuesgofmt -wacross all modulesgo test ./...inforge-core/andforge-cli/— all greencfg.GuardrailsPath), seed-defaults round-trips throughmodels.StructuredGuardrails,scoreAgentConfigflags empty docs / fewer-than-5 secret rules / snake_case compat / camelCase-default-no-warnings,extractCustomRulesdefensive across BSON shape edge cases.FORGE_GUARDRAILS_DB=mongodb://127.0.0.1:1 FORGE_GUARDRAILS_DB_REQUIRED=true forge run— assert non-zero exit + Error log line referencing the env var.