Repository navigation
Commit 09da8c7
fix(objectql): the duplicate refusal reads in the writer's locale and names a declared composite unique index's fields (#33348)
<!-- claude-projects-attribution: {"github_login":"os-elon-musk"} -->
_Requested by **Elon** · [project
thread](https://claude.ai/code/project/chan_01WedDzV26nniwxv7jC88B9D?thread=cmsg_01WedDzV26nniwxv7jC88B9DXuamRUC67fx2TTYrFfyuSH)_
Fixes #33213
Clause-②: yes (widening)
Metadata-protocol: none
## Maintainer quick read
- What changes: the 409 `UNIQUE_VIOLATION` sentence is now composed by
the engine in the writer's language from the platform operation-message
catalog, and a composite unique index names its fields by label when
that can be established.
- Why: a downstream app writing a duplicate of a "Plant + Carbon Grade +
Cost Week" unique index under `Accept-Language: zh-CN` got the English
"A record with this value already exists" and could not tell which
fields collided.
- Risk and rollback: the wire shape is unchanged (`code`, 409 and the
meaning of `field` stay), only the `error` text moves; the English
single-field and no-field sentences are byte-identical. Rollback is a
revert of this PR.
- Read first: `packages/objectql/src/duplicate-record-error.ts`
(`declaredCompositeIndex` and `buildEndUserMessage`),
`packages/types/src/unique-violation.ts` (new `uniqueViolationColumns`;
the two existing functions answer as before),
`packages/spec/src/system/operation-message.ts` (three new catalog
keys).
- Two-minute check: `pnpm --filter @objectstack/objectql exec vitest run
--maxWorkers=2 src/engine-duplicate-record-label-sentence.test.ts`;
expect all green, including the zh-CN composite case asserting
`已存在「工厂」、「牌号」、「成本周次」均相同的记录`.
- Needed from you: nothing; the seat lands it.
## What changes
- **The sentence is composed where the locale and the schema are.**
`DuplicateRecordError` (`@objectstack/objectql`) now renders its
end-user sentence from the platform operation-message catalog
(`renderOperationMessage`, the catalog `DELETE_RESTRICTED` already
renders from) in the write's message context, so a deployment's
`errors.duplicate_record*` translation overrides it. It is carried
in-process as `endUserMessage`, and the `DUPLICATE_RECORD` arm of
`classifyDataError` (`@objectstack/types`) ships it as the 409 body's
`error`. That arm holds neither a locale nor a schema, which is why it
composed fixed English before.
- **Three catalog keys**, every platform locale (`en`, `zh-CN`, `ja-JP`,
`es-ES`): `duplicate_record` (names nothing), `duplicate_record_field`
(`{{field}}`, one label), `duplicate_record_fields` (`{{fields}}`, a
list spelled by the locale's existing `field_label_quoted` /
`field_label_list_separator` rules). The English `duplicate_record` and
`duplicate_record_field` are byte-identical to the fixed text the HTTP
doors answered, so an English deployment reads no change for one field
or none.
- **A composite unique index is named only on a declared match.**
`uniqueViolationColumns(error)` (new, `@objectstack/types`) reads the
composite key's columns from the SQLite and Postgres texts the existing
readers already parse, organization part set aside as
`uniqueViolationField` sets it aside. The engine names them only when
that set equals the `fields` of a unique index (`'global'` or
`'organization'`) the object declares, and the user's sentence names
them only when every one has a label (translation, else declared
`label`). Anything short of that names nothing: driver text alone cannot
tell a `tenancy.tenantField` column from an authored one, and a wrong
field name is worse than none. `uniqueViolationColumn` /
`uniqueViolationField` answer exactly as before (the shared reader was
generalised over a reduction, not changed).
- **The wire shape does not move.** `code` stays `UNIQUE_VIOLATION`, the
status 409, `field` keeps its single-column contract (absent for a
composite), and no key is added. The composite's labels also enter the
engine's `message` (relayed as `developerMessage`) and its API names
enter the in-process `developerMessage`.
- Why not `userMessage`: that name on a thrown error is the ApiError
contract's producer-marked channel, which `withDeclaredUserMessage`
promotes to its own wire key. Whether this refusal joins that channel is
a contract decision this fix does not make, so the property is
`endUserMessage` and the wire is unchanged (pinned:
`not.toHaveProperty('userMessage')`).
## Tests
Pins added (zh-CN and en, single-field and multi-field, plus the
fallbacks):
- `packages/objectql/src/engine-duplicate-record-label-sentence.test.ts`
— new top-level `describe` in the existing harness, through the real
write doors (`insert`, `update` by id) and the real `mapDataError`: a
declared `unique: 'organization'` index over `plant + carbon_grade +
cost_week` named in en (`A record with the same “Plant”, “Carbon Grade”,
“Cost Week” already exists`) and zh-CN (`已存在「工厂」、「牌号」、「成本周次」均相同的记录`),
the Postgres DETAIL shape, an uncatalogued locale (`de-DE`) falling back
to the en sentence, a deployment `errors.duplicate_record_fields`
override; five controls that must name nothing (no matching declared
index, a non-unique match, no indexes, one unlabelled field, a MySQL
index-only refusal), each asserting the unnamed sentence in zh-CN and
en; a single unlabelled field in zh-CN. The two existing zh-CN
single-field pins now expect `已存在相同客户名称的记录` (they pinned the English
sentence around a Chinese label — the reported defect).
- `packages/rest/src/rest-duplicate-record-arm.test.ts` — a real
`ObjectQL` on real better-sqlite3 with a declared `'global'` composite
index: the exact 409 body in en and zh-CN, no `field`, no value.
- `packages/spec/src/system/operation-message.test.ts` — the three keys
in every locale with exactly their own placeholder, en byte-identity
with the previous wire text, zh-CN / base-language / unknown-locale
rendering, the `errors.` override.
- `packages/types/src/unique-violation.test.ts` —
`uniqueViolationColumns` on SQLite and Postgres composites, no overlap
with `uniqueViolationField`, and `undefined` for index names, expression
parts and a NOT NULL refusal.
<details><summary>Runs (heads 330e619 and af12b05)</summary>
- At 330e619: `pnpm --filter @objectstack/types test` — Test Files 39
passed (39), Tests 907 passed (907).
- `pnpm --filter @objectstack/spec test` — Test Files 839 passed, 1
skipped (840); Tests 26834 passed, 1 skipped, 1 todo.
- `pnpm --filter @objectstack/objectql test` — 594 passed, 1 failed:
`federated-injected-column-readers.test.ts` flagged an `organization_id`
literal the first cut used in the matcher. Fixed in af12b05 by
comparing the declared index as written (no literal); re-run at
af12b05: that file plus the three duplicate-record files, Test Files 4
passed (4), Tests 119 passed (119). The full objectql suite was not
re-run at af12b05 (lock contention); CI runs it.
- `pnpm --filter @objectstack/rest exec vitest run` on
`rest-duplicate-record-arm`, `import-runner-unique-violation-row`,
`import-row-report-field-20701`,
`error-response-structured-arm-door-parity`,
`error-response-generic-passthrough-object-parity` at af12b05 — Test
Files 5 passed (5), Tests 135 passed (135).
- `pnpm --filter @objectstack/types typecheck`, `@objectstack/objectql
typecheck`, `@objectstack/spec typecheck` — exit 0 at 330e619 (the
af12b05 objectql source change passed its tsup DTS build in the rest
closure build).
</details>
<details><summary>Ablation (one-shot, not kept)</summary>
`node scripts/ablation-replace.mjs --file
packages/objectql/src/duplicate-record-error.ts --anchor
"this.endUserMessage = buildEndUserMessage(...);" --replacement
"this.endUserMessage = '';"` under the verify lock, running
`engine-duplicate-record-label-sentence.test.ts` (the subject resolves
from `src`, so no rebuild leg): mutation landed (anchor 1 -> 0, blob
55cc407 -> be7c0e75), Tests 24 failed / 37 passed (61) — red, the
expected direction; restored (blob == HEAD 55cc407, `git diff HEAD`
empty).
</details>
## Acceptance notes
- **MySQL still names no field, single or composite.** Its
duplicate-entry text names an index (`for key 'idx_…'`), never columns;
mapping a physical index name to a declared index is the wider
schema-lookup contract `uniqueViolationColumn`'s doc already defers. The
sentence is still in the writer's locale there.
- **A legacy hand-written global index that lists `organization_id`
itself** (`fields: ['organization_id', 'a', 'b']`) matches nothing and
gets the unnamed (localized) sentence: the declared side is compared as
written. The spec already steers new code to `unique: 'organization'`,
which matches.
- **A bare `unique: true` index** (refused at parse since protocol 18,
converted on stored rows) is not treated as unique by the matcher. The
existing real-driver pin in `rest-duplicate-record-arm.test.ts` §2
registers such an object straight into the registry and keeps its
unnamed sentence for that reason; the new real-driver pin declares
`'global'`.
- **The raw driver unique arm** further down `classifyDataError` (a
unique error that did not pass through the engine envelope) keeps its
fixed English sentence: that module holds no locale. Every engine write
door wraps first, so the reported path is the envelope.
- The `envelope without endUserMessage` fallback in the arm keeps the
previous sentence for an envelope built by an older engine; every
envelope this engine builds carries the sentence.
- Locale ladder: like every `renderOperationMessage` refusal, this takes
the one `ExecutionContext.locale` value, so a request locale the catalog
lacks reads `en`, not the workspace default (the limitation
`ApiErrorSchema.userMessage` "Language" already records for the
operation refusals).
## Verification
- Local gate families: `node scripts/pm/dispatch-gates.mjs --commands`
derived 87 families for this diff at af12b05; all 87 run, 80 exit 0.
Seven exited 3 = PREREQUISITE NOT MET, read as NOT MEASURED (they read
built output that predates the source or needs a deeper clone):
`check-plugin-teardown-shape --self-test` (shallow clone), spec
`check:api-surface`, `check:dual-source-exports`,
`check:entry-nameability`, `check:exported-any` (spec `dist` predates a
test-file edit), `check:dual-build-cjs-loads`, `check:type-check-debt`
(closure not built). CI runs all of them on a clean build. `--ran`
reconciliation: 87 derived, 87 run, 0 unrun.
- No control bytes in any changed file (`grep -naP` over the diff: no
match).
- The branch sits on e540baf; `origin/main` has since moved 21 commits,
none touching a file of this diff (`git merge-tree` clean).
## Human Minimum
- [x] 1. Schemas start as Zod; `packages/spec` holds no runtime logic.
- [x] 2. Every name I added uses the casing for its kind.
- [x] 3. No formatter ran; `pnpm lint` passes.
- [x] 4. One issue, one branch off an up-to-date `main`, one pull
request.
- [x] 5. A change that publishes carries a changeset, and both
declaration lines are filled in here and in the changeset.
- [x] 6. `packages/spec` touched: its generated artifacts are
regenerated and committed.
- [x] 7. No hand edits to `content/docs/references/`,
`packages/*/CHANGELOG.md` or `content/docs/releases/`.
- [x] 8. Title, body and comments are in English.
- [x] The *Before Submitting* list in CONTRIBUTING.md is done.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_016WV4cz2MxDkQYCC4U8indM
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent df9fe94 commit 09da8c7
9 files changed
Lines changed: 623 additions & 40 deletions
File tree
- .changeset
- packages
- objectql/src
- rest/src
- spec/src/system
- types/src
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| |||
97 | 98 | | |
98 | 99 | | |
99 | 100 | | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
100 | 127 | | |
101 | 128 | | |
102 | 129 | | |
| |||
111 | 138 | | |
112 | 139 | | |
113 | 140 | | |
114 | | - | |
115 | | - | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
116 | 159 | | |
117 | 160 | | |
118 | 161 | | |
| |||
137 | 180 | | |
138 | 181 | | |
139 | 182 | | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
140 | 189 | | |
141 | 190 | | |
142 | 191 | | |
| |||
147 | 196 | | |
148 | 197 | | |
149 | 198 | | |
150 | | - | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
151 | 206 | | |
152 | 207 | | |
153 | 208 | | |
| 209 | + | |
154 | 210 | | |
155 | 211 | | |
156 | 212 | | |
| 213 | + | |
157 | 214 | | |
158 | 215 | | |
159 | 216 | | |
| |||
173 | 230 | | |
174 | 231 | | |
175 | 232 | | |
176 | | - | |
| 233 | + | |
177 | 234 | | |
178 | 235 | | |
179 | | - | |
180 | | - | |
181 | | - | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
182 | 241 | | |
183 | 242 | | |
184 | 243 | | |
185 | 244 | | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
186 | 272 | | |
187 | 273 | | |
188 | 274 | | |
| |||
210 | 296 | | |
211 | 297 | | |
212 | 298 | | |
213 | | - | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
214 | 354 | | |
215 | 355 | | |
216 | 356 | | |
| |||
0 commit comments