sema-policies 0.2.0
Reusable least-privilege, content-safety, output, and workflow-evidence policies
package.sema
426 lines
bda869f14282
File details
Source stored with sema-policies 0.2.0. Line links refer to these exact bytes.
SHA-256 bda869f142825b7db70d45ae0f124ca25badb82e515ed55d2512921438abacfd
1;; sema-policies — reusable deterministic policies for Sema workflows.23(module sema-policies4(export5policies/model-allowlist6policies/tool-allowlist7policies/subject-allowlist8policies/file-read-rule9policies/file-write-rule10policies/file-delete-rule11policies/network-rule12policies/command-rule13policies/external-action-rule14policies/input-controls15policies/evidence-requirements16policies/read-only-repository17policies/safe-code-agent18policies/customer-support-safe19policies/output-contract20policies/no-tools21policies/no-file-write-tools22policies/no-network-tools23policies/no-command-tools24policies/no-external-action-tools25policies/no-sensitive-data-to-models26policies/public-content27policies/reviewable-output28policies/certainty-audit29policies/no-placeholder-output30policies/human-reviewed-run31policies/change-controlled32policies/public-sector-rag33policies/ai-act-docs-lite34policies/ai-act-human-oversight35policies/employment-assist)3637(define (named-policy name rules)38(assoc (assoc rules :__policy-name name) :__policy-version 1))3940(define (non-empty-sequence? value)41(and (or (list? value) (vector? value)) (not (empty? value))))4243(define (constraint? value)44(or (and (map? value) (not (empty? value)))45(non-empty-sequence? value)))4647(define (single-options who opts allowed)48(when (> (length opts) 1)49(error (format "~a: expected at most one options map" who)))50(let ((options (if (null? opts) {} (first opts))))51(when (not (map? options))52(error (format "~a: options must be a map" who)))53(let ((unknown54(filter (fn (key) (not (member key allowed))) (keys options))))55(when (not (null? unknown))56(error (format "~a: unknown option ~a" who (first unknown)))))57options))5859(define (require-constraint who label value)60(when (not (constraint? value))61(error62(format "~a: ~a must be a non-empty selector map, list, or vector"63who64label))))6566(define (require-optional-sequence who label value)67(when (not (or (list? value) (vector? value)))68(error (format "~a: ~a must be a list or vector" who label))))6970(define (policies/model-allowlist identities . opts)71"Allow only the provider/model identities in identities. Options: :on-deny is :fail (default) or :skip for fallback routing."72(when (not (non-empty-sequence? identities))73(error74"policies/model-allowlist: identities must be a non-empty list or vector — use provider/model strings"))75(let ((o (single-options "policies/model-allowlist" opts [:on-deny])))76(named-policy "policies/model-allowlist"77{:models {:default :deny78:allow identities79:on-deny (get o :on-deny :fail)}})))8081(define (policies/tool-allowlist rules . opts)82"Allow only tools in rules, where each map key is a tool name and each value is its argument constraint map. Options: :on-deny is :tool-error (default) or :fail."83(when (or (not (map? rules)) (empty? rules))84(error85"policies/tool-allowlist: rules must be a non-empty map — map each allowed tool name to its constraints"))86(let ((o (single-options "policies/tool-allowlist" opts [:on-deny])))87(named-policy "policies/tool-allowlist"88{:tools {:default :deny89:allow rules90:on-deny (get o :on-deny :tool-error)}})))9192(define (policies/file-read-rule paths)93"Build a semantic file-read rule for a subject allowlist."94(require-constraint "policies/file-read-rule" "paths" paths)95{:kind :file-read :paths paths})9697(define (policies/file-write-rule paths)98"Build a semantic file-write rule for a subject allowlist."99(require-constraint "policies/file-write-rule" "paths" paths)100{:kind :file-write :paths paths})101102(define (policies/file-delete-rule paths)103"Build a semantic file-delete rule for a subject allowlist."104(require-constraint "policies/file-delete-rule" "paths" paths)105{:kind :file-delete :paths paths})106107(define (policies/network-rule domains . opts)108"Build a semantic network-request rule. Options: :methods is a list or vector of HTTP methods."109(require-constraint "policies/network-rule" "domains" domains)110(let* ((o (single-options "policies/network-rule" opts [:methods]))111(methods (get o :methods []))112(rule {:kind :network-request :domains domains}))113(require-optional-sequence "policies/network-rule" "methods" methods)114(when (and (contains? o :methods) (empty? methods))115(error "policies/network-rule: :methods must not be empty when provided"))116(if (empty? methods) rule (assoc rule :methods methods))))117118(define (policies/command-rule commands)119"Build a semantic command rule. Commands are exact strings, not shell patterns."120(require-constraint "policies/command-rule" "commands" commands)121{:kind :command :commands commands})122123(define (policies/external-action-rule actions)124"Build a semantic external-action rule for exact declared action names."125(when (not (non-empty-sequence? actions))126(error127"policies/external-action-rule: actions must be a non-empty list or vector"))128{:kind :external-action :actions actions})129130(define (policies/subject-allowlist rules . opts)131"Allow only declared semantic subjects matching rules. Options: :deny adds rules that take precedence."132(when (not (non-empty-sequence? rules))133(error134"policies/subject-allowlist: rules must be a non-empty list or vector"))135(let* ((o (single-options "policies/subject-allowlist" opts [:deny]))136(deny (get o :deny [])))137(require-optional-sequence "policies/subject-allowlist" "deny" deny)138(named-policy "policies/subject-allowlist"139{:subjects {:default :deny :allow rules :deny deny}})))140141(define (policies/input-controls detectors . opts)142"Apply deterministic input detectors. Options: :actions maps detector names to :audit, :redact, or :block. Undeclared actions default to :block."143(when (not (non-empty-sequence? detectors))144(error145"policies/input-controls: detectors must be a non-empty list or vector"))146(let* ((o (single-options "policies/input-controls" opts [:actions]))147(actions (get o :actions {})))148(when (not (map? actions))149(error "policies/input-controls: :actions must be a map"))150(named-policy "policies/input-controls"151{:input {:detect detectors :actions actions}})))152153(define (policies/evidence-requirements requirements)154"Require workflow metadata and/or event kinds. Use {:metadata [...] :events [...]}."155(when (or (not (map? requirements)) (empty? requirements))156(error157"policies/evidence-requirements: requirements must be a non-empty map"))158(let ((unknown159(filter (fn (key) (not (member key [:metadata :events])))160(keys requirements))))161(when (not (null? unknown))162(error163(format "policies/evidence-requirements: unknown key ~a"164(first unknown)))))165(let* ((metadata (get requirements :metadata []))166(events (get requirements :events []))167(rules {}))168(require-optional-sequence "policies/evidence-requirements"169"metadata"170metadata)171(require-optional-sequence "policies/evidence-requirements"172"events"173events)174(when (and (empty? metadata) (empty? events))175(error176"policies/evidence-requirements: provide at least one metadata key or event"))177(when (not (empty? metadata))178(set! rules (assoc rules :metadata {:require metadata})))179(when (not (empty? events))180(set! rules (assoc rules :completion {:require-events events})))181(named-policy "policies/evidence-requirements" rules)))182183(define (policies/read-only-repository paths)184"Allow declared file-read subjects only within paths, and deny writes, deletes, commands, network requests, and external actions."185(when (not (non-empty-sequence? paths))186(error187"policies/read-only-repository: paths must be a non-empty list or vector — use workspace-relative glob strings"))188(named-policy "policies/read-only-repository"189{:subjects {:default :deny190:allow [{:kind :file-read :paths paths}]191:deny [{:kind :file-write}192{:kind :file-delete}193{:kind :command}194{:kind :network-request}195{:kind :external-action}]}}))196197(define (policies/safe-code-agent options)198"Build a strict coding-agent profile. Required: :models and :read. Optional: :write, :commands, :domains, and :network-methods."199(when (not (map? options))200(error "policies/safe-code-agent: options must be a map"))201(let ((unknown202(filter203(fn (key)204(not205(member key206[:models :read :write :commands :domains :network-methods])))207(keys options))))208(when (not (null? unknown))209(error210(format "policies/safe-code-agent: unknown option ~a" (first unknown)))))211(let* ((models (get options :models []))212(read-paths (get options :read []))213(write-paths (get options :write []))214(commands (get options :commands []))215(domains (get options :domains []))216(methods (get options :network-methods ["GET"]))217(rules []))218(when (not (non-empty-sequence? models))219(error220"policies/safe-code-agent: :models must be a non-empty list or vector"))221(when (not (non-empty-sequence? read-paths))222(error223"policies/safe-code-agent: :read must be a non-empty list or vector"))224(require-optional-sequence "policies/safe-code-agent" "write" write-paths)225(require-optional-sequence "policies/safe-code-agent" "commands" commands)226(require-optional-sequence "policies/safe-code-agent" "domains" domains)227(require-optional-sequence "policies/safe-code-agent"228"network-methods"229methods)230(when (and (contains? options :network-methods) (empty? methods))231(error232"policies/safe-code-agent: :network-methods must not be empty when provided"))233(when (and (contains? options :network-methods) (empty? domains))234(error235"policies/safe-code-agent: :network-methods requires non-empty :domains"))236(set! rules (append rules (list (policies/file-read-rule read-paths))))237(when (not (empty? write-paths))238(set! rules239(append rules (list (policies/file-write-rule write-paths)))))240(when (not (empty? commands))241(set! rules (append rules (list (policies/command-rule commands)))))242(when (not (empty? domains))243(set! rules244(append rules245(list246(policies/network-rule {:allow domains :schemes ["https"]}247{:methods methods})))))248(named-policy "policies/safe-code-agent"249{:models {:default :deny :allow models :on-deny :fail}250:tools {:default :allow :on-deny :tool-error}251:subjects {:default :deny :allow rules}252:input {:detect [:secret :payment-card :email :phone :ipv4]253:actions {:secret :block254:payment-card :block255:email :redact256:phone :redact257:ipv4 :redact}}})))258259(define (policies/customer-support-safe options)260"Build a draft-only support profile. :read-actions lists exact external-action names for non-mutating CRM or ticket reads."261(when (not (map? options))262(error "policies/customer-support-safe: options must be a map"))263(let ((unknown264(filter (fn (key) (not (member key [:read-actions])))265(keys options))))266(when (not (null? unknown))267(error268(format "policies/customer-support-safe: unknown option ~a"269(first unknown)))))270(let ((read-actions (get options :read-actions [])))271(when (not (non-empty-sequence? read-actions))272(error273"policies/customer-support-safe: :read-actions must be a non-empty list or vector"))274(named-policy "policies/customer-support-safe"275{:metadata {:require [:owner :intended-purpose]}276:tools {:default :allow :on-deny :tool-error}277:subjects {:default :deny278:allow [{:kind :external-action :actions read-actions}]}279:input {:detect [:secret :payment-card :email :phone :ipv4]280:actions {:secret :block281:payment-card :block282:email :redact283:phone :redact284:ipv4 :redact}}285:output {:schema {:draft {:type :string}286:sources {:type :list}287:limitations {:type :string}}288:require [:draft :sources :limitations]}})))289290(define (policies/output-contract contract)291"Require an output contract map using :schema, :require, :max-length, :forbid, :detect, :actions, and/or :action."292(when (or (not (map? contract)) (empty? contract))293(error294"policies/output-contract: contract must be a non-empty map — provide at least one output constraint"))295(named-policy "policies/output-contract" {:output contract}))296297(define policies/no-tools298(named-policy "policies/no-tools"299{:tools {:default :deny :on-deny :fail}}))300301;; Deny-only subject layers use :default :allow so they compose with a separate302;; strict subject allowlist without denying unrelated subject kinds.303(define policies/no-file-write-tools304(named-policy "policies/no-file-write-tools"305{:subjects {:default :allow306:deny [{:kind :file-write} {:kind :file-delete}]}}))307308(define policies/no-network-tools309(named-policy "policies/no-network-tools"310{:subjects {:default :allow :deny [{:kind :network-request}]}}))311312(define policies/no-command-tools313(named-policy "policies/no-command-tools"314{:subjects {:default :allow :deny [{:kind :command}]}}))315316(define policies/no-external-action-tools317(named-policy "policies/no-external-action-tools"318{:subjects {:default :allow :deny [{:kind :external-action}]}}))319320(define policies/no-sensitive-data-to-models321(named-policy "policies/no-sensitive-data-to-models"322{:input {:detect [:secret :payment-card :email :phone :ipv4]323:actions {:secret :block324:payment-card :block325:email :redact326:phone :redact327:ipv4 :redact}}}))328329(define policies/public-content330(named-policy "policies/public-content"331{:input {:detect [:secret :payment-card :email :phone :ipv4]}332:output {:detect [:secret :payment-card :email :phone :ipv4]}}))333334(define policies/reviewable-output335(named-policy "policies/reviewable-output"336{:output {:schema {:answer {:type :string}337:sources {:type :list}338:limitations {:type :string}339:confidence {:type :number :optional #t}}340:require [:answer :sources :limitations]}}))341342(define policies/certainty-audit343(named-policy "policies/certainty-audit"344{:output {:action :audit345:forbid [{:id :absolute-guarantee346:regex "(?i)\\b(guaranteed|zero[- ]risk|100%[[:space:]]+(safe|effective|accurate))\\b"}347{:id :exclusive-certainty348:regex "(?i)\\b(the only correct (answer|solution)|cannot possibly fail)\\b"}349{:id :unnamed-consensus350:regex "(?i)\\b(experts agree|everyone knows|studies (show|prove))\\b"}]}}))351352(define policies/no-placeholder-output353(named-policy "policies/no-placeholder-output"354{:output {:forbid [{:id :unfinished-placeholder355:regex "(?i)\\b(TODO|TBD|FIXME)\\b|\\[(INSERT|REPLACE)[^]]*\\]|\\{\\{[^{}]+\\}\\}"}]}}))356357(define policies/human-reviewed-run358(named-policy "policies/human-reviewed-run"359{:completion {:require-events [:approval.applied]}}))360361(define policies/change-controlled362(named-policy "policies/change-controlled"363{:metadata {:require [:owner :change-id :environment]}364:completion {:require-events [:approval.applied]}}))365366(define policies/public-sector-rag367(named-policy "policies/public-sector-rag"368{:metadata {:require [:owner :data-classification :source]}369:input {:detect [:secret :payment-card :email :phone :ipv4]370:actions {:secret :block371:payment-card :block372:email :redact373:phone :redact374:ipv4 :redact}}375:output {:detect [:secret :payment-card :email :phone]376:schema {:answer {:type :string}377:sources {:type :list}}378:require [:answer :sources]}379:completion {:require-events [:checkpoint]}}))380381(define policies/ai-act-docs-lite382(named-policy "policies/ai-act-docs-lite"383{:metadata {:require [:owner :risk-tier :intended-purpose]}384:output {:schema {:decision {:type :string}385:rationale {:type :string}386:sources {:type :list}}387:require [:decision :rationale :sources]}388:completion {:require-events [:checkpoint]}}))389390(define policies/ai-act-human-oversight391(named-policy "policies/ai-act-human-oversight"392{:metadata {:require [:owner393:risk-tier394:intended-purpose395:not-for396:affected-persons397:users398:models399:human-oversight]}400:output {:schema {:decision {:type :string}401:rationale {:type :string}402:sources {:type :list}403:limitations {:type :string}}404:require [:decision :rationale :sources :limitations]}405:completion {:require-events [:checkpoint :approval.applied]}}))406407(define policies/employment-assist408(named-policy "policies/employment-assist"409{:metadata {:require [:owner410:intended-purpose411:not-for412:affected-persons413:human-oversight]}414:tools {:default :deny :on-deny :fail}415:input {:detect [:secret :payment-card :email :phone :ipv4]416:actions {:secret :block417:payment-card :block418:email :redact419:phone :redact420:ipv4 :redact}}421:output {:schema {:summary {:type :string}422:evidence {:type :list}423:limitations {:type :string}424:review-notice {:type :string}}425:require [:summary :evidence :limitations :review-notice]}426:completion {:require-events [:approval.applied]}})))