Skip to content

Commit cb99e2d

Browse files
fix(lint): the record-write-discarded hint prescribes a write the sandbox installs; app-dependencies lists the closure-judged bindings (#38628)
<!-- claude-projects-attribution: {"github_login":"os-steve"} --> _Requested by **Steve** · [project thread](https://claude.ai/code/project/chan_01FdL1XeUyvVNxLreh3F4Qi9?thread=cmsg_01FdL1XeUyvVNxLreh3F4Qi9JeHmqv6CKCkW8ewrPBLekk)_ Refs #37815 (items c, d) Clause-②: no Metadata-protocol: none ## 维护者速读 - 用户能看到什么变化:应用作者在构建检查里看到的「记录改动被丢弃」提示,现在给出一种在沙箱里真能运行的写法;应用依赖文档补上了保存时按依赖关系判定的完整绑定清单。 - 为什么做:原提示让作者照抄一个沙箱里根本不存在的方法,照做后动作第一次运行就报错;属于脚本动作写入的检查跟进工作。 - 出错会怎样:只改提示文字和文档,检查结论不变,不影响任何已保存的应用;有问题可直接撤回。 - 要你做什么:无。 ## What changes - **Item (c), the `action-record-write-discarded` hint.** It prescribed `ctx.api.object('OBJECT').updateById(ctx.recordId, { … })`. Probed for real (below): a sandboxed action body or hook body has no `updateById` on `ctx.api.object(…)`, so following the hint throws `TypeError: not a function` and writes nothing. The hint now prescribes `ctx.api.object('OBJECT').update({ id: ctx.recordId, FIELD: … })`, the single-record form the sandbox installs and `HookObjectApi.update` documents (the id travels inside the payload). The method name lives in one constant, `ACTION_RECORD_WRITE_REMEDY_METHOD` (not re-exported from the package index), pinned to `SANDBOX_API_WRITE_METHODS`, which is itself pinned to the runner's `WRITE` list. - **The `os explain action-body-write-unknown-field` text** said an action body persists "only through `insert / create / update / updateById`". `create` and `updateById` are not installed in the sandbox either. It now names `insert({ … })` and `update({ id, … })`. Same defect class, same file, bounded fix. - **Item (d), `content/docs/deployment/app-dependencies.mdx`.** The page said the save door judges "a list view" against the package closure. It now has a table of the bindings that door judges, including the two that PR #37807 added: a standalone action's `objectName` and a flow `get_record` / `create_record` / `update_record` / `delete_record` / object-form `screen` node's `objectName`. Sources: `validateObjectReferences` runs on the `flow`, `dataset` and `action` runtime doors (`packages/lint/src/reference-integrity-suite.ts`, member `runtimeTypes: ['flow', 'dataset', 'action']`, handed `ctx.packageScope`), and the flow-node and action sites are in `packages/lint/src/validate-object-references.ts` (`OBJECT_BOUND_FLOW_NODE_TYPES`, `actions[i].objectName`). The form and knowledge-source rows come from `form-object-outside-closure` (`validate-form-items.ts`) and `knowledge-source-object-unknown` (`validate-knowledge-source.ts`). The table says "include" because other closure-aware rules exist (email template holes, AI tool and context references) that this PR did not audit. - What lint judges is unchanged: an `updateById` payload in a body is still graded (`API_WRITE_METHODS`, the ledger's `api-crud-literal` syntax line and example, and the explanations that say what is judged), because a body can still contain the call. - The ADR-0141 D1 note is not in this diff. It goes to the weekly ADR revisions card. ## Tests - `validate-action-body-writes.test.ts`: the existing case that pinned `hint` to contain `updateById` now pins the remedy method. A new case asserts the method the hint calls is in `SANDBOX_API_WRITE_METHODS` and that the payload is addressed by `id: ctx.recordId`. Subjects, not prose. - Ablation (hint put back to `updateById(ctx.recordId, …)` via `scripts/ablation-replace.mjs`, anchor 1 to 0, blob 262ceea to a2f3f908): 2 cases red (`expected [ 'updateById' ] to deeply equal [ 'update' ]`), 50 passed. Restored: `git diff HEAD` empty, 0 hits of the mutation. <details><summary>Sandbox probe (throwaway test, not committed): real ObjectQL, real better-sqlite3 SqlDriver, real QuickJSScriptRunner and body-runner factories</summary> ``` PROBE ACTION updateById (old hint) :: {"err":{"name":"SandboxError","message":"action 'close_deal' threw: TypeError: not a function","inner":"TypeError: not a function"}} :: stage after = open PROBE ACTION update({ id }) (new hint) :: {"ok":"ok"} :: stage after = won PROBE ACTION typeof updateById :: {"ok":"undefined"} :: stage after = open PROBE HOOK updateById :: {"err":{"name":"SandboxError","message":"hook 'note_closes_deal' threw: TypeError: not a function"}} :: stage after = open PROBE HOOK update({ id }) :: {"ok":true} :: stage after = won Test Files 1 passed (1) Tests 5 passed (5) ``` Hook bodies run in the same sandbox and fail the same way. </details> <details><summary>Local runs (head c2b9cff)</summary> - `pnpm --filter @objectstack/lint exec vitest run --maxWorkers=2` (the whole package) at c2b9cff: lock VERDICT command-exit 0, Test Files 246 passed (246), Tests 9959 passed (9959). The first whole-package run, at 0dfeeda, had 1 red in `rule-id-barrel-exports.test.ts`: it reads an unannotated exported string constant as a rule id. The constant is now annotated `string` (second commit). - `pnpm --filter @objectstack/lint typecheck`: VERDICT command-exit 0. - eslint on the three edited lint files: exit 0. - `dispatch-gates.mjs --commands` derived 100 families; all 100 run locally: 97 exit 0; 3 exit 3 PREREQUISITE NOT MET, so NOT MEASURED here and left to CI (`check-plugin-teardown-shape --self-test` needs an unshallow clone; `spec check:skill-examples` and `check:dual-build-cjs-loads` need packages this worktree did not build). None of the three reads a file this PR changes. `--ran` reconciliation: 100 derived, 100 run, 0 UNRUN. Re-run on c2b9cff after the second commit: `check:doc-authoring`, `check:issue-citations`, `check:nul-bytes`, `check:adr-anchors`, `check:cross-package-test-inputs`, `check:undeclared-dep-imports`, `check:type-check-debt`, `check:closed-ceremony-text`, all exit 0. - `test:repo` not run locally (CI's `Test Core`). </details> ## Acceptance notes - **Not fixed here, reported for filing (outside this PR's file surface):** `content/docs/automation/hook-bodies.mdx` (the "exactly one way an action body persists anything" snippet) still teaches `updateById(ctx.recordId, …)` in an action body. The spec contract (`HookObjectApi.updateById` in `packages/spec/src/data/hook-api.ts`, `IScopedObjectRepository.updateById`) declares the member a body cannot call, and `objectstack build` does not refuse `.updateById(` at lowering the way it refuses `.create(`. Whether the sandbox should install the leaf or the body-facing contract should drop it is a decision; it is raised in the report. - The hook ledger's `api-crud-literal` syntax line and example still name `updateById`: they mirror the extractor's graded verbs (`validate-readonly-hook-writes.test.ts` reads the syntax line as that mirror) and are not shown to authors. ## Verification Read first: `packages/lint/src/validate-action-body-writes.ts` (the hint and `ACTION_RECORD_WRITE_REMEDY_METHOD`), then the new test case, then the docs table. Two-minute check: `pnpm --filter @objectstack/lint exec vitest run src/validate-action-body-writes.test.ts` shows all cases passing, and the `prescribes a write the sandbox installs` case among them. --- _Generated by [Claude Code](https://claude.ai/code/session_01ADHkmSwS4mQyXWrddL4r6B)_ 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01ADHkmSwS4mQyXWrddL4r6B <!-- signed: repo:objectstack#3 · os-steve · session_01ADHkmSwS4mQyXWrddL4r6B --> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 809c270 commit cb99e2d

5 files changed

Lines changed: 71 additions & 11 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'@objectstack/lint': patch
3+
---
4+
5+
fix(lint): the `action-record-write-discarded` hint prescribes a write a sandboxed body can run
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
The hint now reads `ctx.api.object('<object>').update({ id: ctx.recordId, <field>: … })`. It used to prescribe `ctx.api.object('<object>').updateById(ctx.recordId, { … })`, and the sandbox an action body runs in installs no `updateById` on `ctx.api.object(…)`: following the hint turned the discarded write into `TypeError: not a function` on the action's first run. The `os explain action-body-write-unknown-field` text no longer lists `create` and `updateById` among the calls an action body persists through, for the same reason. What lint judges is unchanged: an `updateById` payload in a body is still graded.

‎content/docs/deployment/app-dependencies.mdx‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,28 @@ export default defineStack({
3333

3434
The declaration does two things:
3535

36-
- **It opens the reference.** The runtime save door judges a list view against
37-
your package's own objects and those of the packages your manifest declares,
38-
transitively; a view bound to an object outside that set is refused there,
39-
`422`, and the fix it prints is this declaration. A dependency opens the
40-
reference, not the data: users still see the other app's records under that
41-
app's own permission sets and sharing rules.
36+
- **It opens the reference.** The runtime save door judges the bindings below
37+
against your package's own objects and those of the packages your manifest
38+
declares, transitively; a binding to an object outside that set is refused
39+
there, `422`, and the fix it prints is this declaration. A dependency opens
40+
the reference, not the data: users still see the other app's records under
41+
that app's own permission sets and sharing rules.
4242
- **It is checked before install** — below.
4343
- **It lets your package share the depended-on app's namespace** — below.
4444

45+
The bindings the save door judges this way, when the item is saved into a
46+
package, include:
47+
48+
| You save | The binding judged |
49+
|:---|:---|
50+
| A flow | its trigger object |
51+
| A flow | the `objectName` of each `get_record`, `create_record`, `update_record` and `delete_record` node, and of a `screen` node that renders an object's form, nested nodes included |
52+
| A dataset | its base object |
53+
| A list view | its object |
54+
| A form | its object |
55+
| A knowledge source | its object |
56+
| A standalone action | its `objectName` |
57+
4558
Every entry is required; there is no optional arm. Declare only the packages
4659
your app actually references. Two modules of one app need not declare each
4760
other: each declares the app, and that is enough for both to install, in either

‎packages/lint/src/rule-explanations.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,8 +1422,8 @@ const ACTION_BODY_WRITE_UNKNOWN_FIELD_EXPLANATION: RuleExplanation = {
14221422
rule: 'action-body-write-unknown-field',
14231423
covers: 'why an undeclared field write is refused',
14241424
paragraphs: [
1425-
'An action body persists records only through ' +
1426-
'`ctx.api.object(NAME).insert / create / update / updateById(…)`. `ctx.api` is a scoped handle on ' +
1425+
'An action body persists records only through `ctx.api`: ' +
1426+
'`ctx.api.object(NAME).insert({ … })`, or `.update({ id, … })` for one record. `ctx.api` is a scoped handle on ' +
14271427
'the running engine, so the payload arrives as an ordinary CALLER write. An action\'s `ctx.input` ' +
14281428
'is its params bag, validated against the action\'s own `params`, so it is never resolved against ' +
14291429
'fields.',

‎packages/lint/src/validate-action-body-writes.test.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import {
1212
ACTION_BODY_WRITE_UNPROVISIONED_ANCHOR,
1313
ACTION_RECORD_WRITE_DISCARDED,
1414
ACTION_BODY_SOURCE_UNPARSEABLE,
15+
ACTION_RECORD_WRITE_REMEDY_METHOD,
1516
} from './validate-action-body-writes.js';
17+
import { SANDBOX_API_WRITE_METHODS } from './validate-action-body-capabilities.js';
1618
import {
1719
extractHookBodyWrites,
1820
HOOK_BODY_WRITE_PATTERNS,
@@ -347,7 +349,21 @@ describe('validateActionBodyWrites — discarded ctx.record writes (#4345)', ()
347349
expect(explanationOf(ACTION_RECORD_WRITE_DISCARDED)).toContain(
348350
"The snapshot stays read-only by design: an action's write channel is `ctx.api`.",
349351
);
350-
expect(findings[0].hint).toContain('updateById');
352+
expect(findings[0].hint).toContain(`.${ACTION_RECORD_WRITE_REMEDY_METHOD}(`);
353+
});
354+
355+
it('prescribes a write the sandbox installs, addressed to ctx.recordId', () => {
356+
// The remedy is code an author pastes into a body, so the member it names
357+
// must be one the sandbox's `ctx.api.object(…)` actually has. It once named
358+
// `updateById`, which the host repository has and the sandbox does not:
359+
// following the hint was a `TypeError: not a function` on the first run.
360+
// `SANDBOX_API_WRITE_METHODS` is pinned to the runner's own `WRITE` list.
361+
expect(SANDBOX_API_WRITE_METHODS).toContain(ACTION_RECORD_WRITE_REMEDY_METHOD);
362+
const [finding] = validateActionBodyWrites(stackWith("ctx.record.stage = 'won';"));
363+
const called = [...finding.hint.matchAll(/\.object\('<object>'\)\.(\w+)\(/g)].map((m) => m[1]);
364+
expect(called).toEqual([ACTION_RECORD_WRITE_REMEDY_METHOD]);
365+
// The single-record `update` form reads the primary key from the payload.
366+
expect(finding.hint).toMatch(/\(\{ id: ctx\.recordId, stage: /);
351367
});
352368

353369
it('reads from ctx.record do not rescue the write', () => {

‎packages/lint/src/validate-action-body-writes.ts‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,21 @@ export interface ActionBodyWriteFinding {
117117
export const ACTION_BODY_WRITE_UNKNOWN_FIELD = 'action-body-write-unknown-field';
118118
export const ACTION_RECORD_WRITE_DISCARDED = 'action-record-write-discarded';
119119

120+
/**
121+
* The `ctx.api.object(…)` member the `action-record-write-discarded` hint
122+
* prescribes. It must be a write leaf the sandbox installs on a body's
123+
* `ctx.api.object(…)` (`WRITE` in packages/runtime/src/sandbox/quickjs-runner.ts):
124+
* the hint once named `updateById`, which the host repository has and the
125+
* sandbox does not, so following it traded a silent no-op for a
126+
* `TypeError: not a function`. Its test pins it to `SANDBOX_API_WRITE_METHODS`
127+
* (validate-action-body-capabilities.ts), which is itself pinned to the
128+
* runner's list. Not re-exported from the package index. Annotated `string`
129+
* because it is a method name, not a rule id: the rule-id barrel census
130+
* (`rule-id-barrel-exports.test.ts`) reads an unannotated exported string
131+
* constant as one.
132+
*/
133+
export const ACTION_RECORD_WRITE_REMEDY_METHOD: string = 'update';
134+
120135
/**
121136
* [#10653] The action-surface twin of `hook-body-source-unparseable`. Same
122137
* extractor, same synthesised wrapper, same parse — so the body that came back
@@ -382,9 +397,15 @@ export function validateActionBodyWrites(stack: AnyRec): ActionBodyWriteFinding[
382397
message:
383398
`body assigns ctx.record.${w.field}, but an action's ctx.record is a snapshot the runtime never ` +
384399
`writes back, so the assignment is discarded while the action returns success`,
400+
// The remedy names `update({ id, … })`, the single-record write the
401+
// sandbox installs (`WRITE` in packages/runtime/src/sandbox/
402+
// quickjs-runner.ts) and the shape `HookObjectApi.update` documents:
403+
// the id travels inside the payload. ⛔ Not `updateById`: a body's
404+
// `ctx.api.object(…)` has no such member, so that call throws
405+
// `TypeError: not a function` on its first run.
385406
hint:
386-
`To persist it, write through the API: ctx.api.object('<object>').updateById(ctx.recordId, ` +
387-
`{ ${w.field}: … })`,
407+
`To persist it, write through the API: ctx.api.object('<object>').${ACTION_RECORD_WRITE_REMEDY_METHOD}(` +
408+
`{ id: ctx.recordId, ${w.field}: … })`,
388409
});
389410
}
390411
}

0 commit comments

Comments
 (0)