@@ -152,6 +152,9 @@ import { repeatedQueryParamMessage, unknownQueryParamRefusalMessage } from '@obj
152152// defect (#6928) this module exists to stop anyone writing again.
153153import { parseBooleanParam } from '../query-param.js' ;
154154import { setPackageDisabled } from '../package-state-store.js' ;
155+ // [#30962] The host seam for the same state: a store a host registers as the
156+ // `package-state-store` service replaces the file at every door below.
157+ import { asPackageStateStore , PACKAGE_STATE_STORE_SERVICE } from '../package-state-host-store.js' ;
155158import type { HttpProtocolContext , HttpDispatcherResult } from '../http-dispatcher.js' ;
156159import type { DomainHandlerDeps , DomainRoute } from '../domain-handler-registry.js' ;
157160import { readDispatchedBody } from '../deferred-request-body.js' ;
@@ -753,6 +756,99 @@ interface PackageRowRegistryHost {
753756 } ;
754757}
755758
759+ /**
760+ * [#30962] A host's package-state store refused a write (or its lookup
761+ * faulted). Raised by {@link writePackageState} only for a HOST store: the
762+ * file's own failure is rethrown as it is, so the file default's handling
763+ * stays exactly what it was.
764+ */
765+ class HostPackageStateRefusal extends Error {
766+ constructor ( readonly packageId : string , cause : unknown ) {
767+ super ( ( cause as Error ) ?. message ?? String ( cause ) , { cause } ) ;
768+ this . name = 'HostPackageStateRefusal' ;
769+ }
770+ }
771+
772+ /**
773+ * [#30962] Write `packageId`'s disabled flag to this environment's package-state
774+ * store: the host's when one is registered as the `package-state-store`
775+ * service (`../package-state-host-store.ts`), the file under `OS_HOME`
776+ * otherwise. Resolved through the CLASSIFIED lookup, so a host store whose
777+ * factory throws refuses the write instead of reading as "no host store" and
778+ * landing the write in a file nobody reads.
779+ */
780+ async function writePackageState (
781+ deps : DomainHandlerDeps ,
782+ context : HttpProtocolContext ,
783+ packageId : string ,
784+ disabled : boolean ,
785+ ) : Promise < void > {
786+ let host ;
787+ try {
788+ host = asPackageStateStore ( await deps . resolveServiceOrLoud ( context , PACKAGE_STATE_STORE_SERVICE ) ) ;
789+ } catch ( err ) {
790+ throw new HostPackageStateRefusal ( packageId , err ) ;
791+ }
792+ if ( ! host ) {
793+ setPackageDisabled ( context ?. environmentId , packageId , disabled ) ;
794+ return ;
795+ }
796+ try {
797+ await host . setPackageDisabled ( context ?. environmentId , packageId , disabled ) ;
798+ } catch ( err ) {
799+ throw new HostPackageStateRefusal ( packageId , err ) ;
800+ }
801+ }
802+
803+ /**
804+ * [#30962, cloud#3636 ruling acceptance line 1] The enable and disable doors'
805+ * answer to a host store that refused: nothing changed in memory (the doors
806+ * write BEFORE they switch the package), so the door says so, and that the
807+ * same request can be retried, instead of answering a switch the store does
808+ * not hold. The standard `SERVICE_UNAVAILABLE`: the store, not the request,
809+ * is what failed.
810+ */
811+ function hostPackageStateRefused (
812+ deps : DomainHandlerDeps ,
813+ refusal : HostPackageStateRefusal ,
814+ verb : 'enabled' | 'disabled' ,
815+ ) : HttpDispatcherResult {
816+ return {
817+ handled : true ,
818+ response : deps . error (
819+ `Package '${ refusal . packageId } ' was not ${ verb } : the package-state store refused to record it, so `
820+ + `nothing changed. Retry once the store accepts writes. (${ refusal . message } )` ,
821+ 503 ,
822+ { code : 'SERVICE_UNAVAILABLE' } ,
823+ ) ,
824+ } ;
825+ }
826+
827+ /**
828+ * [#30962] {@link writePackageState} for the doors whose package change has
829+ * ALREADY landed when they write (install, delete): the request is not undone
830+ * over the state store. A host store's refusal is logged at `error` here, once
831+ * per failed write, naming what is not durable; the file's failure is rethrown
832+ * to the caller's existing `warn`, unchanged.
833+ */
834+ async function writePackageStateAfterChange (
835+ deps : DomainHandlerDeps ,
836+ context : HttpProtocolContext ,
837+ packageId : string ,
838+ disabled : boolean ,
839+ ) : Promise < void > {
840+ try {
841+ await writePackageState ( deps , context , packageId , disabled ) ;
842+ } catch ( err ) {
843+ if ( ! ( err instanceof HostPackageStateRefusal ) ) throw err ;
844+ console . error (
845+ `[handlePackages] the package-state store refused to record package '${ packageId } ' as `
846+ + `${ disabled ? 'disabled' : 'enabled' } : this process serves it that way, but the next boot reads the `
847+ + `store and will not. Switch it again once the store accepts writes. (${ err . message } )` ,
848+ ) ;
849+ }
850+ }
851+
756852/**
757853 * [#30377] An enable of a disabled package recomputes the roll-up summaries the
758854 * package adds to other packages' objects by extension (`ObjectQL`'s
@@ -1914,12 +2010,14 @@ export async function handlePackagesRequest(deps: DomainHandlerDeps, path: strin
19142010 // explicit action left there. #18058's «每一次 install 都持久化它返回
19152011 // 的状态» is unchanged; what changed is which state that is.
19162012 //
1917- // Same best-effort try/catch as `PATCH /packages/:id/enable` below:
1918- // the in-memory install already succeeded, so a state-file failure
1919- // must not turn a 201 into a 500.
2013+ // Best-effort: the in-memory install already succeeded, so a
2014+ // state-store failure must not turn a 201 into a 500. [#30962] A
2015+ // host store's refusal is logged at `error` instead
2016+ // (`writePackageStateAfterChange`); only the enable and disable
2017+ // doors, which write before they switch, answer it.
19202018 const rowDisabled = pkg ?. enabled === false ;
19212019 try {
1922- setPackageDisabled ( _context ?. environmentId , pkgId , rowDisabled ) ;
2020+ await writePackageStateAfterChange ( deps , _context , pkgId , rowDisabled ) ;
19232021 } catch ( err ) {
19242022 console . warn ( '[handlePackages] failed to persist enableOnInstall' , { id : pkgId , disabled : rowDisabled , error : ( err as Error ) ?. message } ) ;
19252023 }
@@ -1936,14 +2034,20 @@ export async function handlePackagesRequest(deps: DomainHandlerDeps, path: strin
19362034 const walled = await refuseWalledCatalogPackageWrite ( deps , _context , `Enabling package "${ id } "` ) ;
19372035 if ( walled ) return walled ;
19382036 const queryRefused = refusePackageWriteDoorQuery ( deps , query , PACKAGE_WRITE_NO_QUERY_PARAMS ) ; if ( queryRefused ) return queryRefused ;
1939- const pkg = registry . enablePackage ( id ) ;
1940- if ( ! pkg ) return { handled : true , response : deps . error ( `Package '${ id } ' not found` , 404 ) } ;
1941- await summaryRefreshSettled ( qlService ) ;
2037+ // [#30962] The state store FIRST, the registry second: a host store
2038+ // that refuses leaves the package exactly as it was, and the door
2039+ // answers the refusal (`hostPackageStateRefused`). The file keeps
2040+ // its best-effort `warn`, as before.
2041+ if ( ! registry . getPackage ( id ) ) return { handled : true , response : deps . error ( `Package '${ id } ' not found` , 404 ) } ;
19422042 try {
1943- setPackageDisabled ( _context ?. environmentId , id , false ) ;
2043+ await writePackageState ( deps , _context , id , false ) ;
19442044 } catch ( err ) {
2045+ if ( err instanceof HostPackageStateRefusal ) return hostPackageStateRefused ( deps , err , 'enabled' ) ;
19452046 console . warn ( '[handlePackages] failed to persist enable state' , { id, error : ( err as Error ) ?. message } ) ;
19462047 }
2048+ const pkg = registry . enablePackage ( id ) ;
2049+ if ( ! pkg ) return { handled : true , response : deps . error ( `Package '${ id } ' not found` , 404 ) } ;
2050+ await summaryRefreshSettled ( qlService ) ;
19472051 return { handled : true , response : deps . success ( pkg ) } ;
19482052 }
19492053
@@ -1970,13 +2074,18 @@ export async function handlePackagesRequest(deps: DomainHandlerDeps, path: strin
19702074 ? null
19712075 : requireWritablePackage ( deps , qlService , id , 'disable' ) ;
19722076 if ( readOnly ) return readOnly ;
1973- const pkg = registry . disablePackage ( id ) ;
1974- if ( ! pkg ) return { handled : true , response : deps . error ( `Package '${ id } ' not found` , 404 ) } ;
2077+ // [#30962] The state store FIRST, the registry second, exactly as
2078+ // the enable door above: a host store that refuses leaves the
2079+ // package enabled and served, and the door answers the refusal.
2080+ if ( ! registry . getPackage ( id ) ) return { handled : true , response : deps . error ( `Package '${ id } ' not found` , 404 ) } ;
19752081 try {
1976- setPackageDisabled ( _context ?. environmentId , id , true ) ;
2082+ await writePackageState ( deps , _context , id , true ) ;
19772083 } catch ( err ) {
2084+ if ( err instanceof HostPackageStateRefusal ) return hostPackageStateRefused ( deps , err , 'disabled' ) ;
19782085 console . warn ( '[handlePackages] failed to persist disable state' , { id, error : ( err as Error ) ?. message } ) ;
19792086 }
2087+ const pkg = registry . disablePackage ( id ) ;
2088+ if ( ! pkg ) return { handled : true , response : deps . error ( `Package '${ id } ' not found` , 404 ) } ;
19802089 return { handled : true , response : deps . success ( pkg ) } ;
19812090 }
19822091
@@ -3127,12 +3236,12 @@ export async function handlePackagesRequest(deps: DomainHandlerDeps, path: strin
31273236 // whenever its stored row is gone, even if the running registry
31283237 // refused the withdrawal above.
31293238 //
3130- // Same best-effort try/catch as the install and PATCH arms above:
3131- // the uninstall itself already happened, so a state-file failure
3132- // must not turn it into a 500.
3239+ // Same best-effort try/catch as the install arm above: the
3240+ // uninstall itself already happened, so a state-store failure must
3241+ // not turn it into a 500.
31333242 if ( persists ? existed : registryRemoved ) {
31343243 try {
3135- setPackageDisabled ( _context ?. environmentId , id , false ) ;
3244+ await writePackageStateAfterChange ( deps , _context , id , false ) ;
31363245 } catch ( err ) {
31373246 console . warn ( '[handlePackages] failed to clear persisted disable state on delete' , { id, error : ( err as Error ) ?. message } ) ;
31383247 }
0 commit comments