Skip to content

Commit 568d416

Browse files
fix(objectql): the duplicate-record refusal reaches the writer's locale on the action door and on bulk rows (#34304)
<!-- claude-projects-attribution: {"github_login":"zhuangjianguo"} --> _Requested by **Steedos** · [project thread](https://claude.ai/code/project/chan_018ETKaNwMZCNRYD7D8Q9Ubb?thread=cmsg_018ETKaNwMZCNRYD7D8Q9UbbSUQWB2j9eHYsRcrCiq1X1C)_ Fixes #33790 Clause-②: yes (widening). The /data 409 and the row errors gain a `userMessage` key, with the same text as the end-user sentence. `message` keeps its diagnostic text. Metadata-protocol: none ## 维护者速读 - 改了什么:记录重复的提示句(如「已存在「工厂」、「牌号」、「成本周次」均相同的记录」)现在在按钮动作、脚本动作、批量编辑和流程里也按用户语言显示,不再只有表单保存才是中文。 - 为什么改:之前通过动作按钮或批量编辑保存时,用户看到的是给工程师看的英文诊断句;席位裁决 B′(6895764754)按维护者裁决 C(6819749037)由错误的产生方统一声明用户句。 - 风险与回滚:只新增一个已有契约字段 `userMessage`,原有 `message`、`error`、`code` 文字不变;回滚即撤回本 PR。 - 先看:`packages/objectql/src/duplicate-record-error.ts`(新增 `userMessage`)、`packages/metadata-protocol/src/protocol-client-failures.ts`(批量行错误带上它)、新测试文件。 - 两分钟验证:`pnpm --filter @objectstack/runtime exec vitest run --project local src/duplicate-record-user-message-doors-33790.test.ts`,预期 20 个用例全绿(中文与英文两套)。 - 需要你做的:无,席位落地。 ## What changes - `DuplicateRecordError` (`@objectstack/objectql`) declares its already-assembled end-user sentence, composed in the writer's locale, as `userMessage`. `endUserMessage` keeps the same text. `message` stays the diagnostic sentence. The module note that said the sentence was deliberately not `userMessage` is replaced by the B′ rule. - `toRowApiError` (`@objectstack/metadata-protocol`) carries a producer-declared `userMessage` on a row error, through the same read and bound the `/data` door applies (`boundedDeclaredUserMessage`). A row whose producer marked nothing carries no `userMessage`. - The sentence names fields by label and quotes no record value, as before. Every door, changed or declared unchanged (measured on the real stack unless noted): | door | code changed here | wire change | pin | |---|---|---|---| | `POST`/`PATCH /data/:object` 409 | no | gains `userMessage`, same text as `error` | card file, the flipped `engine-duplicate-record-label-sentence.test.ts` pin, and the three rest pins | | `POST /data/:object/createMany` (whole-request 409) | no | gains `userMessage` beside `error` | card file | | `POST /actions/...`, declarative `operation: 'update'` | no | gains `error.userMessage`; `message` unchanged | card file | | `POST /actions/...`, sandboxed script body writing through `ctx.api` (QuickJS hand-back) | no | gains `error.userMessage`; the hand-back keeps it | card file | | `POST /data/:object/updateMany` rows | yes (`toRowApiError`) | rows gain `userMessage`; `message` unchanged | card file | | `POST /data/:object/batch` rows (`create`, `update`, `upsert`) | yes (`toRowApiError`) | rows gain `userMessage` | card file | | Hono handler-throw seam (`declaredEnvelopeForThrow`) | no | gains `error.userMessage` | card file, seam asked directly | | MCP tool error (`actionsDoorAnswerOf`) | no | gains `userMessage` | card file, resolver asked directly | | Flow inner refusal (`refusalCarriedBy`; the trigger and resume doors) | no | gains `userMessage`; `message` and `innerRefusal.message` unchanged | flipped `service-automation/src/flow-inner-refusal.test.ts` and `verify/src/automation-flow-inner-refusal.test.ts` (zh-CN and en) | | Import rows (`@objectstack/core` import runner) | no | none: the row adopts the `/data` verdict's `error`, already in the writer's locale | none | ## Tests - New `packages/runtime/src/duplicate-record-user-message-doors-33790.test.ts`: a real kernel over its Hono app (ObjectQL on sqlite-wasm, REST, dispatcher, auth, security, an app with a declared composite unique index, `zh-CN` labels, a declarative update action and a sandboxed script action). Every door above runs under `Accept-Language: zh-CN` and under an `en` control, with a control row error that carries no `userMessage`. 20 tests. - Flipped `packages/objectql/src/engine-duplicate-record-label-sentence.test.ts` (the `not.toHaveProperty('userMessage')` pin) to assert `userMessage` equals `error`, en and zh-CN. - Flipped the flow door's two pins (claim amendment 6907854103): `packages/services/service-automation/src/flow-inner-refusal.test.ts` (the unique case, now zh-CN and en) and `packages/verify/src/automation-flow-inner-refusal.test.ts` (the unique cases at the trigger and resume doors, now zh-CN and en). Each asserts the run carries the declared `userMessage` and that `message` keeps the diagnostic. - Added `userMessage` (equal to `error`) to the expected bodies of seven exact-shape pins on the /data 409 and row errors (claim amendment for the rest surface): `packages/rest/src/rest-duplicate-record-arm.test.ts` (four cases), `packages/rest/src/data-package-scoped-refusal-candidates.test.ts` (the two CONTROL cases, flat 409 and row) and `packages/rest/src/error-response-structured-arm-door-parity.test.ts` (the DUPLICATE_RECORD door-parity row). - Added `userMessage` (equal to `error`) to the expected 409 bodies of the sku and gsku cases in `packages/qa/dogfood/test/field-unique-rest-refusal.dogfood.test.ts`, the eighth exact-shape pin (claim amendment 6911413595). It pinned the flat `/data` 409 by exact shape and ejected the first enqueue (queue run 43826455262); the dogfood shards run only in the queue. <details><summary>Ablation (both legs rebuilt the package, proved the marker in dist/, then restored and proved it absent)</summary> - Producer declaration (`this.userMessage = this.endUserMessage` replaced by a blank string): 19 of 20 card tests red, for example `AssertionError: expected undefined to be '已存在「工厂」、「牌号」、「成本周次」均相同的记录'` on the sandboxed script action. The one green is the control row. - Row carry (the `userMessage` spread in `toRowApiError` disabled): exactly the 8 row tests red (`updateMany` and the three `batch` operations, both locales); the 12 others stay green. - Producer declaration again, against the flow pins: the 2 unique cases of `flow-inner-refusal.test.ts` and the 4 unique cases of `automation-flow-inner-refusal.test.ts` red (`expected undefined to be '已存在相同客户名称的记录'`); every other case green. - Restore: blob equals HEAD, `git diff HEAD` empty, dist preflight `--absent` exit 0, `git status --porcelain` empty. </details> ## Acceptance notes - The flow door's existing pins asserted that the engine's duplicate carries no `userMessage`; ruling B′ turns that false, so they are flipped here under claim amendment 6907854103. - The dogfood battery is CI's (no `!` in the title, no `(narrowing)` arm). ## Verification <details><summary>Local runs</summary> - Final head `da832b2f8` (after merging `origin/main` again): card file 20/20; `@objectstack/service-automation` 284 files, 4041 tests passed; `@objectstack/verify` 52 files, 441 passed; typecheck of service-automation and verify exit 0. - Head `fd810dc0e`: `@objectstack/objectql` 599 files, 13353 passed; `@objectstack/metadata-protocol` 344 files, 22946 passed, 45 skipped; typecheck of objectql, metadata-protocol and runtime exit 0. Head `3f7c473bc`: `@objectstack/scenarios` 34 files, 922 passed. - Pre-merge head `5b4d1738b`, the other direct dependents of objectql and metadata-protocol: runtime 606 files, 11090 passed, 19 skipped; cli `unit` 423 files, 6010 passed; plugin-sharing, plugin-audit, plugin-webhooks, plugin-email, plugin-pinyin-search, plugin-dev, cloud-connection, example-todo, example-embed-objectql green. - Head `099761b68` (the rest pin flips): `@objectstack/rest` `test` 413 files, 8497 passed, 350 skipped; `test:repo` 5 files, 300 passed, 23 skipped; typecheck exit 0. - Gates: `dispatch-gates --commands` on `da832b2f8` derived 71 families; 69 run locally, all exit 0, plus `pnpm lint` exit 0. The gates that read test files (`check:test-source-alias`, `check:cross-package-test-inputs`, `check:nul-bytes`, `check:error-code-casing`, `check:issue-citations`, `check:tier-file-adoption`) re-run on `099761b68`, exit 0. `check:dual-build-cjs-loads` and `check:type-check-debt` are left to CI (`Lint & Repo Gates` success on `099761b68`; `check:type-check-debt` also exit 0 locally on `3f7c473bc`). - CI on `099761b68`: every required context is success (Lint & Repo Gates, TypeScript Type Check, Test Core 1-8/8, Build Core, Governed Surface Queue Guard, Test UI, Live E2E UI, Dogfood Regression Gate, Check Changeset); the UI build, lint and type-check lanes were skipped by path. </details> --- _Generated by [Claude Code](https://claude.ai/code/session_01QsR2jVPGBoi3MCGoKbW1Hv)_ 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- signed: repo:objectstack#1 · os-project-manager · session_01FEkQ5nNrLRm8ssuwWcBVcw --> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 2c96e0d commit 568d416

11 files changed

Lines changed: 418 additions & 16 deletions
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
'@objectstack/objectql': patch
3+
'@objectstack/metadata-protocol': patch
4+
---
5+
6+
fix(objectql): the duplicate-record refusal reaches the writer's locale on the action door and on bulk rows
7+
8+
Clause-②: yes (widening). The /data 409 and the row errors gain a `userMessage` key, with the same text as the end-user sentence. `message` keeps its diagnostic text.
9+
Metadata-protocol: none
10+
11+
The engine's duplicate-record refusal already composed its end-user sentence in the writer's locale, and `/api/v1/data` answered it as the 409 body's `error`. Other doors read the user-facing sentence from `userMessage` and keep `message` as the diagnostic. The refusal did not declare that key, so an action button or a bulk edit showed the English diagnostic, for example `Duplicate record refused on '周成本': …`, where the form save showed `已存在「工厂」、「牌号」、「成本周次」均相同的记录`.
12+
13+
- `DuplicateRecordError` now declares the same sentence as `userMessage`. The action door (declarative and sandboxed script actions), the Hono handler-throw seam, MCP tool errors and a flow's inner refusal forward it unchanged. `message` is still the diagnostic sentence.
14+
- Row errors from `updateMany` and `batch` (`create`, `update`, `upsert`) carry a declared `userMessage`, with the same bound the `/data` door applies. A row whose producer marked nothing carries no `userMessage`, as before.
15+
- The flat `/data` 409 (and the whole-request `createMany` 409) gains `userMessage` beside `error`, with the same text. `error`, `code`, `field` and `developerMessage` are unchanged.
16+
17+
The sentence names fields by label and quotes no record value.

‎packages/metadata-protocol/src/protocol-client-failures.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// A MOVE, not a redesign: every declaration below is byte-identical to the text that stood
88
// above `ObjectStackProtocolImplementation`; only the imports and the export list at the foot
99
// are new. `protocol.ts` re-exports what it exported.
10-
import { declaredHttpStatus, declaredUserMessage, isSandboxOrigin, packageScopedRefusalCandidates, resolveThrownHttpError, sandboxBusinessMessage, UNDECLARED_HOOK_REFUSAL_CODE } from '@objectstack/types';
10+
import { boundedDeclaredUserMessage, declaredHttpStatus, declaredUserMessage, isSandboxOrigin, packageScopedRefusalCandidates, resolveThrownHttpError, sandboxBusinessMessage, UNDECLARED_HOOK_REFUSAL_CODE } from '@objectstack/types';
1111
import type { ApiError, BatchOperationResult } from '@objectstack/spec/api';
1212
import { ErrorCode, standardErrorCodeForHttpStatus } from '@objectstack/spec/api';
1313
import { zodIssuesToMetadataIssues } from './protocol-metadata-issues.js';
@@ -155,6 +155,16 @@ export type BatchDataRowResult = BatchOperationResult;
155155
* caller which packages to choose between as a single write does. Read through
156156
* `@objectstack/types`' one recogniser; every other row error carries no
157157
* `details`, as before.
158+
*
159+
* ## A producer-marked `userMessage` rides the row
160+
*
161+
* `ApiError.userMessage` is the contract's producer-marked end-user channel,
162+
* and every whole-request door already carries it (`withDeclaredUserMessage`
163+
* on `/data`, the dispatcher's thrown answer). A row carries it by the same
164+
* read and the same bound (`boundedDeclaredUserMessage`, `@objectstack/types`),
165+
* so the engine's duplicate refusal reaches a bulk edit in the writer's locale
166+
* as it reaches the single-record save. `message` is unchanged: it stays the
167+
* diagnostic the limb above decides. An unmarked throw adds no key.
158168
*/
159169
export function toRowApiError(err: any, fallback: string): ApiError {
160170
const thrown = isEngineDuplicateRecordEnvelope(err)
@@ -166,12 +176,14 @@ export function toRowApiError(err: any, fallback: string): ApiError {
166176
const refusal = sandboxRefusalSentence(err) !== undefined;
167177
const servedStatus = declaredStatus ?? (refusal ? 400 : undefined);
168178
const candidates = packageScopedRefusalCandidates(err);
179+
const userMessage = boundedDeclaredUserMessage(err);
169180
return {
170181
code: thrown
171182
?? (isUndeclaredHookAbort(err, refusal, servedStatus) ? UNDECLARED_HOOK_REFUSAL_CODE
172183
: servedStatus !== undefined ? standardErrorCodeForHttpStatus(servedStatus) : 'INTERNAL_ERROR'),
173184
message: clientFacingRowFailureText(err, fallback),
174185
...(declaredStatus !== undefined ? { httpStatus: declaredStatus } : {}),
186+
...(userMessage !== undefined ? { userMessage } : {}),
175187
...(candidates ? { details: { candidates } } : {}),
176188
};
177189
}

‎packages/objectql/src/duplicate-record-error.ts‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,17 @@ import { resolveFieldLabel, type ValidationMessageContext } from './validation/r
107107
* sentence there, so a `zh-CN` writer read English beside a Chinese
108108
* object label. It names the conflicting field by label (one field), the
109109
* fields of a composite unique index by their labels (see `uniqueFields`
110-
* below), or nothing. ⛔ It is deliberately NOT spelled `userMessage`:
111-
* that name is the ApiError contract's producer-marked channel, which every
112-
* boundary promotes to a wire key of its own (`withDeclaredUserMessage`),
113-
* and whether this refusal joins that channel is a contract decision this
114-
* sentence does not make. The wire shape is unchanged: only the text of
115-
* `error` moves.
110+
* below), or nothing. It names labels only and quotes no record value.
111+
* - **`userMessage`** — the same sentence, declared on the ApiError
112+
* contract's producer-marked channel (`declaredUserMessage` in
113+
* `@objectstack/types` is the one read), so every boundary that forwards a
114+
* declared `userMessage` carries it in the writer's locale: the action door,
115+
* a sandboxed body's relay, a flow's inner refusal, the batch and bulk rows,
116+
* and the flat `/data` 409, where it rides beside `error` with the same text
117+
* (`withDeclaredUserMessage`). Seat ruling B′ (record 6895764754), on
118+
* maintainer ruling C (record 6819749037: end-user text in the request's
119+
* language, composed where it is produced). `message` stays the diagnostic
120+
* sentence below: every door that relays `message` as thrown keeps it.
116121
* - **The fields of a composite unique index** — named in `endUserMessage`
117122
* and in `message` ONLY when the columns the store named
118123
* (`uniqueViolationColumns`, organization part set aside) match, as a set,
@@ -183,9 +188,14 @@ export class DuplicateRecordError extends Error {
183188
/**
184189
* The end user's sentence, in the writer's locale, from the platform's
185190
* operation catalog — what the HTTP doors ship as the 409 body's `error`.
186-
* See the module header, and on why it is not `userMessage`.
191+
* The same text is declared as {@link userMessage}; see the module header.
187192
*/
188193
readonly endUserMessage: string;
194+
/**
195+
* {@link endUserMessage}, declared on the producer-marked channel every
196+
* boundary forwards. See the module header.
197+
*/
198+
readonly userMessage: string;
189199

190200
constructor(
191201
/** The object the refused write targeted. */
@@ -207,6 +217,7 @@ export class DuplicateRecordError extends Error {
207217
this.cause = cause;
208218
if (field && labels?.field) this.fieldLabel = labels.field;
209219
this.endUserMessage = buildEndUserMessage(field ? labels?.field : undefined, uniqueFields, labels?.messages);
220+
this.userMessage = this.endUserMessage;
210221
this.developerMessage =
211222
`Duplicate record refused on object '${object}'` +
212223
(field ? `, field '${field}'` : '') +

‎packages/objectql/src/engine-duplicate-record-label-sentence.test.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,10 @@ describe('the duplicate refusal speaks the writer\'s locale, and names a declare
409409
expect(answer.status).toBe(409);
410410
expect(answer.body.code).toBe('UNIQUE_VIOLATION');
411411
expect(answer.body).not.toHaveProperty('field');
412-
expect(answer.body).not.toHaveProperty('userMessage');
412+
// The producer declares the same sentence on the marked channel, and the
413+
// flat 409 carries it beside `error`.
414+
expect(failure.userMessage).toBe(EN_NAMED);
415+
expect(answer.body.userMessage).toBe(EN_NAMED);
413416
expect(answer.body.error).toBe(EN_NAMED);
414417
});
415418

@@ -425,6 +428,7 @@ describe('the duplicate refusal speaks the writer\'s locale, and names a declare
425428
expect(answer.body.code).toBe('UNIQUE_VIOLATION');
426429
expect(answer.body).not.toHaveProperty('field');
427430
expect(answer.body.error).toBe(ZH_NAMED);
431+
expect(answer.body.userMessage).toBe(ZH_NAMED);
428432
});
429433

430434
it('the Postgres DETAIL shape matches the same declared index', async () => {

‎packages/qa/dogfood/test/field-unique-rest-refusal.dogfood.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,15 @@ describe('records-forms.field-unique-enforcement — the unique refusal at the R
101101
it.each([
102102
['sku', 'FUE-SKU-0001', {
103103
error: 'A record with this SKU already exists',
104+
userMessage: 'A record with this SKU already exists',
104105
code: 'UNIQUE_VIOLATION',
105106
developerMessage: `Duplicate record refused on '${LABEL}': a unique constraint on 'SKU' already holds this value. No record was written.`,
106107
field: 'sku',
107108
object: OBJ,
108109
}],
109110
['gsku', 'FUE-GSKU-0001', {
110111
error: 'A record with this Global SKU already exists',
112+
userMessage: 'A record with this Global SKU already exists',
111113
code: 'UNIQUE_VIOLATION',
112114
developerMessage: `Duplicate record refused on '${LABEL}': a unique constraint on 'Global SKU' already holds this value. No record was written.`,
113115
field: 'gsku',

‎packages/rest/src/data-package-scoped-refusal-candidates.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ describe('[#32250] /data names the candidates of a package-scoped refusal — sq
229229
// label, so the end-user sentence stays the unnamed one).
230230
const UNIQUE = {
231231
error: 'A record with this value already exists',
232+
userMessage: 'A record with this value already exists',
232233
code: 'UNIQUE_VIOLATION',
233234
developerMessage: "Duplicate record refused on 'Scoped refusal 32250': a unique constraint on 'code' already holds this value. No record was written.",
234235
field: 'code',
@@ -238,6 +239,7 @@ describe('[#32250] /data names the candidates of a package-scoped refusal — sq
238239
code: 'UNIQUE_VIOLATION',
239240
message: "Duplicate record refused on 'Scoped refusal 32250': a unique constraint on 'code' already holds this value. No record was written.",
240241
httpStatus: 409,
242+
userMessage: UNIQUE.error,
241243
};
242244

243245
it('unique violation on create, update and createMany', async () => {

‎packages/rest/src/error-response-structured-arm-door-parity.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ const CASES: readonly Case[] = [
196196
status: 409,
197197
body: {
198198
error: 'A record with this Email already exists',
199+
userMessage: 'A record with this Email already exists',
199200
code: 'UNIQUE_VIOLATION',
200201
developerMessage: "Duplicate record refused on 'duly_note': a unique constraint on "
201202
+ "'Email' already holds this value. No record was written.",

‎packages/rest/src/rest-duplicate-record-arm.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ describe('#14389 §1 — the engine\'s DUPLICATE_RECORD envelope answers 409 UNI
277277
const env = labelled('duly_note', sqliteRaw());
278278
expect(mapDataError(env, 'duly_note').body).toEqual({
279279
error: CURATED_NAMED,
280+
userMessage: CURATED_NAMED,
280281
code: 'UNIQUE_VIOLATION',
281282
developerMessage: env.message,
282283
field: 'email',
@@ -286,6 +287,7 @@ describe('#14389 §1 — the engine\'s DUPLICATE_RECORD envelope answers 409 UNI
286287
const composite = envelope('duly_pair', sqliteCompositeRaw());
287288
expect(mapDataError(composite, 'duly_pair').body).toEqual({
288289
error: CURATED_UNNAMED,
290+
userMessage: CURATED_UNNAMED,
289291
code: 'UNIQUE_VIOLATION',
290292
developerMessage: composite.message,
291293
object: 'duly_pair',
@@ -388,6 +390,7 @@ describe('#14389 §2 — a real insert conflict, real engine on real better-sqli
388390
expect(r.status).toBe(409);
389391
expect(r.body).toEqual({
390392
error: CURATED_NAMED,
393+
userMessage: CURATED_NAMED,
391394
code: 'UNIQUE_VIOLATION',
392395
developerMessage: env.message,
393396
field: 'email',
@@ -570,6 +573,7 @@ describe('a declared composite unique index: its fields named by label, in the w
570573
expect(r.status).toBe(409);
571574
expect(r.body).toEqual({
572575
error: sentence,
576+
userMessage: sentence,
573577
code: 'UNIQUE_VIOLATION',
574578
developerMessage: env.message,
575579
object: 'duly_pair_declared',

0 commit comments

Comments
 (0)