Skip to content

Ariadne: functions: shared with a document's programs and granted to its modules - #900

Merged
transfix merged 10 commits into
masterfrom
feat/ari-module-functions
Oct 31, 2026
Merged

transfix merged 10 commits into
masterfrom
feat/ari-module-functions

Conversation

@transfix

Copy link
Copy Markdown
Owner

The fourth ask of #894: functions a document defines once and hands to the documents it load:s. Stacked on #896 (ari test and tests:); its base is feat/ari-quit-assert-test, and it retargets to master when #896 merges.

Design

The problem. defun defines a function for one program. The stdlib scope is frozen. A load:ed module is chrooted to its own state subtree and reaches its parent only through link: holes. So shared logic had to be copied into every on: handler, and could not be tested on its own.

Declaring: a top-level functions: block. It is a scalar state_exec program made only of (defun name (params) body...) forms:

functions: |
  (defun semver-part (v i) (int (nth (string.split v ".") i)))
  (defun semver-major (v) (semver-part v 0))
  • The loader splits the block into top-level forms (strings and ; comments are respected) and parses each one.
  • A form that is not a defun, or does not parse, is skipped with a warning. When a name is defined twice, the later definition stands, with a warning.
  • Every program in the document's scope can call the functions: init:, on: actions, residents (on_tick: and the rest), key bindings, tests:, and the functions themselves. The read lane (bind:, visible_when:) keeps its reduced environment.

import: merges a library's functions, as it merges units:.

  • When two imports define the same name, the later one stands, with a warning.
  • The document's own definition replaces an imported one silently.
  • So a library of functions plus an ari test document that imports it is a unit test.

Granting: functions: on a load: entry. Next to link: (state) and channels: (messaging), this is the code grant:

- load: badge.ari
  as: badge
  link: {server: {to: args.server, mode: ro}}
  functions: [semver-major]     # a list, one name, or "*" for all
  • Granted names come from the parent's scope: its own functions, its imports', and those its own mount granted. So grants chain down nested mounts.
  • A granted function brings along, transitively, the parent's functions it calls, found by the same free-head scan as requires:. Those are callable in the module too.
  • A mount with no grant gives its module no functions: modules stay isolated by default.
  • A module's own functions: and imports are visible only inside the module. Replacing a granted name warns.

Code, not authority. A granted function runs in the module's program, chrooted to the module's sub-prefix. (state-set "out" ...) from a granted function writes includes.<as>.out, and the function reaches the parent only through the module's link: holes. Granting a function never grants state.

Mechanism: a per-scope prelude.

  • Widget::functions is a FunctionScope (the defs plus their sources joined into a prelude). It is set on the document root and on every mount wrapper, and is empty when the mount grants nothing.
  • The runtime keeps a function-scope stack beside the mount-scope stack during the walk. A queued action captures its scope's prelude, as it captures the prefix, and submit_action runs prelude + program.
  • Residents, key bindings and run_program use the document's prelude. Runtime::set_root records it at once, so a resident submitted before the swap still gets it.
  • init: and a mount's init: get their own scope's prelude.
  • Each program defines its functions in its own process: about one step per function against its budget. No closure, environment or heap is shared across processes or schedulers, which keeps memory accounting and isolation as they are.
  • The alternative, a shared environment of compiled closures, would share mutable closure environments across processes and break per-process heap measurement. It was rejected.

Checked at load. The free-head preflight (§5.7) is scope-aware.

  • A program may call its scope's functions.
  • A module calling one its mount didn't grant gets the usual "this host does not provide" warning, which makes ari test fail.
  • Function bodies are scanned as functions:, and a mount's init: is now scanned too.

Not in this PR (follow-ups from cypkg's findings):

  • a mounted module's own on_tick: (today only its init: runs);
  • subtree link: holes (a hole to an interior node exposing its descendants);
  • a parent calling a module's functions against the module's state.

Tests

  • ariadne_loader_test (AriadneFunctions.*):
    • splitting (comments, parens inside strings and comments), skipped non-defuns, duplicates, a non-scalar block;
    • import merging and precedence;
    • grants: a list, one name, "*", an unknown name, the transitive callees, an empty scope for an ungranted mount, a module redefining a granted name;
    • grants chaining through nested mounts.
  • ariadne_host_test (AriadneFunctions.*):
    • every lane calls the document's functions: init, on_tick, a button's on:, run_program;
    • a module calls a granted function from its init and its button, and the writes land in the module's subtree, not the parent's;
    • an ungranted module gets the preflight warning and a NameError at run time.
  • ari_test_command_test (AriTest.FunctionsImportedAndGranted): ari test on a document that imports a semver library, tests it directly, and mounts a module granted semver-major (which brings semver-part). Without the grant it fails at load.

Locally all 38 ariadne_* and state_exec_* suites pass, including ariadne_loader_test (207), ariadne_host_test (32), ariadne_runtime_test (200), ariadne_ftxui_test (66) and ari_test_command_test (7).

Docs: docs/ARIADNE.md §6.6 (new), §5.1, §6.3, §7.4 and §13; docs/STATE_EXEC_DEVELOPER_GUIDE.md §11.

- (quit [code]): AppRuntime::register_quit() binds `on: quit` and the verb; the
  first close request's code is exit_code(). ari run/snapshot/check and arigl (window
  and --terminal) exit with it; snapshot stops on the frame the document quit.
- (assert cond [message [data]]): a builtin raising AssertionError (a new class under
  Exception), "assertion failed[: message]"; its traceback ends at the form.
- tests: block (name -> program) captured by the loader; Runtime::run_program runs a
  program on the action lane and reports how it ended to a callback.
- ari test [--tap] [--timeout S] [--frames N] doc.ari...: each document headless until
  it quits, its tests have run, N frames, or the timeout. Fails on a non-zero quit, an
  uncaught error, a failed or warning load, a failed test, or a timeout. One line per
  document with what failed and in which program; TAP with --tap; exit 1 on failure.
  Network flags and settling as snapshot's.
- Action failure warnings name their program; resident ones their lane.
- Docs: ARIADNE.md §13, the exceptions table, the developer guide.
…s modules (#894)

- functions: (top level): a program of (defun ...) forms that every program in the
  document's scope can call (init:, on:, residents, key bindings, tests:); anything
  else is skipped with a warning. import: merges a library's functions.
- load: functions: [names] | name | "*": grants the parent's functions to the module,
  each with the functions it calls; a module gets none otherwise, and its own
  functions: stay inside it. Code, not authority: a granted function runs chrooted to
  the module.
- Mechanism: Widget::functions (FunctionScope) on the root and every mount wrapper; the
  runtime keeps a function-scope stack beside the mount scopes and runs the scope's
  prelude ahead of each program (actions, residents, run_program, init:, mount init:).
- The free-head preflight is scope-aware and scans function bodies and mount init:.
- Docs: ARIADNE.md §6.6.
transfix added a commit to cy-pca/cypkg that referenced this pull request Oct 30, 2026
lib.ari holds cypkg's logic as functions: (transfix/libcvc#900), pure:
reply parsing, the search query and status, failure messages, cutting,
size and date text, build rows and page bounds. main.ari imports it, and
its on_tick only moves values between those functions and state.

tests/lib/lib_test.ari tests each function with assert (15 tests, no
network). tests/run.sh runs it as layer 2b; CI merges #900 too and
checks the ari has functions:.
transfix and others added 5 commits October 30, 2026 12:54
Conflicts with the locks spawn context (#865) and --credential-env (#895): the sweep keeps
run_program's report and the spawned children's reap; ari test settles with AppRuntime::settle,
which replaced settle_async_calls; --timeout joins --credential-env among the value flags.
…illing

stepped_solve::impl did f.assign(nv, f[0]): the value is a reference into f, and libc++'s
assign frees the old buffer before filling the new one, so on macOS every entry was read
from freed memory (Fem.SteppedPoissonMatchesTheOneShotSolve, rel diff 6.7e268 in the macOS
recipe lanes). libstdc++ and MSVC copy the value first, so Linux and Windows passed.
Takes #903's fix for the stepped Poisson solve in place of the same fix made here.
Base automatically changed from feat/ari-quit-assert-test to master October 31, 2026 02:11
#896 landed on master squashed (2d8cf8a), so this branch's own change is b83e92d's diff
from #896's last head a26d53a. That diff applies to master unchanged: 10 files, the
functions: work alone.
@transfix
transfix enabled auto-merge (squash) October 31, 2026 02:38
@transfix
transfix merged commit b2e381a into master Oct 31, 2026
27 of 53 checks passed
@transfix
transfix deleted the feat/ari-module-functions branch October 31, 2026 05:12
transfix pushed a commit that referenced this pull request Oct 31, 2026
#900 landed on master squashed (b2e381a), so this branch's own change is 657290a's diff from
#900's last head b83e92d. It applies to master unchanged: the module on_tick work alone.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant