-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecution.py
More file actions
495 lines (462 loc) · 18.3 KB
/
Copy pathexecution.py
File metadata and controls
495 lines (462 loc) · 18.3 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
"""Execute captured, preflighted source once; observe inputs, assignments and returns."""
from __future__ import annotations
import argparse
import ast
import builtins
import contextvars
import copy
import json
import platform
import sys
import types
from pathlib import Path
from typing import Any
from .exporter import authored_output_to_stderr
from .glyphs import qualify_glyphs
from .planner import Planner
from .source import Invocation, Json, SourceError, number, span, syntax_location
ACTIVE: contextvars.ContextVar[Any] = contextvars.ContextVar(
"cso_execution", default=None
)
def _declared_number(
raw: Any,
numeric_type: str,
*,
location: Json,
code: str,
subject: str,
) -> int | float:
value = number(raw, location, "execution")
if numeric_type == "int" and type(value) is not int:
raise SourceError(
code,
f"{subject} is declared int and requires a Python int; received float",
location=location,
stage="execution",
valueDisplay={"kind": "python-float", "text": repr(value)},
)
return value
class Execution:
def __init__(self, source: Path, function: str, inputs: Json):
self.planner = Planner(source)
self.root = self.planner.plan(
self.planner.capture.entry, function, supplied=inputs
)
qualify_glyphs(self.planner.invocations)
self.code = {}
for module in self.planner.capture.modules.values():
try:
self.code[module.path] = compile(
self.instrument(module),
str(module.path),
"exec",
dont_inherit=True,
)
except SyntaxError as error:
raise SourceError(
"INVALID_SOURCE",
f"{module.id}: {error}",
location=syntax_location(module.id, module.data, error),
) from error
self.observations: list[Json] = []
self.modules: dict[Path, Json] = {}
def instrument(self, module):
selected = {
inv.function.name
for inv in self.planner.invocations
if inv.module.path == module.path
}
tree = copy.deepcopy(module.tree)
class Observer(ast.NodeTransformer):
def visit_AnnAssign(self, node):
hook = ast.Expr(
value=ast.Call(
func=ast.Name(id="__cso_observe__", ctx=ast.Load()),
args=[
ast.Constant(value=node.target.id),
ast.Name(id=node.target.id, ctx=ast.Load()),
],
keywords=[],
)
)
return [node, ast.copy_location(hook, node)]
for node in tree.body:
if isinstance(node, ast.FunctionDef) and node.name in selected:
Observer().generic_visit(node)
return ast.fix_missing_locations(tree)
def observe(self, name: str, raw: Any) -> None:
active = ACTIVE.get()
if active is None or active[0] is not self:
raise SourceError(
"INVALID_OBSERVATION",
"Observation outside its captured invocation",
stage="execution",
)
invocation = active[1]
symbol = invocation.symbols[name]
definition = symbol.definition
if any(o["symbolId"] == definition["symbolId"] for o in self.observations):
raise SourceError(
"DUPLICATE_OBSERVATION",
"An annotated assignment executed more than once",
stage="execution",
)
value = _declared_number(
raw,
symbol.numeric_type,
location=definition["definitionLocation"],
code="SYMBOL_TYPE_MISMATCH",
subject=f"Symbol {name!r}",
)
self.observations.append(
{
**{
key: definition[key]
for key in ("symbolId", "kind", "definitionLocation")
},
"invocationId": invocation.id,
"value": value,
}
)
symbol.cso["valueTree"]["result"] = {"kind": "number", "value": value}
def load_module(self, module):
if module.path in self.modules:
return self.modules[module.path]
namespace = {
"__name__": "_cso_captured_module",
"__file__": str(module.path),
"__cso_observe__": self.observe,
}
self.modules[module.path] = namespace
def imported(name, globals=None, locals=None, fromlist=(), level=0):
if name in module.local_imports:
dependency = self.planner.capture.modules[module.local_imports[name]]
proxy = types.ModuleType(name)
proxy.__dict__.update(self.load_module(dependency))
return proxy
return builtins.__import__(name, globals, locals, fromlist, level)
namespace["__builtins__"] = {**vars(builtins), "__import__": imported}
exec(self.code[module.path], namespace) # noqa: S102 - preflighted, captured trusted local source
return namespace
def run(self, invocation: Invocation, inputs: Json) -> Json:
try:
return self._run(invocation, inputs)
except SourceError as error:
chain = []
current = invocation
while current.parent is not None:
chain.insert(0, current.record["callSite"])
current = current.parent
if len(error.diagnostic.get("callChain", [])) < len(chain):
error.diagnostic["callChain"] = chain
if chain:
related = error.diagnostic.setdefault("relatedLocations", [])
if chain[-1] not in related:
related.append(chain[-1])
error.diagnostic.setdefault("invocationId", invocation.id)
error.diagnostic.setdefault(
"location", span(invocation.module.id, invocation.function)
)
raise
def _run(self, invocation: Invocation, inputs: Json) -> Json:
definition = self.planner.definitions.get(
invocation.module.path, invocation.function.name
)
resolved = dict(inputs)
for binding in invocation.record["inputBindings"]:
if (
binding["parameterName"] not in resolved
and binding["kind"] == "parsedDefault"
):
resolved[binding["parameterName"]] = binding["value"]
invocation.record["resolvedInputs"] = {
name: _declared_number(
resolved[name],
definition.parameters[name].declared.numeric_type,
location=span(invocation.module.id, param),
code="INPUT_TYPE_MISMATCH",
subject=f"Parameter {name!r}",
)
for name, param in invocation.parameters.items()
}
token = ACTIVE.set((self, invocation, iter(invocation.calls.values())))
try:
module = invocation.module
namespace = self.load_module(module)
for name in invocation.parameters:
if name not in invocation.signature_inputs:
continue
self.observe(name, resolved[name])
result = namespace[invocation.function.name](**resolved)
if type(result) is not dict or list(result) != list(invocation.outputs):
raise SourceError(
"INVALID_RETURN_KEYS",
"Returned keys differ from public output declarations",
stage="execution",
location=span(module.id, invocation.function),
)
observed = {o["symbolId"] for o in self.observations}
if any(
symbol.cso["id"] not in observed
for symbol in invocation.symbols.values()
):
raise SourceError(
"MISSING_OBSERVATION",
"An annotated assignment was not observed",
stage="execution",
)
for output in invocation.output_records:
output["value"] = _declared_number(
result[output["name"]],
definition.outputs[output["name"]].annotation.numeric_type,
location=output["location"],
code="OUTPUT_TYPE_MISMATCH",
subject=f"Output {output['name']!r}",
)
return result
except SourceError:
raise
except Exception as error:
raise SourceError(
"EXECUTION_FAILED",
f"{type(error).__name__}: {error}",
stage="execution",
location=span(invocation.module.id, invocation.function),
invocationId=invocation.id,
) from error
finally:
ACTIVE.reset(token)
def response(self) -> Json:
self.run(self.root, self.root.record["resolvedInputs"])
root = self.root
capture = self.planner.capture
manifest = capture.manifest()
source_hash = next(
m["sha256"] for m in manifest if m["moduleId"] == root.module.id
)
return {
"protocolVersion": "2",
"ok": True,
"diagnostics": [],
"execution": {
"authoring": {
"version": "2",
"parameters": [
p for inv in self.planner.invocations for p in inv.parameters_v2
],
"uses": [
use for inv in self.planner.invocations for use in inv.uses
],
"outputDeclarations": [
{k: v for k, v in output.items() if k != "value"}
for inv in self.planner.invocations
for output in inv.output_records
],
"outputs": [
output
for inv in self.planner.invocations
for output in inv.output_records
],
},
"cso": {
"schemaVersion": "1.0.0",
"title": root.calculation["title"],
"source": {
"id": root.calculation["id"],
"metadata": root.calculation.get("metadata", {}),
},
"rootSectionIds": [root.section["id"]],
"sections": self.planner.sections,
},
"entry": {
"moduleId": root.module.id,
"sourceHash": source_hash,
"function": root.function.name,
"invocationId": "root",
"resolvedInputs": root.record["resolvedInputs"],
},
"sourceManifest": manifest,
"sourceClosureHash": capture.closure_hash(),
"invocations": [inv.record for inv in self.planner.invocations],
"observations": self.observations,
"assets": list(self.planner.assets.values()),
"versions": versions(),
},
}
def versions() -> Json:
from importlib.metadata import version
return {
"pythonPackage": version("cso-python"),
"pythonInterpreter": sys.executable,
"pythonVersion": platform.python_version(),
}
def execute(source: Path, function: str, inputs: Json) -> Json:
execution = Execution.__new__(Execution)
try:
execution.__init__(source, function, inputs)
return execution.response()
except (SourceError, OSError, ValueError, UnicodeError, RuntimeError) as error:
diagnostic = (
error.diagnostic
if isinstance(error, SourceError)
else {
"code": "SOURCE_READ_FAILED",
"message": str(error),
"stage": "source",
}
)
provenance: Json = {"versions": versions()}
if function.isidentifier():
provenance["function"] = function
if hasattr(execution, "planner") and execution.planner.capture.modules:
provenance.update(
sourceManifest=execution.planner.capture.manifest(),
)
entry = execution.planner.capture.modules.get(
execution.planner.capture.entry
)
if entry:
record = next(
m for m in provenance["sourceManifest"] if m["moduleId"] == entry.id
)
provenance.update(
entryModuleId=entry.id, entrySourceHash=record["sha256"]
)
if hasattr(execution, "root"):
provenance["sourceClosureHash"] = (
execution.planner.capture.closure_hash()
)
provenance["resolvedInputs"] = execution.root.record["resolvedInputs"]
return {
"protocolVersion": "2",
"ok": False,
"diagnostics": [diagnostic],
"provenance": provenance,
}
def calculation_call(path: str, *, function: str, inputs: Json) -> Json:
return invoke(path, function=function, inputs=inputs, frame=sys._getframe(1))
def invoke(path: str, *, function: str, inputs: Json, frame: Any) -> Json:
active = ACTIVE.get()
if active is None:
# Direct Python authoring: preflight the containing calculation closure,
# then execute this child only. Never replay the caller function.
source = Path(frame.f_code.co_filename)
planner = Planner(source)
module = planner.capture.load(planner.capture.entry)
fn = module.functions.get(frame.f_code.co_name)
if fn is None:
raise SourceError(
"INVALID_CALL", "calculation_call must be inside a calculation"
)
names = [a.arg for a in [*fn.args.args, *fn.args.kwonlyargs]]
engine = Execution(
source, fn.name, {name: frame.f_locals[name] for name in names}
)
children = [
c
for c in engine.root.calls.values()
if c.call_node.lineno <= frame.f_lineno <= c.call_node.end_lineno
]
if len(children) != 1:
raise SourceError(
"INVALID_CALL", "Cannot identify a unique static call site"
)
return engine.run(children[0], inputs)
engine, parent, children = active
child = next(children, None)
if (
child is None
or child.function.name != function
or (parent.module.path.parent / path).resolve() != child.module.path
):
raise SourceError(
"INVALID_CALL",
"Runtime call differs from preflighted call",
stage="execution",
)
return engine.run(child, inputs)
class ExecuteParser(argparse.ArgumentParser):
def error(self, message: str) -> None:
print(
json.dumps(
{
"protocolVersion": "2",
"ok": False,
"diagnostics": [
{"code": "INVALID_USAGE", "message": message, "stage": "usage"}
],
}
)
)
self.exit(2)
def strict_object(pairs: list[tuple[str, Any]]) -> Json:
result = {}
for key, value in pairs:
if key in result:
raise SourceError(
"DUPLICATE_INPUT", f"Duplicate JSON key {key!r}", stage="usage"
)
result[key] = value
return result
def execute_from_argv(argv: list[str]) -> int:
parser = ExecuteParser(
prog="python -m cso_python execute",
description="Capture constrained source and return execution evidence; core performs numerical verification.",
)
parser.add_argument("source", type=Path)
parser.add_argument("--function", required=True, action="append")
parser.add_argument("--inputs-json", action="append", default=[])
parser.add_argument("--input", action="append", default=[])
args = parser.parse_args(argv)
try:
if len(args.function) != 1 or len(args.inputs_json) > 1:
raise SourceError(
"DUPLICATE_OPTION",
"Duplicate --function or --inputs-json",
stage="usage",
)
if not args.function[0].isidentifier():
raise SourceError(
"INVALID_USAGE", "Function must be an identifier", stage="usage"
)
inputs = (
json.loads(args.inputs_json[0], object_pairs_hook=strict_object)
if args.inputs_json
else {}
)
if not isinstance(inputs, dict):
raise SourceError(
"INVALID_INPUTS", "--inputs-json must be an object", stage="usage"
)
for assignment in args.input:
name, sep, value = assignment.partition("=")
if not sep or not name.isidentifier() or name in inputs:
raise SourceError(
"INVALID_INPUTS",
f"Duplicate or invalid input {name!r}",
stage="usage",
)
inputs[name] = json.loads(value)
for name, value in inputs.items():
if not name.isidentifier():
raise SourceError(
"INVALID_INPUTS", f"Invalid parameter {name!r}", stage="usage"
)
number(value, stage="usage")
with authored_output_to_stderr():
response = execute(args.source, args.function[0], inputs)
except (SourceError, ValueError) as error:
diagnostic = (
error.diagnostic
if isinstance(error, SourceError)
else {"code": "INVALID_INPUTS", "message": str(error), "stage": "usage"}
)
response = {"protocolVersion": "2", "ok": False, "diagnostics": [diagnostic]}
print(json.dumps(response, ensure_ascii=False, allow_nan=False))
return (
0
if response["ok"]
else 2
if any(d["stage"] == "usage" for d in response["diagnostics"])
else 1
)