diff --git a/.changeset/37815-record-write-hint-update.md b/.changeset/37815-record-write-hint-update.md new file mode 100644 index 00000000000..09add589336 --- /dev/null +++ b/.changeset/37815-record-write-hint-update.md @@ -0,0 +1,10 @@ +--- +'@objectstack/lint': patch +--- + +fix(lint): the `action-record-write-discarded` hint prescribes a write a sandboxed body can run + +Clause-②: no +Metadata-protocol: none + +The hint now reads `ctx.api.object('').update({ id: ctx.recordId, : … })`. It used to prescribe `ctx.api.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. diff --git a/content/docs/deployment/app-dependencies.mdx b/content/docs/deployment/app-dependencies.mdx index 3e17c51e125..019c52f8ffd 100644 --- a/content/docs/deployment/app-dependencies.mdx +++ b/content/docs/deployment/app-dependencies.mdx @@ -33,15 +33,28 @@ export default defineStack({ The declaration does two things: -- **It opens the reference.** The runtime save door judges a list view against - your package's own objects and those of the packages your manifest declares, - transitively; a view bound to an object outside that set is refused there, - `422`, and the fix it prints is this declaration. A dependency opens the - reference, not the data: users still see the other app's records under that - app's own permission sets and sharing rules. +- **It opens the reference.** The runtime save door judges the bindings below + against your package's own objects and those of the packages your manifest + declares, transitively; a binding to an object outside that set is refused + there, `422`, and the fix it prints is this declaration. A dependency opens + the reference, not the data: users still see the other app's records under + that app's own permission sets and sharing rules. - **It is checked before install** — below. - **It lets your package share the depended-on app's namespace** — below. +The bindings the save door judges this way, when the item is saved into a +package, include: + +| You save | The binding judged | +|:---|:---| +| A flow | its trigger object | +| 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 | +| A dataset | its base object | +| A list view | its object | +| A form | its object | +| A knowledge source | its object | +| A standalone action | its `objectName` | + Every entry is required; there is no optional arm. Declare only the packages your app actually references. Two modules of one app need not declare each other: each declares the app, and that is enough for both to install, in either diff --git a/packages/lint/src/rule-explanations.ts b/packages/lint/src/rule-explanations.ts index 2584f6eff71..da1ba7e4f50 100644 --- a/packages/lint/src/rule-explanations.ts +++ b/packages/lint/src/rule-explanations.ts @@ -1422,8 +1422,8 @@ const ACTION_BODY_WRITE_UNKNOWN_FIELD_EXPLANATION: RuleExplanation = { rule: 'action-body-write-unknown-field', covers: 'why an undeclared field write is refused', paragraphs: [ - 'An action body persists records only through ' + - '`ctx.api.object(NAME).insert / create / update / updateById(…)`. `ctx.api` is a scoped handle on ' + + 'An action body persists records only through `ctx.api`: ' + + '`ctx.api.object(NAME).insert({ … })`, or `.update({ id, … })` for one record. `ctx.api` is a scoped handle on ' + 'the running engine, so the payload arrives as an ordinary CALLER write. An action\'s `ctx.input` ' + 'is its params bag, validated against the action\'s own `params`, so it is never resolved against ' + 'fields.', diff --git a/packages/lint/src/validate-action-body-writes.test.ts b/packages/lint/src/validate-action-body-writes.test.ts index 08eff809664..dad38d88dae 100644 --- a/packages/lint/src/validate-action-body-writes.test.ts +++ b/packages/lint/src/validate-action-body-writes.test.ts @@ -12,7 +12,9 @@ import { ACTION_BODY_WRITE_UNPROVISIONED_ANCHOR, ACTION_RECORD_WRITE_DISCARDED, ACTION_BODY_SOURCE_UNPARSEABLE, + ACTION_RECORD_WRITE_REMEDY_METHOD, } from './validate-action-body-writes.js'; +import { SANDBOX_API_WRITE_METHODS } from './validate-action-body-capabilities.js'; import { extractHookBodyWrites, HOOK_BODY_WRITE_PATTERNS, @@ -347,7 +349,21 @@ describe('validateActionBodyWrites — discarded ctx.record writes (#4345)', () expect(explanationOf(ACTION_RECORD_WRITE_DISCARDED)).toContain( "The snapshot stays read-only by design: an action's write channel is `ctx.api`.", ); - expect(findings[0].hint).toContain('updateById'); + expect(findings[0].hint).toContain(`.${ACTION_RECORD_WRITE_REMEDY_METHOD}(`); + }); + + it('prescribes a write the sandbox installs, addressed to ctx.recordId', () => { + // The remedy is code an author pastes into a body, so the member it names + // must be one the sandbox's `ctx.api.object(…)` actually has. It once named + // `updateById`, which the host repository has and the sandbox does not: + // following the hint was a `TypeError: not a function` on the first run. + // `SANDBOX_API_WRITE_METHODS` is pinned to the runner's own `WRITE` list. + expect(SANDBOX_API_WRITE_METHODS).toContain(ACTION_RECORD_WRITE_REMEDY_METHOD); + const [finding] = validateActionBodyWrites(stackWith("ctx.record.stage = 'won';")); + const called = [...finding.hint.matchAll(/\.object\(''\)\.(\w+)\(/g)].map((m) => m[1]); + expect(called).toEqual([ACTION_RECORD_WRITE_REMEDY_METHOD]); + // The single-record `update` form reads the primary key from the payload. + expect(finding.hint).toMatch(/\(\{ id: ctx\.recordId, stage: /); }); it('reads from ctx.record do not rescue the write', () => { diff --git a/packages/lint/src/validate-action-body-writes.ts b/packages/lint/src/validate-action-body-writes.ts index 159935cc9bb..6278887f019 100644 --- a/packages/lint/src/validate-action-body-writes.ts +++ b/packages/lint/src/validate-action-body-writes.ts @@ -117,6 +117,21 @@ export interface ActionBodyWriteFinding { export const ACTION_BODY_WRITE_UNKNOWN_FIELD = 'action-body-write-unknown-field'; export const ACTION_RECORD_WRITE_DISCARDED = 'action-record-write-discarded'; +/** + * The `ctx.api.object(…)` member the `action-record-write-discarded` hint + * prescribes. It must be a write leaf the sandbox installs on a body's + * `ctx.api.object(…)` (`WRITE` in packages/runtime/src/sandbox/quickjs-runner.ts): + * the hint once named `updateById`, which the host repository has and the + * sandbox does not, so following it traded a silent no-op for a + * `TypeError: not a function`. Its test pins it to `SANDBOX_API_WRITE_METHODS` + * (validate-action-body-capabilities.ts), which is itself pinned to the + * runner's list. Not re-exported from the package index. Annotated `string` + * because it is a method name, not a rule id: the rule-id barrel census + * (`rule-id-barrel-exports.test.ts`) reads an unannotated exported string + * constant as one. + */ +export const ACTION_RECORD_WRITE_REMEDY_METHOD: string = 'update'; + /** * [#10653] The action-surface twin of `hook-body-source-unparseable`. Same * extractor, same synthesised wrapper, same parse — so the body that came back @@ -382,9 +397,15 @@ export function validateActionBodyWrites(stack: AnyRec): ActionBodyWriteFinding[ message: `body assigns ctx.record.${w.field}, but an action's ctx.record is a snapshot the runtime never ` + `writes back, so the assignment is discarded while the action returns success`, + // The remedy names `update({ id, … })`, the single-record write the + // sandbox installs (`WRITE` in packages/runtime/src/sandbox/ + // quickjs-runner.ts) and the shape `HookObjectApi.update` documents: + // the id travels inside the payload. ⛔ Not `updateById`: a body's + // `ctx.api.object(…)` has no such member, so that call throws + // `TypeError: not a function` on its first run. hint: - `To persist it, write through the API: ctx.api.object('').updateById(ctx.recordId, ` + - `{ ${w.field}: … })`, + `To persist it, write through the API: ctx.api.object('').${ACTION_RECORD_WRITE_REMEDY_METHOD}(` + + `{ id: ctx.recordId, ${w.field}: … })`, }); } }