Skip to content

Commit ee9ad83

Browse files
fix(objectql, runtime): a function's bare-name record follows its holders — a re-bind releases the old owner's claim with the function, and the one remaining holder answers the bare read (#32877)
<!-- claude-projects-attribution: {"github_login":"os-sales"} --> _Requested by **Sales** · [project thread](https://claude.ai/code/project/chan_01D5bWFY3rFzgJcytD5gD4Uu?thread=cmsg_01D5bWFY3rFzgJcytD5gD4UuEBomxh3CPXXnuthtBj5jmz)_ Fixes #32534 ## 维护者速读 1. 改了什么:多包制品重绑时,函数换了归属包(或所在包体被丢弃),旧持有包在注册表里的裸名认领随函数一起释放,只放引擎移除之后该包已不再持有的名字;某函数名的最后注册者离开、只剩一个持有者时,按裸名读函数改为答这个持有者。 2. 为什么改:改前冷启动封存后,旧认领还在,新归属包整组 hook 绑定被拒、该函数无人持有;两包持有时最后注册者一离开,裸名读落空,而另一包还持有它。 3. 风险与回滚:只放宽(Clause-② yes (widening)),被拒或落空的答案改为成功;两个及以上持有者仍不挑(两包仍 409 点名双方);第一方受影响 0;回滚即还原本 PR。 4. 先看:`packages/runtime/src/app-artifact-handlers.ts` 的拆除段与 `releaseMovedFunctions`;`packages/objectql/src/registry.ts` 的 `releaseBareNameFunctions`;`packages/objectql/src/engine-hooks-and-functions.ts` 的 `unregisterFunctionsByPackage`。 5. 两分钟验证:`pnpm --filter @objectstack/objectql exec vitest run src/registry-bare-name-one-holder-request-family.test.ts`,预期全绿(含 #32534 的 12 个新用例;runtime 另有 6 个)。 6. 需要你做的:无,席位落地。 Clause-②: yes (widening) Metadata-protocol: none ## What changes The engine's function registry (`ObjectQL`, the bare map plus the holder set `functionHoldersOf`, ADR-0150 D4) and the registry's one-holder rule (`SchemaRegistry`, the function claims the hook binder records) each keep a record of who holds a function name. Two of those records outlived the holding. This PR makes both follow the holders. 1. **`SchemaRegistry#releaseBareNameFunctions(owner, stillHolds)`** (new, `packages/objectql/src/registry.ts`): releases the owner's package's claim, recorded by `claimBareNameFunctions` at the bind, on each function name it claims that `stillHolds(name)` says it no longer holds. Only that package's claims go; each name it still holds and every other holder's claims stay. It judges nothing itself, sealed or not; an owner-less call releases nothing. 2. **The runtime's re-bind calls it** (`packages/runtime/src/app-artifact-handlers.ts`, `releaseFunctionClaims`) right after it removes functions from the engine, in both places a re-bind takes functions off an owner, and asks the engine's holder set (`functionHoldersOf(name).packageItem(<package>)`), never the binder's record of its last bind (red-line read 6839471011 F1, contract review S3): - `releaseMovedFunctions`: after the removal and after what the old owner keeps is registered again, ahead of the gaining owner's bind (card acceptance (a)); - the dropped-body teardown: after the removal of a package body the new bind no longer carries. This is the same defect class measured on the same teardown (below), fixed by the same call; it stays inside the claim's file surface ("`app-artifact-handlers.ts`, only to call the release"). - The engine removes by the exact owner string (`app:<id>`). So a function the host also registered for the package under its bare id stays held and keeps its claim, refused as on main (F1); and a function an earlier bind gave the owner that the last bind no longer named leaves with the removal and is released with it (S3). - An engine without the holder read, or a registry without the release (a host's own double), is left as it is. 3. **The bare read answers the one remaining holder** (`packages/objectql/src/engine-hooks-and-functions.ts`, `unregisterFunctionsByPackage`): when the leaving package's registration was the bare entry and exactly one holder of the name remains, that holder's entry becomes the bare entry. Two or more remaining holders get no pick. An owner-less registration never leaves with a package, so it keeps its rules. `engine.ts` docblock of `unregisterFunctionsByPackage` updated. ### Premises re-measured on `main` at `c04aa295` (scratch probes, not committed) - **(a) sealed re-bind.** A two-body artifact (alpha, the artifact's own, and beta), each body installed, bound once, `sealBareNameHolders()` (no conflict), bound again with a top-level hook naming `qa_moving`, which moves the function from beta to alpha: - beta a co-owner of alpha's namespace (declares `alpha`, depends on alpha): the second bind logs `[AppPlugin] Failed to bind declarative hooks :: Bare-name conflict: package "com.qa.bodies.alpha" … the function "qa_moving", already held by package "com.qa.bodies.beta"`; `functionHoldersOf('qa_moving').packageHolders` = `[]`; alpha's hooks do not fire (its other hook `qa_alpha_other` included); the claims still name beta. - bodies with no installed record (no namespace read): the same refusal and the same state. - two namespaces (alpha, beta): no refusal (the holders do not meet), but the claims read `[beta, alpha]` while only alpha holds the function. - **Dropped body** (same teardown): bound with body beta whose hook names `qa_beta_fn`, sealed, re-bound without beta: holders `[]`, claims `[beta]`, and a later bind of `qa_beta_fn` by another package (no namespace) is refused naming `com.qa.bodies.beta`. - On this PR's head, the same probes read: no error, holders `[alpha]`, claims `[alpha]` in all three modes; the dropped body's claims `[]` and the later bind admitted. - **(b) two-holder leave.** `registerFunction('f', …, { packageId: 'app:com.acme.alpha' })`, then beta's, then `unregisterFunctionsByPackage('app:com.acme.beta')`: `functionHoldersOf('f').packageHolders` = `["com.acme.alpha"]`, `resolveFunctionEntry('f')` = `undefined` (no throw). Also measured: the platform's (owner-less) registration then beta's, beta leaves: `undefined`; platform + alpha + beta, beta leaves: `undefined`; alpha then the platform, alpha leaves: the platform answers. On this PR's head: alpha's entry; the platform's entry; `undefined` (unchanged, two holders); the platform (unchanged). ### FROM → TO (main `c04aa295` → this PR) | # | Door | FROM | TO | Clause-② | Pin | |---|---|---|---|---|---| | 1 | `bindAppArtifactHandlers` re-bind after the cold-boot seal; a function moves from owner A to owner B, and the two meet under the rule (one namespace, or a body with no namespace read) | B's group bind throws `NAMESPACE_CONFLICT` (422) naming A, logged at error by the binder; B's hooks never bind; the moved function is held by nobody | B's whole group binds; the function is held by B; A keeps its other claims | widening | runtime (i) | | 2 | the same re-bind, A and B of two namespaces | binds, but A's claim on the moved name stays: a later bind of that name by a package that meets A (A's namespace, or none) is refused naming A | A's claim leaves with the function | widening | objectql (i) (the release), runtime (i) (the call) | | 3 | `bindAppArtifactHandlers` re-bind that drops a package body, after the seal | the dropped body's claims stay: a later bind of one of its function names by a package that meets it is refused naming the dropped body | admitted | widening | runtime (ii) | | 4 | the same re-binds before the seal (a boot that binds twice) | the seal reports the moved or dropped name as held twice (warned at boot under `'warn'`, the boot refused under `'refuse'`) | no clash reported for a released claim | widening | objectql (ii) | | 5 | `ObjectQL#resolveFunction` / `resolveFunctionEntry` after `unregisterFunctionsByPackage`: two packages held the name, the last-registered left | `undefined` | the remaining package's entry | widening | objectql (iii) | | 6 | the same, the platform's (owner-less) registration was under the leaving package's | `undefined` | the platform's entry | widening | objectql (iv) | | 7 | `unregisterFunctionsByPackage(P)` return value, when P is the remaining holder of row 5 and leaves later | `0` (no bare entry left) | `1` (the bare entry P held since row 5) | widening | objectql (iii) | | 8 | rows 1 to 3 for a function the old owner's `app:<id>` registrations held that the re-bind does not register again, after the seal, whichever way it was bound under that owner: an earlier bind of the artifact that the last bind no longer named (it stayed registered, replaced by name, as before), or a bind under that owner outside the artifact's binder (contract review S3; red-line delta read 6840550973 N6) | the removal takes it, its claim stays: a later bind of it by a package that meets the old owner is refused naming the old owner | admitted | widening | runtime (iv) moved, (v) dropped; objectql (vi) (two binds under the owner, engine level); the outside-binder bind measured by the red-line delta read (6 cells) | Unchanged (each a CONTROL pin): the first-registered holder leaving (the last-registered answers); two package holders remaining (409 naming both); a one-holder name leaving (nothing answers); an owner-less registration over a package's (never leaves with it, still answers); the platform beside one package after the last-registered package leaves (two holders: nothing picked; current behaviour, not the rule, see #32907); a package's other claims and every other package's claims after a release; an owner-less release, and one whose package still holds every name; a moved or dropped name the old owner's package still holds under its bare id (the removal is by `app:<id>`): its claim stays and the bind is refused naming it, as on main (red-line read F1; objectql (vii), runtime (vi), (vii)). When an outside package or the platform still holds a moved name, the rule is unchanged and acts on the new holder set (epic seat ruling on red-line read N1). Red line 2, for the restore: the restore answers nothing by order. A restored bare entry is the only holder the name has; with two or more holders the bare map is left as it was (two packages refuse, naming both). The bare read as a whole still answers the platform beside one package by its last registration while that registration stays; that is older than this PR and #32907 carries it. Reach: the re-bind rows need `bindAppArtifactHandlers` called again with callables on a live engine (the exported binder, or a hot reload); the JSON install-local door carries no function. Production bare readers of the function map: one, `releaseMovedFunctions`, which reads it for a one-holder name only (`git grep` of `resolveFunction` / `resolveFunctionEntry` outside tests). First-party exposure 0 (ruling 6829776521). ## Tests Every pin and ablation below is measured on `0c9fad9f` (the F1 and S3 fix, `658dc995`, and its pin fix-up); the two package suites on the same head. `398a221a` changes the changeset's wording only (red-line delta read N6). Pins added: - `packages/objectql/src/registry-bare-name-one-holder-request-family.test.ts` - (i) `[#32534] after the seal, releasing a package's claims on the names it no longer holds lets a package of its namespace bind them; a name it still holds and the other holders' claims stay`: the re-bind's removal, what alpha keeps registered again, then the release asking the engine; CONTROLs (the name alpha still holds, gamma's claim, an owner-less release, a release whose package still holds every name). - (vii) `[#32534] CONTROL — a name the package still holds under another owner spelling (its bare id) keeps its claim after the removal of its app: registrations` (red-line read F1). - (vi) `[#32534] a name an earlier bind claimed, removed with the owner's registrations, is released with the rest, not only the last bind's names` (contract review S3). - (ii) `[#32534] a claim released before the seal is no clash for the cold boot to report` (the existing ABLATION case beside it, with no release, reports it), with a CONTROL that alpha keeps its other name. - (v) `[#32534] CONTROL — two namespaces' packages claim one name after the seal; releasing one owner's claim leaves the other's, so a package of the remaining holder's namespace is still refused naming it` (contract review S1): alpha and beta of two namespaces both claim `shared_fn`, alpha's claim is released, beta's co-owner is refused naming beta, alpha's co-owner binds. - (iii), (iv) and five CONTROLs under `describe('[#32534] the bare engine function read after a holder leaves')`; the platform-plus-package CONTROL reads "current behaviour, see #32907" (contract review S2). - `packages/runtime/src/app-artifact-handlers.test.ts`, `describe('[#32534] after the seal, a re-bind takes the old owner's bare-name claim off with the function')` - (i) a function moved to another owner of its namespace: every group binds, the gaining owner's whole group fires, the function is held by it; the moved name now refuses a third co-owner naming the new holder only; CONTROL: beta still holds what it keeps. - (ii) a dropped body's function claims leave with its functions: a co-owner then binds the name; CONTROL: the kept owner keeps its claims. - (vi) CONTROL, moved: the old owner's package also holds the moved function under its bare id; the gaining owner's group is refused naming it and beta keeps its claim, as on main (red-line read F1). - (vii) CONTROL, dropped: the same for a dropped body's function (F1). - (v) dropped: a function an earlier bind gave beta, which the last bind no longer named, leaves the engine and its claim with the body; a co-owner binds it (S3). - (iv) moved: the same name through `releaseMovedFunctions` (beta keeps a hook of its own, so it stays an owner); a co-owner binds it; CONTROL: beta's kept name keeps its claim (S3). <details><summary>Suites</summary> - `pnpm --filter @objectstack/objectql exec vitest run src/registry-bare-name-one-holder-request-family.test.ts`: 68/68. - `pnpm --filter @objectstack/runtime exec vitest run src/app-artifact-handlers.test.ts` (objectql dist rebuilt from this head): 34/34. - `pnpm --filter @objectstack/objectql test` and `pnpm --filter @objectstack/runtime test` on `0c9fad9f`: objectql 590 files, 12622/12622; runtime 588 files, 10257 passed, 19 skipped. - `pnpm --filter @objectstack/objectql typecheck` and `pnpm --filter @objectstack/runtime typecheck`: exit 0. eslint on the four changed source and test files: exit 0. - The derived gate set (`dispatch-gates.mjs --commands`, 73 commands) on bc9dbb6: in the os-dev-report on the card. The review delta (69245b3, 8564eaa) touches one test file and the changeset only; on it: the test file 66/66, `check:test-typecheck` for objectql, eslint on the file, `check-changeset-no-major`, `check-empty-changeset` and `check:nul-bytes`, each exit 0. The F1 and S3 round (658dc99, 0c9fad9) touches the same four source and test files and the changeset; on it: both package suites, both typechecks and eslint above, each exit 0; the derived set (`dispatch-gates.mjs --commands`, the same 73 commands) rerun on `0c9fad9f`: 73/73 exit 0, and `--ran` reads 73 derived, 73 run, 0 NOT-MEASURED, 0 UNRUN. `398a221a` changes the changeset text only. - On the merged head `9cd6224d` (`origin/main` `25ee5076` merged; no file of this PR changed): objectql and runtime rebuilt with declarations (`pnpm --filter '@objectstack/runtime...' build`), then `pnpm --filter @objectstack/objectql test` 590 files, 12657/12657; `pnpm --filter @objectstack/runtime test` 588 files, 10262 passed, 19 skipped; `pnpm --filter @objectstack/objectql typecheck` and `pnpm --filter @objectstack/runtime typecheck` (each runs its package's `check:test-typecheck`) exit 0, the runtime one under `NODE_OPTIONS=--max-old-space-size=8192` (at the default heap its test-layer `tsc` ran out of memory before giving a reading); eslint on the six changed source and test files exit 0. - On the second merged head `342fa08f` (`origin/main` `c1a645e2` merged; this PR's diff of each of its seven files unchanged; `main` touched `engine.ts` and `app-artifact-handlers.ts` in other regions, #32848 and #32889): objectql and runtime rebuilt with declarations, then both package typechecks (with `check:test-typecheck`, under the same heap) exit 0; `registry-bare-name-one-holder-request-family.test.ts` 68/68; `app-artifact-handlers.test.ts` 34/34. </details> <details><summary>Ablations (each through scripts/ablation-replace.mjs on the committed head; every restore proved: blob equals HEAD and git diff HEAD empty)</summary> On `0c9fad9f`: - A1, `releaseBareNameFunctions` releases nothing (its two delete lines replaced): objectql 4 failed / 64 passed ((i), (ii), (v), (vi)); with objectql rebuilt, runtime 4 failed / 30 passed ((i), (ii), (iv), (v)). The preflight cannot see a comment marker in the built output (the bundler drops comments); the four runtime reds are the reading that the ablated build ran. Restore leg: rebuilt, working tree clean, runtime 34/34. - A2, the release call in `releaseMovedFunctions` removed: runtime 2 failed / 32 passed ((i), (iv)). - A3, the release call in the dropped-body teardown removed: runtime 2 failed / 32 passed ((ii), (v)). - A5 (contract review S1), the release drops the name's whole claimant set: objectql 1 failed / 67 passed ((v), beta's co-owner admitted). - A7, the release ignores `stillHolds` (every claimed name of the package released): objectql 3 failed / 65 passed ((i), (vii), (ii)). - A9 (red-line read F1), the runtime asks nothing (`() => false`, every claimed name released): runtime 2 failed / 32 passed (the F1 CONTROLs (vi), (vii)). The kept-name CONTROLs of runtime (i) and (iv) stay green under it, since the re-bind claims a kept name again (red-line read N2); objectql (i) and (vii) carry that property (red under A7). - A0, the previous head's two production files (`8564eaac`'s `registry.ts` and `app-artifact-handlers.ts`, file swap, objectql rebuilt) under this head's runtime tests: 4 failed / 30 passed, exactly the four new pins ((iv), (v), (vi), (vii)); restored with `git checkout HEAD`, tree clean, rebuilt. - A4, measured again on `9cd6224d` (the first merged head; the restore is unchanged since `ec785b77`): objectql (iii) and (iv) red, 2 failed / 66 passed; every CONTROL green. </details> ## Acceptance notes - **Dropped-body release in this PR.** The card asks for the moved function's release. The same teardown leaves a dropped body's claims behind (measured above, row 3), so the PR calls the same release there too, under its own pin (runtime (ii)). Review round 1 on PR #32468 named it ("Likewise a dropped body's function claim stays in the registry"); round 2 kept only the moved half as S1. - **Red-line read F1 and contract review S3, fixed here (epic seat ruling).** The release first used the binder's record of its last bind, while the engine's removal it follows is by the exact owner string: a function the host also registered for the package under its bare id stayed held, yet its claim was dropped, so a bind refused on main was admitted. The release now asks the engine which names the package still holds after the removal; the same rule releases a function the old owner's `app:<id>` registrations held that the re-bind does not register again, whether an earlier bind of the artifact gave it or it was bound under that owner outside the artifact's binder (S3 and red-line delta read N6, row 8). The S3 follow-up card is narrowed to what is left. - **The platform's entry is restored too (row 6).** Read as the card's "exactly one holder remains": the platform's owner-less registration is a holder (`functionHoldersOf(...).platformItem`), and ADR-0150 D4 says the platform alone answers a caller with no package. "A package-less registration keeps its rules" is kept: it never leaves with a package, it does not count toward the 409, and when it is the bare entry it stays. - **Observed, not changed:** the platform beside one package answers the bare read with the last registration of the two (epic seat ruling R2-C counts package holders only), while ADR-0150 D4's caller-less rule would refuse it `ambiguous-in-environment`. After the last-registered of three leaves, that pair answers nothing (CONTROL pin), so no holder is guessed. Whether the bare read should refuse the platform-plus-package pair is #32907 (filed by the epic seat): its CONTROL pin here reads "current behaviour, see #32907", so the alignment flips it on purpose. - **File surface (contract review N1).** `packages/objectql/src/engine.ts` changes the `unregisterFunctionsByPackage` docblock only, and it is outside the claim's declared file surface. The dropped-body release call in the runtime teardown goes past "`app-artifact-handlers.ts`, only to call the release" in `releaseMovedFunctions` (named in the first note). - The claim release is not recorded on the registry's install restore point (`noteBareNameClaim`), the same as `claimBareNameFunctions`: neither runs inside an install transaction. - **Merged `main` twice before the queue, never rebased.** First `9cd6224d` (`origin/main` `25ee5076`): 32 first-parent commits had landed since the branch's last merge (`5c0f1134`), at or over the queue's 15; clean, touching none of this PR's seven files; the affected gates run again (Suites above) and A4 measured again. Then `342fa08f` (`origin/main` `c1a645e2`): 20 more since `25ee5076`; clean, a mechanical merge (this PR's diff of each file unchanged); both typechecks and the two changed test files run again (Suites above). Open PR #32815 also edits `registry.ts` (another region); if it lands after this PR, it merges `main` on its side. - No full dogfood battery: the title has no `!` and `Clause-②:` has no `(narrowing)` arm (`os-dev.md`). --- _Generated by [Claude Code](https://claude.ai/code/session_01L6bhgTAAvx8zsU1YcVseLQ)_ 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01L6bhgTAAvx8zsU1YcVseLQ <!-- signed: domain:server#4 · os-sales · session_01L6bhgTAAvx8zsU1YcVseLQ --> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent d4b14a8 commit ee9ad83

7 files changed

Lines changed: 478 additions & 3 deletions
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
'@objectstack/objectql': patch
3+
'@objectstack/runtime': patch
4+
---
5+
6+
fix(objectql, runtime): a function's bare-name record follows its holders (ADR-0150 D4)
7+
8+
Clause-②: yes (widening)
9+
Metadata-protocol: none
10+
11+
The engine's function registry and the registry's one-holder rule each keep a record of who holds a function name, and two of those records outlived the holding:
12+
13+
- **A re-bind takes the old owner's bare-name claim off with the function.** The hook binder records a claim for every function a package's bind brings (`SchemaRegistry#claimBareNameFunctions`), and only `uninstallPackage` released it. When a re-bind of a multi-package artifact (`bindAppArtifactHandlers`) moved a function to another owner, or dropped the package body that held it, the old owner's claim stayed. After the cold-boot seal, a bind of that name by a package of the old owner's namespace was then refused naming a holder that no longer registers it. FROM: the gaining owner's whole group refused at bind (`NAMESPACE_CONFLICT`, logged at error; its hooks never fire and the moved function is held by nobody), and a co-owner's bind of a dropped body's function refused the same way. TO: the group binds and the function is held by its new owner; the co-owner's bind is admitted. The new `SchemaRegistry#releaseBareNameFunctions(owner, stillHolds)` releases the owner's package's claim on each function name it claims that `stillHolds` says it no longer holds, and nothing else; the binder calls it right after the engine's removal and asks the engine's holder set. So a name the package still holds under another owner spelling (its bare id) keeps its claim and is refused as before, and every other holder's claims stay. A function the old owner's `app:<id>` registrations held that the re-bind does not register again leaves with the removal and is released too, whichever way it was bound under that owner: an earlier bind of the artifact that the last bind no longer named (it stayed registered, replaced by name), or a bind under that owner outside the artifact's binder. FROM: its claim stayed, and after the seal a bind of it by a package that meets the old owner was refused naming the old owner. TO: admitted. Before the seal, a released claim is no clash for the seal to report.
14+
- **The last remaining holder answers the bare function read.** `ObjectQL#unregisterFunctionsByPackage` deleted the bare entry of a name only when the leaving package had registered it last, and left the other holders in the holder set. FROM: two packages hold `f`, the last-registered leaves, and `resolveFunction('f')` / `resolveFunctionEntry('f')` answer nothing while `functionHoldersOf('f')` still names the other package. TO: when exactly one holder of the name remains, the bare read answers that holder's entry, a package's or the platform's (owner-less) registration. Two or more remaining holders get no pick: two packages' name stays refused (409 naming both). The platform beside one package is left as it is today: after such a leave it answers nothing. That is today's behaviour, not the rule: ADR-0150 D4 refuses a caller with no package when the platform and a package both hold the name, and #32907 aligns the bare read with it.
15+
16+
Unchanged: the first-registered holder leaving, a one-holder name leaving (nothing answers), an owner-less registration (it never leaves with a package), and every reader that resolves by package (`functionHoldersOf`: the flow `script` and screen nodes, the hook binder). Nothing to migrate.

‎packages/objectql/src/engine-hooks-and-functions.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,12 @@ export class HooksAndFunctionsFacet {
328328
for (const [name, holders] of this.functionHolders) {
329329
for (const [holder, entry] of holders) if (entry.packageId === packageId) holders.delete(holder);
330330
if (holders.size === 0) this.functionHolders.delete(name);
331+
// [#32534] The bare entry left with the last registration while ONE
332+
// holder of the name remains: that holder's entry is the bare answer, as
333+
// a one-holder name's always is. Two or more remaining holders get no
334+
// pick (the bare read of two packages' name stays refused, naming them),
335+
// and an owner-less registration never leaves with a package.
336+
else if (holders.size === 1 && !this.functions.has(name)) this.functions.set(name, [...holders.values()][0]);
331337
}
332338
if (removed > 0) {
333339
this.logger.debug('Unregistered functions by package', { packageId, removed });

‎packages/objectql/src/engine.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,12 @@ export class ObjectQL implements IObjectQLEngine {
803803
return HooksAndFunctionsFacet.functionHoldersOf.call(hooksAndFunctionsHost.of(this), name);
804804
}
805805

806-
/** Remove all functions registered under a given `packageId`. */
806+
/**
807+
* Remove all functions registered under a given `packageId`, from the bare
808+
* map and the holder set ({@link functionHoldersOf}); answers how many bare
809+
* entries left. [#32534] When the last registration of a name leaves and
810+
* exactly one holder of it remains, the bare read answers that holder.
811+
*/
807812
unregisterFunctionsByPackage(packageId: string): number {
808813
// [#29986 codemod] The body moved to `engine-hooks-and-functions.ts` (see `HooksAndFunctionsHost`).
809814
return HooksAndFunctionsFacet.unregisterFunctionsByPackage.call(hooksAndFunctionsHost.of(this), packageId);

‎packages/objectql/src/registry-bare-name-one-holder-request-family.test.ts‎

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,103 @@ describe('function — the engine function registry, through the hook binder\'s
643643
expect(await caught(() => bindFunctions(ql, 'com.acme.beta', ['shared_fn']))).toBeUndefined();
644644
});
645645

646+
// [#32534] The release the runtime's re-bind makes when a function leaves its
647+
// owner (moved to another owner, or with a dropped package body): after the
648+
// engine's removal, on every name the owner's package no longer holds there.
649+
/** Whether the engine still holds `name` for `packageId`: the question the runtime's release asks. */
650+
const heldBy = (ql: ObjectQL, packageId: string) => (name: string) => ql.functionHoldersOf(name).packageItem(packageId) !== undefined;
651+
652+
it('[#32534] after the seal, releasing a package\'s claims on the names it no longer holds lets a package of its namespace bind them; a name it still holds and the other holders\' claims stay (base: no release, the bind refused naming the old owner)', async () => {
653+
const { ql, registry, register } = await boot();
654+
expect(await register(pkg('com.acme.alpha'))).toBeUndefined();
655+
expect(await register(rival('function', 'com.acme.beta'))).toBeUndefined();
656+
expect(await register(rival('function', 'com.acme.gamma'))).toBeUndefined();
657+
bindFunctions(ql, 'com.acme.alpha', ['moving_fn', 'kept_fn']);
658+
bindFunctions(ql, 'com.acme.gamma', ['gamma_fn']);
659+
// The re-bind's removal, then what alpha keeps registered again (`releaseMovedFunctions`).
660+
ql.unregisterFunctionsByPackage('app:com.acme.alpha');
661+
ql.registerFunction('kept_fn', fn('alpha'), { packageId: 'app:com.acme.alpha' });
662+
663+
registry.releaseBareNameFunctions('app:com.acme.alpha', heldBy(ql, 'com.acme.alpha'));
664+
665+
expect(await caught(() => bindFunctions(ql, 'com.acme.beta', ['moving_fn']))).toBeUndefined();
666+
expect(ql.functionHoldersOf('moving_fn').packageHolders).toEqual(['com.acme.beta']);
667+
// CONTROLs — the name alpha still holds, and another package's claim, still refuse.
668+
expectRefusal(await caught(() => bindFunctions(ql, 'com.acme.beta', ['kept_fn'])), 'com.acme.beta', { kind: 'package', packageId: 'com.acme.alpha' });
669+
expectRefusal(await caught(() => bindFunctions(ql, 'com.acme.beta', ['gamma_fn'])), 'com.acme.beta', { kind: 'package', packageId: 'com.acme.gamma' });
670+
// CONTROL — an owner-less release, or one whose package still holds every name, releases nothing.
671+
registry.releaseBareNameFunctions(undefined, () => false);
672+
registry.releaseBareNameFunctions('app:com.acme.alpha', () => true);
673+
expectRefusal(await caught(() => bindFunctions(ql, 'com.acme.beta', ['kept_fn'])), 'com.acme.beta', { kind: 'package', packageId: 'com.acme.alpha' });
674+
});
675+
676+
it('[#32534] CONTROL — a name the package still holds under another owner spelling (its bare id) keeps its claim after the removal of its `app:` registrations, so its co-owner is still refused naming it, as on main', async () => {
677+
const { ql, registry, register } = await boot();
678+
expect(await register(pkg('com.acme.alpha'))).toBeUndefined();
679+
expect(await register(rival('function', 'com.acme.beta'))).toBeUndefined();
680+
bindFunctions(ql, 'com.acme.alpha', ['moving_fn']);
681+
// A host's own registration of the function for alpha, under the package's bare id.
682+
ql.registerFunction('moving_fn', fn('host'), { packageId: 'com.acme.alpha' });
683+
ql.unregisterFunctionsByPackage('app:com.acme.alpha');
684+
expect(ql.functionHoldersOf('moving_fn').packageHolders, 'precondition: the removal by owner string left it').toEqual(['com.acme.alpha']);
685+
686+
registry.releaseBareNameFunctions('app:com.acme.alpha', heldBy(ql, 'com.acme.alpha'));
687+
688+
expectRefusal(await caught(() => bindFunctions(ql, 'com.acme.beta', ['moving_fn'])), 'com.acme.beta', { kind: 'package', packageId: 'com.acme.alpha' });
689+
expect(ql.functionHoldersOf('moving_fn').packageHolders).toEqual(['com.acme.alpha']);
690+
});
691+
692+
it('[#32534] a name an earlier bind claimed, removed with the owner\'s registrations, is released with the rest, not only the last bind\'s names', async () => {
693+
const { ql, registry, register } = await boot();
694+
expect(await register(pkg('com.acme.alpha'))).toBeUndefined();
695+
expect(await register(rival('function', 'com.acme.beta'))).toBeUndefined();
696+
bindFunctions(ql, 'com.acme.alpha', ['old_fn']);
697+
bindFunctions(ql, 'com.acme.alpha', ['new_fn']);
698+
ql.unregisterFunctionsByPackage('app:com.acme.alpha');
699+
700+
registry.releaseBareNameFunctions('app:com.acme.alpha', heldBy(ql, 'com.acme.alpha'));
701+
702+
expect(await caught(() => bindFunctions(ql, 'com.acme.beta', ['old_fn', 'new_fn']))).toBeUndefined();
703+
expect(ql.functionHoldersOf('old_fn').packageHolders).toEqual(['com.acme.beta']);
704+
});
705+
706+
it('[#32534] CONTROL — two namespaces\' packages claim one name after the seal; releasing one owner\'s claim leaves the other\'s, so a package of the remaining holder\'s namespace is still refused naming it (ABLATION: a release that drops the name\'s whole claimant set admits it)', async () => {
707+
const { ql, registry, register } = await boot();
708+
expect(await register(pkg('com.acme.alpha'))).toBeUndefined();
709+
expect(await register(pkg('com.acme.beta'))).toBeUndefined();
710+
expect(await register(coOwner('com.acme.alpha', 'com.acme.alpha2'))).toBeUndefined();
711+
expect(await register(coOwner('com.acme.beta', 'com.acme.beta2'))).toBeUndefined();
712+
// Two namespaces: the function's rule meets neither, so both claim the one name.
713+
bindFunctions(ql, 'com.acme.alpha', ['shared_fn']);
714+
bindFunctions(ql, 'com.acme.beta', ['shared_fn']);
715+
ql.unregisterFunctionsByPackage('app:com.acme.alpha');
716+
717+
registry.releaseBareNameFunctions('app:com.acme.alpha', heldBy(ql, 'com.acme.alpha'));
718+
719+
// beta's claim on the same name stays: beta's co-owner is refused naming beta.
720+
expectRefusal(await caught(() => bindFunctions(ql, 'com.acme.beta2', ['shared_fn'])), 'com.acme.beta2', { kind: 'package', packageId: 'com.acme.beta' });
721+
expect(ql.functionHoldersOf('shared_fn').packageHolders).toEqual(['com.acme.beta']);
722+
// alpha's claim is gone: alpha's co-owner binds the name.
723+
expect(await caught(() => bindFunctions(ql, 'com.acme.alpha2', ['shared_fn']))).toBeUndefined();
724+
});
725+
726+
it('[#32534] a claim released before the seal is no clash for the cold boot to report (CONTROL: the ABLATION above, with no release, reports it)', async () => {
727+
const registry = new SchemaRegistry();
728+
registry.logLevel = 'silent';
729+
registry.claimBareNameFunctions('app:com.acme.alpha', ['shared_fn', 'alpha_fn']);
730+
// alpha no longer holds `shared_fn`; it still holds `alpha_fn`.
731+
registry.releaseBareNameFunctions('app:com.acme.alpha', (name) => name !== 'shared_fn');
732+
registry.claimBareNameFunctions('app:com.acme.beta', ['shared_fn']);
733+
expect(registry.sealBareNameHolders()).toEqual([]);
734+
// CONTROL — after the seal, each name refuses its one remaining holder's rival: alpha keeps `alpha_fn`.
735+
const err = await caught(() => registry.claimBareNameFunctions('app:com.acme.gamma', ['shared_fn', 'alpha_fn']));
736+
expect(err?.code).toBe(NAMESPACE_CONFLICT_CODE);
737+
expect(err!.conflicts!.map((c) => `${c.name}: ${(c.existingHolder as Holder).packageId}`)).toEqual([
738+
'shared_fn: com.acme.beta',
739+
'alpha_fn: com.acme.alpha',
740+
]);
741+
});
742+
646743
describe('the cold boot of a deployment whose packages already bind one function name', () => {
647744
// [#31700] Two packages of one namespace: the function has narrowed to it.
648745
const atBoot = () => [pkg('com.acme.alpha'), rival('function', 'com.acme.beta')];
@@ -705,3 +802,98 @@ describe('the refusal\'s reason names the consumers of the types it lists', () =
705802
}
706803
});
707804
});
805+
806+
// [#32534, ADR-0150 D4] The engine keeps two function maps: the bare map (one
807+
// entry per name, the last registration) and the holder set
808+
// (`functionHoldersOf`). When the last registration leaves and ONE holder of
809+
// the name remains, the bare read answers that holder; two remaining holders
810+
// get no pick.
811+
describe('[#32534] the bare engine function read after a holder leaves', () => {
812+
/** Two namespaces' packages each bind `shared_fn`, alpha first, after the seal. */
813+
const twoNamespaces = async (extra: string[] = []) => {
814+
const booted = await boot();
815+
expect(await booted.register(pkg('com.acme.alpha'))).toBeUndefined();
816+
expect(await booted.register(pkg('com.acme.beta'))).toBeUndefined();
817+
for (const id of extra) expect(await booted.register(pkg(id))).toBeUndefined();
818+
bindFunctions(booted.ql, 'com.acme.alpha', ['shared_fn']);
819+
bindFunctions(booted.ql, 'com.acme.beta', ['shared_fn']);
820+
return booted;
821+
};
822+
823+
it('two package holders, the last-registered leaves: the remaining holder answers (FROM nothing TO its entry)', async () => {
824+
const { ql } = await twoNamespaces();
825+
const alphas = ql.functionHoldersOf('shared_fn').packageItem('com.acme.alpha');
826+
expect((await caught(() => ql.resolveFunctionEntry('shared_fn')))?.status, 'precondition: two holders, refused').toBe(409);
827+
828+
expect(ql.unregisterFunctionsByPackage('app:com.acme.beta')).toBe(1);
829+
830+
expect(ql.functionHoldersOf('shared_fn').packageHolders).toEqual(['com.acme.alpha']);
831+
expect(ql.resolveFunctionEntry('shared_fn')).toBe(alphas);
832+
expect(ql.resolveFunction('shared_fn')).toBe(alphas!.handler);
833+
// …and when it leaves too, nothing (CONTROL: no stale entry stays behind).
834+
expect(ql.unregisterFunctionsByPackage('app:com.acme.alpha')).toBe(1);
835+
expect(ql.resolveFunctionEntry('shared_fn')).toBeUndefined();
836+
});
837+
838+
it('CONTROL — the first-registered leaves: the last-registered answers, unchanged', async () => {
839+
const { ql } = await twoNamespaces();
840+
expect(ql.unregisterFunctionsByPackage('app:com.acme.alpha')).toBe(0);
841+
expect(ql.resolveFunctionEntry('shared_fn')?.packageId).toBe('app:com.acme.beta');
842+
});
843+
844+
it('CONTROL — two package holders remain: the bare read stays refused, naming both, unchanged', async () => {
845+
const { ql } = await twoNamespaces(['com.acme.gamma']);
846+
bindFunctions(ql, 'com.acme.gamma', ['shared_fn']);
847+
ql.unregisterFunctionsByPackage('app:com.acme.gamma');
848+
const err = await caught(() => ql.resolveFunctionEntry('shared_fn'));
849+
expect(err?.name).toBe('PackageScopedNameRefusedError');
850+
expect(err?.status).toBe(409);
851+
expect(err!.message).toContain('package "com.acme.alpha"');
852+
expect(err!.message).toContain('package "com.acme.beta"');
853+
});
854+
855+
it('CONTROL — the one holder of a name leaves: nothing answers, unchanged', async () => {
856+
const { ql, register } = await boot();
857+
expect(await register(pkg('com.acme.alpha'))).toBeUndefined();
858+
bindFunctions(ql, 'com.acme.alpha', ['alpha_fn']);
859+
expect(ql.unregisterFunctionsByPackage('app:com.acme.alpha')).toBe(1);
860+
expect(ql.resolveFunctionEntry('alpha_fn')).toBeUndefined();
861+
expect(ql.functionHoldersOf('alpha_fn').packageHolders).toEqual([]);
862+
});
863+
864+
it('the platform\'s (owner-less) registration under a package\'s, the package leaves: the platform, the one holder left, answers (FROM nothing TO its entry)', async () => {
865+
const { ql } = await boot();
866+
const platform = fn('platform');
867+
ql.registerFunction('platform_fn', platform);
868+
ql.registerFunction('platform_fn', fn('beta'), { packageId: 'app:com.acme.beta' });
869+
ql.unregisterFunctionsByPackage('app:com.acme.beta');
870+
expect(ql.resolveFunction('platform_fn')).toBe(platform);
871+
expect(ql.resolveFunctionEntry('platform_fn')?.packageId).toBeUndefined();
872+
});
873+
874+
it('CONTROL — an owner-less registration over a package\'s never leaves with the package and still answers, unchanged', async () => {
875+
const { ql } = await boot();
876+
const platform = fn('platform');
877+
ql.registerFunction('platform_fn', fn('alpha'), { packageId: 'app:com.acme.alpha' });
878+
ql.registerFunction('platform_fn', platform);
879+
expect(ql.unregisterFunctionsByPackage('app:com.acme.alpha')).toBe(0);
880+
expect(ql.resolveFunction('platform_fn')).toBe(platform);
881+
});
882+
883+
// CURRENT BEHAVIOUR, see #32907 — not the rule. ADR-0150 D4 refuses a caller
884+
// with no package `ambiguous-in-environment` when the platform and a package
885+
// both hold a name (a 409 naming each); the bare read counts package holders
886+
// only (epic seat ruling R2-C), so this pair answers nothing here. #32907
887+
// aligns the bare read with D4 and flips this pin on purpose. What it pins
888+
// for #32534: the restore picks no holder of two.
889+
it('CONTROL (current behaviour, see #32907) — the platform and one package remain after the last-registered package leaves: two holders, so the restore picks nothing', async () => {
890+
const { ql } = await boot();
891+
ql.registerFunction('platform_fn', fn('platform'));
892+
ql.registerFunction('platform_fn', fn('alpha'), { packageId: 'app:com.acme.alpha' });
893+
ql.registerFunction('platform_fn', fn('beta'), { packageId: 'app:com.acme.beta' });
894+
ql.unregisterFunctionsByPackage('app:com.acme.beta');
895+
expect(ql.functionHoldersOf('platform_fn').packageHolders).toEqual(['com.acme.alpha']);
896+
expect(ql.functionHoldersOf('platform_fn').platformItem?.()).toBeDefined();
897+
expect(ql.resolveFunctionEntry('platform_fn')).toBeUndefined();
898+
});
899+
});

0 commit comments

Comments
 (0)