Skip to content

Commit 52e5af7

Browse files
committed
fix(cloud-connection): install-local uninstall asks the registry's refusal before the ledger write
A marketplace install-local uninstall the running registry would refuse (the base's .custom pair installed, or an ADR-0029 extended object) is refused 409 RESOURCE_CONFLICT with the registry's own sentence before the ledger entry is removed, so nothing moves. A non-refused uninstall keeps the ledger-first order. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FEkQ5pQqiDztRTRRHCtcay
1 parent 35b178b commit 52e5af7

3 files changed

Lines changed: 188 additions & 7 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
'@objectstack/cloud-connection': patch
3+
---
4+
5+
fix(cloud-connection): a marketplace install-local uninstall the registry would refuse is refused 409 before the ledger write, naming the pair or the extender, with nothing removed (#28511)
6+
7+
Clause-②: no (narrowing) — `DELETE /api/v1/marketplace/install-local/:manifestId` now refuses `409 RESOURCE_CONFLICT` the uninstall of a package whose paired customization package (`ID.custom`) is installed, or that owns an object another package extends (ADR-0029), an uninstall that answered 200 before; nothing is widened.
8+
Metadata-protocol: none
9+
Skills-docs: none — no file under `skills/**` teaches uninstalling through the install-local door, so every taught use stays true.
10+
11+
<!-- adr-0087: not-required (no-migration-prescription) No key of any schema is added, removed, renamed or re-shaped, and no export or type changes: an uninstall the running registry already refuses is now refused at the install-local door before its ledger write, with the registry's existing RESOURCE_CONFLICT / 409 refusal, so there is nothing for a migration to rewrite and no tombstone. -->
12+
13+
**BREAKING** accept-set narrowing at the install-local uninstall door (`@objectstack/cloud-connection`, ADR-0146 D3).
14+
15+
- **Before.** The door removed the ledger entry first and only then asked the registry to withdraw the package. A registry refusal (the base's `.custom` pair installed, or an extended object) was caught there and folded into a 200 with a failed `registry.uninstallPackage` entry in `cleanups`. The package stayed registered until the next restart, and the restart then dropped it from under its pair, stranding every customization the pair holds.
16+
- **Now.** The door asks the registry the same question first, through `SchemaRegistry.assertPackageUninstallable` (the one predicate `uninstallPackage` asks, which mutates nothing). On a refusal it answers that refusal's own `409 RESOURCE_CONFLICT` and sentence, which names the pair and the extenders and opens with the way out. The ledger entry, the registration and the pair stay as they were, and no cleanup runs.
17+
- **Unchanged.** An uninstall the registry would let go keeps the ledger-first order (the ledger entry is removed, then the package is withdrawn, then the cleanups run). An id this door never installed still answers 404. A host with no `objectql` registry, or a registry without the predicate, is not asked and behaves as before.
18+
19+
**The remedy.** Delete the pair (export it first to keep its customizations: `GET /api/v1/packages/PAIR/export`) or uninstall the extending packages, then uninstall the base again.

‎packages/cloud-connection/src/marketplace-install-local-plugin.ts‎

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,12 @@
103103
* and the declared range, and no seed row is read for it (#21822).
104104
*
105105
* DELETE /api/v1/marketplace/install-local/:manifestId
106-
* → removes the cached manifest, withdraws the package from the
106+
* → refused `409 RESOURCE_CONFLICT`, with the registry's own
107+
* sentence and nothing removed, when the running registry would
108+
* refuse the uninstall: the package's paired customization package
109+
* (`<id>.custom`) is installed (ADR-0146 D3), or another package
110+
* extends an object it owns (ADR-0029) (#28511). Otherwise
111+
* removes the cached manifest, withdraws the package from the
107112
* running kernel through the registry's `uninstallPackage` — the
108113
* verb the protocol's own uninstall uses — so its objects answer
109114
* 404 at once (#21576), and the copies of its objects the install
@@ -2372,6 +2377,46 @@ export class MarketplaceInstallLocalPlugin implements Plugin {
23722377
const walled = this.refuseWalledCatalogPackage(c, ctx, 'Uninstalling', manifestId, admission);
23732378
if (walled) return walled;
23742379
}
2380+
// [#28511, ADR-0146 D3] THE REGISTRY'S UNINSTALL REFUSAL, ASKED BEFORE
2381+
// THE LEDGER WRITE. The withdrawal below asks the registry to
2382+
// uninstall the package, and the registry refuses a base whose paired
2383+
// customization package (`<base>.custom`) it holds, or a package
2384+
// owning an object another package extends (ADR-0029). Met only
2385+
// there, that refusal came after the ledger entry was gone: the door
2386+
// answered 200 with a failed `registry.uninstallPackage` outcome, the
2387+
// base stayed registered until the restart, and the restart then
2388+
// dropped it from under its pair. So the same question is asked here,
2389+
// of the same registry and the same id the withdrawal uninstalls,
2390+
// through `assertPackageUninstallable` — the ONE predicate
2391+
// `uninstallPackage` itself asks, which mutates nothing — and its
2392+
// refusal is the request's: its own `409 RESOURCE_CONFLICT` and its
2393+
// sentence, with nothing moved (the ledger entry, the registration and
2394+
// the pair stay as they were).
2395+
//
2396+
// ⛔ A refusal check ahead of the ledger write, not a reversal of
2397+
// #21576's ledger-then-withdraw order: an uninstall the registry would
2398+
// let go still removes the ledger entry first. No `objectql` registry,
2399+
// or one that does not carry the method (an engine double), is not
2400+
// asked, and the door behaves as before: anything the withdrawal then
2401+
// meets is reported on `cleanups`. A throw that is not the coded
2402+
// refusal is left to the withdrawal the same way, never answered as a
2403+
// conflict it is not.
2404+
{
2405+
let registry: { assertPackageUninstallable?(id: string): void } | undefined;
2406+
try { registry = ctx.getService<IObjectQLEngine>('objectql')?.registry as typeof registry; } catch { /* no data engine — nothing registered */ }
2407+
if (typeof registry?.assertPackageUninstallable === 'function') {
2408+
try {
2409+
registry.assertPackageUninstallable(manifestId);
2410+
} catch (err: any) {
2411+
if (err?.code === 'RESOURCE_CONFLICT' && err?.status === 409) {
2412+
return c.json({
2413+
success: false,
2414+
error: { code: 'RESOURCE_CONFLICT', message: String(err.message) },
2415+
}, 409);
2416+
}
2417+
}
2418+
}
2419+
}
23752420
try {
23762421
this.ledger.remove(manifestId);
23772422
} catch (err: any) {

‎packages/cloud-connection/src/marketplace-install-local-uninstall-withdrawal.test.ts‎

Lines changed: 123 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,17 @@
2323
* - nothing is withdrawn when the uninstall did not happen: a refused caller,
2424
* an id this door never installed (even one the registry holds), a ledger
2525
* write that failed;
26-
* - a refused withdrawal is reported on the response as a failed outcome,
27-
* never swallowed, the cleanups still run, and the cause goes to the
28-
* operator log only;
26+
* - [#28511, ADR-0146 D3] an uninstall the registry would refuse is refused
27+
* BEFORE the ledger write, with the registry's own `409
28+
* RESOURCE_CONFLICT` and its sentence: a base whose paired customization
29+
* package (`BASE.custom`) is installed, and an ADR-0029 extended owner.
30+
* Nothing moves — the ledger entry, the registration and the pair stay —
31+
* and no cleanup runs. Asked of a REAL `SchemaRegistry`, the one predicate
32+
* (`assertPackageUninstallable`) its own `uninstallPackage` asks;
33+
* - a withdrawal refused for a cause nothing asked ahead (a registry that
34+
* does not carry the predicate) is still reported on the response as a
35+
* failed outcome, never swallowed, the cleanups still run, and the cause
36+
* goes to the operator log only;
2937
* - a registry that does not hold the package has nothing to withdraw;
3038
* - [ADR-0090 D5] the permission-set names the package held are captured
3139
* just BEFORE the withdrawal releases them, and handed to the cleanups.
@@ -39,6 +47,7 @@ import { tmpdir } from 'node:os';
3947
// clocked `it` (`scripts/check-test-source-alias.mjs`): the plugin reaches the
4048
// same module through a dynamic `import()` for its handler binder on rehydrate.
4149
import '@objectstack/runtime';
50+
import { SchemaRegistry } from '@objectstack/objectql';
4251
import { MarketplaceInstallLocalPlugin } from './marketplace-install-local-plugin.js';
4352
import { INSTALLER_USER_ID, installerAuthService, withInstallerGrants } from './install-local-principal.fixtures.js';
4453
import { LocalManifestSource } from './local-manifest-source.js';
@@ -106,11 +115,13 @@ async function bootPlugin(opts: {
106115
refuseWithdrawal?: string;
107116
auth?: unknown;
108117
held?: string[];
118+
/** A REAL registry to boot over, in place of the recording double. */
119+
realRegistry?: SchemaRegistry;
109120
} = {}) {
110121
const events: Array<{ step: string; id: string; ledgerOnDisk: boolean }> = [];
111122
const handed: unknown[] = [];
112123
const packages = new Map((opts.registered ?? [APP_ID, USER_CODE_ID]).map((id) => [id, { manifest: { id } }]));
113-
const registry = {
124+
const fake = {
114125
getAllPackages: () => [...packages.values()],
115126
getPackage: (id: string) => packages.get(id),
116127
...(opts.held ? {
@@ -125,6 +136,14 @@ async function bootPlugin(opts: {
125136
return packages.delete(id);
126137
}),
127138
};
139+
const registry: typeof fake = opts.realRegistry
140+
? Object.assign(opts.realRegistry, {
141+
uninstallPackage: vi.fn((id: string) => {
142+
events.push({ step: 'withdraw', id, ledgerOnDisk: existsSync(ledgerFile()) });
143+
return SchemaRegistry.prototype.uninstallPackage.call(opts.realRegistry, id);
144+
}),
145+
}) as unknown as typeof fake
146+
: fake;
128147
const hooks = new Map<string, any>();
129148
const logger = { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() };
130149
const rawApp = makeRawApp();
@@ -245,9 +264,11 @@ describe('#21576: an install-local uninstall withdraws the package from the runn
245264
expect(packages.has(APP_ID)).toBe(true);
246265
});
247266

248-
it('a refused withdrawal is one failed outcome on the response, the cleanups still run, and the cause stays in the log', async () => {
267+
it('a withdrawal refused for a cause nothing asked ahead (a registry without the predicate) is one failed outcome on the response, the cleanups still run, and the cause stays in the log', async () => {
249268
seedLedger();
250-
const cause = 'Cannot uninstall package: an object it owns is extended by another package';
269+
// The double carries no `assertPackageUninstallable`, so nothing is
270+
// asked ahead and its throw is met at the withdrawal itself.
271+
const cause = 'registry withdrawal failed: tasks_task is still referenced';
251272
const { uninstall, events, warnings } = await bootPlugin({ refuseWithdrawal: cause });
252273

253274
const res = await uninstall();
@@ -279,3 +300,99 @@ describe('#21576: an install-local uninstall withdraws the package from the runn
279300
expect(warnings()).toEqual([]);
280301
});
281302
});
303+
304+
const PAIR_ID = `${APP_ID}.custom`;
305+
const EXTENDER_ID = 'com.example.tasksaddon';
306+
307+
/**
308+
* [#28511, ADR-0146 D3] The REAL registry: a marketplace-installed base
309+
* (`com.example.tasksapp`, owning `tasks_task`), and — per case — its paired
310+
* customization package installed, or another package extending its object.
311+
*/
312+
function realRegistry(opts: { pair?: boolean; extender?: boolean } = {}): SchemaRegistry {
313+
const r = new SchemaRegistry({ multiTenant: false });
314+
r.logLevel = 'silent';
315+
r.installPackage({ id: APP_ID, name: 'Tasks App', version: '0.1.0', type: 'app' } as never, 'marketplace');
316+
r.registerObject({ name: 'tasks_task', fields: {} } as never, APP_ID, undefined, 'own');
317+
if (opts.pair) {
318+
r.installPackage({ id: PAIR_ID, name: 'Tasks App customizations', version: '0.1.0', type: 'app' } as never, 'authored');
319+
}
320+
if (opts.extender) {
321+
r.installPackage({ id: EXTENDER_ID, name: 'Tasks add-on', version: '0.1.0', type: 'app' } as never, 'marketplace');
322+
r.registerObject({ name: 'tasks_task', fields: {} } as never, EXTENDER_ID, undefined, 'extend');
323+
}
324+
return r;
325+
}
326+
327+
describe('#28511: an install-local uninstall the registry would refuse is refused before the ledger write', () => {
328+
async function refused(registry: SchemaRegistry) {
329+
seedLedger();
330+
const contributorsBefore = registry.getObjectContributors('tasks_task').map((c) => `${c.packageId}:${c.ownership}`);
331+
const booted = await bootPlugin({ realRegistry: registry });
332+
333+
const res = await booted.uninstall();
334+
335+
// ADR-0112 envelope: the registry's own refusal, code and status.
336+
expect(res.status, JSON.stringify(res.payload)).toBe(409);
337+
expect(res.payload.success).toBe(false);
338+
expect(res.payload.error.code).toBe('RESOURCE_CONFLICT');
339+
// Nothing moved: the ledger entry, the registration, the contributions.
340+
expect(existsSync(ledgerFile())).toBe(true);
341+
expect(registry.getPackage(APP_ID)).toBeDefined();
342+
expect(registry.getObjectContributors('tasks_task').map((c) => `${c.packageId}:${c.ownership}`))
343+
.toEqual(contributorsBefore);
344+
// No withdrawal was attempted and no cleanup ran.
345+
expect(booted.events).toEqual([]);
346+
expect(booted.warnings()).toEqual([]);
347+
return res;
348+
}
349+
350+
it('a base whose paired customization package is installed: 409 naming the pair, and the pair stays', async () => {
351+
const registry = realRegistry({ pair: true });
352+
353+
const res = await refused(registry);
354+
355+
expect(res.payload.error.message).toContain(`"${PAIR_ID}"`);
356+
expect(registry.getPackage(PAIR_ID)).toBeDefined();
357+
});
358+
359+
it('an ADR-0029 extended owner: 409 naming the extender, and the extension stays', async () => {
360+
const registry = realRegistry({ extender: true });
361+
362+
const res = await refused(registry);
363+
364+
expect(res.payload.error.message).toContain(EXTENDER_ID);
365+
expect(registry.getPackage(EXTENDER_ID)).toBeDefined();
366+
});
367+
368+
it('control — a base the registry would let go keeps #21576\'s ledger-first order: ledger removed, then withdrawn, then the cleanups', async () => {
369+
seedLedger();
370+
const registry = realRegistry();
371+
const { uninstall, events, warnings } = await bootPlugin({ realRegistry: registry });
372+
373+
const res = await uninstall();
374+
375+
expect(res.status, JSON.stringify(res.payload)).toBe(200);
376+
expect(res.payload.success).toBe(true);
377+
expect(events).toEqual([
378+
{ step: 'withdraw', id: APP_ID, ledgerOnDisk: false },
379+
{ step: 'cleanups', id: APP_ID, ledgerOnDisk: false },
380+
]);
381+
expect(registry.getPackage(APP_ID)).toBeUndefined();
382+
expect(res.payload.data.cleanups).toEqual(CLEANUP_OUTCOMES);
383+
expect(warnings()).toEqual([]);
384+
});
385+
386+
it('control — an id this door never installed answers 404 as before, even with a refusable registry', async () => {
387+
seedLedger();
388+
const registry = realRegistry({ pair: true });
389+
const { uninstall, events } = await bootPlugin({ realRegistry: registry });
390+
391+
const res = await uninstall(USER_CODE_ID);
392+
393+
expect(res.status).toBe(404);
394+
expect(res.payload.error.code).toBe('RESOURCE_NOT_FOUND');
395+
expect(events).toEqual([]);
396+
expect(existsSync(ledgerFile())).toBe(true);
397+
});
398+
});

0 commit comments

Comments
 (0)