You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit b070e1c
Browse filesBrowse the repository at this point in the historyBrowse files
fix(core): an import lookup naming a detail resolves within the row's own master (#33613)
<!-- claude-projects-attribution: {"github_login":"os-elon-musk"} -->
_Requested by **Elon** · [project
thread](https://claude.ai/code/project/chan_01WedDzV26nniwxv7jC88B9D?thread=cmsg_01WedDzV26nniwxv7jC88B9DFWmDYdUPFhME7Z7Ky3ySCZ)_Fixes#33523
Area-lease pilot #33059, area A3.
Clause-②: yes (narrowing) — widening: a row whose master identifies one
of several same-named details now resolves it, where it was refused
`reference_ambiguous`; `@objectstack/core` gains the export
`referenceScopeSources`, the types `ReferenceMaster` /
`ReferenceScopeSource`, and optional parameters on `RefResolver`,
`coerceFieldValue`, `coerceRow`, `CoerceContext` and `ExportFieldMeta`.
Narrowing: a row that names a master and a detail only another master
carries is refused `reference_not_found`, where that detail was linked
across masters; and a caller who cannot read the target's master field
(or a `dependsOn` `param` field) is refused `reference_not_found` for a
scoped lookup cell that the unscoped lookup resolved, because the scope
puts that field into the caller's own filter and a filter on a field the
caller cannot read is refused.
Metadata-protocol: none
Skills-docs: `content/docs/data-modeling/import-mappings.mdx` gains "A
lookup cell is resolved within the row's own master";
`skills/objectstack-data/rules/relationships.md` names the scoping under
`master_detail`.
## 维护者速读
- What changed: a data import resolving a lookup cell now searches only
within the master record the same row sets, when the target is that
master's detail (`master_detail`) or the field declares `dependsOn`.
- Why: with two contracts each carrying an "Advance 30%" billing step,
an invoice import row failed with `reference_ambiguous` although its own
contract picks one; the record form's picker already narrows by contract
(measured by HotCRM's invoice import template).
- Risk and rollback: two answers narrow. A row naming contract B and a
step that only contract A has used to link A's step; it now fails
`reference_not_found`. A caller who cannot read the step's contract
field now gets `reference_not_found` for a step the row scopes by
contract, even when its name is unique. Remedy for both: correct the
master cell, or leave the master column out and set it in a second
update. Rollback is a revert of this PR; no data migration.
- Look first: `referenceScopeSources` and the two-pass `coerceRow` in
`packages/core/src/utils/import-coerce.ts`; the `where` merge and cache
key in `packages/core/src/utils/import-runner.ts`; the new
`import-runner-reference-master-scope.test.ts`.
- Two-minute check: `pnpm --filter @objectstack/core exec vitest run
src/utils/import-runner-reference-master-scope.test.ts`; expect 10
passed.
- What you need to do: this PR touches `skills/**` (Tier H), so it lands
by the maintainer's approval and merge.
## What changes
- **Scope sources** (`referenceScopeSources`,
`packages/core/src/utils/import-coerce.ts`). For a reference column of
the row, the scope is built from (1) the field's own `dependsOn` (string
form: same name on both sides; `{ field, param }`: target field `param`)
when the row fills the sibling, and (2) each `master_detail` field the
lookup TARGET declares, when exactly one other single-value reference
column of the row points at that master object and is filled. Two such
columns (bill-to / ship-to) do not say which is the master, so neither
scopes.
- **Ordering** (`coerceRow`). Unscoped columns coerce first in row
order; a scoped column coerces after its source columns, with their
coerced ids as its scope. A failed or non-scalar source scopes nothing.
When no pending column is ready (a cycle, or a column whose source waits
behind one), every pending column resolves unscoped, as on `main`.
Errors are still reported in the row's column order. An optional
`sourceRow` lets the match-key pass (`rowsSharingKey`) read the scope
from the whole row, so a key column naming a detail groups by the same
id the row's own pass resolves, for one level of master: a source column
outside the key columns is coerced unscoped, so in a chain (account →
contract → milestone) the two passes can differ.
- **Resolver** (`import-runner.ts`). The scope is merged into every
candidate's `where` (`{ [candidate]: display, ...scope }`), so a tie and
a miss are judged inside it; the cache key carries the scope; a
candidate the scope already fixes is skipped. The read is the same
`findData` with the same caller `context` as before, so RLS is unchanged
and a record the caller cannot read never matches. FLS is not unchanged:
the scope puts the target's master field (or a `dependsOn` `param`) into
the caller's own `where`, plugin-security's predicate guard refuses a
caller `where` on a field the caller cannot read, and the resolver reads
that throw as no match. So a caller who cannot read that field gets
`reference_not_found` for a scoped cell that resolved on `main`. It
fails closed and discloses nothing; it is declared in the `Clause-②`
narrowing, the changeset and the docs (option (b) of review#2's S1), and
the unscoped retry (option (a)) is not implemented. The target's masters
are read off the same served-target declaration the resolver already
reads (`servesReferenceTarget`, now also `masters` via
`referenceTargetOf`), so a target the caller cannot serve answers no
masters.
- **Field meta** (`import-field-meta.ts`). `ExportFieldMeta.dependsOn`
carries the normalised `{ field, param }` pairs; present only when
declared.
- **Remedy for a narrowed row** (in the changeset and the docs). A
pasted id is scoped too, so a row that means another master's detail has
no way through in one import: correct the master cell, or, when the
cross-master link is intended, leave the master column out of the import
and set it in a second update. For the field-permission case, grant the
caller read access to that field, or leave the master column out the
same way.
- **Docs.** `content/docs/data-modeling/import-mappings.mdx` (new
subsection under cell coercion, with the field-permission case and the
remedy) and the data skill's relationships rule (5 lines under
`master_detail`). The docs and changeset say the `dependsOn` scope is
the filter the record form's picker applies, and that the
`master_detail` inference has no picker counterpart, so for a lookup
without `dependsOn` the import is stricter than the form.
- Not touched: `packages/spec/**`, the REST import door
(`packages/rest/src/import*.ts` feeds `runImport` unchanged; the
premise's "likely code" there was measured and the fix lives entirely in
core), no stored-data migration.
## Why
Measured premise on `origin/main` 4bba8b8: `runImport`'s resolver
queried `where: { [field]: display }` over the whole target for every
candidate and returned `ambiguous` on two hits; nothing read the row's
other columns. The record picker scopes by `dependsOn` / the master, so
the two doors disagreed.
## Tests
New
`packages/core/src/utils/import-runner-reference-master-scope.test.ts`
(10 cases, fake protocol answering equality over every `where` key):
- acceptance 1: two contracts each with "Advance 30%"; rows naming A and
B link `m1` and `m2`; column order does not matter.
- acceptance 2: a row with no (or a blank) master cell is
`reference_ambiguous`; a unique name still resolves object-wide.
- acceptance 3: a name absent under the row's master, a name unique
object-wide but under another master, and a pasted id of another
master's detail are all `reference_not_found`.
- every read carries the caller's context; two master columns stay
unscoped; a field's own `dependsOn` scopes a plain lookup; a master cell
that fails scopes nothing.
- the match-key pass: two keys under different masters are two keys; a
key naming another master's detail answers `reference_not_found` on that
row alone (unscoped it would fail both rows as a duplicate key).
Ablations (one-shot, restored, `scripts/ablation-replace.mjs`; tests
import `src` directly, no dist involved):
- `where: { [f]: display, ...(scope ?? {}) }` → `where: { [f]: display
}`: 7 failed | 3 passed (the 3 that stay green are the unscoped cases).
Restored: blob == HEAD `f33b0cb042ea`, `git diff HEAD` empty.
- `coerceRow(cells, metaMap, coerceCtx, rows[i])` → without `rows[i]`: 1
failed | 9 passed (the match-key case). Restored likewise.
## Acceptance notes
- `dependsOn` is honoured as the picker reads it (`param` names the
target field). A `dependsOn` entry naming a column the row leaves blank
scopes nothing.
- Review#2 nit N3 is carried, not fixed here: the scoped and unscoped
cache keys share one string space (`obj::display` against
`obj::display::[...]`), so a display value that itself ends in a
serialized scope could collide within one import and one caller.
- The HotCRM import template text
(`packages/rest/src/import-template.ts` `howToFill`) still says a name
matching more than one record is refused; it is not wrong for a row
without its master and was left as is.
## Verification
Readings at head `81e7c21e` (this round's commit edits the changeset,
`import-mappings.mdx` and two docblocks in `import-coerce.ts` /
`import-runner.ts`; no code line changes since `dad002e6ec`). Every
build and test below ran under `scripts/pm/os-verify-lock.sh`, and each
reading is quoted from that run with its `VERDICT command-exit` line.
<details><summary>CI reds on earlier heads, and their fixes</summary>
- `7b9cc0b17a`: `TypeScript Type Check` red (TS2339 on `where.contract`
in the new test, `check:test-typecheck`). Fixed by `9467884463`, which
types the scoped `where` read.
- `9467884463`: `Test Core` shards 4/8 and 5/8 red on two `test:repo`
pins, both fixed by `dad002e6ec`:
- core `src/security/second-object-read-exposure.pin.test.ts`:
`import-runner.ts` now calls `referenceTargetOf` (in `mastersOf`) and
was not classified. It is now a `CALLERS` row, classified as `executes`:
`mastersOf` runs inside `servesReferenceTarget`, after the target's
exposure is asked.
- rest `src/rest-server-canonical-query-ast.test.ts`: it pinned the old
literal `where: { [f]: display }`; it now pins `where: { [f]: display,
...(scope ?? {}) }`.
- The earlier local runs used each package's `test` script (vitest
project `local`), which does not include `test:repo` (project `repo`).
That is why the core and rest greens reported in round 1 missed both
pins. This round ran `test:repo` for core and rest as well (below).
</details>
<details><summary>Core and rest: test, test:repo, typecheck</summary>
- `pnpm --filter @objectstack/core typecheck`: `VERDICT command-exit 0`.
- `pnpm --filter @objectstack/core test`: Test Files 147 passed (147),
Tests 3390 passed (3390); `VERDICT command-exit 0`.
- `pnpm --filter @objectstack/core run test:repo`: Test Files 8 passed
(8), Tests 103 passed (103); `VERDICT command-exit 0`.
- `pnpm --filter @objectstack/rest run test:repo`: Test Files 5 passed
(5), Tests 300 passed, 23 skipped (323); `VERDICT command-exit 0`.
</details>
<details><summary>Direct dependents that call the changed code (run
locally at 81e7c21)</summary>
The closure `--filter '@objectstack/core' --filter
'@objectstack/objectql^...' --filter '@objectstack/plugin-auth^...'
--filter '@objectstack/service-automation^...' --filter
'@objectstack/runtime^...' --filter '@objectstack/rest^...'` was built
first (`VERDICT command-exit 0`). Each run is the package's `test`
script, not `test:repo`.
- `@objectstack/objectql` (`runImport` against the real engine): Test
Files 595 passed (595), Tests 13028 passed (13028); `VERDICT
command-exit 0`.
- `@objectstack/plugin-auth` (`admin-import-users` runs `runImport`):
Test Files 274 passed (274), Tests 5303 passed, 10 skipped (5313);
`VERDICT command-exit 0`.
- `@objectstack/service-automation` (connector pull through
`runImport`): Test Files 281 passed (281), Tests 3916 passed (3916);
`VERDICT command-exit 0`.
- `@objectstack/runtime`: Test Files 602 passed (602), Tests 10781
passed, 19 skipped (10800); `VERDICT command-exit 0`.
- `@objectstack/rest` (the import door): Test Files 411 passed (411),
Tests 8393 passed, 332 skipped (8725); `VERDICT command-exit 0`.
`@objectstack/core` has 62 direct dependents. The five above are the
ones whose source reaches `runImport`, `coerceRow`, `coerceFieldValue`,
`buildFieldMetaMap` or `ExportFieldMeta` (`git grep` over `packages/`).
CI's `Test Core` shards cover 61 of the 62; `@objectstack/dogfood` runs
in the merge queue's `Dogfood Regression Gate`.
</details>
<details><summary>Derived gate families (`dispatch-gates.mjs`, 104
families, tree 81e7c21)</summary>
- `node scripts/pm/dispatch-gates.mjs --ran` with every exit code
recorded: 104 derived, 101 run, 3 NOT MEASURED, 0 UNRUN.
- Exit 0: 101, including `check-changeset-no-major`,
`check-adr-0087-registration` (`[clause-②-narrowing] not-required
(no-migration-prescription)`), `check-empty-changeset`,
`check:changeset-gate-self-tests`, `check:doc-authoring`,
`check:doc-anchors`, `check:docs-*`, `check:skill-links`,
`check:nul-bytes`, `check:issue-citations` and `check:type-check-debt`.
- NOT MEASURED locally, prerequisite not met (exit 3), all run by CI:
`check-plugin-teardown-shape --self-test` (needs an unshallow clone),
`@objectstack/spec` `check:skill-examples`, and
`check:dual-build-cjs-loads` (needs a full `pnpm build`).
</details>
<details><summary>Published skills readings</summary>
- Unchanged this round.
`skills/objectstack-data/rules/relationships.md`: 399 → 393 lines; 4127
(ceiling) → 4098 tokens.
- All published `SKILL.md`: 4597 → 4597 lines (none edited).
- Whole `skills/` tree: 20679 → 20673 lines.
</details>
CI at `81e7c21e`: 68 check runs, 58 success and 10 skipped, none failed.
The successes include `Test Core`, `TypeScript Type Check`, `Type Check
· workspace`, `Build Core`, `Build Docs`, `Lint & Repo Gates`, `Lint ·
ESLint` and `Check Changeset`. The skipped runs include the dogfood
shards, which do not run on a pull request.
Dogfood: the full battery was not completed locally (the round-1 run
printed 1689 passing lines and then stalled, with no summary, so no
count is claimed). It runs in the merge queue's required `Dogfood
Regression Gate` before this lands. The three dogfood files that drive
the import on a real engine were run at `81e7c21e` after building the
dogfood closure (`pnpm --filter @objectstack/dogfood exec vitest run
--maxWorkers=2 test/import-lands-readable-rows.dogfood.test.ts
test/import-reference-exposure.dogfood.test.ts
test/import-user-field-target.dogfood.test.ts`): Test Files 3 passed
(3), Tests 17 passed (17); `VERDICT command-exit 0`.
## 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 (eslint on the changed
files: exit 0; the repo-wide run is CI's).
- [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: not touched.
- [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.
- [ ] The *Before Submitting* list in CONTRIBUTING.md is done.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_016WV4cwotoQMyrtSr5EqqAF
<!-- signed: domain:server#5 · os-elon-musk ·
session_016WV4d1QwJJ5KGZTThP4fnq -->
---------
Co-authored-by: Claude <noreply@anthropic.com>
fix(core): a data import resolves a lookup cell naming a detail record within the master the same row names (#33523)
7
+
8
+
Clause-②: yes (narrowing) — widening: a row whose master identifies one of several same-named details now resolves it, where it was refused `reference_ambiguous`; `@objectstack/core` exports `referenceScopeSources` and the types `ReferenceMaster` and `ReferenceScopeSource`, `RefResolver` and `coerceFieldValue` take an optional scope, `coerceRow` an optional source row, `CoerceContext` an optional `referenceMasters`, and `ExportFieldMeta` an optional `dependsOn`. Narrowing: a row that names a master and a detail only another master carries is refused `reference_not_found`, where the detail was linked across masters; and a caller who cannot read the target's master field (or a `dependsOn``param` field) is refused `reference_not_found` for a scoped lookup cell that the unscoped lookup resolved, because the scope puts that field into the caller's own filter and a filter on a field the caller cannot read is refused.
9
+
Metadata-protocol: none
10
+
Skills-docs: `content/docs/data-modeling/import-mappings.mdx` gains "A lookup cell is resolved within the row's own master"; `skills/objectstack-data/rules/relationships.md` names the scoping under `master_detail`.
11
+
12
+
<!-- adr-0087: not-required (no-migration-prescription) No key of any authorable schema is added, removed, renamed or re-shaped. `master_detail` and `dependsOn` are read as declared; the change is in how the import runtime resolves a cell, and no stored row is rewritten. -->
13
+
14
+
**What changes.** A detail's name is often unique only inside its master: two contracts can each carry an "Advance 30%" billing milestone. The import resolved a lookup cell across every record of the target, so a row naming contract B and "Advance 30%" failed with `reference_ambiguous`. A lookup cell is now resolved within the row's own master when either:
15
+
16
+
- the lookup's target declares a `master_detail` field, and exactly one other single-value reference column of the row points at that master object and is filled in; or
17
+
- the lookup field declares `dependsOn`, and the row fills in the sibling field it names (the string form compares the target field of the same name; `{ field, param }` compares `param`). This is the filter the record form's picker applies; the `master_detail` inference above has no counterpart in the picker, so for a lookup without `dependsOn` the import is stricter than the form.
18
+
19
+
Inside that scope a tie is `reference_ambiguous`, and a name that only another master carries is `reference_not_found`; it never resolves to another master's record. The same holds for the match-key pass of an update or upsert import, so a key column naming a detail is grouped by the value the row's own pass resolves, for one level of master: a master column outside the key columns is resolved unscoped in that pass, so in a chain such as account → contract → milestone the two passes can differ.
20
+
21
+
**What stays.** A row that leaves the master blank is resolved across the whole target as before, `reference_ambiguous` on a tie. Two columns of the row pointing at the master object (a bill-to and a ship-to account) do not say which one is the master, so neither scopes; `dependsOn` says it. A master cell that fails its own lookup scopes nothing. Every lookup still reads as the importing caller: the scope only adds conditions to the same read, so a record the caller cannot read never matches.
22
+
23
+
**Field permissions.** Because the scope is part of the caller's own filter, a caller who cannot read the target's master field, or the field a `dependsOn` entry names as `param`, has every scoped candidate refused, and the cell answers `reference_not_found` even for a detail the caller can read and whose name is unique. The same row resolved before this change. It fails closed and discloses nothing.
24
+
25
+
**Remedy for a narrowed row.** A pasted id is scoped too, so a row that means another master's detail has no way through in one import. Correct the master cell; or, when the cross-master link is intended, leave the master column out of the import and set it in a second update. For the field-permission case, grant the importing caller read access to that field, or leave the master column (or the `dependsOn` sibling) out in the same way.
26
+
27
+
**Docs.**`content/docs/data-modeling/import-mappings.mdx` describes the scoping under cell coercion; the data skill's relationships rule names it under `master_detail`.
read: "the data door's nested-relation filter condition (`ObjectQL.lowerRelationConditions` reads the related object to lower it), refused rather than withheld",
0 commit comments