Skip to content

Commit 6844e6b

Browse files
fix(objectql): a following amount whose parent code field is no longer served is refused with a message that says so (#34024)
<!-- claude-projects-attribution: {"github_login":"zhuangjianguo"} --> _Requested by **Steedos** · [project thread](https://claude.ai/code/project/chan_018ETKaNwMZCNRYD7D8Q9Ubb?thread=cmsg_018ETKaNwMZCNRYD7D8Q9UbbTGhxJSYAmQXU7efVdNqrpw)_ Fixes #33888 Clause-②: no Metadata-protocol: none ## 维护者速读 - 改了什么:子表金额跟随父记录币种、而父对象已不再提供该币种字段(提供它的扩展包被停用或卸载)时,写入仍以 `ERR_CURRENCY_CODE_REQUIRED` / 400 拒绝,但提示改为说明"父对象不再提供该字段",并在注册表仍记得时点名被停用的包。 - 为什么改:旧提示说"父记录币种为空,请先写入币种",而那个字段已经不存在,照做只会得到另一个错误。 - 风险与回滚:只改提示文案;错误码、状态码、`field`、`amountField` 不变,币种字段确实为空时的原文案一字不变。回滚即还原本 PR。 - 先看:`packages/objectql/src/validation/record-currency-code.ts` 的 `parentCodeFieldServingOf` 与 `codeRequiredMessage` 新分支;再看 `record-currency-guards.test.ts` 末尾新增的 describe。 - 两分钟验证:`pnpm --filter @objectstack/objectql exec vitest run --project local src/record-currency-guards.test.ts -t "no longer served"`,预期 2 个用例通过。 - 需要你做的:无,席位落地。 ## What changes `RecordCurrencyCodeRequiredError` gains a third case for a following amount (`currencyField: 'MASTER_DETAIL.CODE'`): the parent object, as served at the write, declares no field under the code segment. Before, that case fell into the blank-code text, which asks for a code to be written into the field that is not there. Now the refusal reads, for the measured disable path: > Insert on 'suite_line' was REFUSED: 'amount' holds an amount in its parent record's currency (currencyMode: 'record', following 'invoice.currency_code' through the master_detail 'invoice'), and 'suite_invoice' does not serve a field 'currency_code' (the package 'com.test.supplier' that contributed it is disabled); the amount has no code to follow until that field is served again, or leave 'amount' empty. The row was not written. - **Package clause.** The registry keeps a disabled package's `extend` contribution, so on the disable path the package is named. An uninstall removes the contribution, so there the clause is the card's generic one: "(a package that contributed it may be disabled or uninstalled)". - **Where the fact comes from.** The path pair is resolved in `recordCurrencyPairs` / `recordCurrencyPathOf` from the child's own field map only, so the guard cannot know what the parent serves. `assertRecordCurrencyCodes` takes one new optional argument, a `ParentCodeFieldServing` reader. `parentCodeFieldServingOf(registry)` builds it from `getObject` (the served fold, a disabled extension left out), `getObjectContributors` and `isPackageDisabled`. The four write-side callers pass it: insert, by-id update, predicate update and the `validate()` preview, in `engine-insert.ts`, `engine-update.ts` (two sites) and `engine-validate.ts`. It is asked only after a pair is refused, about the first refused pair, so the accepted write path reads nothing new. - **Unchanged.** `ERR_CURRENCY_CODE_REQUIRED` / 400, `field`, `fields`, `amountField`, and the full text for a served code field that is blank. A parent object that is not served at all keeps today's text. The guard is not exported from the package entry, so the published API surface does not move. ## Tests `packages/objectql/src/record-currency-guards.test.ts` (the shared harness for this guard) gains one top-level describe on the measured fixture: `billing` owns `suite_invoice` and `suite_line`, `supplier` extends `suite_invoice` with `currency_code`, `suite_line.amount` follows `invoice.currency_code`. - disable path: after `disablePackage(supplier)` a child insert is refused `ERR_CURRENCY_CODE_REQUIRED` / 400 with `field` `invoice.currency_code`, the message carries the not-served clause naming the disabled package, and does not carry "has a blank". The same test then uninstalls the supplier and asserts the generic package clause. - CONTROL: under a served but blank parent code, the message equals today's full text. Ablation (committed fix first, mutation through `scripts/ablation-replace.mjs`, which restored the blob to HEAD and found `git diff HEAD` empty): replacing the reader call `parentCodeServing?.(path.master, path.code)` with `undefined` turns the disable-path test red and leaves the blank-text CONTROL green. ## Acceptance notes - `Clause-②: no` confirmed against the ADR-0112 ledger row for `ERR_CURRENCY_CODE_REQUIRED` (`packages/spec/src/api/error-code-ledger.zod.ts`): it records the code and status, not the text. - The lifecycle doors' own behaviour stays parked on #33628 (seat ruling D). This PR changes only what the write's refusal says. ## Verification Readings at head `d50cea4559` (branch merged with `origin/main` at `0ac112fcd`), each command under `scripts/pm/os-verify-lock.sh` where it builds or tests: - `pnpm --filter @objectstack/objectql typecheck`: tsc clean; `check:test-typecheck: OK` (39 files / 233 errors / 64 pinned signatures held, unchanged). - `pnpm --filter @objectstack/objectql exec vitest run --project local --maxWorkers=2`: VERDICT command-exit 0, Test Files 598 passed (598), Tests 13235 passed (13235). - `record-currency-guards.test.ts` alone: 73 passed (73), the two new tests included. - Derived gate families (`node scripts/pm/dispatch-gates.mjs --commands`, 68 families at `d50cea4559`): 66 run locally, every one exit 0. `check:dual-build-cjs-loads` is left to CI; `check:type-check-debt` runs under the lock and its reading is in the report comment on #33888. - `eslint --no-inline-config` on the five touched source files: exit 0. `check-nul-bytes`: OK. - `test:repo` is not run locally: the diff changes no repo-project test and the derivation names none; CI's `Test Core` runs it. <details><summary>Ablation output (disable-path test, mutation landed and restored)</summary> ```text ablation-replace: ok mutation landed: anchor 1 -> 0, blob c74cd8b -> f833530689a9 × disable: the supplier disabled, a line with an amount is refused naming the field as not served and the disabled package Tests 1 failed | 1 passed | 71 skipped (73) Received: "... and the 'suite_invoice' record it names has a blank 'currency_code', or it names none. The row was not written. Write the parent's ISO 4217 code (for example 'USD') into 'suite_invoice.currency_code' first ..." ablation-replace: ok restored: blob == HEAD (c74cd8b) and `git diff HEAD` is empty ``` </details> Session: https://claude.ai/code/session_01QsR2jTEivi2wogPpeo3xd5 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent f81e4b6 commit 6844e6b

6 files changed

Lines changed: 207 additions & 35 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'@objectstack/objectql': patch
3+
---
4+
5+
fix(objectql): a following amount whose parent code field is no longer served is refused with a message that says so
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
A `record`-mode amount that follows its parent's currency (`currencyField: 'MASTER_DETAIL.CODE'`) is still refused `ERR_CURRENCY_CODE_REQUIRED` / 400 when the parent object, as served at the write, no longer declares the code field (the package that contributed it was disabled or uninstalled). The message used to say the parent's code was blank and asked for a code to be written into that field, which is not there. It now says the parent does not serve the field, names the disabled package when the registry still holds its contribution, and says the amount has no code to follow until the field is served again, or that the amount can be left empty. The code, status, `field` and `amountField` are unchanged, and so is the message for a served code field that is blank.

‎packages/objectql/src/engine-insert.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ import { ASSIGNMENT_RULE_TYPE, AssignmentRuleEvaluator, OWNERSHIP_QUEUE_SOURCE_S
5151
import type { AssignmentQueueService } from './assignment-rules.js';
5252
import { redactBoundStatement } from '@objectstack/types';
5353
import { ORGANIZATION_OBJECT } from './tenancy/system-write-organization.js';
54-
import { assertRecordCurrencyCodes, followParentCurrencyCodes, recordCurrencyPairs } from './validation/record-currency-code.js';
54+
import { assertRecordCurrencyCodes, followParentCurrencyCodes, parentCodeFieldServingOf, recordCurrencyPairs } from './validation/record-currency-code.js';
5555
import { planHomeAmounts, projectHomeAmounts } from './home-amount-projection.js';
5656
import { stripSearchCompanion } from './search-companion.js';
5757
import type { EvalPermissions } from '@objectstack/formula';
@@ -1337,7 +1337,7 @@ export class InsertFacet {
13371337
// [ADR-0144 D8 rule 4] A `record`-mode amount is stored with its
13381338
// currency code, or the row is refused (`ERR_CURRENCY_CODE_REQUIRED`).
13391339
// [ADR-0144 D2, amended] A child amount's code is its parent's, laid on a copy of the row.
1340-
assertRecordCurrencyCodes(object, (schemaForValidation as { fields?: unknown } | undefined)?.fields, rows[i] && typeof rows[i] === 'object' ? followCodes(rows[i]) : rows[i], 'insert', undefined, undefined, msgCtx);
1340+
assertRecordCurrencyCodes(object, (schemaForValidation as { fields?: unknown } | undefined)?.fields, rows[i] && typeof rows[i] === 'object' ? followCodes(rows[i]) : rows[i], 'insert', undefined, undefined, msgCtx, parentCodeFieldServingOf(this._registry));
13411341
// [#29029] A write that is not a system write has its references
13421342
// checked BEFORE the record's validation rules: a reference the
13431343
// caller cannot read answers `reference_not_found`, whatever a rule

‎packages/objectql/src/engine-update.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import { NotNullRefusalError, envelopeDriverWriteRefusal } from './not-null-refu
4949
import { redactBoundStatement } from '@objectstack/types';
5050
import { ORGANIZATION_OBJECT } from './tenancy/system-write-organization.js';
5151
import { rejectCredentialEvaluation } from './credential-evaluation-guard.js';
52-
import { assertRecordCurrencyCodes, followParentCurrencyCodes, recordCurrencyPairs, touchesRecordCurrency } from './validation/record-currency-code.js';
52+
import { assertRecordCurrencyCodes, followParentCurrencyCodes, parentCodeFieldServingOf, recordCurrencyPairs, touchesRecordCurrency } from './validation/record-currency-code.js';
5353
import { homeAmountsTouched, planHomeAmounts, projectHomeAmounts, settleBulkHomeAmounts } from './home-amount-projection.js';
5454
import { assertFollowedCurrencyCodesUnchanged, parentCurrencyLockReads, touchesFollowedCurrencyCode } from './record-currency-parent-lock.js';
5555
import { stripSearchCompanion } from './search-companion.js';
@@ -1545,7 +1545,7 @@ export class UpdateFacet {
15451545
: undefined;
15461546
assertRecordCurrencyCodes(
15471547
object, currencyFields, followCodes ? followCodes(currencyImage) : currencyImage,
1548-
'update', currencyPayload, hookContext.input.id, updateMsgCtx,
1548+
'update', currencyPayload, hookContext.input.id, updateMsgCtx, parentCodeFieldServingOf(this._registry),
15491549
);
15501550
}
15511551
// [ADR-0144 D2, amended] …and a record a child row follows the
@@ -1954,7 +1954,7 @@ export class UpdateFacet {
19541954
recordCurrencyPairs(currencyFields), images.map((i) => i.image), this.homeAmountServing(opCtx.context).parentRows,
19551955
);
19561956
for (const { row, image } of images) {
1957-
assertRecordCurrencyCodes(object, currencyFields, followCodes(image), 'update', currencyPayload, row?.id, updateMsgCtx);
1957+
assertRecordCurrencyCodes(object, currencyFields, followCodes(image), 'update', currencyPayload, row?.id, updateMsgCtx, parentCodeFieldServingOf(this._registry));
19581958
}
19591959
}
19601960
// [ADR-0144 D2, amended] The predicate-path twin of the

‎packages/objectql/src/engine-validate.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import type { WriteObservabilityOptions } from '@objectstack/spec/contracts';
5050
import { validateRecordInScope, normalizeBlankTypedValues, normalizeNumericStringValues, ValidationError, valueShapeStrictEffective, mediaStrictEffective } from './validation/record-validator.js';
5151
import { evaluateValidationRules, hasParentScopedRequiredWhen, stripReadonlyFields, stripRuntimeOwnedFields } from './validation/rule-validator.js';
5252
import { createBatchSummaryAccumulator } from './summary-batch-accumulation.js';
53-
import { assertRecordCurrencyCodes, followParentCurrencyCodes, recordCurrencyPairs, RecordCurrencyCodeRequiredError, touchesRecordCurrency } from './validation/record-currency-code.js';
53+
import { assertRecordCurrencyCodes, followParentCurrencyCodes, parentCodeFieldServingOf, recordCurrencyPairs, RecordCurrencyCodeRequiredError, touchesRecordCurrency } from './validation/record-currency-code.js';
5454
import { assertFollowedCurrencyCodesUnchanged, parentCurrencyLockReads, touchesFollowedCurrencyCode } from './record-currency-parent-lock.js';
5555
import { isHomeAmountRefusal } from './home-amount-projection.js';
5656
import { referencesCheckedBeforeRules } from './engine-referential-integrity.js';
@@ -580,7 +580,7 @@ export async function validate(
580580
assertRecordCurrencyCodes(
581581
object, currencyFields,
582582
followCodes && currencyImage ? followCodes(currencyImage) : currencyImage, mode, mode === 'insert' ? undefined : row, storedIds[i],
583-
messages,
583+
messages, parentCodeFieldServingOf(this._registry),
584584
);
585585
// [ADR-0144 D2, amended] …and the write's parent-code lock, on the
586586
// stored row the by-id update writes: a record a child row follows

‎packages/objectql/src/record-currency-guards.test.ts‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,3 +1342,67 @@ describe('a single-object save is refused a `dynamic` roll-up over a child field
13421342
expect(() => save(valid.engine, project('ext_home'))).not.toThrow();
13431343
});
13441344
});
1345+
1346+
// ── [ADR-0144 D2, amended] A followed parent code field the parent no longer serves ──
1347+
//
1348+
// The fixture is the package-lifecycle measurement's shape: `billing` owns
1349+
// `suite_invoice` and `suite_line`, `supplier` extends `suite_invoice` with the
1350+
// code field, and `suite_line.amount` follows `invoice.currency_code`. After the
1351+
// supplier leaves, the next child write is still refused ERR_CURRENCY_CODE_REQUIRED
1352+
// / 400, and the message says the field is not served instead of asking for a
1353+
// code to be written into a field that is not there.
1354+
1355+
describe('a following amount whose parent code field is no longer served is refused, and the message says so (ADR-0144 D2, amended)', () => {
1356+
const BILLING = 'com.test.billing';
1357+
const SUPPLIER = 'com.test.supplier';
1358+
const NOT_SERVED = "and 'suite_invoice' does not serve a field 'currency_code' (";
1359+
const NO_CODE_TO_FOLLOW = "; the amount has no code to follow until that field is served again, or leave 'amount' empty. The row was not written.";
1360+
1361+
async function bootSuite() {
1362+
const booted = await bootEngine();
1363+
booted.engine.registerApp({
1364+
id: BILLING, name: 'billing',
1365+
objects: [
1366+
{ name: 'suite_invoice', fields: { title: { type: 'text' } } },
1367+
{ name: 'suite_line', fields: { title: { type: 'text' }, invoice: { type: 'master_detail', reference: 'suite_invoice' }, amount: { type: 'currency', currencyConfig: FOLLOWS } } },
1368+
],
1369+
} as any);
1370+
booted.engine.registerApp({ id: SUPPLIER, name: 'supplier', objectExtensions: [{ extend: 'suite_invoice', fields: { currency_code: CODE_FIELD } }] } as any);
1371+
await booted.engine.insert('suite_invoice', { id: 'inv_usd', title: 'usd', currency_code: 'USD' });
1372+
await booted.engine.insert('suite_invoice', { id: 'inv_blank', title: 'blank' });
1373+
// CONTROL: while the supplier serves the field, a line under a parent holding a code is written.
1374+
await booted.engine.insert('suite_line', { title: 'before', invoice: 'inv_usd', amount: 100 });
1375+
return booted;
1376+
}
1377+
1378+
it('disable: the supplier disabled, a line with an amount is refused naming the field as not served and the disabled package', async () => {
1379+
const { engine, storeFor } = await bootSuite();
1380+
engine.registry.disablePackage(SUPPLIER);
1381+
expect((engine.registry.getObject('suite_invoice')?.fields as any)?.currency_code).toBeUndefined();
1382+
const err = await refusalOf(engine.insert('suite_line', { title: 'after', invoice: 'inv_usd', amount: 100 }));
1383+
expect(envelopeOf(err)).toEqual(CODE_REQUIRED);
1384+
expect(err?.field).toBe('invoice.currency_code');
1385+
expect(err?.message).toContain(`${NOT_SERVED}the package '${SUPPLIER}' that contributed it is disabled)${NO_CODE_TO_FOLLOW}`);
1386+
expect(err?.message).not.toContain('has a blank');
1387+
expect(storeFor('suite_line').size).toBe(1);
1388+
// Uninstalled, the registry no longer holds the contribution: the package clause is the generic one.
1389+
engine.registry.uninstallPackage(SUPPLIER);
1390+
const gone = await refusalOf(engine.insert('suite_line', { title: 'gone', invoice: 'inv_usd', amount: 100 }));
1391+
expect(envelopeOf(gone)).toEqual(CODE_REQUIRED);
1392+
expect(gone?.message).toContain(`${NOT_SERVED}a package that contributed it may be disabled or uninstalled)${NO_CODE_TO_FOLLOW}`);
1393+
});
1394+
1395+
it('CONTROL: a served code field that is blank on the parent keeps the blank-code text', async () => {
1396+
const { engine } = await bootSuite();
1397+
const err = await refusalOf(engine.insert('suite_line', { title: 'blank', invoice: 'inv_blank', amount: 100 }));
1398+
expect(envelopeOf(err)).toEqual(CODE_REQUIRED);
1399+
expect(err?.message).toBe(
1400+
"Insert on 'suite_line' was REFUSED: 'amount' holds an amount in its parent record's currency (currencyMode: 'record', "
1401+
+ "following 'invoice.currency_code' through the master_detail 'invoice'), and the 'suite_invoice' record it names "
1402+
+ "has a blank 'currency_code', or it names none. The row was not written. Write the parent's ISO 4217 code (for "
1403+
+ "example 'USD') into 'suite_invoice.currency_code' first, and name that parent in 'invoice', or leave 'amount' "
1404+
+ 'empty. A child stores no code of its own: it follows its parent\'s. The server never fills a currency in: a '
1405+
+ 'form pre-fills the organization\'s currency, and the stored code is always the one submitted.',
1406+
);
1407+
});
1408+
});

0 commit comments

Comments
 (0)