Repository navigation
pycvc: state_exec parity part 3 (evaluators, processes, policy, intrinsics) - #675
Merged
Merged
Conversation
… P0) Declare the state_exec s-expression grammar in docs/STATE_EXEC_GRAMMAR.md (W3C EBNF, Unicode source rules, literal tables, and a table of every intentional change from the hand-written parser, tagged by phase P2-P4). The parser itself is unchanged; later phases build it from this grammar. state_exec_grammar_test tags each case with the phase it holds from: landed cases must hold, pending cases must still differ, the doc's EBNF must be closed, and every production needs a case. state_exec_corpus_test pins how the 597 DSL scripts the repo carries parse today (state_exec_corpus/, regenerated with gen_state_exec_corpus.py and CVC_UPDATE_GOLDENS=1). Also correct parser.h (no quasiquote) and the developer guide (false and #f read as nil, bytes literals exist).
Spec only: parse_options::max_depth on parse()/parse_all()/parser and execute_options::max_parse_depth for submitted scripts, both from P2. Records why the limit exists (the reader recurses on the native stack; the evaluators are stackless) and the corpus's real depths (max 13, median 2).
Spec only. max_depth resolves from state_exec.schedulers.<id>.parser.max_depth, then state_exec.defaults.parser.max_depth, then 256, matching scheduler::load_settings(); parse_options remains for callers without a tree. Drops the execute_options::max_parse_depth idea so the tree is the single source.
…grammar P1b) The parser now stops at parser.max_depth nested lists and quotes (default 256) with a parse_error, instead of recursing until the native stack overflows. Like the scheduler's other settings, the limit lives on the state tree: state_exec.schedulers.<id>.parser.max_depth, then state_exec.defaults.parser.max_depth, then 256; a missing, malformed or zero value is skipped. - parser: parse_options (max_depth) on parse()/parse_all()/parser, a depth guard on lists and quotes, and parse_options_from_state(). - scheduler: load_settings() resolves parser.* and publishes the effective value under state_exec.schedulers.<id>.parser.max_depth; execute() parses with it. - async_scheduler (the app's exec scheduler) reads the defaults key on every submit; Ariadne predicates read it from the app tree. - stackless evaluators: create_state(script, env, parse_options). The grammar test moves from a numeric kLandedPhase to named phases; the 257-deep case is P1b and holds now. Tests cover the default, a custom depth, quotes, 100000 open parens, the error position, tree resolution, bad values, and both schedulers.
…e read) Lists and quotes were parsed by native recursion, so input depth was bounded by the thread's stack. The parser now keeps open lists and quotes on a heap stack of frames and reads in a loop: native stack depth is constant however deeply the input nests (a test reads a million open parens and reports the unclosed list instead of crashing). The loop is a resumable read(out, budget): it reads up to `budget` tokens and returns datum / end / more, carrying open frames between calls, so a scheduler can parse a script a slice at a time. parse() and parse_all() call it with no budget. Output is unchanged, quirks included (a stray top-level `)` still reads as a symbol until P2): the conformance cases, the 597-script corpus and the evaluator suites all pass. parser.max_depth stays on the state tree; it now bounds parsed data for the code that still walks values recursively (list destruction, the snapshot codec, to_string) rather than the parser's own stack. parse_fib20_expr: about 3.4 us vs 3.1 us for the recursive parser (median of 6, MSVC Release, loaded machine).
The stackless parser's state is an input position plus a heap stack of open frames, so it can pause anywhere. New setting parser.step_tokens (on the state tree, resolved like parser.max_depth; default 0) lets a scheduler parse a submitted script inside the new process instead of at submit: each scheduler step reads N tokens, and the step that reaches the end sets up evaluation exactly as parsing up front would have. - Reading steps are process steps: they count against max_steps and max_time and interleave with other processes. - A syntax error surfaces like a runtime error: the async scheduler kills the process with it as the exit error; the sync scheduler throws it from step()/run(). With step_tokens = 0, execute() still throws. - Signals and watch events wait until the script is read (their handlers run on the evaluation stack, which doesn't exist yet). - The snapshot codec stores a mid-read process's source and settings and re-reads it after a restore. Adds parse_options::step_tokens, script_reader (owns its source and drives parser::read() a budget at a time) and evaluator_state::reading; stackless_evaluator::step() reads until the script is read. Tests cover the reader, the evaluator, the codec and both schedulers (results, syntax errors, max_steps, deferred signals).
…ens) app::exec_scheduler(), which runs Ariadne's actions and residents, now defaults parser.step_tokens to 1024, so it parses submitted scripts inside their processes. async_scheduler gains set_parser_defaults(): the built-in values under its watch root's state_exec.defaults.parser.* keys. A plain async_scheduler (pycvc's Exec) and the sync scheduler stay at 0, so they still report syntax errors at submit. Why 1024: every script the repo carries reads in one slice (the largest is 159 tokens, .ari scripts at most 24), so a normal action pays one extra step of its 5000; a slice costs about 80 us at the parser's measured ~80 ns/token, so a pathologically large script is spread over steps and frames instead of stalling a drain. Ariadne now says why a process died: a killed action's warning carries its exit error, and the per-frame sweep reports a killed resident (a syntax error in its body, a runtime error, its per-step cap) once, instead of letting it vanish silently.
…uler
pycvc's Exec runs programs on a private async_scheduler that already
watches the app's state tree, but its built-in parser defaults were
plain parse_options{} while app::exec_scheduler() read scripts in
1024-token steps. Both now take async_scheduler::host_parser_defaults(),
one definition (with the reasoning for 1024) for every libcvc host, and
the app tree's state_exec.defaults.parser.* keys override both.
Exec.run() still raises a RuntimeError with the parse error's message:
from the killed process's exit error when the script is read in steps,
or from execute() when the tree sets step_tokens to 0.
Tests: test_pycvc_exec.py checks the parse error's message and that
max_depth / step_tokens on the app tree configure Exec; the integration
test pins the app scheduler to host_parser_defaults().
pycvc's state_exec surface was only Exec; the parser settings these PRs add had no Python counterpart, so the defaults weren't even observable. In pycvc's direct-wrap style (the real C++ types, no facade): - pycvc.parse_options: the real cvc::state_exec::parse_options struct (max_depth, step_tokens), C++ defaults included. - pycvc.async_scheduler.host_parser_defaults(): the real static, declared to SWIG on its own (async_scheduler isn't wrapped yet, so no constructor). - pycvc.parse_options_from_state(app, scheduler_id="", base=...): the C++ resolver, taking the app where C++ takes the root (pycvc's convention). - Exec.parser_defaults() / Exec.set_parser_defaults(): the same calls as the async_scheduler Exec wraps. test_pycvc_exec.py covers each against the C++ behaviour; the README gains a state_exec section. Built locally (SWIG 4.4.1, Python 3.11): exec, exec_async, exec_coro and ari tests, 26 passed.
Wrap the real cvc::state_exec::parse_options_from_state(state&, id, base) instead of a pycvc adapter that took the app: Python passes the tree root, pycvc.State.instance(app), exactly as C++ passes cvc::state&. This is the first decision of the pycvc/C++ parity approach (C++ cvc::state& parameters take a pycvc.State in Python).
…codec) Start bringing pycvc's state_exec API to the C++ surface under the C++ names (docs/PYCVC_STATE_EXEC_PARITY.md: the audit of all 20 state_exec headers, the agreed approach, and the PR plan). - One strict value_t <-> Python conversion (pycvc_value.h), used by every API and by Exec: native types for data, pycvc.symbol (the real struct) for symbols, and pycvc.value (the real value_t, opaque) for closures, native fns, data objects and generators, which convert back unchanged. Non-str dict keys and unsupported types raise TypeError, ints beyond 64 bits OverflowError, and nesting past 1000 levels or a cycle ValueError. This replaces the lossy converter (symbols became str, closures "<closure>", anything else str(obj)), which could also put a null item in a list on invalid UTF-8 and leave an OverflowError pending. - pycvc.parse / parse_all, pycvc.parse_error (a RuntimeError with .line and .col), pycvc.script_reader, to_string, values_equal, and the value codec encode_value / decode_value on a State node. - Exec.run raises pycvc.parse_error for a syntax error whether the script was parsed at submit or read inside the process (it re-parses a killed process's script to recover the position). - The pycvc README's Status/Design sections described a v0 facade; they now describe the direct-wrap design. test_pycvc_state_exec.py (13 tests) covers each against the C++ behaviour. Locally: those plus pycvc's exec, async, coro, ari, state and core suites pass (87), except test_volume_rawiv_roundtrip, which fails the same way on the unmodified build (a Windows temp-file lock).
Wrap the scheduler surface of cvc::state_exec under the C++ names: - environment (shared, as environment_ptr) and builtins: make_default_environment, and register_fn taking a Python callable or a pycvc.value holding a native fn. - execute_options (on_complete is a Python callable, signal_handlers a dict), process_info, scheduler_stats, percent_encode/percent_decode. - process_status and scheduling_policy as enum.IntEnum classes; an enum argument must be a member, so to_string(process_status) and to_string(value_t) keep their shared name. - scheduler and async_scheduler as real classes with their non-coroutine members. run/step and sync_run/sync_step release the GIL; Python callbacks retake it. - Exec.scheduler() returns Exec's async_scheduler, which keeps the Exec alive. Empty optionals are None, except for values: get_result, pop_pending_message and environment.lookup raise KeyError, since None is nil. The value_t typecheck now runs last, so execute(str) picks the script overload. test_pycvc_state_exec_sched.py repeats the C++ scheduler tests on both classes. The parity doc and README are updated.
…its comment is fixed
…nsics) Wrap the rest of cvc::state_exec's public surface under the C++ names: - stackless_evaluator and evaluator, with evaluator_state, eval_frame, eval_phase (an IntEnum) and evaluation_stats; evaluation_interrupted and evaluation_timeout as RuntimeError subclasses. The evaluators release the GIL while they evaluate, so another thread can interrupt, pause or resume them. on_complete is a Python callable. - The process struct (shared, as process_ptr), watch_event and watch_handler_entry, make_process, and the scheduler calls that take them: current_process() and queue_watch_event(). scheduler_base is the common base of the two schedulers. - encode_/decode_environment and encode_/decode_evaluator_state. - resource_policy (mode as an IntEnum), process_limits, validate_limits. - intrinsics_context, channel_policy, register_intrinsics, apply_chroot, resolve_channel_key, make_future, future_channel_of. Every function register_intrinsics adds keeps its context alive, and the context keeps the scheduler and channel policy assigned to it. - From #660: kDefaultMaxAllocBytes, eval_alloc_limit, eval_alloc_check, alloc_bytes, scheduler.max_alloc_bytes(), max_alloc_bytes_from_state. A reference into an object (evaluator_state.stats, evaluator.stats(), process.state) keeps its owner alive; other members read and write as copies. evaluator_state is move-only, so it is moved into Python. test_pycvc_state_exec_eval.py repeats the C++ tests. The parity doc, whose inventory now includes what landed on master since the audit, and the README are updated.
… data_options wrap)
…rap #658's data settings and budget fields on the schedulers
…rap #658's step_kind and the per-kind stats
…-state-exec-parity-3
transfix
changed the base branch from
feat/pycvc-state-exec-parity-2
to
master
October 18, 2026 05:20
transfix
enabled auto-merge (squash)
October 18, 2026 05:27
transfix
added a commit
that referenced
this pull request
Oct 18, 2026
…p-finish Conflicts: STATE_EXEC_GRAMMAR.md's native-calls list (state-json/state-from-json from #681, state-set here: both kept), and the tests #681/#682 and this branch each added to state_exec_data_steps_test.cpp and state_exec_deep_data_test.cpp (all kept). #675's pycvc tests asserted the old parser.max_depth default of 256; they now compare with pycvc.parse_options().max_depth, as agreed with the parity session.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Targets master. #661 and #671 (parts 1 and 2) merged as 50c6d02 and 1087b09, and master is merged in (5e59800), so the diff here is part 3 only. #679 (part 4) is stacked on this PR.
Part 3 of bringing pycvc's state_exec API to the C++ surface, under the C++ names. With it, everything in
docs/PYCVC_STATE_EXEC_PARITY.md's inventory is wrapped or listed as an exception with its reason, exceptstdlib_registryand the API that #658, #667 and #665 add (PR 4).This PR
The evaluators:
pycvc.stackless_evaluatorandpycvc.evaluator.evaluator_state,eval_frame,eval_phase(anIntEnum) andevaluation_stats.evaluation_interruptedandevaluation_timeoutareRuntimeErrorsubclasses.interrupt(),pause()andresume().on_completeis a Python callable.restrict_special_formstakes any iterable of str, or None;all_special_forms()returns a set.pycvc.process: shared, asprocess_ptr.watch_eventandwatch_handler_entry(alsoprocess.watch_event, ...) andmake_process().current_process()andqueue_watch_event().stateis a reference into the process, andinboxreads as a list.steady_clocktime points aren't wrapped;elapsed_time()andaccumulated_timecarry the same information.pycvc.scheduler_base: the common base of the two schedulers, which is whatintrinsics_context.schedholds.The codec:
encode_environment/decode_environmentandencode_evaluator_state/decode_evaluator_state.pycvc.resource_policy: itsmodeispycvc.resource_policy.mode, anIntEnum. Alsoprocess_limitsandvalidate_limits.The intrinsics:
intrinsics_context,channel_policy(globalisglobal_, sinceglobalis a Python keyword) andregister_intrinsics;apply_chroot,resolve_channel_key,make_futureandfuture_channel_of.From state_exec: load the stdlib modules in every environment, with a per-call allocation limit #660, which landed after the audit:
kDefaultMaxAllocBytes,eval_alloc_limit,eval_alloc_checkandalloc_bytes;scheduler.max_alloc_bytes()andmax_alloc_bytes_from_state.A Python function a program calls runs on the stepping thread, where the step's limit is armed, so it can check the limit the way a builtin does.
Lifetimes (approach items 10 and 11 in the doc)
evaluator_state.stats,evaluator.stats()andprocess.state. Other members read and write as copies.register_intrinsicsholds its context. C++ requires the caller to keep the context valid while the environment is used. In Python, each function it adds holds the context, and the context holds the scheduler and channel policy assigned to it. A test drops the context and the scheduler, collects garbage, then runs the environment's intrinsics on another scheduler.evaluator_stateis move-only (its stats hold a mutex). SWIG returns objects by copy, so a typemap moves a returned state into its Python object.Not wrapped, with reasons in the doc
eval_alloc_guard: RAII that must nest last-in first-out, which a Python object's lifetime doesn't promise.return_exception: the internal unwinding of(return ...).document_scope: its slots are C++ templates.park_on_channel: it takes raw process pointers and is valid only inside a running native function.tracker,documentandwatches.Tests
bindings/pycvc/test_pycvc_state_exec_eval.py(18 tests, registered with CTest) repeats the C++ tests instate_exec_stackless_evaluator_test.cpp,state_exec_evaluator_test.cpp,state_exec_codec_test.cppandstate_exec_intrinsics_test.cpp. Among them:PauseResumeProducesCorrectResultdoes);current_process()seen from a Python function during a run;max_alloc_bytesfrom the tree, and the step's allocation limit seen from a Python function;validate_limits;Local results (Windows MSVC 14.38 Release, SWIG 4.4.1, Python 3.11, cvcpkg prefix, on the merged master):
test_pycvc_state_exec_eval.pytest_volume_rawiv_roundtrip, the Windows file lock #663 fixes (not in this stack)pycvc.iclang-format 18 is clean on the C++ files, and
ruff(E, F, W) is clean on the new test.pycvc_gl: the build (cvcGL, pycvc and
pycvc_gl, VTK-Python bridge off) succeeds, with the same SWIG warnings as #661's GL build.test_pycvc_gl.py: the volume scattering default;test_pycvc_gl_world.py: the grid node's bounds;test_pycvc_gl_lod.py: an access violation;test_pycvc_gl_streaming.pyandtest_pycvc_gl_clip.py: heap corruption.Since opening
data_defaults,set_data_defaults,data_settings) andprocess_info's steps and times by kind;step_kind(anIntEnum) andevaluation_stats' counts by kind (steps,step_time,data_nodes,last_step_kind,add_*), plusevaluator_state.data_step_nodes;data_work/backlog/pending_data_workare listed as exceptions: deep-data work in flight between steps.test_pycvc_state_exec_eval.pypass, including the newtest_stats_by_kind_of_step;test_pycvc_lod's two GIL-timing tests;Coordination
Agreed with the sessions working on #658, #667 and #665: their chain lands first. Then:
I merge master into pycvc: state_exec parity part 1 (values, parsing, parse_error, value codec) #661, pycvc: state_exec parity part 2 (environments, builtins, schedulers) #671 and this PR, resolving the
pycvc.i/pycvc_execconflicts with state_exec: deep-data work a slice per step, and the budget by kind #658'sdata_optionswrap.PR 4 wraps the API they add, from the inventories they sent:
data_job.h,step_kindandevaluation_stats.steps(),process_info's per-kind steps and times, and the data defaults;heap.h, and the heap fields onprocess/process_info;data_job.hadditions.PR 4 also wraps
stdlib_registry.