test: run action and hook bodies through the real QuickJS sandbox (#575 A1) - #576
Merged
Merged
Conversation
The action side had no sandbox harness. `test/global-actions.test.ts` ran each `body.source` through `new Function`, which proves what a body computes and is structurally blind to everything the runtime imposes on it: no module scope, a JSON-only boundary, a capability gate, and a `ctx.api` built by the engine rather than by the author. So the one constraint this repo relies on by name — the shared factory in `src/objects/_line-item-price-fill.ts` is safe only while its handler body never reads a factory parameter — lived in a comment, with nothing anywhere failing if it were violated. `test/helpers/action-sandbox.ts` runs nothing of its own: it hands bodies to the same `QuickJSScriptRunner` + `actionBodyRunnerFactory` / `hookBodyRunnerFactory` the runtime wires at boot, lowered by the same `extractHookBody` pass the CLI build uses. The only stand-in is the engine underneath — an ObjectQL-shaped recorder, deliberately without `.object()` so the runtime builds the repo facade itself and a body meets the real `update(data, options)` signature. Its two hand-written rules are pinned against a real ObjectQL on the in-memory driver, the way `hook-query-predicate.test.ts` pins the read predicate. `test/action-sandbox.test.ts` then executes all seven script action bodies, shows the capability gate denying an undeclared capability, shows a body has no module scope, and turns the price-fill constraint into a guard with a negative control — plus the generalisation the build only mentions in passing: all 24 callables still lower to metadata-only bodies, and a hook that stops doing so is silently bundled instead, not caught. `global-actions.test.ts` keeps its assertions and switches to the real executor. One finding it surfaced is recorded, not fixed: `mass_update_stage` calls `update(id, doc)`, which the engine facade rejects on every invocation (#508 already documents the action as having no live invocation path). Refs #575. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ku5mr51i1UNcbq9VNbaNQb
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
yinlianghui
marked this pull request as ready for review
July 31, 2026 15:01
Merged
15 tasks
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.
Description
Issue #575 item A1 only — the action-sandbox test gap.
The action side had no sandbox harness.
test/global-actions.test.tsran eachbody.sourcethroughnew Function, which proves what a body computes and is structurally blind to everything the runtime imposes on it: no module scope, a JSON-only boundary, a capability gate, and actx.apibuilt by the engine rather than by the author. So the one constraint this repo relies on by name — the shared factory insrc/objects/_line-item-price-fill.tsis safe only while its handler body never reads a factory parameter — lived in a comment, with nothing anywhere failing if it were violated.test/helpers/action-sandbox.tsruns nothing of its own: it hands bodies to the sameQuickJSScriptRunner+actionBodyRunnerFactory/hookBodyRunnerFactorythe runtime wires at boot, lowered by the sameextractHookBodypass the CLI build uses. The only stand-in is the engine underneath — an ObjectQL-shaped recorder, deliberately without.object()so the runtime builds the repo facade itself and a body meets the realupdate(data, options)signature. Its two hand-written rules are pinned against a real ObjectQL on the in-memory driver, the waytest/hook-query-predicate.test.tspins the read predicate.Verified against
main@3ed27b26before starting: the gap still existed.Type of Change
_line-item-price-fill.tsnow names its guard)Related Issues
Related to #575 (item A1 only — B-group items untouched, A2–A4 untouched)
Changes Made
test/helpers/action-sandbox.ts(new) — real QuickJS harness:runActionBodybuilds the exactactionCtxboth dispatch paths in@objectstack/runtimebuild (params: { ...params, recordId, objectName }, and noobjectkey, soctx.objectisundefinedin the sandbox);runHookBodyexecutes a hook's shipped lowered body rather than its handler closure;extractSandboxBodyis the CLI build's own lowering pass;makeSandboxEngineis the recording engine.test/action-sandbox.test.ts(new, 52 tests) —updatecontract asserted against a real ObjectQL on the in-memory driver, then the same two calls made through the runtime's facade inside QuickJS;input;clone_opportunity,mark_primary,create_campaignandsend_email;ReferenceErrors in the VM);test/global-actions.test.ts— same 18 assertions, now executed by the real sandbox instead ofnew Function.src/objects/_line-item-price-fill.ts— the sandbox note points at its guard, and records why a test has to do this: when extraction fails the build does not — it silently keeps the handler bundled and the hook stops being deployable as pure metadata.Finding recorded, not fixed
mass_update_stagecallsctx.api.object(...).update(id, { stage }), butctx.apiis the engine repo facade, whoseupdatetakes(data, options)— the id lands in thedataslot and the engine rejects the write withUpdate requires an ID or options.multi=trueon every invocation. Nothing before this harness could say so: the metadata is valid, the body parses, and #508 already documents the action as having no live invocation path. Fixing a shipped action is outside A1, sotest/action-sandbox.test.tsasserts the current behaviour under a test name that says what it is; whoever revives the action will land on that assertion.Same shape, larger and out of scope here:
_hook-api.tsdocumentsupdate: (id, doc)and nine hooks call it that way, while the kernel'sScopedContext/ repo facade both take(data, options). Worth its own issue.Testing
pnpm verifygreen end to end (validate → typecheck → lint → hygiene → build → test): 532 passed, 2 skipped, 26 filesquickjs-emscriptenarrives via@objectstack/runtime, already a direct dependency; lockfile untouchedChecklist
.changeset/real-quickjs-action-sandbox-harness.md)test/metadata-references.test.tsRefs, notCloses— this PR is one item of Follow-up to #514: remaining behaviour fixes, conventions, and the action-sandbox test gap #575.object(), why extraction failure is silent at build time)Additional Notes
Cost: ~50 ms per sandboxed run (a fresh WASM context each time, as the runtime does). The suite went from 3.6 s to 9.3 s wall clock overall.
Convention 5 from the issue — re-run
pnpm verifyonmainafter merge — still applies.Generated by Claude Code