zanzibar-index 0.0.2__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 (47) hide show
  1. zanzibar_index-0.0.2/CHANGELOG.md +38 -0
  2. zanzibar_index-0.0.2/LICENSE +202 -0
  3. zanzibar_index-0.0.2/MANIFEST.in +14 -0
  4. zanzibar_index-0.0.2/PKG-INFO +135 -0
  5. zanzibar_index-0.0.2/PYPI_README.md +103 -0
  6. zanzibar_index-0.0.2/README.md +686 -0
  7. zanzibar_index-0.0.2/pyproject.toml +66 -0
  8. zanzibar_index-0.0.2/setup.cfg +4 -0
  9. zanzibar_index-0.0.2/src/zanzibar/__init__.py +13 -0
  10. zanzibar_index-0.0.2/src/zanzibar/connectedstore/__init__.py +44 -0
  11. zanzibar_index-0.0.2/src/zanzibar/connectedstore/apply.py +191 -0
  12. zanzibar_index-0.0.2/src/zanzibar/connectedstore/build.py +281 -0
  13. zanzibar_index-0.0.2/src/zanzibar/connectedstore/models.py +74 -0
  14. zanzibar_index-0.0.2/src/zanzibar/connectedstore/schema_io.py +111 -0
  15. zanzibar_index-0.0.2/src/zanzibar/connectedstore/source.py +641 -0
  16. zanzibar_index-0.0.2/src/zanzibar/connectedstore/store.py +511 -0
  17. zanzibar_index-0.0.2/src/zanzibar/graphindex/__init__.py +19 -0
  18. zanzibar_index-0.0.2/src/zanzibar/graphindex/bulk_backfill.py +767 -0
  19. zanzibar_index-0.0.2/src/zanzibar/graphindex/bulk_build.py +400 -0
  20. zanzibar_index-0.0.2/src/zanzibar/graphindex/core.py +1320 -0
  21. zanzibar_index-0.0.2/src/zanzibar/graphindex/invariants.py +861 -0
  22. zanzibar_index-0.0.2/src/zanzibar/graphindex/models.py +192 -0
  23. zanzibar_index-0.0.2/src/zanzibar/graphindex/multiset.py +36 -0
  24. zanzibar_index-0.0.2/src/zanzibar/graphindex/outbox.py +93 -0
  25. zanzibar_index-0.0.2/src/zanzibar/graphindex/processor.py +1671 -0
  26. zanzibar_index-0.0.2/src/zanzibar/graphindex/wildcard.py +1105 -0
  27. zanzibar_index-0.0.2/src/zanzibar/py.typed +0 -0
  28. zanzibar_index-0.0.2/src/zanzibar/schema/__init__.py +174 -0
  29. zanzibar_index-0.0.2/src/zanzibar/schema/boolean.py +738 -0
  30. zanzibar_index-0.0.2/src/zanzibar/schema/compiler.py +605 -0
  31. zanzibar_index-0.0.2/src/zanzibar/schema/errors.py +223 -0
  32. zanzibar_index-0.0.2/src/zanzibar/schema/json_frontend.py +230 -0
  33. zanzibar_index-0.0.2/src/zanzibar/schema/parser.py +586 -0
  34. zanzibar_index-0.0.2/src/zanzibar/schema/reports.py +293 -0
  35. zanzibar_index-0.0.2/src/zanzibar/schema/rules.py +494 -0
  36. zanzibar_index-0.0.2/src/zanzibar/schema/syntax.py +95 -0
  37. zanzibar_index-0.0.2/src/zanzibar/schema/unparse.py +64 -0
  38. zanzibar_index-0.0.2/src/zanzibar/setengine/__init__.py +20 -0
  39. zanzibar_index-0.0.2/src/zanzibar/setengine/engine.py +1656 -0
  40. zanzibar_index-0.0.2/src/zanzibar/setengine/memberset.py +150 -0
  41. zanzibar_index-0.0.2/src/zanzibar/setengine/models.py +42 -0
  42. zanzibar_index-0.0.2/src/zanzibar/setengine/setops.py +60 -0
  43. zanzibar_index-0.0.2/src/zanzibar_index.egg-info/PKG-INFO +135 -0
  44. zanzibar_index-0.0.2/src/zanzibar_index.egg-info/SOURCES.txt +45 -0
  45. zanzibar_index-0.0.2/src/zanzibar_index.egg-info/dependency_links.txt +1 -0
  46. zanzibar_index-0.0.2/src/zanzibar_index.egg-info/requires.txt +9 -0
  47. zanzibar_index-0.0.2/src/zanzibar_index.egg-info/top_level.txt +1 -0
@@ -0,0 +1,38 @@
1
+ # Changelog
2
+
3
+ All notable changes to the `zanzibar-index` distribution (import package `zanzibar`).
4
+ The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/); versions follow
5
+ [Semantic Versioning](https://semver.org/), with the usual 0.x caveat that any minor release
6
+ may break the API. The newest version heading must equal `zanzibar.__version__`
7
+ (`tests/test_tk122_release_metadata.py`).
8
+
9
+ ## [Unreleased]
10
+
11
+ ## [0.0.2]
12
+
13
+ First release published to PyPI. Same library as 0.0.1.
14
+
15
+ ### Fixed
16
+ - `formal/conformance/test_runner_retry.py`'s spawn-error stub now sets `.winerror`
17
+ explicitly. Only Windows CPython derives it from `OSError`'s fourth argument, so on the
18
+ Linux CI runner the retry test failed and the no-retry test passed for the wrong reason.
19
+ Test-only; no library change.
20
+
21
+ ## [0.0.1]
22
+
23
+ Tagged but never published: the release gate failed on the Linux CI runner (the test
24
+ above), so the upload was skipped. A trial of the release pipeline (tag -> gate -> fuzz ->
25
+ PyPI), not a stability promise.
26
+
27
+ ### Added
28
+ - The `zanzibar` package: `zanzibar.schema` (OpenFGA-DSL parser and compiler, including
29
+ `and` / `but not`), `zanzibar.setengine`, `zanzibar.graphindex` and
30
+ `zanzibar.connectedstore`.
31
+ - Apache-2.0 licence, `py.typed` marker, PyPI metadata.
32
+
33
+ ### Changed
34
+ - All database tables are prefixed `zanzibar_` (`node` -> `zanzibar_node`, ...), as are the
35
+ explicit constraint and index names. The tables no longer set `extend_existing`, so a
36
+ name clash with an application's own table raises instead of silently merging the two.
37
+ This is a schema change for any existing database. There is no migration: rebuild the
38
+ store from its tuples.
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,14 @@
1
+ # sdist contents (TK122). The library is src/zanzibar/; the test suite, the Lean proofs,
2
+ # benchmarks, scripts and docs live in the repository, not the sdist -- the tests need
3
+ # formal/ and the root conftest.py to run, so shipping them alone would ship a broken suite.
4
+ include LICENSE
5
+ include PYPI_README.md
6
+ include CHANGELOG.md
7
+ prune tests
8
+ prune formal
9
+ prune benchmarks
10
+ prune scripts
11
+ prune docs
12
+ prune tasks
13
+ global-exclude __pycache__
14
+ global-exclude *.py[cod]
@@ -0,0 +1,135 @@
1
+ Metadata-Version: 2.4
2
+ Name: zanzibar-index
3
+ Version: 0.0.2
4
+ Summary: Zanzibar-style relationship/permission evaluation: a set engine and a materialized graph index with identical semantics.
5
+ Author: Avery Khoo
6
+ License-Expression: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/averykhoo/graph-reachability-zanzibar-index
8
+ Project-URL: Source, https://github.com/averykhoo/graph-reachability-zanzibar-index
9
+ Project-URL: Documentation, https://github.com/averykhoo/graph-reachability-zanzibar-index/blob/master/docs/architecture/overview.md
10
+ Project-URL: Changelog, https://github.com/averykhoo/graph-reachability-zanzibar-index/blob/master/CHANGELOG.md
11
+ Keywords: zanzibar,rebac,authorization,permissions,openfga,acl
12
+ Classifier: Development Status :: 3 - Alpha
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: Operating System :: OS Independent
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3 :: Only
17
+ Classifier: Programming Language :: Python :: 3.13
18
+ Classifier: Topic :: Database
19
+ Classifier: Topic :: Security
20
+ Classifier: Typing :: Typed
21
+ Requires-Python: >=3.13
22
+ Description-Content-Type: text/markdown
23
+ License-File: LICENSE
24
+ Requires-Dist: sqlmodel>=0.0.39
25
+ Requires-Dist: pyroaring>=1.1.0
26
+ Provides-Extra: postgres
27
+ Requires-Dist: psycopg2-binary; extra == "postgres"
28
+ Provides-Extra: test
29
+ Requires-Dist: pytest; extra == "test"
30
+ Requires-Dist: hypothesis; extra == "test"
31
+ Dynamic: license-file
32
+
33
+ # zanzibar-index
34
+
35
+ Zanzibar-style relationship-based access control (ReBAC) for Python, on top of SQLModel.
36
+ Schemas are written in the [OpenFGA](https://openfga.dev) modelling DSL, including the
37
+ `and` / `but not` operators. The package is not affiliated with Google or OpenFGA.
38
+
39
+ Two evaluation backends give the **same answers** with **opposite cost models**:
40
+
41
+ * `zanzibar.setengine` stores only the raw tuples and computes memberships on the fly
42
+ with bitmap algebra (roaring bitmaps). Writes are cheap, reads do the work.
43
+ * `zanzibar.graphindex` materializes the transitive closure as reference-counted edges,
44
+ so `check` is a single indexed lookup. Writes do the work, reads are cheap.
45
+
46
+ `zanzibar.connectedstore` composes them: an append-only tuple log is the source of truth,
47
+ the graph index is a materialized view over it, and reads fall back to the set engine
48
+ when the index is behind.
49
+
50
+ ## Status: alpha, research code, no support promised
51
+
52
+ This is a personal research project. It is tested heavily: a differential suite checks
53
+ both backends against an independent reference oracle, there is property-based fuzzing,
54
+ and the core algorithms have machine-checked Lean proofs. It has **never run in
55
+ production**, there is **no performance target**, and the API may change between 0.x
56
+ releases. Issues and pull requests are welcome, but answers are best-effort.
57
+
58
+ ## Install
59
+
60
+ ```
61
+ pip install zanzibar-index # SQLite
62
+ pip install "zanzibar-index[postgres]" # adds psycopg2-binary for PostgreSQL
63
+ ```
64
+
65
+ Python 3.13 or later. The import name is `zanzibar`.
66
+
67
+ ## Quickstart
68
+
69
+ ```python
70
+ from sqlmodel import SQLModel, Session, create_engine
71
+ from zanzibar.connectedstore import ConnectedStore
72
+
73
+ SCHEMA = """
74
+ model
75
+ schema 1.1
76
+ type user
77
+ type group
78
+ relations
79
+ define member: [user, group#member]
80
+ type doc
81
+ relations
82
+ define banned: [user]
83
+ define viewer: [user, group#member] but not banned
84
+ """
85
+
86
+ engine = create_engine("sqlite:///perms.db")
87
+ SQLModel.metadata.create_all(engine) # creates the zanzibar_* tables (see below)
88
+
89
+ with Session(engine) as session:
90
+ store = ConnectedStore(session, "my-app", schema=SCHEMA)
91
+ # add_tuple(subject_predicate, subject_type, subject_name, relation, object_type, object_name)
92
+ store.add_tuple("...", "user", "alice", "member", "group", "eng") # "..." = the bare user
93
+ store.add_tuple("member", "group", "eng", "viewer", "doc", "d1") # group#member is a viewer
94
+ store.check("...", "user", "alice", "viewer", "doc", "d1") # True
95
+
96
+ store.add_tuple("...", "user", "alice", "banned", "doc", "d1")
97
+ store.check("...", "user", "alice", "viewer", "doc", "d1") # False
98
+ ```
99
+
100
+ Each write commits and returns a log id. Pass it back as `check(..., at_least=token)` to
101
+ read your own writes when the index is maintained asynchronously (`sync=False`).
102
+ Reopening a store with `ConnectedStore(session, "my-app")` reads its persisted schema;
103
+ a store's schema is write-once.
104
+
105
+ ## Things to know before you use it
106
+
107
+ * **Tables.** The library declares its tables on SQLModel's default metadata, all named
108
+ `zanzibar_*`, so `SQLModel.metadata.create_all(engine)` creates them next to your own.
109
+ A table of yours that reuses one of those names is a loud `InvalidRequestError`,
110
+ whichever is declared first; the two are never silently merged.
111
+ * **Databases.** SQLite and PostgreSQL only. On PostgreSQL the session must use
112
+ `READ COMMITTED`; any other isolation level is refused at construction.
113
+ * **Runtime checks.** `ConnectedStore(..., paranoia="residue")` or the
114
+ `ZANZIBAR_PARANOIA=residue` environment variable turns on a cheap runtime corruption
115
+ detector for the index. It is off by default and recommended for anything
116
+ security-sensitive.
117
+ * **Schema shapes.** Some valid OpenFGA shapes are refused with an explanation of what to
118
+ write instead: for example, a `from` tupleset must be a direct relation. Wildcard
119
+ usersets (`[group:*#member]`) and object wildcards go beyond OpenFGA. They work, but
120
+ they are outside the proved fragment and emit `UnprovenExtensionWarning`.
121
+ * **Write cap.** A single write that would add more than `ZANZIBAR_MAX_CLOSURE_FANOUT`
122
+ closure edges (default 100,000; `0` disables) is refused with `ClosureFanoutExceeded`.
123
+ The write then lands in neither backend.
124
+ * **Public API.** Each subpackage's `__all__` is the public surface. Underscore-prefixed
125
+ names that happen to be re-exported are internal.
126
+
127
+ ## Documentation
128
+
129
+ Design docs, the correctness argument and the Lean proofs live in the repository:
130
+ <https://github.com/averykhoo/graph-reachability-zanzibar-index>. Start at
131
+ [`docs/architecture/overview.md`](https://github.com/averykhoo/graph-reachability-zanzibar-index/blob/master/docs/architecture/overview.md).
132
+
133
+ ## License
134
+
135
+ Apache-2.0. See `LICENSE`.
@@ -0,0 +1,103 @@
1
+ # zanzibar-index
2
+
3
+ Zanzibar-style relationship-based access control (ReBAC) for Python, on top of SQLModel.
4
+ Schemas are written in the [OpenFGA](https://openfga.dev) modelling DSL, including the
5
+ `and` / `but not` operators. The package is not affiliated with Google or OpenFGA.
6
+
7
+ Two evaluation backends give the **same answers** with **opposite cost models**:
8
+
9
+ * `zanzibar.setengine` stores only the raw tuples and computes memberships on the fly
10
+ with bitmap algebra (roaring bitmaps). Writes are cheap, reads do the work.
11
+ * `zanzibar.graphindex` materializes the transitive closure as reference-counted edges,
12
+ so `check` is a single indexed lookup. Writes do the work, reads are cheap.
13
+
14
+ `zanzibar.connectedstore` composes them: an append-only tuple log is the source of truth,
15
+ the graph index is a materialized view over it, and reads fall back to the set engine
16
+ when the index is behind.
17
+
18
+ ## Status: alpha, research code, no support promised
19
+
20
+ This is a personal research project. It is tested heavily: a differential suite checks
21
+ both backends against an independent reference oracle, there is property-based fuzzing,
22
+ and the core algorithms have machine-checked Lean proofs. It has **never run in
23
+ production**, there is **no performance target**, and the API may change between 0.x
24
+ releases. Issues and pull requests are welcome, but answers are best-effort.
25
+
26
+ ## Install
27
+
28
+ ```
29
+ pip install zanzibar-index # SQLite
30
+ pip install "zanzibar-index[postgres]" # adds psycopg2-binary for PostgreSQL
31
+ ```
32
+
33
+ Python 3.13 or later. The import name is `zanzibar`.
34
+
35
+ ## Quickstart
36
+
37
+ ```python
38
+ from sqlmodel import SQLModel, Session, create_engine
39
+ from zanzibar.connectedstore import ConnectedStore
40
+
41
+ SCHEMA = """
42
+ model
43
+ schema 1.1
44
+ type user
45
+ type group
46
+ relations
47
+ define member: [user, group#member]
48
+ type doc
49
+ relations
50
+ define banned: [user]
51
+ define viewer: [user, group#member] but not banned
52
+ """
53
+
54
+ engine = create_engine("sqlite:///perms.db")
55
+ SQLModel.metadata.create_all(engine) # creates the zanzibar_* tables (see below)
56
+
57
+ with Session(engine) as session:
58
+ store = ConnectedStore(session, "my-app", schema=SCHEMA)
59
+ # add_tuple(subject_predicate, subject_type, subject_name, relation, object_type, object_name)
60
+ store.add_tuple("...", "user", "alice", "member", "group", "eng") # "..." = the bare user
61
+ store.add_tuple("member", "group", "eng", "viewer", "doc", "d1") # group#member is a viewer
62
+ store.check("...", "user", "alice", "viewer", "doc", "d1") # True
63
+
64
+ store.add_tuple("...", "user", "alice", "banned", "doc", "d1")
65
+ store.check("...", "user", "alice", "viewer", "doc", "d1") # False
66
+ ```
67
+
68
+ Each write commits and returns a log id. Pass it back as `check(..., at_least=token)` to
69
+ read your own writes when the index is maintained asynchronously (`sync=False`).
70
+ Reopening a store with `ConnectedStore(session, "my-app")` reads its persisted schema;
71
+ a store's schema is write-once.
72
+
73
+ ## Things to know before you use it
74
+
75
+ * **Tables.** The library declares its tables on SQLModel's default metadata, all named
76
+ `zanzibar_*`, so `SQLModel.metadata.create_all(engine)` creates them next to your own.
77
+ A table of yours that reuses one of those names is a loud `InvalidRequestError`,
78
+ whichever is declared first; the two are never silently merged.
79
+ * **Databases.** SQLite and PostgreSQL only. On PostgreSQL the session must use
80
+ `READ COMMITTED`; any other isolation level is refused at construction.
81
+ * **Runtime checks.** `ConnectedStore(..., paranoia="residue")` or the
82
+ `ZANZIBAR_PARANOIA=residue` environment variable turns on a cheap runtime corruption
83
+ detector for the index. It is off by default and recommended for anything
84
+ security-sensitive.
85
+ * **Schema shapes.** Some valid OpenFGA shapes are refused with an explanation of what to
86
+ write instead: for example, a `from` tupleset must be a direct relation. Wildcard
87
+ usersets (`[group:*#member]`) and object wildcards go beyond OpenFGA. They work, but
88
+ they are outside the proved fragment and emit `UnprovenExtensionWarning`.
89
+ * **Write cap.** A single write that would add more than `ZANZIBAR_MAX_CLOSURE_FANOUT`
90
+ closure edges (default 100,000; `0` disables) is refused with `ClosureFanoutExceeded`.
91
+ The write then lands in neither backend.
92
+ * **Public API.** Each subpackage's `__all__` is the public surface. Underscore-prefixed
93
+ names that happen to be re-exported are internal.
94
+
95
+ ## Documentation
96
+
97
+ Design docs, the correctness argument and the Lean proofs live in the repository:
98
+ <https://github.com/averykhoo/graph-reachability-zanzibar-index>. Start at
99
+ [`docs/architecture/overview.md`](https://github.com/averykhoo/graph-reachability-zanzibar-index/blob/master/docs/architecture/overview.md).
100
+
101
+ ## License
102
+
103
+ Apache-2.0. See `LICENSE`.