@withgateway/sdk 0.6.0 → 0.9.3
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.
- package/dist/agents/index.d.mts +581 -0
- package/dist/agents/index.d.ts +581 -0
- package/dist/agents/index.js +1473 -0
- package/dist/agents/index.js.map +1 -0
- package/dist/agents/index.mjs +1424 -0
- package/dist/agents/index.mjs.map +1 -0
- package/dist/apiError-DHpfbmdx.d.mts +17 -0
- package/dist/apiError-DHpfbmdx.d.ts +17 -0
- package/dist/cli.js +11611 -6845
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +10090 -5325
- package/dist/cli.mjs.map +1 -1
- package/dist/files/index.d.mts +3 -1
- package/dist/files/index.d.ts +3 -1
- package/dist/files/index.js +371 -102
- package/dist/files/index.js.map +1 -1
- package/dist/files/index.mjs +371 -102
- package/dist/files/index.mjs.map +1 -1
- package/dist/hooks/index.js +346 -136
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +346 -136
- package/dist/hooks/index.mjs.map +1 -1
- package/dist/{index-LA-PMRJT.d.ts → index-DIN14Lhg.d.ts} +1 -1
- package/dist/{index-Di9nNqOY.d.mts → index-DuB-eQ0Z.d.mts} +1 -1
- package/dist/index.d.mts +7 -4
- package/dist/index.d.ts +7 -4
- package/dist/index.js +3177 -748
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2987 -590
- package/dist/index.mjs.map +1 -1
- package/dist/media-CQUC7TkS.d.mts +39 -0
- package/dist/media-DQnBloaX.d.ts +39 -0
- package/dist/traces/index.d.mts +3 -1
- package/dist/traces/index.d.ts +3 -1
- package/dist/traces/index.js +371 -102
- package/dist/traces/index.js.map +1 -1
- package/dist/traces/index.mjs +371 -102
- package/dist/traces/index.mjs.map +1 -1
- package/dist/tracing/index.d.mts +3 -2
- package/dist/tracing/index.d.ts +3 -2
- package/dist/tracing/index.js +257 -16
- package/dist/tracing/index.js.map +1 -1
- package/dist/tracing/index.mjs +257 -16
- package/dist/tracing/index.mjs.map +1 -1
- package/dist/transport-CpRe7zgd.d.mts +123 -0
- package/dist/transport-CpRe7zgd.d.ts +123 -0
- package/dist/worlds/index.d.mts +1986 -1746
- package/dist/worlds/index.d.ts +1986 -1746
- package/dist/worlds/index.js +1470 -290
- package/dist/worlds/index.js.map +1 -1
- package/dist/worlds/index.mjs +1465 -286
- package/dist/worlds/index.mjs.map +1 -1
- package/package.json +6 -1
- package/runtime/gatewaysdk/environment/bundle_manifest.py +531 -0
- package/runtime/gatewaysdk/environment/factory_documents.py +670 -0
- package/runtime/gatewaysdk/environment/factory_rules.py +973 -0
- package/runtime/gatewaysdk/environment/factory_shapes.py +253 -0
- package/runtime/gatewaysdk/environment/factory_spine.py +1155 -0
- package/runtime/gatewaysdk/environment/factory_spine_shapes.py +480 -0
- package/runtime/gatewaysdk/environment/schema/__main__.py +2 -2
- package/runtime/gatewaysdk/environment/schema/_slack_fidelity.py +8 -23
- package/runtime/gatewaysdk/environment/schema/admission.py +61 -59
- package/runtime/gatewaysdk/environment/schema/api.py +3 -4
- package/runtime/gatewaysdk/environment/schema/assertions.py +7 -1
- package/runtime/gatewaysdk/environment/schema/bundle_runtime.py +1 -0
- package/runtime/gatewaysdk/environment/schema/call_log.py +20 -0
- package/runtime/gatewaysdk/environment/schema/cast.py +133 -67
- package/runtime/gatewaysdk/environment/schema/compiler.py +16 -17
- package/runtime/gatewaysdk/environment/schema/database/base.py +2 -2
- package/runtime/gatewaysdk/environment/schema/handler_errors.py +58 -4
- package/runtime/gatewaysdk/environment/schema/host.py +94 -12
- package/runtime/gatewaysdk/environment/schema/host_browser.py +48 -0
- package/runtime/gatewaysdk/environment/schema/host_control.py +35 -2
- package/runtime/gatewaysdk/environment/schema/host_handoff.py +4 -1
- package/runtime/gatewaysdk/environment/schema/host_loops.py +27 -4
- package/runtime/gatewaysdk/environment/schema/host_metrics.py +69 -0
- package/runtime/gatewaysdk/environment/schema/host_requests.py +35 -0
- package/runtime/gatewaysdk/environment/schema/host_session.py +20 -2
- package/runtime/gatewaysdk/environment/schema/host_surfaces.py +6 -0
- package/runtime/gatewaysdk/environment/schema/host_versions.py +100 -19
- package/runtime/gatewaysdk/environment/schema/host_workers.py +172 -50
- package/runtime/gatewaysdk/environment/schema/ingest.py +12 -3
- package/runtime/gatewaysdk/environment/schema/journal.py +7 -2
- package/runtime/gatewaysdk/environment/schema/local.py +32 -7
- package/runtime/gatewaysdk/environment/schema/mapping.py +1 -1
- package/runtime/gatewaysdk/environment/schema/measured_tests.py +187 -0
- package/runtime/gatewaysdk/environment/schema/page_checks.py +123 -0
- package/runtime/gatewaysdk/environment/schema/platform.py +80 -270
- package/runtime/gatewaysdk/environment/schema/platform_query.py +4 -3
- package/runtime/gatewaysdk/environment/schema/platform_templates.py +420 -0
- package/runtime/gatewaysdk/environment/schema/process_memory.py +76 -0
- package/runtime/gatewaysdk/environment/schema/redaction.py +13 -0
- package/runtime/gatewaysdk/environment/schema/request_ledger.py +4 -1
- package/runtime/gatewaysdk/environment/schema/run_parameters.py +25 -0
- package/runtime/gatewaysdk/environment/schema/runtime.py +57 -5
- package/runtime/gatewaysdk/environment/schema/scaffold.py +386 -219
- package/runtime/gatewaysdk/environment/schema/session_files.py +13 -2
- package/runtime/gatewaysdk/environment/schema/shape_catalog.py +493 -0
- package/runtime/gatewaysdk/environment/schema/shape_declarations.py +219 -0
- package/runtime/gatewaysdk/environment/schema/skill.py +1 -0
- package/runtime/gatewaysdk/environment/schema/skills/benchmarks-getting-started/SKILL.md +1 -1
- package/runtime/gatewaysdk/environment/schema/skills/connector-schema-authoring/SKILL.md +2 -2
- package/runtime/gatewaysdk/environment/schema/skills/factory-orchestrator/SKILL.md +147 -0
- package/runtime/gatewaysdk/environment/schema/skills/world-data-ingestion/ROWS.md +5 -1
- package/runtime/gatewaysdk/environment/schema/skills/world-data-ingestion/SKILL.md +8 -6
- package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/SKILL.md +9 -9
- package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/cli-mcp-map.md +33 -2
- package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/gotchas.md +6 -6
- package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/journal-checks.md +17 -2
- package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/new-world-sandbox.md +1 -1
- package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/shape-catalog.md +59 -0
- package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/ui-map.md +3 -2
- package/runtime/gatewaysdk/environment/schema/slack.py +55 -10
- package/runtime/gatewaysdk/environment/schema/snapshot.py +28 -2
- package/runtime/gatewaysdk/environment/schema/tasks.py +25 -2
- package/runtime/gatewaysdk/environment/schema/template_record.py +131 -0
- package/runtime/gatewaysdk/environment/schema/tools_world.py +7 -5
- package/runtime/gatewaysdk/environment/schema/ui.py +54 -33
- package/runtime/gatewaysdk/environment/schema/ui_interactions.py +419 -0
- package/runtime/gatewaysdk/environment/schema/ui_recorder.js +895 -0
- package/runtime/gatewaysdk/environment/schema/ui_replay.py +366 -0
- package/runtime/gatewaysdk/environment/schema/ui_replay_record.LICENSE +25 -0
- package/runtime/gatewaysdk/environment/schema/ui_replay_record.js +109 -0
- package/runtime/gatewaysdk/environment/schema/validation.py +24 -1
- package/runtime/gatewaysdk/environment/schema/worker.py +12 -5
- package/runtime/gatewaysdk/environment/schema/worker_key.py +38 -0
- package/runtime/gatewaysdk/environment/schema/worker_peaks.py +38 -0
- package/runtime/gatewaysdk/environment/schema/worker_watch.py +50 -0
- package/runtime/gatewaysdk/environment/schema/workers.py +245 -69
- package/runtime/gatewaysdk/environment/schema/world_host.py +96 -73
- package/runtime/gatewaysdk/environment/schema/world_tests.py +3 -1
- package/runtime/gatewaysdk/environment/secret_values.py +1 -1
- package/runtime/gatewaysdk/environment/world.py +34 -43
- package/runtime/gatewaysdk/environment/world_session_contract.py +7 -34
- package/skills/benchmarks-getting-started/SKILL.md +1 -1
- package/skills/connector-schema-authoring/SKILL.md +2 -2
- package/skills/connector-schema-authoring/connector-worlds.md +3 -2
- package/skills/connector-schema-authoring/world-schema.md +36 -23
- package/skills/factory-orchestrator/SKILL.md +147 -0
- package/skills/world-data-ingestion/ROWS.md +5 -1
- package/skills/world-data-ingestion/SKILL.md +8 -6
- package/skills/worlds-getting-started/SKILL.md +9 -9
- package/skills/worlds-getting-started/references/cli-mcp-map.md +33 -2
- package/skills/worlds-getting-started/references/gotchas.md +6 -6
- package/skills/worlds-getting-started/references/journal-checks.md +17 -2
- package/skills/worlds-getting-started/references/new-world-sandbox.md +1 -1
- package/skills/worlds-getting-started/references/shape-catalog.md +59 -0
- package/skills/worlds-getting-started/references/ui-map.md +3 -2
- package/dist/media-BIApiPzS.d.mts +0 -102
- package/dist/media-BIApiPzS.d.ts +0 -102
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
"""``MeasuredTestRun``: a world's tests run in a child interpreter the bridge measures from outside.
|
|
2
|
+
|
|
3
|
+
World code (its handlers, its tools, the engine under them) runs in that child, and the run's peak is
|
|
4
|
+
what the kernel reports for the child when the bridge reaps it (``wait4``'s ``ru_maxrss``: the larger
|
|
5
|
+
of the child and the pytest it ran). So code in the world can neither set nor lower the number, and
|
|
6
|
+
none of the bridge's own request decoding is in it; a ``memory`` the child's report carries is dropped.
|
|
7
|
+
A run that served nothing (no case ran, every Python test skipped) has no peak. The child leads its own
|
|
8
|
+
process group, takes no core dump (and on Linux is not dumpable), and has no stdio of the bridge's, so
|
|
9
|
+
the bridge's callers never wait on world code. When the bridge goes, the kernel kills the child (Linux)
|
|
10
|
+
and a sweeper the child forked before any world code ran, which shares no lock with it, kills the group
|
|
11
|
+
(its pytest and whatever the world started in it) once a lifeline descriptor the bridge holds closes. The
|
|
12
|
+
bridge kills the group itself once the child has exited (before reaping it where ``waitid`` exists).
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
import json
|
|
18
|
+
import os
|
|
19
|
+
import resource
|
|
20
|
+
import signal
|
|
21
|
+
import stat
|
|
22
|
+
import subprocess
|
|
23
|
+
import sys
|
|
24
|
+
import tempfile
|
|
25
|
+
from contextlib import suppress
|
|
26
|
+
from dataclasses import dataclass
|
|
27
|
+
from pathlib import Path
|
|
28
|
+
|
|
29
|
+
import gatewaysdk
|
|
30
|
+
|
|
31
|
+
from .process_memory import maxrss_mb
|
|
32
|
+
from .validation import ContractError, fail
|
|
33
|
+
from .workers import ProcessLimit, die_with_parent, seal_process
|
|
34
|
+
|
|
35
|
+
_BOOT = (
|
|
36
|
+
"import runpy, sys, types; "
|
|
37
|
+
"package = types.ModuleType('gatewaysdk'); package.__path__ = [sys.argv[1]]; "
|
|
38
|
+
"sys.modules['gatewaysdk'] = package; "
|
|
39
|
+
"runpy.run_module('gatewaysdk.environment.schema.measured_tests', run_name='__main__')"
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@dataclass(frozen=True)
|
|
44
|
+
class MeasuredTestRun:
|
|
45
|
+
report: dict
|
|
46
|
+
peak_mb: int | None
|
|
47
|
+
|
|
48
|
+
@classmethod
|
|
49
|
+
def of(cls, root: Path, scenario: str | None) -> MeasuredTestRun:
|
|
50
|
+
with tempfile.TemporaryDirectory(prefix="gateway-measured-") as directory:
|
|
51
|
+
answer = Path(directory) / "answer.json"
|
|
52
|
+
lifeline, held = os.pipe()
|
|
53
|
+
argv = [sys.executable, "-I", "-B", "-c", _BOOT, list(gatewaysdk.__path__)[0], str(os.getpid())]
|
|
54
|
+
try:
|
|
55
|
+
child = subprocess.Popen(
|
|
56
|
+
[*argv, str(lifeline), str(root), scenario or "", str(answer)],
|
|
57
|
+
stdin=subprocess.DEVNULL,
|
|
58
|
+
stdout=subprocess.DEVNULL,
|
|
59
|
+
stderr=subprocess.DEVNULL,
|
|
60
|
+
pass_fds=(lifeline,),
|
|
61
|
+
start_new_session=True,
|
|
62
|
+
)
|
|
63
|
+
finally:
|
|
64
|
+
os.close(lifeline)
|
|
65
|
+
try:
|
|
66
|
+
code, peak_mb = _reaped(child)
|
|
67
|
+
except BaseException:
|
|
68
|
+
_stop_group(child.pid)
|
|
69
|
+
raise
|
|
70
|
+
finally:
|
|
71
|
+
os.close(held)
|
|
72
|
+
return cls._read(answer, code, peak_mb)
|
|
73
|
+
|
|
74
|
+
@classmethod
|
|
75
|
+
def _read(cls, answer: Path, code: int, peak_mb: int | None) -> MeasuredTestRun:
|
|
76
|
+
result = _answer_of(answer)
|
|
77
|
+
if result is None:
|
|
78
|
+
reached = "" if peak_mb is None else f", having reached {peak_mb} MiB"
|
|
79
|
+
fail("tests", f"the world's test run stopped before it reported (exit {code}{reached})")
|
|
80
|
+
refusal = result.get("error")
|
|
81
|
+
if isinstance(refusal, dict):
|
|
82
|
+
raise _reported(refusal)
|
|
83
|
+
if not isinstance(result.get("report"), dict):
|
|
84
|
+
fail("tests", "the world's test run reported nothing readable")
|
|
85
|
+
report = {name: value for name, value in result["report"].items() if name != "memory"}
|
|
86
|
+
return cls(report, peak_mb if result.get("served") is True else None)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _answer_of(answer: Path) -> dict | None:
|
|
90
|
+
"""The child's answer: a regular file of at most the bridge's own answer limit, never followed through a
|
|
91
|
+
link or waited on as a pipe, since the child runs world code; None when there is none to read."""
|
|
92
|
+
from .platform import MAX_BYTES
|
|
93
|
+
|
|
94
|
+
flags = os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0) | getattr(os, "O_NONBLOCK", 0)
|
|
95
|
+
try:
|
|
96
|
+
fd = os.open(answer, flags)
|
|
97
|
+
except OSError:
|
|
98
|
+
return None
|
|
99
|
+
with os.fdopen(fd, "rb") as file:
|
|
100
|
+
if not stat.S_ISREG(os.fstat(file.fileno()).st_mode):
|
|
101
|
+
return None
|
|
102
|
+
raw = file.read(2 * MAX_BYTES + 1)
|
|
103
|
+
if len(raw) > 2 * MAX_BYTES:
|
|
104
|
+
fail("tests", "the world's test run reported more than the bridge answers")
|
|
105
|
+
try:
|
|
106
|
+
result = json.loads(raw)
|
|
107
|
+
except ValueError:
|
|
108
|
+
return None
|
|
109
|
+
return result if isinstance(result, dict) else None
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def _reported(refusal: dict) -> ContractError:
|
|
113
|
+
"""The refusal the child reported, rebuilt from its strings alone: the child runs world code."""
|
|
114
|
+
text = {name: str(refusal.get(name) or "") for name in ("code", "message", "schema_path", "path")}
|
|
115
|
+
return ContractError(text["code"], text["message"], schema_path=text["schema_path"], path=text["path"])
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def _reaped(child: subprocess.Popen) -> tuple[int, int | None]:
|
|
119
|
+
"""The child's exit code and, where ``wait4`` exists, its peak; whatever of its group still runs once it
|
|
120
|
+
has exited is killed."""
|
|
121
|
+
if not hasattr(os, "wait4"):
|
|
122
|
+
return child.wait(), None
|
|
123
|
+
stopped = _exited_unreaped(child.pid)
|
|
124
|
+
_, status, usage = os.wait4(child.pid, 0)
|
|
125
|
+
if not stopped:
|
|
126
|
+
_stop_group(child.pid)
|
|
127
|
+
child.returncode = os.waitstatus_to_exitcode(status)
|
|
128
|
+
return child.returncode, maxrss_mb(usage.ru_maxrss)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def _exited_unreaped(leader: int) -> bool:
|
|
132
|
+
"""Waits for ``leader`` to exit without reaping it, then kills what of its group still runs, so the group
|
|
133
|
+
id cannot be another process's yet; False where there is no ``waitid`` (macOS), whose caller kills after."""
|
|
134
|
+
if not hasattr(os, "waitid"):
|
|
135
|
+
return False
|
|
136
|
+
os.waitid(os.P_PID, leader, os.WEXITED | os.WNOWAIT)
|
|
137
|
+
_stop_group(leader)
|
|
138
|
+
return True
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def _stop_group(leader: int) -> None:
|
|
142
|
+
with suppress(ProcessLookupError, PermissionError, AttributeError):
|
|
143
|
+
os.killpg(leader, signal.SIGKILL)
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def _sweeper(lifeline: int) -> int:
|
|
147
|
+
"""Forks, while this process is still one thread with no world code loaded, a process of its group that
|
|
148
|
+
waits for the lifeline to close and then kills the group, itself included. A process of its own: the
|
|
149
|
+
kernel's kill of the child on the bridge's exit (which a fork does not inherit) would take a thread with
|
|
150
|
+
it, and no lock world code holds can delay it."""
|
|
151
|
+
sweeper = os.fork()
|
|
152
|
+
if sweeper == 0:
|
|
153
|
+
while os.read(lifeline, 1 << 16):
|
|
154
|
+
pass
|
|
155
|
+
os.killpg(0, signal.SIGKILL)
|
|
156
|
+
os._exit(1)
|
|
157
|
+
os.close(lifeline)
|
|
158
|
+
return sweeper
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def _done_with(sweeper: int) -> None:
|
|
162
|
+
"""The child reaps its own sweeper once it has answered, so no zombie waits on whatever PID 1 is."""
|
|
163
|
+
with suppress(ProcessLookupError, ChildProcessError):
|
|
164
|
+
os.kill(sweeper, signal.SIGKILL)
|
|
165
|
+
os.waitpid(sweeper, 0)
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def _serve_and_report(bridge: int, lifeline: int, root: Path, scenario: str | None, answer: Path) -> None:
|
|
169
|
+
die_with_parent(bridge)
|
|
170
|
+
seal_process()
|
|
171
|
+
ProcessLimit(resource.RLIMIT_CORE, 0, "core_bytes").apply()
|
|
172
|
+
sweeper = _sweeper(lifeline)
|
|
173
|
+
from .world_tests import run
|
|
174
|
+
|
|
175
|
+
try:
|
|
176
|
+
report = run(root, scenario=scenario)
|
|
177
|
+
result = {"report": report.to_json(), "served": report.served}
|
|
178
|
+
except ContractError as error:
|
|
179
|
+
result = {"error": error.detail}
|
|
180
|
+
except Exception as error: # noqa: BLE001 - the child has no stderr; its answer is the only report it can give
|
|
181
|
+
result = {"error": {"code": "tests", "message": f"the world's test run failed: {type(error).__name__}"}}
|
|
182
|
+
answer.write_text(json.dumps(result), encoding="utf-8")
|
|
183
|
+
_done_with(sweeper)
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
if __name__ == "__main__":
|
|
187
|
+
_serve_and_report(int(sys.argv[2]), int(sys.argv[3]), Path(sys.argv[4]), sys.argv[5] or None, Path(sys.argv[6]))
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"""The grader's view of a UI world's page (``via: page``): which checks read it, and whether what
|
|
2
|
+
the page reported can decide them. A check that does not name the page reads what it read before
|
|
3
|
+
pages reported anything; the host's notes (``ui.recording`` at open, ``ui.served`` per page) are
|
|
4
|
+
evidence for :class:`PageReport`, never a row a check reads."""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from collections import defaultdict
|
|
9
|
+
from collections.abc import Iterable, Mapping
|
|
10
|
+
from dataclasses import dataclass, field
|
|
11
|
+
from typing import Any
|
|
12
|
+
|
|
13
|
+
from .tasks import (
|
|
14
|
+
DOOR_FIELD,
|
|
15
|
+
JOURNAL,
|
|
16
|
+
ORDER_KEYS,
|
|
17
|
+
PAGE_DOOR,
|
|
18
|
+
PAGE_GAP,
|
|
19
|
+
PAGE_NOTES,
|
|
20
|
+
PAGE_RECORDING,
|
|
21
|
+
PAGE_SERVED,
|
|
22
|
+
PAGE_UNHEARD,
|
|
23
|
+
PAGE_UNJOURNALED,
|
|
24
|
+
PAGE_UNRECORDED,
|
|
25
|
+
PAGE_UNRECORDED_LIFE,
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
PAGE_OPERATIONS = "ui."
|
|
29
|
+
PAGE_PATHS = ("args.target.", "request.body.target.")
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _listed(value: Any) -> list[Any]:
|
|
33
|
+
return list(value) if isinstance(value, (list, tuple)) else [value]
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _names_door(value: Any) -> bool:
|
|
37
|
+
return any(item == PAGE_DOOR for item in _listed(value))
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _names_operation(value: Any) -> bool:
|
|
41
|
+
return any(isinstance(item, str) and item.startswith(PAGE_OPERATIONS) for item in _listed(value))
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@dataclass(frozen=True)
|
|
45
|
+
class PageCheck:
|
|
46
|
+
"""One check as the page sees it: it names the page when a clause or its ``field`` pins the
|
|
47
|
+
page door or a ``ui.*`` operation, or a clause reads what a page interaction acted on."""
|
|
48
|
+
|
|
49
|
+
check: Mapping[str, Any]
|
|
50
|
+
|
|
51
|
+
@property
|
|
52
|
+
def names_the_page(self) -> bool:
|
|
53
|
+
clauses = [self.check.get("where") or {}, *(self.check.get(key) or {} for key in ORDER_KEYS)]
|
|
54
|
+
return any(self._clause(clause) for clause in clauses) or self._field()
|
|
55
|
+
|
|
56
|
+
def _clause(self, clause: Mapping[str, Any]) -> bool:
|
|
57
|
+
pinned = _names_door(clause.get(DOOR_FIELD)) or _names_operation(clause.get("operation"))
|
|
58
|
+
return pinned or any(path.startswith(PAGE_PATHS) for path in clause)
|
|
59
|
+
|
|
60
|
+
def _field(self) -> bool:
|
|
61
|
+
value = self.check.get("value")
|
|
62
|
+
named = {DOOR_FIELD: _names_door, "operation": _names_operation}.get(self.check.get("field") or "")
|
|
63
|
+
return named is not None and named(value)
|
|
64
|
+
|
|
65
|
+
def keeps(self, values: Mapping[str, Any]) -> bool:
|
|
66
|
+
if values.get(DOOR_FIELD) != PAGE_DOOR:
|
|
67
|
+
return True
|
|
68
|
+
return self.names_the_page and values.get("operation") not in PAGE_NOTES
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
@dataclass
|
|
72
|
+
class PageReport:
|
|
73
|
+
"""What the session's journal says about its pages: whether a host noted it recorded them,
|
|
74
|
+
whether it served any, whether any interaction came back, and each page load's numbered
|
|
75
|
+
interactions. A grade with no journal has none of this to read."""
|
|
76
|
+
|
|
77
|
+
journaled: bool = False
|
|
78
|
+
recording: bool = False
|
|
79
|
+
unrecorded: bool = False
|
|
80
|
+
served: bool = False
|
|
81
|
+
reported: bool = False
|
|
82
|
+
loads: dict[str, set[int]] = field(default_factory=lambda: defaultdict(set))
|
|
83
|
+
|
|
84
|
+
@classmethod
|
|
85
|
+
def of(cls, state: Mapping, entity: str) -> PageReport:
|
|
86
|
+
prefix = entity[: entity.rfind(".") + 1]
|
|
87
|
+
rows = state.get(f"{prefix}{JOURNAL}")
|
|
88
|
+
report = cls(journaled=isinstance(rows, list))
|
|
89
|
+
for row in rows if isinstance(rows, list) else ():
|
|
90
|
+
if isinstance(row, Mapping) and row.get(DOOR_FIELD) == PAGE_DOOR:
|
|
91
|
+
report._note(row)
|
|
92
|
+
return report
|
|
93
|
+
|
|
94
|
+
def _note(self, row: Mapping[str, Any]) -> None:
|
|
95
|
+
operation = row.get("operation")
|
|
96
|
+
self.recording = self.recording or operation == PAGE_RECORDING
|
|
97
|
+
self.unrecorded = self.unrecorded or operation == PAGE_UNRECORDED_LIFE
|
|
98
|
+
self.served = self.served or operation == PAGE_SERVED
|
|
99
|
+
if operation in PAGE_NOTES:
|
|
100
|
+
return
|
|
101
|
+
self.reported = True
|
|
102
|
+
query = row.get("query")
|
|
103
|
+
load, seq = (query.get("load"), query.get("seq")) if isinstance(query, Mapping) else (None, None)
|
|
104
|
+
if isinstance(load, str) and isinstance(seq, int) and not isinstance(seq, bool):
|
|
105
|
+
self.loads[load].add(seq)
|
|
106
|
+
|
|
107
|
+
def missing(self) -> int:
|
|
108
|
+
return sum(max(seqs) - len(seqs) for seqs in self.loads.values() if seqs)
|
|
109
|
+
|
|
110
|
+
def refusal(self) -> str | None:
|
|
111
|
+
if not self.journaled:
|
|
112
|
+
return PAGE_UNJOURNALED
|
|
113
|
+
if self.unrecorded or not self.recording:
|
|
114
|
+
return PAGE_UNRECORDED
|
|
115
|
+
if self.served and not self.reported:
|
|
116
|
+
return PAGE_UNHEARD
|
|
117
|
+
gaps = self.missing()
|
|
118
|
+
return PAGE_GAP.format(missing=gaps) if gaps else None
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def page_rows(check: Mapping[str, Any], rows: Iterable[Any]) -> list[Any]:
|
|
122
|
+
view = PageCheck(check)
|
|
123
|
+
return [row for row in rows if view.keeps(row.values)]
|