phocinae-rulepack 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (24) hide show
  1. phocinae_rulepack-0.1.0/LICENSE +201 -0
  2. phocinae_rulepack-0.1.0/PKG-INFO +217 -0
  3. phocinae_rulepack-0.1.0/README.md +191 -0
  4. phocinae_rulepack-0.1.0/phocinae_rulepack/__init__.py +58 -0
  5. phocinae_rulepack-0.1.0/phocinae_rulepack/__main__.py +10 -0
  6. phocinae_rulepack-0.1.0/phocinae_rulepack/cli.py +257 -0
  7. phocinae_rulepack-0.1.0/phocinae_rulepack/engine.py +232 -0
  8. phocinae_rulepack-0.1.0/phocinae_rulepack/errors.py +142 -0
  9. phocinae_rulepack-0.1.0/phocinae_rulepack/expr.py +463 -0
  10. phocinae_rulepack-0.1.0/phocinae_rulepack/packs/expense-policy/rulepack.yaml +179 -0
  11. phocinae_rulepack-0.1.0/phocinae_rulepack/packs/expense-policy/sources/s15_feasibility_excerpt.md +3 -0
  12. phocinae_rulepack-0.1.0/phocinae_rulepack/packs/expense-policy/sources/s15_policy_constants.txt +6 -0
  13. phocinae_rulepack-0.1.0/phocinae_rulepack/packs/expense-policy/sources/s15_rule_check.txt +11 -0
  14. phocinae_rulepack-0.1.0/phocinae_rulepack/schema.py +670 -0
  15. phocinae_rulepack-0.1.0/phocinae_rulepack/yamlsubset.py +445 -0
  16. phocinae_rulepack-0.1.0/phocinae_rulepack.egg-info/PKG-INFO +217 -0
  17. phocinae_rulepack-0.1.0/phocinae_rulepack.egg-info/SOURCES.txt +22 -0
  18. phocinae_rulepack-0.1.0/phocinae_rulepack.egg-info/dependency_links.txt +1 -0
  19. phocinae_rulepack-0.1.0/phocinae_rulepack.egg-info/entry_points.txt +2 -0
  20. phocinae_rulepack-0.1.0/phocinae_rulepack.egg-info/requires.txt +3 -0
  21. phocinae_rulepack-0.1.0/phocinae_rulepack.egg-info/top_level.txt +1 -0
  22. phocinae_rulepack-0.1.0/pyproject.toml +53 -0
  23. phocinae_rulepack-0.1.0/setup.cfg +4 -0
  24. phocinae_rulepack-0.1.0/tests/test_rulepack.py +701 -0
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2026 phocinae-guard authors (Nous Research)
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,217 @@
1
+ Metadata-Version: 2.4
2
+ Name: phocinae-rulepack
3
+ Version: 0.1.0
4
+ Summary: Deterministic rule-pack engine: read a ledger, apply a versioned rule pack, emit only the differences for human review. Zero model calls, zero third-party runtime dependencies.
5
+ Author: Phocinae
6
+ License: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/Phocinae/phocinae-rulepack
8
+ Project-URL: Repository, https://github.com/Phocinae/phocinae-rulepack
9
+ Project-URL: Issues, https://github.com/Phocinae/phocinae-rulepack/issues
10
+ Keywords: rule-engine,deterministic,compliance,checklist,fail-closed,decision-model,phocinae,local-first
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: License :: OSI Approved :: Apache Software License
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.9
16
+ Classifier: Programming Language :: Python :: 3.10
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Classifier: Topic :: Software Development :: Quality Assurance
20
+ Requires-Python: >=3.9
21
+ Description-Content-Type: text/markdown
22
+ License-File: LICENSE
23
+ Provides-Extra: yamltest
24
+ Requires-Dist: PyYAML>=6.0; extra == "yamltest"
25
+ Dynamic: license-file
26
+
27
+ # phocinae-rulepack
28
+
29
+ **Read a ledger, apply one version of a rule pack, and emit only the differences
30
+ for a human to review.** Zero model calls. Zero third-party runtime dependencies.
31
+
32
+ <p>
33
+ <img alt="python" src="https://img.shields.io/static/v1?label=python&message=%3E%3D3.9&color=3776AB&style=flat-square">
34
+ <img alt="deps" src="https://img.shields.io/static/v1?label=runtime%20deps&message=0&color=success&style=flat-square">
35
+ <img alt="model calls" src="https://img.shields.io/static/v1?label=model%20calls&message=0&color=blueviolet&style=flat-square">
36
+ <img alt="License" src="https://img.shields.io/static/v1?label=License&message=Apache-2.0&color=blue&style=flat-square">
37
+ </p>
38
+
39
+ [中文](./README-zh.md)
40
+
41
+ ---
42
+
43
+ ## Why this exists, in one measurement
44
+
45
+ The same decision model, asked the same question two ways, scored like this on 53
46
+ labelled commands:
47
+
48
+ | how the question was asked | recall on destructive commands |
49
+ |---|---|
50
+ | "Should this be blocked?" (boolean) | **0.00** — it passed all 31 |
51
+ | allow / ask / deny (three-way) | 0.74 |
52
+ | harmless / risky / destructive (scale) | 0.97 |
53
+
54
+ And on a policy-check battery of 14 claims, the **rule kernel scored 14/14 with
55
+ zero model calls**, while the same model, asked to approve or reject the claims
56
+ directly, rejected **all 8** compliant ones.
57
+
58
+ The conclusion this package is built on: *"the rule kernel is worth shipping as a
59
+ deterministic plugin that uses no model at all."* This is that kernel.
60
+
61
+ If you want the measurement harness rather than the kernel, see
62
+ [`phocinae-formulate`](https://github.com/Phocinae/phocinae-formulate).
63
+
64
+ ## Install
65
+
66
+ ```sh
67
+ pip install phocinae-rulepack==0.1.0
68
+ ```
69
+
70
+ Python 3.9 or newer. No dependencies. Nothing in this package opens a socket.
71
+
72
+ ## Use
73
+
74
+ ```sh
75
+ # validate a pack: schema, citation text, corpus hashes
76
+ phocinae-rulepack validate expense-policy
77
+
78
+ # run one document (JSON or YAML; '-' reads stdin)
79
+ phocinae-rulepack check expense-policy claim.json
80
+
81
+ # run a ledger and get one envelope
82
+ phocinae-rulepack check expense-policy ledger.json --format json
83
+
84
+ # what ships in the box
85
+ phocinae-rulepack list --packs-dir ./packs
86
+ ```
87
+
88
+ Exit codes are the contract:
89
+
90
+ | code | meaning |
91
+ |---|---|
92
+ | `0` | every result is `pass` |
93
+ | `1` | at least one `fail` |
94
+ | `2` | no `fail`, but at least one `review` (grey zone → a human decides) |
95
+ | `3` | usage or configuration error, **or an empty scan** |
96
+
97
+ Priority is fixed and total: `3 > 1 > 2 > 0`.
98
+
99
+ ## The three-state result
100
+
101
+ ```json
102
+ {"rulepack":"expense-policy","verdict":"fail","model_calls":0,
103
+ "reasons":[{"rule":"r2_meal_cap","verdict":"fail","reason_code":"OVER_CATEGORY_CAP",
104
+ "cite":["c2","c4"],"evidence":"matched:r2_meal_cap"}],
105
+ "unmatched_paths":["id"]}
106
+ ```
107
+
108
+ * **`review` means a human decides.** It is not "look again later".
109
+ * **`pass` requires a rule that says pass.** A document no rule matched is
110
+ `review` (`NO_RULE_MATCHED`) — nothing falls through to a silent allow.
111
+ * **A missing field or a wrongly-typed field never becomes `pass`.** Missing
112
+ fields are `review`; they are never treated as compliance.
113
+ * **A real violation outranks a missing field.** If one rule finds a breach and
114
+ another cannot be evaluated, the verdict is `fail` and both are reported.
115
+ * **`model_calls` is a real counter, and it is always `0`.**
116
+
117
+ ## The evidence model is the point
118
+
119
+ A rule is only as good as the text behind it, so a citation must say where its
120
+ text came from and whether anyone has actually read the instrument:
121
+
122
+ | `evidence_level` | meaning | can it `fail` something? |
123
+ |---|---|---|
124
+ | `verbatim` | the quote appears character-for-character in the corpus | yes |
125
+ | `derived` | an authority's restatement, not the instrument's own words | yes, with a warning |
126
+ | `unavailable` | the instrument text was **not obtained** | **no — the verdict is capped to `review`** |
127
+
128
+ That cap is the difference between an honest tool and a fake gate. A rule whose
129
+ basis nobody has read must not be able to reject a customer's paperwork. The cap
130
+ is verified in both directions by the test suite: flip the citation to `derived`
131
+ and the same input must move from `review` to `fail`.
132
+
133
+ `validate` resolves every quote against the corpus and recomputes every
134
+ sha256. A one-character change to a quote, or a wrong hash, fails validation.
135
+ `--strict` additionally rejects sample corpus and `unavailable` citations.
136
+
137
+ ## Writing a pack
138
+
139
+ ```yaml
140
+ schema: phocinae-rulepack/v1
141
+ id: my-check
142
+ title: What this checks
143
+ version: 0.1.0
144
+ missing_field_policy: review # only 'review' is accepted in v1
145
+ corpus:
146
+ - path: sources/spec.txt
147
+ role: authority # authority | production | sample
148
+ sha256: <64 uppercase hex>
149
+ citations:
150
+ - id: c1
151
+ source: Where the text is from
152
+ locator: clause 4.2
153
+ quote: "the exact words, character for character"
154
+ document: sources/spec.txt
155
+ evidence_level: verbatim
156
+ rules:
157
+ - id: r1_limit
158
+ when:
159
+ all_of:
160
+ - {field: amount, op: ">", value: 500}
161
+ - {field: receipt, op: "==", value: false}
162
+ then: {verdict: fail, reason_code: OVER_LIMIT, cite: [c1]}
163
+ require_fields: [amount, receipt]
164
+ ```
165
+
166
+ The expression language is deliberately small: `field OP value` combined with
167
+ `all_of` / `any_of` / `not`, and a `value` may be `{"ref": "other.field"}` to
168
+ compare two fields. Operators: `== != < <= > >= in not_in matches exists missing`.
169
+
170
+ There is **no** arithmetic, no custom function, no loop, and no network access.
171
+ If you need a computed value, compute it in the adapter that builds the input
172
+ document — that keeps the judgement criterion in the pack, where it can be cited
173
+ and tested.
174
+
175
+ ### The two YAML traps worth knowing
176
+
177
+ Both of these parse into something plausible and wrong, which is why the loader
178
+ raises instead of guessing:
179
+
180
+ ```yaml
181
+ op: > # WRONG: a folded block scalar. It swallows the lines below.
182
+ op: ">" # right: quote operators
183
+
184
+ note: text with a ": " colon inside # WRONG: YAML wants a new key there
185
+ note: "text with a \": \" colon inside" # right: quote it, or use a block scalar
186
+ ```
187
+
188
+ ## What ships
189
+
190
+ | pack | scenario | rules |
191
+ |---|---|---|
192
+ | `expense-policy` | S15 (the expense pre-approval demo) | 8 |
193
+
194
+ > Naming note: "S15" here is the gallery's S15 expense demo. In the 182-scenario
195
+ > inventory S15 is something else (multilingual low-resource expansion).
196
+
197
+ ## Testing
198
+
199
+ ```sh
200
+ python tests/run_all.py
201
+ ```
202
+
203
+ No network, no model, no third-party packages. The suite exits non-zero when
204
+ zero assertions ran — *"a check with nothing to check must not report success"*
205
+ is the same rule the engine applies to an empty scan.
206
+
207
+ Tests never use `tempfile`: scratch goes to `$PHOCINAE_RULEPACK_TMP`, or to a
208
+ directory beside the repository.
209
+
210
+ ## What this is not
211
+
212
+ It is not a compliance, legal, medical or financial conclusion. It reports
213
+ differences for a human to review. It is not a model, and it does not call one.
214
+
215
+ ## License
216
+
217
+ Apache-2.0.
@@ -0,0 +1,191 @@
1
+ # phocinae-rulepack
2
+
3
+ **Read a ledger, apply one version of a rule pack, and emit only the differences
4
+ for a human to review.** Zero model calls. Zero third-party runtime dependencies.
5
+
6
+ <p>
7
+ <img alt="python" src="https://img.shields.io/static/v1?label=python&message=%3E%3D3.9&color=3776AB&style=flat-square">
8
+ <img alt="deps" src="https://img.shields.io/static/v1?label=runtime%20deps&message=0&color=success&style=flat-square">
9
+ <img alt="model calls" src="https://img.shields.io/static/v1?label=model%20calls&message=0&color=blueviolet&style=flat-square">
10
+ <img alt="License" src="https://img.shields.io/static/v1?label=License&message=Apache-2.0&color=blue&style=flat-square">
11
+ </p>
12
+
13
+ [中文](./README-zh.md)
14
+
15
+ ---
16
+
17
+ ## Why this exists, in one measurement
18
+
19
+ The same decision model, asked the same question two ways, scored like this on 53
20
+ labelled commands:
21
+
22
+ | how the question was asked | recall on destructive commands |
23
+ |---|---|
24
+ | "Should this be blocked?" (boolean) | **0.00** — it passed all 31 |
25
+ | allow / ask / deny (three-way) | 0.74 |
26
+ | harmless / risky / destructive (scale) | 0.97 |
27
+
28
+ And on a policy-check battery of 14 claims, the **rule kernel scored 14/14 with
29
+ zero model calls**, while the same model, asked to approve or reject the claims
30
+ directly, rejected **all 8** compliant ones.
31
+
32
+ The conclusion this package is built on: *"the rule kernel is worth shipping as a
33
+ deterministic plugin that uses no model at all."* This is that kernel.
34
+
35
+ If you want the measurement harness rather than the kernel, see
36
+ [`phocinae-formulate`](https://github.com/Phocinae/phocinae-formulate).
37
+
38
+ ## Install
39
+
40
+ ```sh
41
+ pip install phocinae-rulepack==0.1.0
42
+ ```
43
+
44
+ Python 3.9 or newer. No dependencies. Nothing in this package opens a socket.
45
+
46
+ ## Use
47
+
48
+ ```sh
49
+ # validate a pack: schema, citation text, corpus hashes
50
+ phocinae-rulepack validate expense-policy
51
+
52
+ # run one document (JSON or YAML; '-' reads stdin)
53
+ phocinae-rulepack check expense-policy claim.json
54
+
55
+ # run a ledger and get one envelope
56
+ phocinae-rulepack check expense-policy ledger.json --format json
57
+
58
+ # what ships in the box
59
+ phocinae-rulepack list --packs-dir ./packs
60
+ ```
61
+
62
+ Exit codes are the contract:
63
+
64
+ | code | meaning |
65
+ |---|---|
66
+ | `0` | every result is `pass` |
67
+ | `1` | at least one `fail` |
68
+ | `2` | no `fail`, but at least one `review` (grey zone → a human decides) |
69
+ | `3` | usage or configuration error, **or an empty scan** |
70
+
71
+ Priority is fixed and total: `3 > 1 > 2 > 0`.
72
+
73
+ ## The three-state result
74
+
75
+ ```json
76
+ {"rulepack":"expense-policy","verdict":"fail","model_calls":0,
77
+ "reasons":[{"rule":"r2_meal_cap","verdict":"fail","reason_code":"OVER_CATEGORY_CAP",
78
+ "cite":["c2","c4"],"evidence":"matched:r2_meal_cap"}],
79
+ "unmatched_paths":["id"]}
80
+ ```
81
+
82
+ * **`review` means a human decides.** It is not "look again later".
83
+ * **`pass` requires a rule that says pass.** A document no rule matched is
84
+ `review` (`NO_RULE_MATCHED`) — nothing falls through to a silent allow.
85
+ * **A missing field or a wrongly-typed field never becomes `pass`.** Missing
86
+ fields are `review`; they are never treated as compliance.
87
+ * **A real violation outranks a missing field.** If one rule finds a breach and
88
+ another cannot be evaluated, the verdict is `fail` and both are reported.
89
+ * **`model_calls` is a real counter, and it is always `0`.**
90
+
91
+ ## The evidence model is the point
92
+
93
+ A rule is only as good as the text behind it, so a citation must say where its
94
+ text came from and whether anyone has actually read the instrument:
95
+
96
+ | `evidence_level` | meaning | can it `fail` something? |
97
+ |---|---|---|
98
+ | `verbatim` | the quote appears character-for-character in the corpus | yes |
99
+ | `derived` | an authority's restatement, not the instrument's own words | yes, with a warning |
100
+ | `unavailable` | the instrument text was **not obtained** | **no — the verdict is capped to `review`** |
101
+
102
+ That cap is the difference between an honest tool and a fake gate. A rule whose
103
+ basis nobody has read must not be able to reject a customer's paperwork. The cap
104
+ is verified in both directions by the test suite: flip the citation to `derived`
105
+ and the same input must move from `review` to `fail`.
106
+
107
+ `validate` resolves every quote against the corpus and recomputes every
108
+ sha256. A one-character change to a quote, or a wrong hash, fails validation.
109
+ `--strict` additionally rejects sample corpus and `unavailable` citations.
110
+
111
+ ## Writing a pack
112
+
113
+ ```yaml
114
+ schema: phocinae-rulepack/v1
115
+ id: my-check
116
+ title: What this checks
117
+ version: 0.1.0
118
+ missing_field_policy: review # only 'review' is accepted in v1
119
+ corpus:
120
+ - path: sources/spec.txt
121
+ role: authority # authority | production | sample
122
+ sha256: <64 uppercase hex>
123
+ citations:
124
+ - id: c1
125
+ source: Where the text is from
126
+ locator: clause 4.2
127
+ quote: "the exact words, character for character"
128
+ document: sources/spec.txt
129
+ evidence_level: verbatim
130
+ rules:
131
+ - id: r1_limit
132
+ when:
133
+ all_of:
134
+ - {field: amount, op: ">", value: 500}
135
+ - {field: receipt, op: "==", value: false}
136
+ then: {verdict: fail, reason_code: OVER_LIMIT, cite: [c1]}
137
+ require_fields: [amount, receipt]
138
+ ```
139
+
140
+ The expression language is deliberately small: `field OP value` combined with
141
+ `all_of` / `any_of` / `not`, and a `value` may be `{"ref": "other.field"}` to
142
+ compare two fields. Operators: `== != < <= > >= in not_in matches exists missing`.
143
+
144
+ There is **no** arithmetic, no custom function, no loop, and no network access.
145
+ If you need a computed value, compute it in the adapter that builds the input
146
+ document — that keeps the judgement criterion in the pack, where it can be cited
147
+ and tested.
148
+
149
+ ### The two YAML traps worth knowing
150
+
151
+ Both of these parse into something plausible and wrong, which is why the loader
152
+ raises instead of guessing:
153
+
154
+ ```yaml
155
+ op: > # WRONG: a folded block scalar. It swallows the lines below.
156
+ op: ">" # right: quote operators
157
+
158
+ note: text with a ": " colon inside # WRONG: YAML wants a new key there
159
+ note: "text with a \": \" colon inside" # right: quote it, or use a block scalar
160
+ ```
161
+
162
+ ## What ships
163
+
164
+ | pack | scenario | rules |
165
+ |---|---|---|
166
+ | `expense-policy` | S15 (the expense pre-approval demo) | 8 |
167
+
168
+ > Naming note: "S15" here is the gallery's S15 expense demo. In the 182-scenario
169
+ > inventory S15 is something else (multilingual low-resource expansion).
170
+
171
+ ## Testing
172
+
173
+ ```sh
174
+ python tests/run_all.py
175
+ ```
176
+
177
+ No network, no model, no third-party packages. The suite exits non-zero when
178
+ zero assertions ran — *"a check with nothing to check must not report success"*
179
+ is the same rule the engine applies to an empty scan.
180
+
181
+ Tests never use `tempfile`: scratch goes to `$PHOCINAE_RULEPACK_TMP`, or to a
182
+ directory beside the repository.
183
+
184
+ ## What this is not
185
+
186
+ It is not a compliance, legal, medical or financial conclusion. It reports
187
+ differences for a human to review. It is not a model, and it does not call one.
188
+
189
+ ## License
190
+
191
+ Apache-2.0.
@@ -0,0 +1,58 @@
1
+ """phocinae-rulepack — deterministic rule-pack engine.
2
+
3
+ Read a ledger, apply a versioned rule pack, emit only the differences for human
4
+ review. Zero model calls, zero third-party runtime dependencies.
5
+
6
+ Design contract (see the project's design document, section 2):
7
+ * three-state verdict: pass / fail / review
8
+ * `review` is the grey-zone escalation to a human, not "look again later"
9
+ * a missing field or an unmatched rule NEVER defaults to `pass` (fail-closed)
10
+ * `model_calls` is a real counter and is always 0 here
11
+ * an empty scan is a FAILURE, not a success
12
+ * the same input produces byte-identical output
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ from .errors import (
18
+ PackError,
19
+ EXIT_PASS,
20
+ EXIT_FAIL,
21
+ EXIT_REVIEW,
22
+ EXIT_USAGE,
23
+ EXIT_CODES,
24
+ exit_code,
25
+ exit_code_for_reason,
26
+ )
27
+ from .schema import Citation, Pack, Rule, load_pack, validate_pack, ValidationReport
28
+ from .engine import check, check_many, dump_canonical
29
+
30
+ __version__ = "0.1.0"
31
+
32
+ #: Verdict vocabulary. Ordered by strictness so aggregation is a max().
33
+ VERDICTS = ("pass", "review", "fail")
34
+
35
+ __all__ = [
36
+ "__version__",
37
+ "VERDICTS",
38
+ # errors / exit codes
39
+ "PackError",
40
+ "EXIT_PASS",
41
+ "EXIT_FAIL",
42
+ "EXIT_REVIEW",
43
+ "EXIT_USAGE",
44
+ "EXIT_CODES",
45
+ "exit_code",
46
+ "exit_code_for_reason",
47
+ # data model
48
+ "Citation",
49
+ "Pack",
50
+ "Rule",
51
+ "ValidationReport",
52
+ "load_pack",
53
+ "validate_pack",
54
+ # engine
55
+ "check",
56
+ "check_many",
57
+ "dump_canonical",
58
+ ]
@@ -0,0 +1,10 @@
1
+ """``python -m phocinae_rulepack`` entry point."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import sys
6
+
7
+ from .cli import main
8
+
9
+ if __name__ == "__main__":
10
+ sys.exit(main())