@yahyahammad/venastine 0.2.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 +233 -94
- 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 +277 -43
- package/agents/tui_commands.py +7 -12
- 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 +158 -20
- 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 +1238 -82
- package/core/commands.py +454 -0
- package/core/compaction.py +830 -66
- 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 +1516 -440
- package/core/memory.py +120 -8
- 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 +158 -26
- 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 +1671 -199
- 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 +7 -12
- package/package.json +234 -4
- 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 +51 -2
- package/requirements.txt +252 -29
- package/safety/policy_enforcement.py +353 -17
- 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 +368 -35
- 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 +347 -30
- package/tui/app.py +4645 -438
- package/tui/app.tcss +185 -2
- package/tui/commands.py +10 -154
- package/tui/diffs.py +286 -265
- package/tui/hosted_driver.py +102 -0
- package/tui/markdown.py +60 -10
- package/tui/meters.py +103 -7
- package/tui/screens.py +346 -92
- package/tui/themes.py +59 -19
- package/tui/widgets.py +1575 -111
- package/SECURITY.md +0 -88
- package/THIRD_PARTY_NOTICES.md +0 -100
|
@@ -60,18 +60,23 @@ allowlist excludes every module, so nothing depends on it.
|
|
|
60
60
|
from __future__ import annotations
|
|
61
61
|
|
|
62
62
|
import ast
|
|
63
|
+
import json
|
|
64
|
+
import re
|
|
63
65
|
import types
|
|
64
66
|
from typing import Any, Optional
|
|
65
67
|
|
|
66
68
|
import sympy
|
|
69
|
+
from pydantic import ValidationError
|
|
67
70
|
from sympy.parsing.sympy_parser import (
|
|
71
|
+
convert_equals_signs,
|
|
68
72
|
eval_expr,
|
|
73
|
+
implicit_multiplication_application,
|
|
69
74
|
standard_transformations,
|
|
70
75
|
stringify_expr,
|
|
71
|
-
implicit_multiplication_application,
|
|
72
|
-
convert_equals_signs,
|
|
73
76
|
)
|
|
74
77
|
|
|
78
|
+
import config
|
|
79
|
+
|
|
75
80
|
# SymPy's own public namespace (sin, cos, sqrt, pi, Symbol, Matrix, oo, ...)
|
|
76
81
|
# with __builtins__ explicitly neutered. Built once at import time.
|
|
77
82
|
#
|
|
@@ -85,7 +90,29 @@ _SAFE_GLOBALS["__builtins__"] = {}
|
|
|
85
90
|
|
|
86
91
|
|
|
87
92
|
class MathParseError(ValueError):
|
|
88
|
-
"""Raised when an expression string can't be safely parsed.
|
|
93
|
+
"""Raised when an expression string can't be safely parsed.
|
|
94
|
+
|
|
95
|
+
ALSO THE BASE OF THE TWO BELOW, neither of which is a parse failure.
|
|
96
|
+
They are in this family for one mechanical reason: every tool already
|
|
97
|
+
has exactly one `except MathParseError as e: return {"error": str(e)}`
|
|
98
|
+
arm, so a subclass is caught by a handler that is already there and
|
|
99
|
+
already tested. Two new `except` arms across six tools would be twelve
|
|
100
|
+
places to add and twelve chances to miss one, which is the shape #96
|
|
101
|
+
and EP6 are both about (batch 143).
|
|
102
|
+
"""
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
class MathResultTooLarge(MathParseError):
|
|
106
|
+
"""A result that was computed CORRECTLY and cannot be returned.
|
|
107
|
+
|
|
108
|
+
Two causes, one message family, because the model's remedy is the same
|
|
109
|
+
for both: CPython caps `int -> str` at 4,300 digits, and a math result
|
|
110
|
+
is bounded at `config.MAX_READ_CHARS`. See `serialize`.
|
|
111
|
+
"""
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
class MathParamsError(MathParseError):
|
|
115
|
+
"""The tool's own params did not validate. See `parse_params`."""
|
|
89
116
|
|
|
90
117
|
|
|
91
118
|
# What a math EXPRESSION may name. Grouped by category rather than listed
|
|
@@ -224,6 +251,20 @@ def _validate_ast(code: str, local_dict: dict) -> None:
|
|
|
224
251
|
f"{type(node).__name__} is not allowed in a math "
|
|
225
252
|
f"expression")
|
|
226
253
|
|
|
254
|
+
# `**` UNPACKING IS THE ONE CONSTRUCT A LISTED NODE SPELLS
|
|
255
|
+
# (TECHNICAL_DEBT 55, batch 160). `f(*args)` is `ast.Starred`,
|
|
256
|
+
# which is unlisted. `f(**mapping)` is `ast.keyword(arg=None)`, and
|
|
257
|
+
# `keyword` is listed for ordinary keyword arguments, so the second
|
|
258
|
+
# form walked past the node check and was refused only later, by
|
|
259
|
+
# SymPy ("sin takes exactly 1 argument"). It was inert, because no
|
|
260
|
+
# str-keyed mapping is reachable here, but inert because of what is
|
|
261
|
+
# unreachable is not what this allowlist claims. No legitimate
|
|
262
|
+
# expression needs it: measured, even ordinary keyword arguments do
|
|
263
|
+
# not survive `auto_symbol` (`prec=20` is refused as a name).
|
|
264
|
+
if isinstance(node, ast.keyword) and node.arg is None:
|
|
265
|
+
raise MathParseError(
|
|
266
|
+
"** unpacking is not allowed in a math expression")
|
|
267
|
+
|
|
227
268
|
# Every name is resolved against _SAFE_GLOBALS at eval time, so
|
|
228
269
|
# every name must be one we chose. Unknown names never reach here
|
|
229
270
|
# -- auto_symbol has already rewritten them to Symbol('name') --
|
|
@@ -260,6 +301,21 @@ def _validate_ast(code: str, local_dict: dict) -> None:
|
|
|
260
301
|
# other string keeps the property closed rather than filtered. This
|
|
261
302
|
# also removes `"%s" % pi` and every other string-valued construct
|
|
262
303
|
# without needing a rule per construct.
|
|
304
|
+
#
|
|
305
|
+
# A `bytes` LITERAL IS REFUSED IN THE SAME BREATH, and that is the one
|
|
306
|
+
# non-`str` type it has to be: `eval()` and `compile()` both accept
|
|
307
|
+
# bytes where they reject every other non-string object, so a future
|
|
308
|
+
# SymPy callable that sympifies what it is handed would land there.
|
|
309
|
+
# The rule here used to name `str` alone, and `factor(b"1+1")`,
|
|
310
|
+
# `b"abc"` and `roots(b"x**2-1")` were all accepted -- measured, with
|
|
311
|
+
# the `str` spelling of each refused as the control. No expression
|
|
312
|
+
# needs one: `stringify_expr` never emits a bytes argument, so the
|
|
313
|
+
# licensing pass below deliberately licenses `str` only, which refuses
|
|
314
|
+
# even `Symbol(b"x")` (batch 143).
|
|
315
|
+
#
|
|
316
|
+
# Naming one of the two string types is the same category of mistake
|
|
317
|
+
# as the dunder tripwire above: a near-miss on a type check rather
|
|
318
|
+
# than on a payload.
|
|
263
319
|
licensed = set()
|
|
264
320
|
for node in ast.walk(tree):
|
|
265
321
|
if (isinstance(node, ast.Call) and isinstance(node.func, ast.Name)
|
|
@@ -269,7 +325,8 @@ def _validate_ast(code: str, local_dict: dict) -> None:
|
|
|
269
325
|
licensed.add(id(arg))
|
|
270
326
|
|
|
271
327
|
for node in ast.walk(tree):
|
|
272
|
-
if (isinstance(node, ast.Constant)
|
|
328
|
+
if (isinstance(node, ast.Constant)
|
|
329
|
+
and isinstance(node.value, (str, bytes))
|
|
273
330
|
and id(node) not in licensed):
|
|
274
331
|
raise MathParseError(
|
|
275
332
|
"string literals are not allowed in a math expression")
|
|
@@ -296,7 +353,22 @@ def _reject_escaped_value(value: Any, _depth: int = 0) -> None:
|
|
|
296
353
|
of `Basic`, which is what separates them from `type` or `os`.
|
|
297
354
|
"""
|
|
298
355
|
if _depth > 6:
|
|
299
|
-
|
|
356
|
+
# FAILS CLOSED. This used to `return`, which gave the one check
|
|
357
|
+
# written for an escape nobody anticipated an anticipated DEPTH:
|
|
358
|
+
# `[[[os]]]` was refused and `[[[[[[[[os]]]]]]]]` was accepted
|
|
359
|
+
# uninspected -- measured, with the depth-3 case as the control
|
|
360
|
+
# proving the function was live. The comment that stood here
|
|
361
|
+
# ("containers are shallow here") was an assertion about today's
|
|
362
|
+
# return shapes inside the one function whose stated job is the
|
|
363
|
+
# opposite of relying on them.
|
|
364
|
+
#
|
|
365
|
+
# The cap's job is to bound the WALK, which it still does. The
|
|
366
|
+
# deepest legitimate return shape in the battery is `eigenvects()`
|
|
367
|
+
# at 4, and `test_the_backstop_allows_every_legitimate_return_shape`
|
|
368
|
+
# is what goes red if that stops being true (batch 143).
|
|
369
|
+
raise MathParseError(
|
|
370
|
+
"expression evaluated to a value nested more than 6 deep, "
|
|
371
|
+
"which is not a mathematical value")
|
|
300
372
|
|
|
301
373
|
if isinstance(value, types.ModuleType):
|
|
302
374
|
raise MathParseError(
|
|
@@ -320,6 +392,16 @@ def _reject_escaped_value(value: Any, _depth: int = 0) -> None:
|
|
|
320
392
|
"expression evaluated to a function or method, which is "
|
|
321
393
|
"never a mathematical value")
|
|
322
394
|
|
|
395
|
+
# Raw bytes are not a mathematical value either, and they are listed
|
|
396
|
+
# here for the same reason the AST rule names them: `b'1+1'`,
|
|
397
|
+
# `[b'x']` and `{"k": b'x'}` all walked past this function, because
|
|
398
|
+
# bytes is not a module, a type, a function, or one of the four
|
|
399
|
+
# containers below (batch 143).
|
|
400
|
+
if isinstance(value, (bytes, bytearray, memoryview)):
|
|
401
|
+
raise MathParseError(
|
|
402
|
+
"expression evaluated to raw bytes, which is never a "
|
|
403
|
+
"mathematical value")
|
|
404
|
+
|
|
323
405
|
if isinstance(value, dict):
|
|
324
406
|
for k, v in value.items():
|
|
325
407
|
_reject_escaped_value(k, _depth + 1)
|
|
@@ -329,14 +411,146 @@ def _reject_escaped_value(value: Any, _depth: int = 0) -> None:
|
|
|
329
411
|
_reject_escaped_value(item, _depth + 1)
|
|
330
412
|
|
|
331
413
|
|
|
332
|
-
|
|
414
|
+
# A name token, ignoring one preceded by a word character or a dot, so the
|
|
415
|
+
# `e5` inside `1e5` is not read as a name the caller wrote.
|
|
416
|
+
_NAME_TOKEN = re.compile(r"(?<![\w.])[A-Za-z_][A-Za-z0-9_]*")
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
def _rewritten_names(expr_str: str, result: Any, local_dict: dict) -> set:
|
|
420
|
+
"""The multi-character names the caller wrote that did NOT survive the
|
|
421
|
+
parse as themselves.
|
|
422
|
+
|
|
423
|
+
`_TRANSFORMATIONS` carries `split_symbols`, so a multi-character name
|
|
424
|
+
that is not in `local_dict` is rewritten into a PRODUCT of single-letter
|
|
425
|
+
Symbols before anything is computed: `mass` becomes `a*m*s**2`, and
|
|
426
|
+
`determinant([[mass,0],[0,mass]])` then answers `a**2*m**2*s**4` as a
|
|
427
|
+
successful result naming a quantity nobody asked about (batch 131).
|
|
428
|
+
|
|
429
|
+
Decided by comparing what was written against what came back rather than
|
|
430
|
+
by reimplementing SymPy's `_token_splittable`: whatever that predicate
|
|
431
|
+
does with a given spelling, a name is reported here exactly when it is
|
|
432
|
+
not among the result's free symbols. Three exclusions, each for its own
|
|
433
|
+
reason:
|
|
434
|
+
|
|
435
|
+
* a ONE-character name cannot be split, so it is never interesting;
|
|
436
|
+
* a name in `_ALLOWED_NAMES` is a SymPy binding the caller may well
|
|
437
|
+
have meant -- `E` is Euler's number and `sin` is a function, and
|
|
438
|
+
deciding that those were mistakes is not this function's business
|
|
439
|
+
(the `symbols=` route is how a caller overrides one);
|
|
440
|
+
* a DECLARED name is in `local_dict` and therefore already protected,
|
|
441
|
+
which is the whole point of declaring it.
|
|
442
|
+
"""
|
|
443
|
+
try:
|
|
444
|
+
survived = {str(s) for s in result.free_symbols}
|
|
445
|
+
except AttributeError:
|
|
446
|
+
# A plain bool/int/str result has no free symbols; nothing survived,
|
|
447
|
+
# which is the right answer for a name that should have.
|
|
448
|
+
survived = set()
|
|
449
|
+
found = set()
|
|
450
|
+
for token in _NAME_TOKEN.findall(expr_str):
|
|
451
|
+
if len(token) < 2 or token in _ALLOWED_NAMES or token in local_dict:
|
|
452
|
+
continue
|
|
453
|
+
if token not in survived:
|
|
454
|
+
found.add(token)
|
|
455
|
+
return found
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
def _split_warnings(rewritten: set, field: str = "symbols") -> list[str]:
|
|
459
|
+
"""One sentence per split name, for a tool's `warnings` list.
|
|
460
|
+
|
|
461
|
+
ONE copy, shared by every tool that reports this: four lists once
|
|
462
|
+
answered "what counts as a project file" and no two agreed (#96), and
|
|
463
|
+
four spellings of this sentence would be the same defect. The text names
|
|
464
|
+
the remedy because the model is the only caller there is, and nothing in
|
|
465
|
+
any schema told it the rule until batch 131.
|
|
466
|
+
|
|
467
|
+
`field` EXISTS BECAUSE THE REMEDY IS NOT THE SAME WORD EVERYWHERE
|
|
468
|
+
(batch 143). Three tools call the declaring parameter `symbols`;
|
|
469
|
+
`logic` calls it `variables`. One sentence with the field named by its
|
|
470
|
+
caller is one copy; a sentence that hardcodes `symbols=` is one copy
|
|
471
|
+
that is wrong for a quarter of its callers, which is worse than four.
|
|
472
|
+
"""
|
|
473
|
+
return [
|
|
474
|
+
"'%s' was read as a product of single letters, not as one symbol. "
|
|
475
|
+
"Pass %s=['%s'] to keep it whole." % (name, field, name)
|
|
476
|
+
for name in sorted(rewritten)
|
|
477
|
+
]
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
def parse_params(model, params: dict):
|
|
481
|
+
"""Validate one tool's params, or refuse in a sentence the model can act on.
|
|
482
|
+
|
|
483
|
+
EVERY math tool built `Model(**params)` on the line ABOVE its `try`, so
|
|
484
|
+
a field constraint that fired left `run()` as a bare
|
|
485
|
+
`pydantic.ValidationError`. Measured through the real transport, with
|
|
486
|
+
`operation="nope"` -- the most ordinary mistake a caller can make:
|
|
487
|
+
|
|
488
|
+
{"error": "symbolic_math did not return a usable result (exit 1).
|
|
489
|
+
... line 263, in __init__ ... pydantic_core._pydantic_core.
|
|
490
|
+
ValidationError: 1 validation error for SymbolicMathParams ..."}
|
|
491
|
+
|
|
492
|
+
plus the same traceback at WARNING, which the TUI's
|
|
493
|
+
`TranscriptLogHandler` draws on screen. That is the branch
|
|
494
|
+
`test_a_crashed_child_is_NOT_reported_as_a_limit` reserves for a
|
|
495
|
+
harness BUG, and it is H6's defect one line higher up the same
|
|
496
|
+
function: a routine, input-shaped outcome taking the path that means
|
|
497
|
+
"this tool is broken". Three of the three constraints that already
|
|
498
|
+
existed -- `precision` (le=50), `order` (ge=1) and the `operation`
|
|
499
|
+
`Literal` itself -- reached it.
|
|
500
|
+
|
|
501
|
+
It is also why each tool's `else: return {"error": "Unknown
|
|
502
|
+
operation"}` arm has never run: the `Literal` refuses first, so the
|
|
503
|
+
readable message that arm was written to give was unreachable. This is
|
|
504
|
+
where it gets given.
|
|
505
|
+
|
|
506
|
+
Not a pydantic `ValidationError` passed through verbatim: that names
|
|
507
|
+
a file, a line and a pydantic.dev URL, none of which a model can act
|
|
508
|
+
on (batch 143).
|
|
509
|
+
"""
|
|
510
|
+
try:
|
|
511
|
+
return model(**params)
|
|
512
|
+
except ValidationError as exc:
|
|
513
|
+
raise MathParamsError(_params_message(exc)) from None
|
|
514
|
+
|
|
515
|
+
|
|
516
|
+
def _params_message(exc: ValidationError) -> str:
|
|
517
|
+
"""One sentence per rejected field, in the model's own vocabulary.
|
|
518
|
+
|
|
519
|
+
`err["msg"]` is pydantic's own wording, which for a `Literal` lists the
|
|
520
|
+
operations verbatim -- exactly what the caller needs and the one thing
|
|
521
|
+
the generic transport error cannot say.
|
|
522
|
+
"""
|
|
523
|
+
parts = []
|
|
524
|
+
for err in exc.errors():
|
|
525
|
+
field = ".".join(str(p) for p in err.get("loc", ())) or "parameters"
|
|
526
|
+
parts.append("'%s' %s" % (field, err.get("msg", "is not valid")))
|
|
527
|
+
return ("The parameters were not accepted: %s. Correct the parameter "
|
|
528
|
+
"and call again." % "; ".join(parts))
|
|
529
|
+
|
|
530
|
+
|
|
531
|
+
def safe_parse(expr_str: str, symbols: Optional[list[str]] = None,
|
|
532
|
+
rewritten: Optional[set] = None):
|
|
333
533
|
"""
|
|
334
534
|
Parses a math expression string into a SymPy object without exposing
|
|
335
|
-
Python builtins.
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
535
|
+
Python builtins.
|
|
536
|
+
|
|
537
|
+
`symbols` IS THE ONLY WAY TO KEEP A MULTI-CHARACTER NAME WHOLE, and
|
|
538
|
+
that is the load-bearing half of this parameter rather than the
|
|
539
|
+
convenience it used to be documented as. `_TRANSFORMATIONS` carries
|
|
540
|
+
`split_symbols`, so a name absent from `local_dict` is rewritten into a
|
|
541
|
+
product of single letters before anything is computed -- `mass` parses
|
|
542
|
+
as `a*m*s**2`. Declaring it binds the name to a Symbol first, so it
|
|
543
|
+
survives. (It also keeps variable identity consistent across calls, e.g.
|
|
544
|
+
the same 'x' in an expression and a separate bound for a definite
|
|
545
|
+
integral, which is what this docstring said when `symbols=` was
|
|
546
|
+
reachable from only two of the four parsing tools.)
|
|
547
|
+
|
|
548
|
+
`rewritten`, when a set is passed, collects the names that were split
|
|
549
|
+
anyway -- a caller that cannot know in advance what the model will send
|
|
550
|
+
reports those rather than returning a confident answer about a
|
|
551
|
+
different expression. See `_rewritten_names`. The tools put them in a
|
|
552
|
+
`warnings` list, which is `probability_stats`'s existing convention for
|
|
553
|
+
"keep forgiving but explain".
|
|
340
554
|
"""
|
|
341
555
|
local_dict = {name: sympy.Symbol(name) for name in (symbols or [])}
|
|
342
556
|
try:
|
|
@@ -348,6 +562,8 @@ def safe_parse(expr_str: str, symbols: Optional[list[str]] = None):
|
|
|
348
562
|
_validate_ast(code, local_dict)
|
|
349
563
|
result = eval_expr(code, local_dict, _SAFE_GLOBALS)
|
|
350
564
|
_reject_escaped_value(result)
|
|
565
|
+
if rewritten is not None:
|
|
566
|
+
rewritten.update(_rewritten_names(expr_str, result, local_dict))
|
|
351
567
|
return result
|
|
352
568
|
except MathParseError:
|
|
353
569
|
# A refusal from _validate_ast already says which name or node was
|
|
@@ -360,18 +576,92 @@ def safe_parse(expr_str: str, symbols: Optional[list[str]] = None):
|
|
|
360
576
|
raise MathParseError(f"Could not parse expression '{expr_str}': {e}") from e
|
|
361
577
|
|
|
362
578
|
|
|
363
|
-
def serialize(value: Any) -> Any:
|
|
579
|
+
def serialize(value: Any, what: str = "") -> Any:
|
|
364
580
|
"""
|
|
365
581
|
Generic SymPy-object -> JSON-safe converter, used across all six math
|
|
366
|
-
tools
|
|
367
|
-
|
|
582
|
+
tools -- AND THE ONE PLACE A MATH RESULT IS BOUNDED (batch 143).
|
|
583
|
+
|
|
584
|
+
Two raises, both `MathResultTooLarge`, both caught by the one
|
|
585
|
+
`except MathParseError` arm every caller already has.
|
|
586
|
+
|
|
587
|
+
H6, AT THE PRODUCER (batch 143). `str()` of an Integer above CPython's 4,300-digit cap
|
|
588
|
+
raises `ValueError`, and this function was called in the `return`
|
|
589
|
+
statement -- OUTSIDE every `try` -- in `symbolic_math`,
|
|
590
|
+
`linear_algebra` and `geometry`. So `series order=4000` and
|
|
591
|
+
`determinant([["factorial(3000)","0"],["0","1"]])` left `run()` as a
|
|
592
|
+
bare `ValueError`, were reported as `exit 1` ("did not return a usable
|
|
593
|
+
result"), and handed the model 400 characters of traceback naming
|
|
594
|
+
`C:\\Users\\<username>\\AppData\\Roaming\\...`. `discrete_math`
|
|
595
|
+
caught it and the other three did not; the guard belongs here because
|
|
596
|
+
this is what the three of them share. ROADMAP_v2 H6: *"A result the
|
|
597
|
+
tool cannot serialise is the tool's own error."*
|
|
598
|
+
|
|
599
|
+
THE CEILING (batch 143). No math tool bounded its own output. Measured,
|
|
600
|
+
the only ceiling was `tools/isolation.MAX_CHILD_OUTPUT_BYTES` at 4 MB
|
|
601
|
+
-- a TRANSPORT limit, 62x the premise the compaction arithmetic is
|
|
602
|
+
sized from (`config.yaml`: *"max_read_chars (50,000 chars = 12,500
|
|
603
|
+
proxy tokens, the largest a single bounded read can return)"*):
|
|
604
|
+
|
|
605
|
+
truth_table, 14 free symbols, 27 chars in 786,441 proxy tokens
|
|
606
|
+
series order=1000 -- the documented worst case 296,272
|
|
607
|
+
TECHNICAL_DEBT 41, the arxiv result already fixed 31,733
|
|
608
|
+
|
|
609
|
+
and `core/compaction.py` says what the headroom is for: nothing in
|
|
610
|
+
this harness recovers from a prompt that overflows the window. A
|
|
611
|
+
result 49x `compaction_min_headroom_tokens` cannot be contained by it.
|
|
612
|
+
|
|
613
|
+
`config.MAX_READ_CHARS` rather than a new constant, because that is
|
|
614
|
+
the number the arithmetic already names, and it is read at CALL time
|
|
615
|
+
so a config change needs no reimport. Measured blast radius at 50,000:
|
|
616
|
+
nothing in the legitimacy battery is refused -- `series order=200` is
|
|
617
|
+
35,088 characters, `expand((x+y+z)**40)` is 27,340, every matrix,
|
|
618
|
+
geometry and factorial case is under 3,000 -- and the two things that
|
|
619
|
+
are refused are `series order=1000` and a truth table of 9+ symbols.
|
|
620
|
+
`logic` carries its own bound because it does not come through here
|
|
621
|
+
and because 2**n rows are predictable from the input, which is the H5
|
|
622
|
+
move; see `_MAX_TRUTH_TABLE_SYMBOLS` there.
|
|
623
|
+
|
|
624
|
+
REFUSE rather than truncate, per the owner's ruling: half a truth
|
|
625
|
+
table is a wrong answer presented as an answer, and `discrete_math`'s
|
|
626
|
+
H6 message and `isolation._child_main`'s own refusal are the wording
|
|
627
|
+
family this joins rather than a second convention.
|
|
368
628
|
"""
|
|
629
|
+
label = ("The %s result" % what) if what else "The result"
|
|
630
|
+
try:
|
|
631
|
+
rendered = _rendered(value)
|
|
632
|
+
except ValueError as e:
|
|
633
|
+
# The ONLY ValueError `_rendered` can raise: it does isinstance
|
|
634
|
+
# checks, container recursion and `str()`, and `str()` of a large
|
|
635
|
+
# Integer is the documented cap.
|
|
636
|
+
raise MathResultTooLarge(
|
|
637
|
+
"%s was computed but is too large to return (%s). Ask for the "
|
|
638
|
+
"same operation with smaller inputs." % (label, e)) from None
|
|
639
|
+
|
|
640
|
+
# The JSON length rather than the character count of the pieces: this
|
|
641
|
+
# is what the child actually writes, escaping included, so the number
|
|
642
|
+
# in the message is the number that would have been sent.
|
|
643
|
+
size = len(json.dumps(rendered))
|
|
644
|
+
ceiling = int(config.MAX_READ_CHARS)
|
|
645
|
+
if size > ceiling:
|
|
646
|
+
raise MathResultTooLarge(
|
|
647
|
+
"%s was computed but is too large to return (%d characters, "
|
|
648
|
+
"against a ceiling of %d). Ask for the same operation with "
|
|
649
|
+
"smaller inputs -- fewer series terms, a lower order, a "
|
|
650
|
+
"smaller matrix." % (label, size, ceiling))
|
|
651
|
+
return rendered
|
|
652
|
+
|
|
653
|
+
|
|
654
|
+
def _rendered(value: Any) -> Any:
|
|
655
|
+
"""`serialize` without the bounds. Separate so the size is measured
|
|
656
|
+
ONCE, on the finished structure, rather than on each recursion -- a
|
|
657
|
+
cap applied per level would pass a 4 MB matrix of one-character
|
|
658
|
+
cells."""
|
|
369
659
|
if isinstance(value, sympy.Matrix):
|
|
370
660
|
return [[str(cell) for cell in row] for row in value.tolist()]
|
|
371
661
|
if isinstance(value, dict):
|
|
372
|
-
return {str(k):
|
|
662
|
+
return {str(k): _rendered(v) for k, v in value.items()}
|
|
373
663
|
if isinstance(value, (list, tuple)):
|
|
374
|
-
return [
|
|
664
|
+
return [_rendered(v) for v in value]
|
|
375
665
|
if isinstance(value, bool):
|
|
376
666
|
return value
|
|
377
667
|
return str(value)
|