@@ -312,6 +312,7 @@ import { ConnectionCredentialStore } from './connection-credential-store.js';
312312import { readJsonRequestBody } from './request-json-body.js' ;
313313import { MARKETPLACE_INSTALLED_UI_BUNDLE } from './marketplace-ui.js' ;
314314import { countSeedRows , matchSeedRows , purgeSeedRows , seedDatasetOfReceiptItem , type PurgeSeedDataset } from './marketplace-install-local-purge.js' ;
315+ import { UnmanagedCopyRecord } from './marketplace-install-local-unmanaged-copies.js' ;
315316import {
316317 isStampableOwner ,
317318 stampSeedOwner ,
@@ -324,6 +325,7 @@ import type {
324325} from '@objectstack/spec/contracts' ;
325326import type {
326327 DeletePackageRequest ,
328+ UninstallCleanup ,
327329 UninstallCleanupOutcome ,
328330 UninstallDeclared ,
329331 ObjectStackProtocolImplementation ,
@@ -885,6 +887,9 @@ type UnmanagedCopySeedNameStore = Pick<IObjectQLEngine, 'find'>;
885887 */
886888const UNMANAGED_COPY_SEED_TYPE = 'seed' ;
887889
890+ /** [#30808] The uninstall cleanup that removes the door's record of a copy ({@link UnmanagedCopyRecord}). */
891+ const UNMANAGED_COPY_RECORD_UNINSTALL_CLEANUP = 'install-local:unmanaged-copy-record' ;
892+
888893/**
889894 * [ADR-0146 D2 / D3, #30106] The three verbs a pair install composes, typed
890895 * from the producer's own method signatures and asked as a capability
@@ -1550,10 +1555,18 @@ export class MarketplaceInstallLocalPlugin implements Plugin {
15501555 */
15511556 private readonly refusedAtRehydrate = new Map < string , { installedAt : string ; marker : NotLoadedMarker } > ( ) ;
15521557
1558+ /**
1559+ * [#30808] The unmanaged copies this door made, beside the ledger
1560+ * ({@link UnmanagedCopyRecord}): the only bases its purge reaches without
1561+ * a ledger entry.
1562+ */
1563+ private readonly unmanagedCopies : UnmanagedCopyRecord ;
1564+
15531565 constructor ( config : MarketplaceInstallLocalPluginConfig = { } ) {
15541566 this . cloudUrl = resolveCloudUrl ( config . controlPlaneUrl ) ;
15551567 this . ledger = new LocalManifestSource ( config . storageDir ) ;
15561568 this . storageDir = this . ledger . dir ;
1569+ this . unmanagedCopies = new UnmanagedCopyRecord ( this . storageDir ) ;
15571570 this . credentials = new ConnectionCredentialStore ( ) ;
15581571 }
15591572
@@ -1580,6 +1593,9 @@ export class MarketplaceInstallLocalPlugin implements Plugin {
15801593 // 1. Rehydrate previously installed packages so they survive restart.
15811594 await this . rehydrate ( ctx ) ;
15821595
1596+ // [#30808] An uninstalled copy leaves the door's record of it.
1597+ this . registerUnmanagedCopyCleanup ( ctx ) ;
1598+
15831599 // 2. Mount HTTP endpoints.
15841600 // [#4251] Read canonical-first with a REAL per-name fallback:
15851601 // `getService` THROWS for an empty slot, so a single try around
@@ -4684,6 +4700,25 @@ export class MarketplaceInstallLocalPlugin implements Plugin {
46844700 + `${ userId ? ` by ${ userId } ` : '' } . Nothing is registered under '${ manifestId } '; the copy follows no upgrade.` ,
46854701 ) ;
46864702 if ( withheldMaps . length > 0 ) ctx . logger ?. warn ?.( withheldMapsLogLine ( manifestId , 'unmanaged' , withheldMaps ) ) ;
4703+ // [#30808] The door's record of this copy, in the same act — before
4704+ // any row is loaded, so a load that fails partway is still purgeable.
4705+ // A record that cannot be written leaves the copy unreachable by the
4706+ // purge (its 404), never reachable by mistake; the operator is told.
4707+ try {
4708+ this . unmanagedCopies . write ( {
4709+ baseId : result . targetPackageId ,
4710+ sourcePackageId : result . sourcePackageId ,
4711+ ...( result . sourceVersion !== undefined ? { sourceVersion : result . sourceVersion } : { } ) ,
4712+ seeded : seedSampleData ,
4713+ copiedAt : new Date ( ) . toISOString ( ) ,
4714+ ...( userId ? { copiedBy : userId } : { } ) ,
4715+ } ) ;
4716+ } catch ( err : any ) {
4717+ ctx . logger ?. warn ?.(
4718+ `[MarketplaceInstallLocal] the record of the unmanaged copy '${ result . targetPackageId } ' could not be written `
4719+ + `under ${ this . unmanagedCopies . dir } : ${ err ?. message ?? err } . Its sample data cannot be purged through this door.` ,
4720+ ) ;
4721+ }
46874722 const declaresSeeds = unmanagedInstallValueCarries ( packageBody . data ) ;
46884723 // [#30808] Asked for: the rows, from the receipt the verb just wrote.
46894724 const seeded = seedSampleData
@@ -5427,12 +5462,23 @@ export class MarketplaceInstallLocalPlugin implements Plugin {
54275462 return c . json ( { success : false , error : { code : 'INVALID_REQUEST' , message : 'manifestId path param required.' } } , 400 ) ;
54285463 }
54295464 // [#30808] An unmanaged copy writes no install-ledger entry: its base
5430- // holds the seed receipt instead, and the purge deletes by it.
5465+ // holds the seed receipt instead, and the purge deletes by it — for a
5466+ // base this door copied ({@link UnmanagedCopyRecord}) and no other.
54315467 if ( ! this . ledger . has ( manifestId ) ) {
54325468 const copy = await this . unmanagedCopyPurgeDatasets ( ctx , manifestId ) ;
54335469 if ( copy === null ) {
54345470 return c . json ( { success : false , error : { code : 'RESOURCE_NOT_FOUND' , message : `No marketplace install for ${ manifestId } .` } } , 404 ) ;
54355471 }
5472+ if ( 'notSeeded' in copy ) {
5473+ return c . json ( {
5474+ success : false ,
5475+ error : {
5476+ code : 'NOTHING_TO_PURGE' ,
5477+ message : `No sample data was copied into '${ manifestId } ': it was installed unmanaged without `
5478+ + '`seedSampleData`. Nothing was deleted.' ,
5479+ } ,
5480+ } , 400 ) ;
5481+ }
54365482 if ( 'failure' in copy ) {
54375483 ctx . logger ?. warn ?.( `[MarketplaceInstallLocal] purge of the unmanaged copy ${ manifestId } refused: ${ copy . failure } ` ) ;
54385484 return c . json ( {
@@ -5445,7 +5491,8 @@ export class MarketplaceInstallLocalPlugin implements Plugin {
54455491 } ,
54465492 } , 503 ) ;
54475493 }
5448- return this . purgeSeedDatasets ( c , ctx , manifestId , copy . datasets , null ) ;
5494+ return this . purgeSeedDatasets ( c , ctx , manifestId , copy . datasets , null ,
5495+ `The sample data receipt of '${ manifestId } ' holds no seed item any more, so no row can be matched. Nothing was deleted.` ) ;
54495496 }
54505497 // #5426 — same shape as reseed: `has()` said the file is there, so a
54515498 // missing entry here is an unreadable one, and the operator gets the
@@ -5462,19 +5509,45 @@ export class MarketplaceInstallLocalPlugin implements Plugin {
54625509 return this . purgeSeedDatasets ( c , ctx , manifestId , seedDatasetsOf ( entry . manifest ) , entry ) ;
54635510 } ;
54645511
5512+ /**
5513+ * [#30808] Remove the door's record of a copy when its base is uninstalled
5514+ * (the protocol's uninstall cleanups, which `deletePackage` runs), so a
5515+ * later base under the same id is never taken for the copy. A protocol
5516+ * without the cleanup registry leaves the records as they are; the purge
5517+ * still answers 404 for a record whose base is gone.
5518+ */
5519+ private registerUnmanagedCopyCleanup = ( ctx : PluginContext ) : void => {
5520+ let protocol : { registerUninstallCleanup ?: ( name : string , cleanup : UninstallCleanup ) => void } | undefined ;
5521+ try { protocol = ctx . getService < typeof protocol > ( 'protocol' ) ; } catch { return ; /* no protocol service */ }
5522+ if ( ! protocol || typeof protocol . registerUninstallCleanup !== 'function' ) return ;
5523+ protocol . registerUninstallCleanup ( UNMANAGED_COPY_RECORD_UNINSTALL_CLEANUP , async ( { packageId } ) => {
5524+ try {
5525+ return { success : true , removed : this . unmanagedCopies . remove ( packageId ) ? 1 : 0 } ;
5526+ } catch ( err : any ) {
5527+ return { success : false , removed : 0 , error : String ( err ?. message ?? err ) } ;
5528+ }
5529+ } ) ;
5530+ } ;
5531+
54655532 /**
54665533 * [#30808] The datasets a purge of an UNMANAGED COPY deletes by: its
54675534 * base's seed receipt ({@link readUnmanagedCopySeedReceipt}). `null` when
5468- * `packageId` is no installed writable base — the purge's 404, as for no
5469- * install at all; `failure` when the receipt could not be read.
5535+ * `packageId` is no copy this door made ({@link UnmanagedCopyRecord}) or
5536+ * is no longer an installed writable base — the purge's 404, as for no
5537+ * install at all; `notSeeded` for a copy made without the sample data;
5538+ * `failure` when the record or the receipt could not be read.
54705539 */
54715540 private unmanagedCopyPurgeDatasets = async (
54725541 ctx : PluginContext ,
54735542 packageId : string ,
5474- ) : Promise < { datasets : PurgeSeedDataset [ ] } | { failure : string } | null > => {
5543+ ) : Promise < { datasets : PurgeSeedDataset [ ] } | { notSeeded : true } | { failure : string } | null > => {
5544+ const record = this . unmanagedCopies . read ( packageId ) ;
5545+ if ( record . failure !== undefined ) return { failure : record . failure } ;
5546+ if ( record . entry === null ) return null ;
54755547 let ql : NamespaceHoldingEngine | undefined ;
54765548 try { ql = ctx . getService < NamespaceHoldingEngine > ( 'objectql' ) ; } catch { return null ; /* no data engine — no base */ }
54775549 if ( ! ql ?. registry ?. getPackage ?.( packageId ) || ! isWritablePackage ( ql , packageId ) ) return null ;
5550+ if ( ! record . entry . seeded ) return { notSeeded : true } ;
54785551 const listed = await this . unmanagedCopySeedNames ( ctx , packageId ) ;
54795552 if ( listed . failure !== undefined ) return { failure : listed . failure } ;
54805553 const receipt = await this . readUnmanagedCopySeedReceipt ( ctx , packageId , listed . names ) ;
@@ -5493,12 +5566,13 @@ export class MarketplaceInstallLocalPlugin implements Plugin {
54935566 manifestId : string ,
54945567 datasets : PurgeSeedDataset [ ] ,
54955568 entry : InstalledEntry | null ,
5569+ nothingToPurge = 'This package declares no seed datasets.' ,
54965570 ) : Promise < Response > => {
54975571
54985572 if ( datasets . length === 0 ) {
54995573 return c . json ( {
55005574 success : false ,
5501- error : { code : 'NOTHING_TO_PURGE' , message : 'This package declares no seed datasets.' } ,
5575+ error : { code : 'NOTHING_TO_PURGE' , message : nothingToPurge } ,
55025576 } , 400 ) ;
55035577 }
55045578
0 commit comments