@@ -46,6 +46,12 @@ import { canonicalMetaType, captureUninstallDeclared, metadataReadFailureError,
4646export interface PackageUninstallHost {
4747 readonly deleteMetaItem : ObjectStackProtocolImplementation [ 'deleteMetaItem' ] ;
4848 readonly engine : ObjectStackProtocolImplementation [ 'engine' ] ;
49+ /**
50+ * [#31186] The environment the protocol is bound to. The class field is
51+ * private, so it cannot be named by an indexed access; it is read for the
52+ * package's ledger row and nothing else.
53+ */
54+ readonly environmentId : string | undefined ;
4955 readonly findEnvironmentRows : ObjectStackProtocolImplementation [ 'findEnvironmentRows' ] ;
5056 readonly getServicesRegistry : ObjectStackProtocolImplementation [ 'getServicesRegistry' ] ;
5157 readonly packagelessAddress : ObjectStackProtocolImplementation [ 'packagelessAddress' ] ;
@@ -54,7 +60,7 @@ export interface PackageUninstallHost {
5460}
5561
5662/** The body of `ObjectStackProtocolImplementation#deletePackage`; the method's docblock there is its contract. */
57- export async function deletePackage ( this : Pick < PackageUninstallHost , 'deleteMetaItem' | 'engine' | 'findEnvironmentRows' | 'getServicesRegistry' | 'packagelessAddress' | 'runUninstallCleanups' | 'shouldDropStorage' > , request : DeletePackageRequest ) : Promise < DeletePackageResponse > {
63+ export async function deletePackage ( this : Pick < PackageUninstallHost , 'deleteMetaItem' | 'engine' | 'environmentId' | ' findEnvironmentRows' | 'getServicesRegistry' | 'packagelessAddress' | 'runUninstallCleanups' | 'shouldDropStorage' > , request : DeletePackageRequest ) : Promise < DeletePackageResponse > {
5864 // [ADR-0131 D6/D12] An uninstall is environment-wide by construction:
5965 // every `sys_metadata` row bound to the package, in this environment,
6066 // whatever organization a legacy row was stored under. The
@@ -140,6 +146,11 @@ export async function deletePackage(this: Pick<PackageUninstallHost, 'deleteMeta
140146 packageRegistry . assertPackageUninstallable ( request . packageId ) ;
141147 }
142148
149+ // [#31186] What the running registry holds for this id, read before
150+ // anything is removed: whether there is a package to uninstall at all, and
151+ // its version, for the package's own ledger row at the end of this verb.
152+ const installed = readInstalledPackage ( packageRegistry , request . packageId ) ;
153+
143154 // [#23473, ADR-0048] THE SHARED-OBJECT REFUSAL, ALSO ASKED BEFORE THE
144155 // STORE DELETE. A `sys_metadata` row is keyed by its package, so an
145156 // object this package holds can also be held by another package's row,
@@ -354,6 +365,24 @@ export async function deletePackage(this: Pick<PackageUninstallHost, 'deleteMeta
354365 // uninstall door calls too.
355366 const cleanups = await this . runUninstallCleanups ( { ...request , ...( declared ? { declared } : { } ) } ) ;
356367
368+ // [#31186] The package's own ledger row, written once, here, after its
369+ // stored row is gone — the one place every uninstall door passes. A
370+ // refusal above threw before this line, so it writes none. An id the
371+ // registry did not hold and the store bound no metadata to was not a
372+ // package, so it writes none either: the `DELETE` door answers that `404`.
373+ if ( installed . held || deleted . length > 0 || failed . length > 0 ) {
374+ await recordPackageUninstall ( this . getServicesRegistry ?.( ) ?. get ( 'audit' ) , {
375+ action : 'package_uninstall' ,
376+ packageId : request . packageId ,
377+ ...( installed . version ? { version : installed . version } : { } ) ,
378+ ...( this . environmentId ? { environmentId : this . environmentId } : { } ) ,
379+ ...( request . actor ? { actor : request . actor } : { } ) ,
380+ ...( request . keepData === true ? { keepData : true } : { } ) ,
381+ itemsRemoved : deleted . length ,
382+ itemsFailed : failed . length ,
383+ } ) ;
384+ }
385+
357386 return {
358387 success : failed . length === 0 && deleted . length > 0 ,
359388 deletedCount : deleted . length ,
@@ -363,3 +392,61 @@ export async function deletePackage(this: Pick<PackageUninstallHost, 'deleteMeta
363392 cleanups,
364393 } ;
365394}
395+
396+ /**
397+ * [#31186] The event the uninstall verb hands the `audit` slot's
398+ * `recordPackageLifecycleEvent` — declared here, structurally, so this package
399+ * does not depend on `@objectstack/plugin-audit`, which owns the row shape
400+ * (`package-lifecycle-audit.ts` there).
401+ */
402+ export interface PackageUninstallAuditEvent {
403+ action : 'package_uninstall' ;
404+ packageId : string ;
405+ version ?: string ;
406+ environmentId ?: string ;
407+ actor ?: string ;
408+ keepData ?: boolean ;
409+ itemsRemoved : number ;
410+ itemsFailed : number ;
411+ }
412+
413+ /** The `audit` slot member the verb calls. */
414+ interface PackageLifecycleAuditSurface {
415+ recordPackageLifecycleEvent ?( event : PackageUninstallAuditEvent ) : unknown ;
416+ }
417+
418+ /** Whether the registry holds `packageId`, and the version it holds. Never throws. */
419+ function readInstalledPackage ( registry : unknown , packageId : string ) : { held : boolean ; version ?: string } {
420+ try {
421+ const pkg = ( registry as { getPackage ?( id : string ) : unknown } | undefined ) ?. getPackage ?.( packageId ) as
422+ | { manifest ?: { version ?: unknown } }
423+ | undefined ;
424+ if ( pkg === undefined || pkg === null ) return { held : false } ;
425+ const version = pkg . manifest ?. version ;
426+ return { held : true , ...( typeof version === 'string' && version !== '' ? { version } : { } ) } ;
427+ } catch {
428+ // A registry that cannot answer is not asked again: the row is then
429+ // written only when stored items were removed, without a version.
430+ return { held : false } ;
431+ }
432+ }
433+
434+ /**
435+ * Hand the event to the `audit` slot. A host with no audit plugin has no
436+ * ledger at all, so the call is skipped silently. The writer never throws; a
437+ * slot that does anyway is a foreign writer, and the uninstall it reports on
438+ * has already completed, so its failure is logged, never answered.
439+ */
440+ async function recordPackageUninstall ( audit : unknown , event : PackageUninstallAuditEvent ) : Promise < void > {
441+ const record = ( audit as PackageLifecycleAuditSurface | undefined ) ?. recordPackageLifecycleEvent ;
442+ if ( typeof record !== 'function' ) return ;
443+ try {
444+ await record . call ( audit , event ) ;
445+ } catch ( e ) {
446+ console . error (
447+ `[protocol.deletePackage] '${ event . packageId } ' was uninstalled, but the audit service refused its `
448+ + '`package_uninstall` row, so the compliance ledger does not record who uninstalled it: '
449+ + `${ ( e as Error ) ?. message ?? String ( e ) } ` ,
450+ ) ;
451+ }
452+ }
0 commit comments