Skip to content

Security, Guardrails & PII Policies

When enterprise workflows process customer tickets, financial records, or medical notes, personal data and credentials must never leak into external LLM prompts or observability logs. AGBoss provides three defense-in-depth mechanisms: @PiiPolicy for declarative or custom PII masking and reversible pseudonymization at the model boundary, @InputGuardrail and @OutputGuardrail for pre-model and post-model safety enforcement, and secret() for environment credential masking.

How @PiiPolicy masks and reveals personal data at the model boundary (without breaking KV caches)

Section titled “How @PiiPolicy masks and reveals personal data at the model boundary (without breaking KV caches)”

A @PiiPolicy class (whose name must end with -pii-policy) can declare built-in rules: [pii(Kind | Group, Action)] or implement PiiPolicyHandler.findMatches(text) for custom project detectors. Masking happens at the model boundary: models only see masked tokens, while PiiAction.replaceWithToken() computes a deterministic HMAC pseudonym with pseudonymKey so the exact same email or badge ID maps to the exact same token across every turn and step — preserving multi-turn LLM KV prefix cache hits while restoring real values before tools, actions, and finishes run:

Deterministic Code Guardrails (DecidedBy.Code)

Section titled “Deterministic Code Guardrails (DecidedBy.Code)”

When a safety rule is deterministic — such as a tenant-specific blocklist from a database, a regex/checksum check, or an external moderation microservice — declare decidedBy: DecidedBy.Code on @InputGuardrail or @OutputGuardrail and implement GuardrailHandler.check(text). Code guardrails receive the PII-masked text, support full DI (deps: [...]), emit GuardrailStartEvent / GuardrailEndEvent with $0.00 cost, and require no LLM call:

  • Hierarchical policy lists (piiPolicies & excludePiiPolicies): Policies listed on @Application, @Workflow, @Agent, and @Router add up automatically. An agent can opt out of an inherited policy for its own model calls via excludePiiPolicies, while logs, traces, and result.record remain masked by the application and workflow policies.
  • Cross-instance token consistency: Set @Application({ pseudonymKey: "piiPseudonymKey" }) pointing to a Secret field in your environment contract so every replica generates identical HMAC tokens for logs and traces.
  • Unit testing with testPii and testGuardrail: Validate mask(text) and reveal(text) offline with testPii([CustomerPiiPolicy]) and verify both DecidedBy.Model and DecidedBy.Code guardrails with testGuardrail(TenantBlocklistGuardrail) from @agboss/core/testing.
  • Standalone executable proof: Inspect 03-support-refund-desk in agbossjs/agboss-examples for end-to-end HMAC token roundtrips (<email:d1ac5c> $\to$ revealPii: true in IssueRefundTool), PiiRefusedError boundary refusal, @ToolInterceptor policy enforcement, and DecidedBy.Code / DecidedBy.Model guardrails (see 7 Verified Examples).