You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 5a77a70
Browse filesBrowse the repository at this point in the historyBrowse files
feat(cli): the hook-body extractor infers api.transaction, and api.read through a bound object handle (#38686)
<!-- claude-projects-attribution: {"github_login":"os-steve"} -->
_Requested by **Steve** · [project
thread](https://claude.ai/code/project/chan_01FdL1XeUyvVNxLreh3F4Qi9?thread=cmsg_01FdL1XeUyvVNxLreh3F4Qi9JeHmqv6CKCkW8ewrPBLekk)_Fixes#38653
Clause-②: yes (widening)
Metadata-protocol: none
## 维护者速读
-
用户能看到什么变化:应用作者把内联处理函数交给构建后,调用事务或通过对象句柄读数据的处理函数,构建时会自动获得对应权限,运行时不再被沙箱拒绝。
- 为什么做:属于“处理函数能力推断准确”这一族工作,替所有用内联函数写钩子与动作的作者解决运行时莫名被拒的问题。
- 出错会怎样:最坏是某个处理函数被多授予一个只开事务、不含读写的权限;改动只在构建期推断,撤回即可恢复原样。
- 要你做什么:无。
## What changes
`os build` lowers an inline hook `handler` / action `target` function to
a `js` body and infers its `capabilities`
(`packages/cli/src/utils/extract-hook-body.ts`). Two gated calls were
never inferred, so a lowered handler making them was refused at the call
by the sandbox:
1. **`api.transaction`** — a new `CAPABILITY_PATTERNS` entry for a call
of `.transaction` (`?.` allowed on either side) and for the member
destructured off its receiver (`const { transaction } = ctx.api`, nested
`const { api: { transaction } } = ctx`). Measured against the runner:
`installTxLeaf` gates `__txBegin` / `__txCommit` / `__txRollback` behind
`api.transaction`, and the VM sugar `ctx.api.transaction` closes over
the context and reads no `this`, so every receiver that reaches
`ctx.api` (the context itself, a root alias such as `const api =
ctx.api`, a destructured `api`, or the destructured member called bare)
hits the same gate. These are the shapes the hook-face refusal of
`ctx.transaction` (the `exemptCalls` limb) already leaves standing as
"the body's transaction route". Receiver-loose like the `.object(...)`
patterns: `api.transaction` is no door and grants no data access, so an
over-inferred grant opens nothing the body does not call.
2. **`api.read` through a bound handle** — the bound-handle walk
`callsObjectWriteThroughHandle` becomes
`callsObjectLeafThroughHandle(source, leaves)`, asked once for the write
leaves (unchanged) and once for the read leaves
(`callsObjectReadThroughHandle`, new). The read leaves are the direct
`api.read` pattern's own (`find|findOne|count|aggregate|get|list`, now
named once as `OBJECT_READ_LEAVES`); the runner's `READ` list (`find`,
`findOne`, `count`, `aggregate`) adds none. The twin also covers the two
other shapes the direct read regex missed: `?.` before the leaf and an
object argument holding a call.
3. **The published union** — `ExtractedBody.capabilities` on
`@objectstack/cli/hook-body` widens to seven tokens, `'api.transaction'`
added: exactly the `HookBodyCapability` set of `@objectstack/spec`.
Contract face: this is a published type's accept set (a widening;
nothing accepted before is refused), so the PR stays draft for its
contract-review-tier review.
4. **Carrier note N4** — a CLI-side pin that `lowerActionCallable` still
leaves a non-empty string `target` beside the minted `body` record
(top-level and object-owned), the shape `@objectstack/lint`'s
`isLoweredTargetBody` keys on.
5. **Docs** — the `### Capability inference` table in
`content/docs/automation/hook-bodies.mdx` gains two rows:
`*.transaction(…)` (with the alias, `?.` and destructured-member
spellings the pattern reads) ⇒ `api.transaction`, and the read leaves on
a bound handle ⇒ `api.read`. The sentences beside the table stay true as
written.
Changeset: `.changeset/38653-extractor-transaction-handle-read.md`,
`@objectstack/cli` patch, the level `Metadata-protocol: none` takes (a
widened published type with no change to what a metadata author can
write).
<details><summary>Read first (risk order)</summary>
1. `packages/cli/src/utils/extract-hook-body.ts` — the `api.transaction`
entry in `CAPABILITY_PATTERNS` (the receiver-loose regex and its
destructure limb) and `callsObjectLeafThroughHandle` (the write walk is
byte-identical apart from the leaves parameter).
2. `packages/cli/test/published-subpath-hook-body.pin.test.ts` — the one
line that widens the ratified union.
3. `packages/cli/test/extract-hook-body.test.ts` — the new pins and
their controls.
</details>
## Tests
- `test/extract-hook-body.test.ts`: the `verify-delete-behaviors`
control hook's body verbatim infers `['api.read', 'api.write']`;
`api.read` through a bound handle, a handle off an alias of `ctx.api`,
`?.` before the leaf and an argument holding a call; a write-only bound
handle stays `['api.write']` (control); `api.transaction` through
`ctx.api.transaction`, a root alias, `?.` on both sides, and the
destructured member; a record field named `transaction` infers nothing
(control).
- `test/lower-callables.test.ts`: the N4 shape pin.
- `test/published-subpath-hook-body.pin.test.ts`: the consumer compiled
against the packed `.d.ts` asserts the seven-token union.
<details><summary>Ablations (one per new behaviour, each through
`scripts/ablation-replace.mjs`, mutation proven on disk and restored to
the HEAD blob)</summary>
- Remove `if (callsObjectReadThroughHandle(source))
inferred.add('api.read');` — 5 failed / 73 passed (the five `api.read`
cases red, both controls green).
- Turn the `api.transaction` entry's token into `log` — 4 failed / 74
passed (the four `api.transaction` cases red).
- Delete `action.target = ref;` in `lowerActionCallable` — 2 failed / 31
passed (both N4 cases red).
- Put the pin's union back to six tokens — 1 failed / 14 passed:
`conformance.ts(17,3): error TS2344: Type 'false' does not satisfy the
constraint 'true'.`, so the pin reads the rebuilt, packed `.d.ts`.
</details>
## Acceptance notes
- **Done-when 3: can `validate-action-body-capabilities.ts` now judge
lowered bodies for the non-write tokens?** Measured, no `packages/lint`
change here. Population: every first-party config (`examples/app-crm`,
`app-multi-package`, `app-showcase`, `app-starter`, `app-todo`,
`packages/apps/connector-docusign`, `connector-github`,
`connector-stripe`), each loaded, normalized and lowered, then read with
the rule's own `isLoweredHandlerBody` / `isLoweredTargetBody` and
`bodyGatedCalls`. `packages/platform-objects` and
`packages/apps/{account,setup,studio}` carry no inline callable (their
hooks are installed on the engine in-process). Result: 1 lowered hook
body (`crm_opportunity_stage_probability`, no gated call), 0 lowered
action bodies, **0 bodies newly refused** with the lowered skips
removed. The `verify-delete-behaviors` fixture (a test fixture, outside
those trees) goes from one refusal (`api.read`) to none. So for
`api.read`, `api.write` and `api.transaction` the extractor now reads
every shape the rule reads. It does not yet for the member tokens: a
body-local alias the rule reads (`const l = ctx.log; l.info(…)`, `const
{ log } = ctx`, `const { crypto } = ctx`, `const a = ctx.activity`, a
hook's `const t = ctx.title; t(FIELD)`) infers nothing, because the
extractor's patterns for those members spell `ctx.` and its rebind
covers only the handler's parameter. Dropping the skips now would refuse
such a lowered body naming a grant the author never wrote. That is
follow-up #38690: the extractor first reads those body-local aliases,
then the rule drops its lowered skips.
- The union could be typed as `HookBodyCapability` from
`@objectstack/spec` (it is exactly the seven tokens), but this PR keeps
it restated as ordered: typing it from spec would make the published
`.d.ts` import spec types, and the conformance consumer in the pin,
which installs only the packed CLI and `typescript`, would need
`@objectstack/spec` linked too.
- The destructured-leaf call `transaction(fn)` and a nested `const {
api: { transaction } } = ctx` are inferred but not read by the lint rule
(a missed finding there, never a false one).
## Verification
Two-minute check: `pnpm --filter @objectstack/cli exec vitest run
--maxWorkers=2 test/extract-hook-body.test.ts
test/lower-callables.test.ts
test/published-subpath-hook-body.pin.test.ts`. Expect 3 files and 126
tests passed.
Readings at head `a6f711ac84` (the source is unchanged since
`6c67329b6a`; the later commits touch only the changeset, where
`ffbc961a01` moves its level to `patch`, and the docs page,
`c8373cd352`):
<details><summary>Build, typecheck and tests (local)</summary>
- `pnpm --filter '@objectstack/cli^...' build` then `pnpm --filter
@objectstack/cli build`, under the verify lock: `VERDICT command-exit
0`.
- `pnpm --filter @objectstack/cli typecheck`: `VERDICT command-exit 0`
(`tsc --noEmit` plus `check:test-typecheck: OK`).
- The three touched files: Test Files 3 passed (3), Tests 126 passed
(126).
- The whole `unit` project, `pnpm --filter @objectstack/cli exec vitest
run --maxWorkers=2 --project unit`: Test Files 450 passed (450), Tests
6451 passed (6451), `VERDICT command-exit 0`. The `integration` project
is left to CI.
</details>
<details><summary>Derived gates (local subset)</summary>
`node scripts/pm/dispatch-gates.mjs --commands --repo
objectstack-ai/objectstack` derived 69 families for the 5 changed paths;
all 69 run, each exit code captured before any pipe. 68 exited 0. `pnpm
check:dual-build-cjs-loads` exited 3 (PREREQUISITE NOT MET: it reads the
dist of packages outside this build closure), so it is NOT MEASURED here
and is CI's. Reconciliation: `dispatch-gates --ran`: 69 derived families
accounted for, 68 run, 1 NOT-MEASURED (1 DERIVED from a recorded exit
3).
</details>
<details><summary>Docs gates for the page (local, at
c8373cd)</summary>
Adding the page makes `dispatch-gates --commands` derive 31 more
families (100 in all). All 31 ran at `c8373cd352`, each exit code
captured before any pipe, and all 31 exited 0. `pnpm --filter
@objectstack/spec run check:skill-examples` first exited 3 (PREREQUISITE
NOT MET: no client-react dist); after the client-react closure was built
it exited 0. Reconciliation over all 100: `dispatch-gates --ran`
accounts for every family, 99 run and 1 NOT-MEASURED
(`check:dual-build-cjs-loads`, exit 3 in the first round).
</details>
---
_Generated by [Claude
Code](https://claude.ai/code/session_01ADHkmSwS4mQyXWrddL4r6B)_
<!-- signed: repo:objectstack#3 · os-steve ·
session_01ADHkmSwS4mQyXWrddL4r6B -->
---------
Co-authored-by: Claude <noreply@anthropic.com>
feat(cli): a lowered hook or action handler is granted `api.transaction` when it calls `ctx.api.transaction(…)`, and `api.read` when it reads through a bound object handle
6
+
7
+
Clause-②: yes (widening)
8
+
Metadata-protocol: none
9
+
10
+
`os build` lowers an inline hook `handler` or action `target` function to a `js` body and infers the body's `capabilities` from the calls in its source. Two calls the sandbox gates were never inferred, so a lowered handler making them was refused at the call (403 INSUFFICIENT_PRIVILEGES) on every run:
11
+
12
+
-`ctx.api.transaction(fn)` — now infers `api.transaction`, whether it is called on `ctx.api`, on a local bound to `ctx.api` (`const api = ctx.api`), with `?.`, or after destructuring the member (`const { transaction } = ctx.api`). `api.write` stays its own grant, inferred from the writes inside the callback.
13
+
- a read through a bound handle — `const rows = ctx.api.object('x'); … rows.find(…)` — now infers `api.read`, the same way a write through a bound handle already inferred `api.write`. The other read shapes the direct pattern missed (`?.` before the leaf, an object argument holding a call) are read too.
14
+
15
+
`ExtractedBody.capabilities`, published from `@objectstack/cli/hook-body`, widens to include `'api.transaction'` (seven tokens, the `HookBodyCapability` set of `@objectstack/spec`). A dependent with an exhaustive `switch` over the union gains one case to handle; nothing that was accepted before is refused.
|`*.transaction(…)` — `ctx.api.transaction(…)`, the same call on an alias of `ctx.api` (`const api = ctx.api; api.transaction(…)`), `?.` allowed on either side, or the member destructured off it (`const { transaction } = ctx.api; transaction(…)`) |`api.transaction`|
456
458
|`ctx.crypto.randomUUID`|`crypto.uuid`|
457
459
|`ctx.log.info / warn / error / debug`|`log`|
458
460
|`*.title(<argument>)` — the related-record form only; bare `ctx.title()` names this record and needs no token |`api.read`|
0 commit comments