@@ -527,6 +527,8 @@ import type {
527527 UninstallCleanup,
528528 UninstallCleanupOutcome,
529529 UninstallDeclared,
530+ UninstallGuard,
531+ UninstallGuardRefusal,
530532} from './protocol-package-lifecycle-types.js';
531533export { captureUninstallDeclared } from './protocol-package-lifecycle-types.js';
532534export type {
@@ -537,6 +539,8 @@ export type {
537539 UninstallCleanup,
538540 UninstallCleanupOutcome,
539541 UninstallDeclared,
542+ UninstallGuard,
543+ UninstallGuardRefusal,
540544} from './protocol-package-lifecycle-types.js';
541545import { METADATA_MUTATION_CLUSTER_CHANNEL } from './protocol-mutation-events.js';
542546import type {
@@ -761,6 +765,9 @@ export class ObjectStackProtocolImplementation implements
761765 /** [#2747] Named uninstall cleanups, run by {@link deletePackage}. */
762766 private uninstallCleanups = new Map<string, UninstallCleanup>();
763767
768+ /** [#31232] Named pre-uninstall guards, asked by {@link deletePackage} before it reads or moves anything. */
769+ private uninstallGuards = new Map<string, UninstallGuard>();
770+
764771 /**
765772 * Awaited per-type mutation projectors (ADR-0094), keyed by singular
766773 * metadata type. Unlike {@link publishMaterializers} (publish-only,
@@ -1379,6 +1386,7 @@ export class ObjectStackProtocolImplementation implements
13791386 tenantAuthoredWriteRefusal: (...a) => protocol.tenantAuthoredWriteRefusal(...a),
13801387 throwMetadataServiceUnavailable: (...a) => protocol.throwMetadataServiceUnavailable(...a),
13811388 get uninstallCleanups() { return protocol.uninstallCleanups; },
1389+ get uninstallGuards() { return protocol.uninstallGuards; },
13821390 uninstalledNamedBaseRefusal: (...a) => protocol.uninstalledNamedBaseRefusal(...a),
13831391 unknownDatasourceSaveRefusal: (...a) => protocol.unknownDatasourceSaveRefusal(...a),
13841392 unknownGrantObjectSaveRefusal: (...a) => protocol.unknownGrantObjectSaveRefusal(...a),
@@ -1413,6 +1421,27 @@ export class ObjectStackProtocolImplementation implements
14131421 return ProtocolClassHeadFacet.registerUninstallCleanup(protocolHost(this), name, cleanup);
14141422 }
14151423
1424+ /**
1425+ * [#31232] Register a named pre-uninstall guard: the before-half of
1426+ * {@link registerUninstallCleanup}. {@link deletePackage} asks every
1427+ * registered guard, in registration order, before it reads or moves
1428+ * anything, and throws the first refusal one answers as its own, with
1429+ * `code` and `status` as the guard spelled them; nothing is removed. A
1430+ * guard that throws refuses with its throw. With no guard registered the
1431+ * verb behaves exactly as it did before guards existed.
1432+ *
1433+ * Called by a plugin that owns a verdict this package must not know — the
1434+ * runtime registers its dependent-rule scan (an active validation rule of
1435+ * another enabled package reading a field the package adds through
1436+ * `objectExtensions`), the one the package doors ask, so every caller of
1437+ * the verb is refused on the verdict those doors answer, and this package
1438+ * holds no copy of it. One guard per name; a second registration replaces
1439+ * the first (idempotent re-init).
1440+ */
1441+ registerUninstallGuard(name: string, guard: UninstallGuard): void {
1442+ return ProtocolClassHeadFacet.registerUninstallGuard(protocolHost(this), name, guard);
1443+ }
1444+
14161445 /**
14171446 * Run every registered uninstall cleanup for one package and report each
14181447 * outcome (ADR-0086 D3, #2747) — THE one runner of the registry above.
@@ -10915,7 +10944,10 @@ export class ObjectStackProtocolImplementation implements
1091510944 *
1091610945 * [#21276] The steps, in order. Nothing durable happens before step 4, so
1091710946 * a refusal at any of steps 1–4 leaves everything as it was:
10918- * 1. the retired-key refusal (`INVALID_REQUEST`) — pure;
10947+ * 1. the retired-key refusal (`INVALID_REQUEST`) — pure; then [#31232]
10948+ * every registered pre-uninstall guard ({@link registerUninstallGuard}),
10949+ * in registration order — the first refusal is thrown with its own
10950+ * `code` and `status`, a guard's throw as is;
1091910951 * 2. the `sys_metadata` read — a read; a failure is thrown;
1092010952 * 3. the registry's uninstall refusal (another package extends an object
1092110953 * this one owns, ADR-0029), asked through
@@ -10937,6 +10969,7 @@ export class ObjectStackProtocolImplementation implements
1093710969 // [#25467 cut 35] The body moved to `protocol-package-uninstall.ts` (see `PackageUninstallHost`).
1093810970 const readEngine = () => this.engine;
1093910971 const readGetServicesRegistry = () => this.getServicesRegistry;
10972+ const readUninstallGuards = () => this.uninstallGuards;
1094010973 const host: ThisParameterType<typeof deletePackage> = {
1094110974 deleteMetaItem: (...a) => this.deleteMetaItem(...a),
1094210975 get engine() { return readEngine(); },
@@ -10945,6 +10978,7 @@ export class ObjectStackProtocolImplementation implements
1094510978 packagelessAddress: (...a) => this.packagelessAddress(...a),
1094610979 runUninstallCleanups: (...a) => this.runUninstallCleanups(...a),
1094710980 shouldDropStorage: (...a) => this.shouldDropStorage(...a),
10981+ get uninstallGuards() { return readUninstallGuards(); },
1094810982 };
1094910983 return deletePackage.call(host, request);
1095010984 }
0 commit comments