Repository navigation
Expand file tree
/
Copy pathpython_shop.py
More file actions
270 lines (235 loc) · 8.79 KB
/
Copy pathpython_shop.py
File metadata and controls
270 lines (235 loc) · 8.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
"""Shop-defined Python constraints with technical relationships and business rules."""
from datetime import datetime, timezone
from pathlib import Path
from chipload import (
BuildIdentity,
Capability,
Choice,
Evidence,
Fact,
Process,
PythonCheck,
Quantity,
Range,
Resource,
Shop,
Step,
Workflow,
compile_shop,
quantity,
)
from chipload.evaluate import EvaluationRequest
from chipload.project import AcceptanceCase, SourceFile, SourceProject, run_cases
AT = datetime(2026, 10, 20, 12, tzinfo=timezone.utc)
PROFILES = {
"machining": ("depth", "mm", "diameter", "mm", "20", "10", "mill", "prototype", "production"),
"thermal": ("hold", "min", "mass", "kg", "30", "10", "furnace", "trial", "contract"),
"review": ("pages", "1", "reviewers", "1", "40", "2", None, "one-off", "retainer"),
}
def measurements(facts, left, right, dimensions):
a, b = facts[left], facts[right]
if not isinstance(a, Quantity) or not isinstance(b, Quantity):
return None
if (a.dimension, b.dimension) != dimensions:
return None
return a.base_value(), b.base_value()
def machining_fit(facts):
values = measurements(facts, "depth", "diameter", ("length", "length"))
if values is None:
return "unknown"
depth, diameter = values
return "supported" if depth <= 3 * diameter else "unsupported"
def thermal_fit(facts):
values = measurements(facts, "hold", "mass", ("time", "mass"))
if values is None:
return "unknown"
duration, mass = values
# Fictional declared relationship, not a qualified heat-treatment recipe.
minimum = quantity("2", "min").base_value() * mass / quantity("1", "kg").base_value()
return "supported" if duration >= minimum else "unsupported"
def review_fit(facts):
values = measurements(facts, "pages", "reviewers", ("ratio", "ratio"))
if values is None:
return "unknown"
pages, reviewers = values
return "supported" if pages <= 50 * reviewers else "unsupported"
def business(facts, small, contracted):
amount = facts["quantity"]
mode = facts["engagement"]
ready = facts["documentation-ready"]
if (
not isinstance(amount, Quantity)
or amount.dimension != "ratio"
or mode is None
or ready is None
):
return "unknown"
if mode == small and amount.base_value() > 20:
return "unsupported"
if mode == contracted and amount.base_value() < 100:
return "unsupported"
return "supported" if ready else "conditional"
def machining_business(facts):
return business(facts, "prototype", "production")
def thermal_business(facts):
return business(facts, "trial", "contract")
def review_business(facts):
return business(facts, "one-off", "retainer")
def define_shop(profile="machining"):
first, first_unit, second, second_unit, _, _, resource, small, contracted = PROFILES[profile]
requirements = (
Range(fact=first, minimum=quantity("1", first_unit), maximum=quantity("1000", first_unit)),
Range(
fact=second, minimum=quantity("1", second_unit), maximum=quantity("1000", second_unit)
),
Range(
fact="quantity",
minimum=quantity("1", "1"),
maximum=quantity("10000", "1"),
multiple_of=quantity("1", "1"),
),
Choice(fact="engagement", allowed=(small, contracted)),
Choice(fact="documentation-ready", allowed=(True, False)),
)
return Shop(
id="python-" + profile,
name="Python " + profile,
visibility="public",
processes=(Process(id="provide", name="Provide service", visibility="public"),),
resources=(Resource(id="resource", name="Example resource", kind=resource),)
if resource
else (),
capabilities=(
Capability(
id="service",
name="Service",
visibility="public",
process="provide",
resources=("resource",) if resource else (),
requirements=requirements,
evidence=("declaration",),
python_checks=(
PythonCheck(
id="relationship",
name="Input relationship",
visibility="public",
entrypoint=f"shop:{profile}_fit",
facts=(first, second),
),
PythonCheck(
id="business",
name="Business conditions",
visibility="public",
entrypoint=f"shop:{profile}_business",
facts=("engagement", "quantity", "documentation-ready"),
remedy="Confirm the required input documentation before proceeding.",
),
),
),
),
evidence=(
Evidence(
id="declaration",
name="Simulated declaration",
level="declared",
source="simulated",
claim="Fictional example scope",
recorded_at=AT,
capabilities=("service",),
resources=("resource",) if resource else (),
requirements=requirements,
),
),
workflows=(
Workflow(
id="service-route",
name="Service route",
visibility="public",
steps=(Step(id="provide", capability="service"),),
),
),
)
def define_machining():
return define_shop("machining")
def define_thermal():
return define_shop("thermal")
def define_review():
return define_shop("review")
def cases(profile):
first, first_unit, second, second_unit, a, b, _, small, contracted = PROFILES[profile]
base = {
first: quantity(a, first_unit),
second: quantity(b, second_unit),
"quantity": quantity("1", "1"),
"engagement": small,
"documentation-ready": True,
}
scenarios = (
("supported", {}, "supported"),
(
"relationship-rejected",
{first: quantity("1" if profile == "thermal" else "200", first_unit)},
"unsupported",
),
("contracted", {"engagement": contracted, "quantity": quantity("100", "1")}, "supported"),
("contract-too-small", {"engagement": contracted}, "unsupported"),
("small-lot-too-large", {"quantity": quantity("21", "1")}, "unsupported"),
("documentation-needed", {"documentation-ready": False}, "conditional"),
("missing-technical", {first: None}, "unknown"),
("missing-business", {"engagement": None}, "unknown"),
(
"static-limit",
{"engagement": contracted, "quantity": quantity("10001", "1")},
"unsupported",
),
("qualification-missing", {}, "unknown"),
)
return tuple(
AcceptanceCase(
id=name,
at=AT,
expected=expected,
request=EvaluationRequest(
target_kind="workflow",
target="service-route",
allow_simulated=True,
minimum_evidence="qualified" if name == "qualification-missing" else "declared",
facts=tuple(
Fact(name=key, value=value)
for key, value in (base | changes).items()
if value is not None
),
),
)
for name, changes, expected in scenarios
)
def project(profile):
return SourceProject(
shop_id="python-" + profile,
entrypoint="shop:define_" + profile,
files=(SourceFile(path="shop.py", content=Path(__file__).read_text()),),
tests=cases(profile),
)
if __name__ == "__main__":
# This command runs only this trusted example locally. Public services never
# invoke these functions; isolated builds execute their exported source.
import sys
from chipload.check_worker import run_checks
from chipload.python_checks import bind_case_results
sys.modules["shop"] = sys.modules[__name__]
for profile in PROFILES:
source = project(profile)
shop = define_shop(profile)
artifact = compile_shop(
shop,
BuildIdentity(
source_sha256=source.revision,
sdk_sha256="a" * 64,
dependencies_sha256="b" * 64,
runtime="trusted-example",
),
)
checks = bind_case_results(artifact, source.tests, run_checks(shop, source))
tests = run_cases(artifact, source.tests, python_evaluations=checks)
assert all(case.passed for case in tests), tests
print(f"{profile}: {len(tests)} Python technical/business cases passed")