@yahyahammad/venastine 0.1.0 → 0.3.8
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/.env.example +19 -14
- package/CONFIG_ARCHITECTURE.md +2002 -0
- package/NOTICE +1 -1
- package/README.md +249 -93
- package/agents/builtin/build.md +73 -0
- package/agents/builtin/general.md +80 -0
- package/agents/builtin/plan.md +23 -5
- package/agents/builtin/test.md +65 -0
- package/agents/builtin/writer.md +67 -0
- package/agents/manager.py +261 -195
- package/agents/subagent_tool.py +280 -23
- package/agents/tui_commands.py +31 -21
- package/bin/venastine.mjs +528 -382
- package/bin/venastine.py +71 -0
- package/cli_commands.py +835 -0
- package/config.py +95 -1198
- package/config.yaml +1090 -0
- package/config_edit.py +1301 -0
- package/config_schema.py +966 -0
- package/config_update.py +749 -0
- package/core/agent_activity.py +256 -0
- package/core/ansi.py +452 -0
- package/core/approval.py +27 -5
- package/core/call_view.py +364 -0
- package/core/cancel.py +121 -0
- package/core/chatgpt_auth.py +747 -0
- package/core/claims_view.py +107 -0
- package/core/client.py +1249 -82
- package/core/commands.py +454 -0
- package/core/compaction.py +1735 -959
- package/core/config_loader.py +516 -97
- package/core/connect.py +686 -0
- package/core/events.py +68 -6
- package/core/file_mentions.py +280 -0
- package/core/interaction.py +87 -4
- package/core/line_pattern.py +128 -0
- package/core/loop.py +2566 -1353
- package/core/memory.py +482 -370
- package/core/pipeline_models.py +23 -9
- package/core/provider_errors.py +234 -0
- package/core/reasoning/confidence_scoring.py +70 -54
- package/core/reasoning/json_retry.py +19 -1
- package/core/reasoning/orchestrator.py +232 -53
- package/core/reasoning/output_writer.py +90 -31
- package/core/reasoning/payload_validation.py +133 -7
- package/core/reasoning/pipeline_storage.py +427 -361
- package/core/reasoning/review.py +659 -615
- package/core/reasoning/scholar.py +38 -14
- package/core/reasoning/source_corpus.py +110 -4
- package/core/replay.py +315 -167
- package/core/session.py +36 -12
- package/core/session_view.py +259 -0
- package/core/session_wake.py +259 -0
- package/core/shell_sessions.py +1553 -0
- package/core/workspace_trust.py +1 -1
- package/credentials.py +308 -180
- package/database.py +82 -1
- package/{PRIVACY.md → docs/PRIVACY.md} +19 -7
- package/docs/SECURITY.md +108 -0
- package/docs/THIRD_PARTY_NOTICES.md +148 -0
- package/env_secrets.py +118 -77
- package/json_store.py +236 -16
- package/logging_setup.py +121 -11
- package/main.py +1672 -198
- package/mcp_client/client.py +257 -23
- package/mcp_client/config.py +484 -379
- package/mcp_client/registration.py +163 -2
- package/memories/manager.py +15 -0
- package/memories/tui_commands.py +21 -14
- package/package.json +303 -73
- package/project_init/config_files.py +4 -0
- package/project_init/generator.py +201 -37
- package/project_init/manifest.py +418 -406
- package/project_init/tui_commands.py +7 -4
- package/prompts/revalidate.md +1 -1
- package/prompts/revise.md +17 -15
- package/prompts/system_prompts.py +64 -10
- package/providers.json.example +94 -92
- package/pyproject.toml +149 -100
- package/requirements.txt +252 -29
- package/safety/policy_enforcement.py +1082 -682
- package/security/askpass.py +419 -0
- package/security/capability.py +37 -7
- package/security/confine.py +484 -0
- package/security/hosted.py +182 -0
- package/security/permissions.py +54 -7
- package/security/posture.py +124 -12
- package/security/protected_paths.py +198 -6
- package/security/sandbox.py +3262 -139
- package/security/secrets.py +855 -0
- package/skills/tui_commands.py +3 -4
- package/storage.py +1338 -1005
- package/tools/base.py +465 -305
- package/tools/builtin/_math_common.py +306 -16
- package/tools/builtin/_net_common.py +267 -61
- package/tools/builtin/arxiv.py +681 -223
- package/tools/builtin/ask_user.py +231 -213
- package/tools/builtin/discrete_math.py +35 -4
- package/tools/builtin/fetch_url.py +239 -21
- package/tools/builtin/file_ops.py +341 -33
- package/tools/builtin/geometry.py +184 -148
- package/tools/builtin/get_time.py +2 -2
- package/tools/builtin/linear_algebra.py +66 -23
- package/tools/builtin/load_skill.py +10 -0
- package/tools/builtin/logic.py +171 -12
- package/tools/builtin/pin.py +46 -12
- package/tools/builtin/probability_stats.py +224 -228
- package/tools/builtin/project_docs.py +80 -21
- package/tools/builtin/remember.py +21 -4
- package/tools/builtin/shell.py +586 -67
- package/tools/builtin/shell_sessions.py +613 -0
- package/tools/builtin/symbolic_math.py +80 -10
- package/tools/builtin/todo.py +5 -3
- package/tools/builtin/web_search.py +329 -151
- package/tools/context.py +23 -0
- package/tools/isolation.py +387 -314
- package/tools/registry.py +374 -29
- package/tui/app.py +5281 -397
- package/tui/app.tcss +290 -9
- package/tui/commands.py +26 -61
- package/tui/diffs.py +286 -265
- package/tui/history.py +128 -0
- package/tui/hosted_driver.py +102 -0
- package/tui/markdown.py +1329 -0
- package/tui/meters.py +364 -0
- package/tui/screens.py +346 -92
- package/tui/themes.py +599 -366
- package/tui/widgets.py +2673 -116
- package/SECURITY.md +0 -88
- package/THIRD_PARTY_NOTICES.md +0 -100
|
@@ -41,7 +41,7 @@ logger = logging.getLogger(__name__)
|
|
|
41
41
|
def _tier_counts(run: PipelineRun) -> tuple[list[str], list[int]]:
|
|
42
42
|
"""Returns (tier_labels, counts) for the confidence chart. Extracted
|
|
43
43
|
as a pure function so the counting logic can be unit-tested without
|
|
44
|
-
rendering
|
|
44
|
+
rendering the chart."""
|
|
45
45
|
tiers = ["HIGH", "MEDIUM", "LOW", "UNVERIFIED"]
|
|
46
46
|
counts = [sum(1 for c in run.claims if c.confidence_tier == t) for t in tiers]
|
|
47
47
|
tiers.append("UNVERIFIED_COVERAGE")
|
|
@@ -76,7 +76,7 @@ def write_run_artifacts(run: PipelineRun) -> str:
|
|
|
76
76
|
07_review.json §20 review findings + decisions (only if reviewed)
|
|
77
77
|
granted_calls.json §25 audit trail (only if a grant was spent)
|
|
78
78
|
pass_threads.json §27 one entry per pass, {"pass", "thread_id"}
|
|
79
|
-
confidence_chart.
|
|
79
|
+
confidence_chart.svg Tier distribution bar chart (batch 212)
|
|
80
80
|
trace.md Full trace log
|
|
81
81
|
report.md Final synthesis report
|
|
82
82
|
report.pdf PDF rendering (only if markdown+weasyprint installed)
|
|
@@ -231,41 +231,100 @@ def _write_sources(run: PipelineRun, output_dir: str) -> None:
|
|
|
231
231
|
logger.warning("Source texts skipped (write failed): %s", e)
|
|
232
232
|
|
|
233
233
|
|
|
234
|
+
def _axis_step(top: int) -> int:
|
|
235
|
+
"""A 1, 2 or 5 times a power of ten that puts at most ten gridlines
|
|
236
|
+
under `top`, so the axis reads in round numbers whatever the count."""
|
|
237
|
+
step = 1
|
|
238
|
+
while True:
|
|
239
|
+
for factor in (1, 2, 5):
|
|
240
|
+
if top <= 10 * step * factor:
|
|
241
|
+
return step * factor
|
|
242
|
+
step *= 10
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
def confidence_svg(tiers: list, counts: list) -> str:
|
|
246
|
+
"""The tier distribution as one standalone SVG document.
|
|
247
|
+
|
|
248
|
+
BATCH 212, THE OWNER'S CHOICE. This was matplotlib, which with numpy,
|
|
249
|
+
fonttools, pillow and seven smaller packages was 126 MB of a 367 MB
|
|
250
|
+
install, measured, for these five bars; it also rebuilt a font cache
|
|
251
|
+
for minutes in a fresh environment. Drawn by hand now: fixed geometry,
|
|
252
|
+
integer gridlines, the count over each bar, and a white background so
|
|
253
|
+
the chart reads in a dark-themed viewer too. Every label is escaped,
|
|
254
|
+
though today they are the fixed tier names.
|
|
255
|
+
"""
|
|
256
|
+
# html.escape, not xml.sax.saxutils.escape (batch 217): it escapes
|
|
257
|
+
# both quotes by default, which an attribute needs, and importing
|
|
258
|
+
# xml.sax is bandit's B406 whatever is used from it.
|
|
259
|
+
from html import escape
|
|
260
|
+
|
|
261
|
+
width, height = 640, 400
|
|
262
|
+
left, right, top_pad, bottom = 64, 40, 56, 52
|
|
263
|
+
plot_w = width - left - right
|
|
264
|
+
plot_h = height - top_pad - bottom
|
|
265
|
+
peak = max(counts, default=0)
|
|
266
|
+
step = _axis_step(max(peak, 1))
|
|
267
|
+
axis_top = max(step, -(-peak // step) * step)
|
|
268
|
+
base = top_pad + plot_h
|
|
269
|
+
|
|
270
|
+
def y_of(value: int) -> float:
|
|
271
|
+
return base - plot_h * value / axis_top
|
|
272
|
+
|
|
273
|
+
out = [
|
|
274
|
+
f'<svg xmlns="http://www.w3.org/2000/svg" width="{width}" '
|
|
275
|
+
f'height="{height}" viewBox="0 0 {width} {height}" '
|
|
276
|
+
f'font-family="DejaVu Sans, Arial, Helvetica, sans-serif">',
|
|
277
|
+
'<title>Confidence tier distribution</title>',
|
|
278
|
+
f'<rect width="{width}" height="{height}" fill="#ffffff"/>',
|
|
279
|
+
f'<text x="{width / 2:g}" y="32" font-size="18" '
|
|
280
|
+
f'text-anchor="middle" fill="#222222">Confidence tier distribution'
|
|
281
|
+
f'</text>',
|
|
282
|
+
f'<text x="18" y="{top_pad + plot_h / 2:g}" font-size="13" '
|
|
283
|
+
f'text-anchor="middle" fill="#222222" transform="rotate(-90 18 '
|
|
284
|
+
f'{top_pad + plot_h / 2:g})">Number of claims</text>',
|
|
285
|
+
]
|
|
286
|
+
for value in range(0, axis_top + 1, step):
|
|
287
|
+
y = y_of(value)
|
|
288
|
+
out.append(f'<line x1="{left}" y1="{y:g}" x2="{width - right}" '
|
|
289
|
+
f'y2="{y:g}" stroke="#dddddd" stroke-width="1"/>')
|
|
290
|
+
out.append(f'<text x="{left - 8}" y="{y + 4:g}" font-size="12" '
|
|
291
|
+
f'text-anchor="end" fill="#444444">{value}</text>')
|
|
292
|
+
slot = plot_w / max(len(tiers), 1)
|
|
293
|
+
bar_w = slot * 0.6
|
|
294
|
+
for index, (tier, count) in enumerate(zip(tiers, counts)):
|
|
295
|
+
x = left + slot * index + (slot - bar_w) / 2
|
|
296
|
+
y = y_of(count)
|
|
297
|
+
centre = x + bar_w / 2
|
|
298
|
+
label = escape(str(tier))
|
|
299
|
+
out.append(f'<rect class="bar" data-tier="{label}" '
|
|
300
|
+
f'data-count="{int(count)}" x="{x:g}" y="{y:g}" '
|
|
301
|
+
f'width="{bar_w:g}" height="{base - y:g}" '
|
|
302
|
+
f'fill="#1f77b4"/>')
|
|
303
|
+
out.append(f'<text x="{centre:g}" y="{y - 6:g}" font-size="12" '
|
|
304
|
+
f'text-anchor="middle" fill="#222222">{int(count)}</text>')
|
|
305
|
+
out.append(f'<text x="{centre:g}" y="{base + 20:g}" font-size="11" '
|
|
306
|
+
f'text-anchor="middle" fill="#222222">'
|
|
307
|
+
f'{escape(str(tier).replace("_", " "))}</text>')
|
|
308
|
+
out.append(f'<line x1="{left}" y1="{base:g}" x2="{width - right}" '
|
|
309
|
+
f'y2="{base:g}" stroke="#444444" stroke-width="1"/>')
|
|
310
|
+
out.append("</svg>")
|
|
311
|
+
return "\n".join(out) + "\n"
|
|
312
|
+
|
|
313
|
+
|
|
234
314
|
def _write_confidence_chart(run: PipelineRun, output_dir: str) -> None:
|
|
235
315
|
"""Bar chart of confidence tier distribution. Degrades loudly, never
|
|
236
|
-
fatally
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
on every run is exactly the signal worth shouting, not hiding."""
|
|
241
|
-
try:
|
|
242
|
-
import matplotlib
|
|
243
|
-
matplotlib.use("Agg") # headless, no display needed
|
|
244
|
-
import matplotlib.pyplot as plt
|
|
245
|
-
except Exception as e:
|
|
246
|
-
logger.warning(
|
|
247
|
-
"Confidence chart skipped (matplotlib unavailable): %s", e)
|
|
248
|
-
return
|
|
249
|
-
|
|
250
|
-
# #81. `fig` is bound INSIDE the try, so a failure before the binding
|
|
251
|
-
# used to leave `finally: plt.close(fig)` raising UnboundLocalError --
|
|
252
|
-
# the containment guard inverting into exactly the crash it existed to
|
|
253
|
-
# prevent. Binding None first makes "nothing to clean up" a state
|
|
254
|
-
# instead of an exception.
|
|
255
|
-
fig = None
|
|
316
|
+
fatally: a failure here must not crash a completed pipeline run over a
|
|
317
|
+
chart (#81), and every degrade path warns naming the helper and the
|
|
318
|
+
cause, because a chart failing on every run is exactly the signal
|
|
319
|
+
worth shouting, not hiding."""
|
|
256
320
|
try:
|
|
257
321
|
tiers, counts = _tier_counts(run)
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
fig.tight_layout()
|
|
263
|
-
fig.savefig(os.path.join(output_dir, "confidence_chart.png"))
|
|
322
|
+
svg = confidence_svg(tiers, counts)
|
|
323
|
+
with open(os.path.join(output_dir, "confidence_chart.svg"), "w",
|
|
324
|
+
encoding="utf-8") as f:
|
|
325
|
+
f.write(svg)
|
|
264
326
|
except Exception as e:
|
|
265
327
|
logger.warning("Confidence chart skipped (render failed): %s", e)
|
|
266
|
-
finally:
|
|
267
|
-
if fig is not None:
|
|
268
|
-
plt.close(fig)
|
|
269
328
|
|
|
270
329
|
|
|
271
330
|
def _try_write_pdf(report_markdown: str, output_dir: str) -> None:
|
|
@@ -39,10 +39,34 @@ demonstrated consequence in #76's table, and nothing else does:
|
|
|
39
39
|
`per_claim_flags`
|
|
40
40
|
a declared value type `"severity": "0.0"` -- a string that
|
|
41
41
|
fails two passes later, in Pass 5
|
|
42
|
+
the type of its MEMBERS `"entities": ["madrid", 1947]` -- a list,
|
|
43
|
+
which Pass 3a then sorts
|
|
42
44
|
that entry ids resolve 3a/3b answering about ids Pass 2 never
|
|
43
45
|
issued, which grounded nothing while the
|
|
44
46
|
trace said it had grounded everything
|
|
45
47
|
|
|
48
|
+
THE FOURTH ROW IS BATCH 149, AND IT IS NOT A RICHER VOCABULARY FOR ITS OWN
|
|
49
|
+
SAKE -- it is the same property as the third, asked of the values a
|
|
50
|
+
container holds, because that is where the arithmetic actually happens.
|
|
51
|
+
`entities` is not measured; every element of it is hashed and then
|
|
52
|
+
compared against every other (`sorted({e for c in claims for e in
|
|
53
|
+
c.entities})`), and `asserted_by_candidates` is hashed the same way by
|
|
54
|
+
`len(set(...))`. A declared CONTAINER type closes the three container
|
|
55
|
+
cases and leaves the three member cases open, each driven against the real
|
|
56
|
+
consumer:
|
|
57
|
+
|
|
58
|
+
["madrid", 1947] TypeError: '<' not supported between
|
|
59
|
+
instances of 'int' and 'str'
|
|
60
|
+
[["madrid"]] TypeError: unhashable type: 'list'
|
|
61
|
+
[{"name": "madrid"}] TypeError: unhashable type: 'dict'
|
|
62
|
+
|
|
63
|
+
A MEMBER is a dict's VALUES or a list's ELEMENTS, deliberately one concept
|
|
64
|
+
and one spec field per level: Pass 4's flag lists live under claim-id keys
|
|
65
|
+
and Pass 2's entities live in an array, and the thing being declared is
|
|
66
|
+
the same in both -- what the pure code downstream will iterate, hash, sort
|
|
67
|
+
or measure. Pass 4's flag lists are typed and their STRINGS are not, which
|
|
68
|
+
is the same rule: `len()` is the only thing that reads them.
|
|
69
|
+
|
|
46
70
|
Not a schema language, and no new dependency (#145 tracks those). A
|
|
47
71
|
richer vocabulary would let a spec describe things no pass has ever got
|
|
48
72
|
wrong, at the price of a second place where a payload's meaning lives.
|
|
@@ -66,6 +90,17 @@ from typing import Any, Optional
|
|
|
66
90
|
# they come from, and the scorer reads the same two names (§30, B9).
|
|
67
91
|
from core.reasoning.base import CLAIM_TYPES, GROUNDING_STATUSES # noqa: F401
|
|
68
92
|
|
|
93
|
+
# A BOOLEAN IS NOT A NUMBER, which Python disagrees with: `isinstance(
|
|
94
|
+
# True, int)` is True, so `"severity": true` satisfied this tuple and
|
|
95
|
+
# Pass 5 computed `1.0 - True` as 0.0 -- the MAXIMUM critic penalty, the
|
|
96
|
+
# single harshest value the field can take -- with nothing in the trace
|
|
97
|
+
# and no corrective retry (batch 149). `"severity": false` took the other
|
|
98
|
+
# extreme for the same reason.
|
|
99
|
+
#
|
|
100
|
+
# The exclusion is not new to the codebase, only to the boundary:
|
|
101
|
+
# `source_scoring._as_number` and `confidence_scoring.source_quality` both
|
|
102
|
+
# spell it out already, each guarding a value that is supposed to have
|
|
103
|
+
# been rejected HERE. See `_of_type` below, which is what applies it.
|
|
69
104
|
NUMBER = (int, float)
|
|
70
105
|
|
|
71
106
|
|
|
@@ -93,6 +128,10 @@ class Spec:
|
|
|
93
128
|
key_types top-level key -> permitted Python type(s)
|
|
94
129
|
entry_required keys every entry must carry (top="array")
|
|
95
130
|
entry_types entry key -> permitted Python type(s)
|
|
131
|
+
key_member_types top-level key -> permitted type for every MEMBER of
|
|
132
|
+
the container under it (a dict's values, a list's
|
|
133
|
+
elements)
|
|
134
|
+
entry_member_types the same, per entry key
|
|
96
135
|
enums entry key -> the set of permitted values
|
|
97
136
|
ids entry key holding a claim id, cross-checked against
|
|
98
137
|
the claims the apply step will resolve against
|
|
@@ -107,6 +146,8 @@ class Spec:
|
|
|
107
146
|
key_types: dict = field(default_factory=dict)
|
|
108
147
|
entry_required: frozenset = frozenset()
|
|
109
148
|
entry_types: dict = field(default_factory=dict)
|
|
149
|
+
key_member_types: dict = field(default_factory=dict)
|
|
150
|
+
entry_member_types: dict = field(default_factory=dict)
|
|
110
151
|
enums: dict = field(default_factory=dict)
|
|
111
152
|
ids: Optional[str] = None
|
|
112
153
|
unique: Optional[str] = None
|
|
@@ -165,12 +206,36 @@ SPECS = {
|
|
|
165
206
|
# before handing the plan whole to Pass 3c. So requiring all five
|
|
166
207
|
# would fail runs over an omission nothing depends on. The
|
|
167
208
|
# demonstrated defect is a bare list, which is what `top` catches.
|
|
168
|
-
|
|
209
|
+
#
|
|
210
|
+
# THE ONE KEY THE ORCHESTRATOR READS IS TYPED, AND STILL NOT REQUIRED
|
|
211
|
+
# (batch 149) -- `key_types` is checked only `if key in payload`, so
|
|
212
|
+
# the defensive `.get` above keeps its meaning and an omission still
|
|
213
|
+
# costs nothing. What it stops is the read itself: `len(123)` raised
|
|
214
|
+
# inside the Pass 0 checkpoint, and `len("madrid")` did not raise at
|
|
215
|
+
# all -- it reported SIX key entities anticipated, from one word.
|
|
216
|
+
"Pass 0": Spec(top="object",
|
|
217
|
+
key_types={"key_entities_or_subjects": list}),
|
|
169
218
|
|
|
170
219
|
"Pass 2": Spec(
|
|
171
220
|
top="array",
|
|
172
221
|
entry_required=frozenset({"id", "text", "type"}),
|
|
173
|
-
|
|
222
|
+
# `entities` and `asserted_by_candidates` are TYPED BUT NOT
|
|
223
|
+
# REQUIRED, which is the same asymmetry `sources` carries below
|
|
224
|
+
# and for the same reason: base.py defaults both to an empty list,
|
|
225
|
+
# a claim with no entities is an ordinary answer, and
|
|
226
|
+
# `asserted_by_candidates` is required only in ensemble mode (B7).
|
|
227
|
+
# What they are not is optional to get RIGHT -- Pass 3a sorts a
|
|
228
|
+
# set built from every claim's entities, and Pass 5 measures
|
|
229
|
+
# `len(set(asserted_by_candidates))`, so both the container and
|
|
230
|
+
# its members are declared (batch 149).
|
|
231
|
+
entry_types={"id": str, "text": str, "entities": list,
|
|
232
|
+
"asserted_by_candidates": list},
|
|
233
|
+
# str and int from base.py's own annotations -- `entities:
|
|
234
|
+
# list[str]` and `asserted_by_candidates: list[int]`. The
|
|
235
|
+
# annotations are not enforced (the comment at the top of base.py
|
|
236
|
+
# says so, and #75 is what that cost); this is where they become a
|
|
237
|
+
# check.
|
|
238
|
+
entry_member_types={"entities": str, "asserted_by_candidates": int},
|
|
174
239
|
enums={"type": CLAIM_TYPES},
|
|
175
240
|
# B6. Two claims sharing an id split in half: _apply_grounding
|
|
176
241
|
# builds {c.id: c} (last wins) while _apply_assumption_flags uses
|
|
@@ -202,6 +267,14 @@ SPECS = {
|
|
|
202
267
|
top="object",
|
|
203
268
|
required=frozenset({"per_claim_flags"}),
|
|
204
269
|
key_types={"per_claim_flags": dict},
|
|
270
|
+
# Every VALUE under those claim ids is a list, because Pass 5
|
|
271
|
+
# measures it: `w["assumption_flag_penalty"] * len(flags)` raised
|
|
272
|
+
# on `{"c001": 123}` and silently charged a THREE-flag penalty for
|
|
273
|
+
# `{"c001": "abc"}` (batch 149). The strings inside are not typed
|
|
274
|
+
# -- `len()` is all that reads them, so a type on the elements
|
|
275
|
+
# would be a check with no demonstrated consequence, which is the
|
|
276
|
+
# rule B4 states.
|
|
277
|
+
key_member_types={"per_claim_flags": list},
|
|
205
278
|
ids_in_keys="per_claim_flags",
|
|
206
279
|
),
|
|
207
280
|
|
|
@@ -230,9 +303,58 @@ def _type_name(value: Any) -> str:
|
|
|
230
303
|
|
|
231
304
|
|
|
232
305
|
def _permitted(types) -> str:
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
306
|
+
return " or ".join(sorted({_type_name(t()) for t in _as_tuple(types)}))
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
def _as_tuple(types) -> tuple:
|
|
310
|
+
return types if isinstance(types, tuple) else (types,)
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
def _of_type(value, types) -> bool:
|
|
314
|
+
"""isinstance, except that a boolean is only ever a boolean.
|
|
315
|
+
|
|
316
|
+
See the comment on NUMBER. Applied to every declared type rather than
|
|
317
|
+
to NUMBER alone, because "a boolean satisfies a type it was not
|
|
318
|
+
declared as" is the defect, and `severity` is simply the only place
|
|
319
|
+
the tree has reached it so far.
|
|
320
|
+
"""
|
|
321
|
+
if isinstance(value, bool) and bool not in _as_tuple(types):
|
|
322
|
+
return False
|
|
323
|
+
return isinstance(value, types)
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
def _members(container):
|
|
327
|
+
"""(label, value) for everything a container holds.
|
|
328
|
+
|
|
329
|
+
A dict's VALUES, keyed by the key that named them, and a list's
|
|
330
|
+
elements by position -- so the message can say `'per_claim_flags'
|
|
331
|
+
['c001']` or `entities[1]` and the model can find the one it got
|
|
332
|
+
wrong.
|
|
333
|
+
"""
|
|
334
|
+
if isinstance(container, dict):
|
|
335
|
+
return [(f"[{key!r}]", value) for key, value in container.items()]
|
|
336
|
+
return [(f"[{index}]", value) for index, value in enumerate(container)]
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
def _check_members(pass_id, container, member_types, at, prefix="") -> None:
|
|
340
|
+
for key, types in member_types.items():
|
|
341
|
+
if key not in container:
|
|
342
|
+
continue
|
|
343
|
+
value = container[key]
|
|
344
|
+
if not isinstance(value, (dict, list)):
|
|
345
|
+
# The declared CONTAINER type has already refused this, or
|
|
346
|
+
# there isn't one and the member rule has nothing to stand on.
|
|
347
|
+
# Either way, iterating here would report the members of a
|
|
348
|
+
# string.
|
|
349
|
+
continue
|
|
350
|
+
for label, member in _members(value):
|
|
351
|
+
if _of_type(member, types):
|
|
352
|
+
continue
|
|
353
|
+
raise PayloadShapeError(
|
|
354
|
+
f"{pass_id}: {prefix}{key}{label}{at} is {member!r}, which "
|
|
355
|
+
f"is {_type_name(member)}; every value in {key!r} must be "
|
|
356
|
+
f"{_permitted(types)}."
|
|
357
|
+
)
|
|
236
358
|
|
|
237
359
|
|
|
238
360
|
def validate(pass_id: str, payload: Any, claim_ids=None,
|
|
@@ -291,12 +413,14 @@ def _validate_against(pass_id, payload, spec, claim_ids, ensemble,
|
|
|
291
413
|
)
|
|
292
414
|
|
|
293
415
|
for key, types in spec.key_types.items():
|
|
294
|
-
if key in payload and not
|
|
416
|
+
if key in payload and not _of_type(payload[key], types):
|
|
295
417
|
raise PayloadShapeError(
|
|
296
418
|
f"{pass_id}: {key!r}{at} is {_type_name(payload[key])}; "
|
|
297
419
|
f"expected {_permitted(types)}."
|
|
298
420
|
)
|
|
299
421
|
|
|
422
|
+
_check_members(pass_id, payload, spec.key_member_types, at)
|
|
423
|
+
|
|
300
424
|
for key, nested in spec.nested.items():
|
|
301
425
|
_validate_against(pass_id, payload[key], nested, claim_ids,
|
|
302
426
|
ensemble, where=key)
|
|
@@ -330,12 +454,14 @@ def _validate_entries(pass_id, entries, spec, claim_ids, ensemble, at) -> None:
|
|
|
330
454
|
# raises TypeError from inside the validator rather than
|
|
331
455
|
# producing a message the model can act on.
|
|
332
456
|
for key, types in spec.entry_types.items():
|
|
333
|
-
if key in entry and not
|
|
457
|
+
if key in entry and not _of_type(entry[key], types):
|
|
334
458
|
raise PayloadShapeError(
|
|
335
459
|
f"{pass_id}: entry {index}{at} has {key}="
|
|
336
460
|
f"{entry[key]!r}, which is {_type_name(entry[key])}; "
|
|
337
461
|
f"expected {_permitted(types)}."
|
|
338
462
|
)
|
|
463
|
+
_check_members(pass_id, entry, spec.entry_member_types, at,
|
|
464
|
+
prefix=f"entry {index} ")
|
|
339
465
|
for key, allowed in spec.enums.items():
|
|
340
466
|
if key not in entry:
|
|
341
467
|
continue
|