mkp-contracts 0.2.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.
- mkp_contracts-0.2.0/.gitignore +43 -0
- mkp_contracts-0.2.0/LICENSE +201 -0
- mkp_contracts-0.2.0/PKG-INFO +9 -0
- mkp_contracts-0.2.0/README.md +66 -0
- mkp_contracts-0.2.0/pyproject.toml +61 -0
- mkp_contracts-0.2.0/src/mkp_contracts/__init__.py +12 -0
- mkp_contracts-0.2.0/src/mkp_contracts/archive.py +70 -0
- mkp_contracts-0.2.0/src/mkp_contracts/canonical.py +42 -0
- mkp_contracts-0.2.0/src/mkp_contracts/cli_versions.py +34 -0
- mkp_contracts-0.2.0/src/mkp_contracts/errors.py +69 -0
- mkp_contracts-0.2.0/src/mkp_contracts/expressions.py +103 -0
- mkp_contracts-0.2.0/src/mkp_contracts/gates.py +243 -0
- mkp_contracts-0.2.0/src/mkp_contracts/py.typed +0 -0
- mkp_contracts-0.2.0/src/mkp_contracts/roles.py +35 -0
- mkp_contracts-0.2.0/src/mkp_contracts/schemas.py +82 -0
- mkp_contracts-0.2.0/src/mkp_contracts/signature.py +120 -0
- mkp_contracts-0.2.0/tests/__init__.py +0 -0
- mkp_contracts-0.2.0/tests/test_canonical.py +149 -0
- mkp_contracts-0.2.0/tests/test_expressions.py +62 -0
- mkp_contracts-0.2.0/tests/test_gates.py +186 -0
- mkp_contracts-0.2.0/tests/test_schemas.py +150 -0
- mkp_contracts-0.2.0/tests/test_signature.py +51 -0
- mkp_contracts-0.2.0/uv.lock +275 -0
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
.venv/
|
|
5
|
+
venv/
|
|
6
|
+
*.egg-info/
|
|
7
|
+
dist/
|
|
8
|
+
build/
|
|
9
|
+
.mypy_cache/
|
|
10
|
+
.ruff_cache/
|
|
11
|
+
.pytest_cache/
|
|
12
|
+
.coverage
|
|
13
|
+
htmlcov/
|
|
14
|
+
|
|
15
|
+
# Node (web/)
|
|
16
|
+
node_modules/
|
|
17
|
+
.next/
|
|
18
|
+
.turbo/
|
|
19
|
+
|
|
20
|
+
# Local config and secrets — never tracked
|
|
21
|
+
.env
|
|
22
|
+
.env.*
|
|
23
|
+
!.env.example
|
|
24
|
+
deploy/.local/
|
|
25
|
+
deploy/secrets/
|
|
26
|
+
# Database dumps written by the pg-backup sidecar on the host
|
|
27
|
+
deploy/backups/
|
|
28
|
+
*.pem
|
|
29
|
+
*.key
|
|
30
|
+
!**/*.pub.key
|
|
31
|
+
# age identities generated for laptop bring-up
|
|
32
|
+
*.age
|
|
33
|
+
|
|
34
|
+
# Runtime state
|
|
35
|
+
.mklang-platform/
|
|
36
|
+
*.db
|
|
37
|
+
*.sqlite3
|
|
38
|
+
run-artifacts/
|
|
39
|
+
|
|
40
|
+
# Editors / OS
|
|
41
|
+
.DS_Store
|
|
42
|
+
.idea/
|
|
43
|
+
.vscode/
|
|
@@ -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 Derivative
|
|
95
|
+
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 Gianluca
|
|
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,9 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: mkp-contracts
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: The cross-boundary contracts as code: canonical form, digests, schema validation
|
|
5
|
+
License-Expression: Apache-2.0
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Requires-Python: <3.14,>=3.11
|
|
8
|
+
Requires-Dist: jsonschema>=4.21
|
|
9
|
+
Requires-Dist: pyyaml>=6.0
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# mkp-contracts
|
|
2
|
+
|
|
3
|
+
The contracts that cross component boundaries, as code. [`schema/`](../../schema/)
|
|
4
|
+
holds them as data; this holds the rules a JSON Schema cannot express.
|
|
5
|
+
|
|
6
|
+
A library: no CLI, no side effects, no I/O beyond reading a document it was handed.
|
|
7
|
+
|
|
8
|
+
| Module | What it owns |
|
|
9
|
+
| -------------- | --------------------------------------------------------------------------------------- |
|
|
10
|
+
| `canonical.py` | The canonical form of a manifest, therefore its digest, therefore a bundle's identity |
|
|
11
|
+
| `schemas.py` | Locating the schemas, deciding which contract a document is against, validating it |
|
|
12
|
+
| `errors.py` | `Failure` and `Report` — reporting every rule a document breaks rather than the first |
|
|
13
|
+
| `roles.py` | The four role **names**, which the API, `mkctl`, the console and the glossary all spell |
|
|
14
|
+
|
|
15
|
+
## Why it is not inside the builder
|
|
16
|
+
|
|
17
|
+
Because "what is this bundle" must have one answer, and neither side of the question
|
|
18
|
+
owns it.
|
|
19
|
+
|
|
20
|
+
`mkbundle` computes a digest when it builds. The control plane recomputes it when the
|
|
21
|
+
bundle is registered, because a digest asserted by the caller and never checked makes
|
|
22
|
+
every later reference — the environment pointer, the run's pinned revision, the audit
|
|
23
|
+
trail — a claim rather than a fact. If those two computed it separately they would
|
|
24
|
+
agree until they did not, and the day they stopped agreeing would be the day a bundle
|
|
25
|
+
had two identities.
|
|
26
|
+
|
|
27
|
+
Putting it in the builder and having the service import from it would work, and would
|
|
28
|
+
say that the control plane is downstream of a CLI, which is not true. Both depend on
|
|
29
|
+
this instead.
|
|
30
|
+
|
|
31
|
+
## The one rule worth knowing
|
|
32
|
+
|
|
33
|
+
Identity excludes `created`, `source`, and `signature`.
|
|
34
|
+
|
|
35
|
+
`created` is a build timestamp: inside the digest, identical content would get two
|
|
36
|
+
identities and promote-by-digest would be false. `source` is documented in
|
|
37
|
+
`bundle.schema.json` as informational. A `signature` cannot be inside the bytes it
|
|
38
|
+
signs. The signature covers exactly the canonical bytes the digest is taken over, so
|
|
39
|
+
there is one canonical form and not two — which is why signing a bundle does not move
|
|
40
|
+
its digest.
|
|
41
|
+
|
|
42
|
+
## Development
|
|
43
|
+
|
|
44
|
+
```
|
|
45
|
+
uv sync
|
|
46
|
+
uv run pytest -q
|
|
47
|
+
uv run --with ruff ruff check .
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## What is tested here, and what is not
|
|
51
|
+
|
|
52
|
+
The split follows the same line as the package itself. `tools/mkbundle/tests` and
|
|
53
|
+
`services/api/tests` test what each side _does_ with a digest — builds one, recomputes
|
|
54
|
+
one on registration, checks a signature against one. What is tested here is what a
|
|
55
|
+
digest **is**, because that is the thing neither of them owns and neither of them was
|
|
56
|
+
asserting.
|
|
57
|
+
|
|
58
|
+
The load-bearing one is `test_the_canonical_encoding_is_frozen`: a digest computed once,
|
|
59
|
+
written down, and compared against — not recomputed by the test from the code under
|
|
60
|
+
test. Four independent choices decide those bytes (sorted keys, no whitespace,
|
|
61
|
+
`ensure_ascii=False`, and the exclusion set), each of them changeable by an edit that
|
|
62
|
+
looks like tidying, and each of them changing **every digest at once**. The blast radius
|
|
63
|
+
is not a wrong answer somewhere: every environment pointer and every run's pinned
|
|
64
|
+
revision would name a bundle that no longer exists under that name. That is not a defect
|
|
65
|
+
a downstream test can catch, because downstream everything still agrees — it agrees on
|
|
66
|
+
the new answer.
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "mkp-contracts"
|
|
3
|
+
version = "0.2.0"
|
|
4
|
+
description = "The cross-boundary contracts as code: canonical form, digests, schema validation"
|
|
5
|
+
license = "Apache-2.0"
|
|
6
|
+
license-files = ["LICENSE"]
|
|
7
|
+
requires-python = ">=3.11,<3.14"
|
|
8
|
+
dependencies = ["jsonschema>=4.21", "pyyaml>=6.0"]
|
|
9
|
+
|
|
10
|
+
[dependency-groups]
|
|
11
|
+
# `types-*` are development dependencies of *this* project rather than flags on the
|
|
12
|
+
# check command: which stubs a package needs is a fact about what it imports.
|
|
13
|
+
dev = ["pytest>=8.0", "types-PyYAML", "types-jsonschema"]
|
|
14
|
+
|
|
15
|
+
[build-system]
|
|
16
|
+
requires = ["hatchling"]
|
|
17
|
+
build-backend = "hatchling.build"
|
|
18
|
+
|
|
19
|
+
[tool.hatch.build.targets.wheel]
|
|
20
|
+
# `src/mkp_contracts/py.typed` ships with it (PEP 561). Without that marker a consumer's
|
|
21
|
+
# type checker treats every symbol this package exports as `Any` — which is the opposite
|
|
22
|
+
# of what a package named "contracts" is for, and is how `workloads/nate` was reading
|
|
23
|
+
# `mkp_contracts.gates` until 2026-08-04.
|
|
24
|
+
packages = ["src/mkp_contracts"]
|
|
25
|
+
|
|
26
|
+
[tool.ruff]
|
|
27
|
+
line-length = 100
|
|
28
|
+
|
|
29
|
+
[tool.ruff.lint]
|
|
30
|
+
select = ["E", "F", "I", "UP", "B", "SIM", "RUF", "S"]
|
|
31
|
+
|
|
32
|
+
[tool.ruff.lint.per-file-ignores]
|
|
33
|
+
# Project rule: no inline noqa — exemptions live here with the reason. signature.py
|
|
34
|
+
# runs `minisign` with a fixed argv and no shell.
|
|
35
|
+
"src/mkp_contracts/signature.py" = ["S603", "S607"]
|
|
36
|
+
# `S` reads production code. In a test the same rules are noise with a cost: a test that
|
|
37
|
+
# asserts is doing its job (S101), a fixture's fake credential is a literal on purpose
|
|
38
|
+
# (S105-S107), and a query built to set up a fixture is not a query an attacker reaches
|
|
39
|
+
# (S608). Ignored by path rather than by line, because there would be one line per test.
|
|
40
|
+
"tests/**" = ["S101", "S105", "S106", "S107", "S608"]
|
|
41
|
+
|
|
42
|
+
[tool.pytest.ini_options]
|
|
43
|
+
testpaths = ["tests"]
|
|
44
|
+
|
|
45
|
+
[tool.mypy]
|
|
46
|
+
# The same settings `mklang` checks with, so a contract crossing the two repositories is
|
|
47
|
+
# read the same way on both sides. `warn_unused_ignores` earns its place immediately: it
|
|
48
|
+
# is what would have caught the eight suppressions in `web/bff` that named a mypy error
|
|
49
|
+
# code in a repository where mypy had never run.
|
|
50
|
+
files = ["src"]
|
|
51
|
+
python_version = "3.11"
|
|
52
|
+
check_untyped_defs = true
|
|
53
|
+
disallow_incomplete_defs = true
|
|
54
|
+
strict_equality = true
|
|
55
|
+
no_implicit_reexport = true
|
|
56
|
+
warn_redundant_casts = true
|
|
57
|
+
warn_unused_ignores = true
|
|
58
|
+
|
|
59
|
+
[tool.pyright]
|
|
60
|
+
typeCheckingMode = "basic"
|
|
61
|
+
exclude = [".venv"]
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"""The contracts that cross component boundaries, as code.
|
|
2
|
+
|
|
3
|
+
`schema/` holds them as data; this holds the rules a schema cannot express — what a
|
|
4
|
+
bundle's canonical form is, therefore what its digest is, therefore what its identity
|
|
5
|
+
is. Both the builder and the control plane depend on this rather than on each other,
|
|
6
|
+
because "what is this bundle" must have one answer and neither of them owns it.
|
|
7
|
+
|
|
8
|
+
A library, deliberately: no CLI, no side effects, no I/O beyond reading a document it
|
|
9
|
+
was handed.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
__version__ = "0.1.0"
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"""What is inside a bundle archive, and whether it is what it claims to be.
|
|
2
|
+
|
|
3
|
+
Here for the same reason `canonical.py` is: **a bundle's identity is the digest of its
|
|
4
|
+
manifest, not the hash of its archive**, so "does this archive belong under this
|
|
5
|
+
digest?" is not a question either the builder or the control plane can answer alone
|
|
6
|
+
without one of them defining the answer for the other.
|
|
7
|
+
|
|
8
|
+
The distinction bites the first time someone stores an archive. Two builds of identical
|
|
9
|
+
content produce byte-identical archives — that is what the `filename=""` in the gzip
|
|
10
|
+
header exists for — but the archive is still not the identity: the manifest inside it
|
|
11
|
+
is, and it is the manifest that `signature` covers and that `created` is excluded from.
|
|
12
|
+
An implementation that filed archives under `sha256(bytes)` would be filing them under a
|
|
13
|
+
name nothing else in the system uses.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import io
|
|
19
|
+
import json
|
|
20
|
+
import tarfile
|
|
21
|
+
from typing import Any
|
|
22
|
+
|
|
23
|
+
from .canonical import manifest_digest
|
|
24
|
+
from .errors import ContractError
|
|
25
|
+
|
|
26
|
+
MANIFEST_NAME = "manifest.json"
|
|
27
|
+
|
|
28
|
+
# A bundle is workflows, machines and small assets. Refusing to expand beyond this is
|
|
29
|
+
# what stops a crafted archive turning a read into a memory exhaustion — the check
|
|
30
|
+
# happens before the tar is walked, on a number the header states rather than on what
|
|
31
|
+
# it actually contains.
|
|
32
|
+
MAX_ARCHIVE_BYTES = 64 * 1024 * 1024
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def manifest_of(archive: bytes) -> dict[str, Any]:
|
|
36
|
+
"""The manifest at the root of an archive."""
|
|
37
|
+
if len(archive) > MAX_ARCHIVE_BYTES:
|
|
38
|
+
raise ContractError(f"archive is larger than {MAX_ARCHIVE_BYTES} bytes")
|
|
39
|
+
try:
|
|
40
|
+
with tarfile.open(fileobj=io.BytesIO(archive), mode="r:gz") as tar:
|
|
41
|
+
member = tar.extractfile(MANIFEST_NAME)
|
|
42
|
+
if member is None:
|
|
43
|
+
raise ContractError(f"no {MANIFEST_NAME} at the root of the archive")
|
|
44
|
+
return json.loads(member.read())
|
|
45
|
+
except tarfile.TarError as exc:
|
|
46
|
+
raise ContractError(f"not a readable bundle archive: {exc}") from exc
|
|
47
|
+
except ValueError as exc:
|
|
48
|
+
raise ContractError(f"{MANIFEST_NAME} is not JSON: {exc}") from exc
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def digest_of(archive: bytes) -> str:
|
|
52
|
+
"""The bundle identity this archive carries.
|
|
53
|
+
|
|
54
|
+
Read off the manifest inside, never off the bytes: the archive is a container and
|
|
55
|
+
the manifest is the thing with a name.
|
|
56
|
+
"""
|
|
57
|
+
return manifest_digest(manifest_of(archive))
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def belongs_under(archive: bytes, digest: str) -> bool:
|
|
61
|
+
return digest_of(archive) == digest
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
__all__ = [
|
|
65
|
+
"MANIFEST_NAME",
|
|
66
|
+
"MAX_ARCHIVE_BYTES",
|
|
67
|
+
"belongs_under",
|
|
68
|
+
"digest_of",
|
|
69
|
+
"manifest_of",
|
|
70
|
+
]
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"""Canonical form and digests.
|
|
2
|
+
|
|
3
|
+
A bundle's identity is the digest of its content (ADR 0008), which only works if
|
|
4
|
+
building the same content twice produces the same digest. Three manifest fields
|
|
5
|
+
are therefore outside the canonical form:
|
|
6
|
+
|
|
7
|
+
- `created` is a build timestamp; including it would give identical content two
|
|
8
|
+
identities and defeat the whole model — promote-by-digest, "nothing is rebuilt",
|
|
9
|
+
and "a superseded bundle is retained until no run references it" all rest on
|
|
10
|
+
content and identity being the same thing.
|
|
11
|
+
- `source` is documented in `bundle.schema.json` as informational: "identity is
|
|
12
|
+
the content digest, not this".
|
|
13
|
+
- `signature` cannot be inside the bytes it signs.
|
|
14
|
+
|
|
15
|
+
The signature covers exactly these canonical bytes, so there is one canonical
|
|
16
|
+
form and not two.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
import hashlib
|
|
22
|
+
import json
|
|
23
|
+
from typing import Any
|
|
24
|
+
|
|
25
|
+
EXCLUDED_FROM_IDENTITY = ("created", "source", "signature")
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def canonical_bytes(document: dict[str, Any]) -> bytes:
|
|
29
|
+
"""The bytes a digest and a signature are both computed over."""
|
|
30
|
+
stripped = {k: v for k, v in document.items() if k not in EXCLUDED_FROM_IDENTITY}
|
|
31
|
+
return json.dumps(stripped, sort_keys=True, separators=(",", ":"), ensure_ascii=False).encode(
|
|
32
|
+
"utf-8"
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def manifest_digest(manifest: dict[str, Any]) -> str:
|
|
37
|
+
"""The bundle's identity: sha256 over the canonical manifest, bare hex."""
|
|
38
|
+
return hashlib.sha256(canonical_bytes(manifest)).hexdigest()
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def file_digest(data: bytes) -> str:
|
|
42
|
+
return hashlib.sha256(data).hexdigest()
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"""The CLI versions the control plane serves (ADR 0014, `C33`).
|
|
2
|
+
|
|
3
|
+
Here rather than in the api, because this package is where wire compatibility
|
|
4
|
+
already lives: the PR that changes a canonical form is the PR that moves
|
|
5
|
+
`CLI_MINIMUM`, reviewed as one change. `CLI_LATEST` moves in the commit that tags a
|
|
6
|
+
CLI release — `release-cli.yml` fails when the tag disagrees, so the two cannot
|
|
7
|
+
drift silently.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
# The oldest mkctl the wire still serves. Raised deliberately, in the same PR as the
|
|
13
|
+
# change that stops serving older ones — never as housekeeping.
|
|
14
|
+
CLI_MINIMUM = "0.1.0"
|
|
15
|
+
|
|
16
|
+
# What `mkctl upgrade` and the login advisory point at. 0.4.0 is the first version on
|
|
17
|
+
# PyPI: before it, `mkbundle` and this package shipped as `0.1.0` in three different
|
|
18
|
+
# GitHub releases, which is three builds behind one number — fine for assets fetched by
|
|
19
|
+
# tag, wrong for an index where a version is permanent.
|
|
20
|
+
CLI_LATEST = "0.4.0"
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def parse(version: str) -> tuple[int, ...] | None:
|
|
24
|
+
"""`"0.2.0"` → `(0, 2, 0)`; anything else → None.
|
|
25
|
+
|
|
26
|
+
None means "do not gate": a User-Agent this module cannot read belongs to a
|
|
27
|
+
client this contract does not cover, and refusing what you cannot parse would
|
|
28
|
+
gate curl and every future tool along with it.
|
|
29
|
+
"""
|
|
30
|
+
parts = version.strip().split(".")
|
|
31
|
+
try:
|
|
32
|
+
return tuple(int(part) for part in parts)
|
|
33
|
+
except ValueError:
|
|
34
|
+
return None
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"""Failures, reported as data rather than raised one at a time.
|
|
2
|
+
|
|
3
|
+
A build reports every rule a bundle breaks, not the first. Fixing one problem
|
|
4
|
+
only to be told about the next is what makes a validator feel like an obstacle
|
|
5
|
+
instead of a tool.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from dataclasses import dataclass, field
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@dataclass(frozen=True)
|
|
14
|
+
class Failure:
|
|
15
|
+
"""One broken rule, attributable to one place."""
|
|
16
|
+
|
|
17
|
+
rule: str
|
|
18
|
+
"""Stable id, e.g. `machine-missing`. Cited in messages and in tests."""
|
|
19
|
+
|
|
20
|
+
message: str
|
|
21
|
+
|
|
22
|
+
path: str = ""
|
|
23
|
+
"""Bundle-relative path of the document at fault, when there is one."""
|
|
24
|
+
|
|
25
|
+
location: str = ""
|
|
26
|
+
"""Where inside the document, e.g. `steps[2].idempotency_key`."""
|
|
27
|
+
|
|
28
|
+
def __str__(self) -> str:
|
|
29
|
+
where = self.path or "<bundle>"
|
|
30
|
+
if self.location:
|
|
31
|
+
where = f"{where}:{self.location}"
|
|
32
|
+
return f"{where}: [{self.rule}] {self.message}"
|
|
33
|
+
|
|
34
|
+
def as_dict(self) -> dict[str, str]:
|
|
35
|
+
return {
|
|
36
|
+
"rule": self.rule,
|
|
37
|
+
"message": self.message,
|
|
38
|
+
"path": self.path,
|
|
39
|
+
"location": self.location,
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@dataclass
|
|
44
|
+
class Report:
|
|
45
|
+
"""The outcome of a validation or a build."""
|
|
46
|
+
|
|
47
|
+
failures: list[Failure] = field(default_factory=list)
|
|
48
|
+
skipped_rules: list[str] = field(default_factory=list)
|
|
49
|
+
"""Rules that could not run. Never silent: a build says what it did not check."""
|
|
50
|
+
|
|
51
|
+
@property
|
|
52
|
+
def ok(self) -> bool:
|
|
53
|
+
return not self.failures
|
|
54
|
+
|
|
55
|
+
def fail(self, rule: str, message: str, path: str = "", location: str = "") -> None:
|
|
56
|
+
self.failures.append(Failure(rule, message, path, location))
|
|
57
|
+
|
|
58
|
+
def extend(self, other: Report) -> None:
|
|
59
|
+
self.failures.extend(other.failures)
|
|
60
|
+
self.skipped_rules.extend(other.skipped_rules)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class ContractError(Exception):
|
|
64
|
+
"""A document cannot be read or placed against a contract at all.
|
|
65
|
+
|
|
66
|
+
Distinct from a `Failure`: a failure is a rule a document breaks, reported
|
|
67
|
+
alongside every other one. This is the case where there is nothing to report
|
|
68
|
+
on — an unparseable file, a missing schema, a document of no known kind.
|
|
69
|
+
"""
|