@@ -3,7 +3,7 @@ import { readRuntimePort } from "../config/process-state";
33import { readClientConnectionState } from "../client/state" ;
44import { getActiveTurnCount , isDraining , isRecyclingForExit , isShutdownDraining } from "../server/lifecycle" ;
55import type { OcxConfig } from "../types" ;
6- import { observeCatalogHealFile , sameCatalogHealPath , selectCatalogHealPath , type CatalogObservation } from "./catalog/heal-observation" ;
6+ import { compareCatalogHealPaths , observeCatalogHealFile , retainCatalogHealPath , selectCatalogHealPath , type CatalogObservation } from "./catalog/heal-observation" ;
77import { subscribeCatalogPublication } from "./catalog/publication-observer" ;
88import { configEnablesRoutedNamespace , ocxRoutedNamespaceCounts } from "./catalog/routed-removal" ;
99import { inspectCodexHomeOwner } from "./codex-home-owner" ;
@@ -51,6 +51,7 @@ export interface CatalogSelfHealDeps {
5151 now ?: ( ) => number ;
5252 catalogPath ?: ( ) => string | null ;
5353 observe ?: ( path : string , readContent : boolean ) => CatalogObservation | null ;
54+ retainPath ?: typeof retainCatalogHealPath ;
5455 converge ?: ( config : OcxConfig , lifecycle : CatalogHealLifecycle ) => Promise < CatalogHealOutcome > ;
5556 subscribe ?: typeof subscribeCatalogPublication ;
5657 gates ?: Partial < CatalogSelfHealGates > ;
@@ -135,14 +136,19 @@ export function startCodexCatalogSelfHeal(options: { port?: number; deps?: Catal
135136 const attempts : number [ ] = [ ] ;
136137 const heals : number [ ] = [ ] ;
137138
138- const read = ( path : string ) : NamespaceObservation | null => {
139+ const read = ( path : string , fresh = false ) : NamespaceObservation | null => {
139140 const quick = observe ( path , false ) ;
140141 if ( quick === null ) return null ;
141- if ( observed !== null && sameCatalogHealPath ( observed . path , path ) && observed . signature === quick . signature ) return observed ;
142+ if ( ! fresh && observed !== null ) {
143+ const relation = compareCatalogHealPaths ( observed . path , path ) ;
144+ if ( relation === "unknown" ) return null ;
145+ if ( relation === "same" && observed . signature === quick . signature ) return observed ;
146+ }
142147 const seen = observe ( path , true ) ;
143148 if ( seen === null ) return null ;
144- observed = { path, signature : seen . signature , namespaces : seen . catalog ? new Set ( ocxRoutedNamespaceCounts ( seen . catalog ) . keys ( ) ) : null } ;
145- return observed ;
149+ const result = { path, signature : seen . signature , namespaces : seen . catalog ? new Set ( ocxRoutedNamespaceCounts ( seen . catalog ) . keys ( ) ) : null } ;
150+ if ( ! fresh ) observed = result ;
151+ return result ;
146152 } ;
147153 const accept = ( path : string ) : void => {
148154 const seen = read ( path ) ;
@@ -154,18 +160,23 @@ export function startCodexCatalogSelfHeal(options: { port?: number; deps?: Catal
154160 pendingRetry = null ;
155161 recheckAt = 0 ;
156162 } ;
157- const updateTarget = ( path : string | null ) : void => {
163+ const updateTarget = ( path : string | null ) : boolean => {
158164 // Unavailable selection is not evidence of a different target or lost authority.
159- if ( path === null ) return ;
160- if ( target !== null && sameCatalogHealPath ( target , path ) ) return ;
165+ if ( path === null ) return false ;
166+ if ( target !== null ) {
167+ const relation = compareCatalogHealPaths ( target , path ) ;
168+ if ( relation === "unknown" ) return false ;
169+ if ( relation === "same" ) return true ;
170+ }
161171 clear ( ) ;
162172 target = path ;
173+ return true ;
163174 } ;
164175 const beforeCommit = ( path : string , entryGeneration : number ) : boolean => {
165176 try {
166177 const selected = catalogPath ( ) ;
167178 return ! stopped && ! released && generation === entryGeneration
168- && selected !== null && sameCatalogHealPath ( selected , path ) && evaluateCatalogSelfHealGates ( gates ) . open ;
179+ && selected !== null && compareCatalogHealPaths ( selected , path ) === "same" && evaluateCatalogSelfHealGates ( gates ) . open ;
169180 } catch { return false ; }
170181 } ;
171182 const capRetryAt = ( now : number ) : number => {
@@ -182,8 +193,7 @@ export function startCodexCatalogSelfHeal(options: { port?: number; deps?: Catal
182193 running = true ;
183194 try {
184195 const path = catalogPath ( ) ;
185- updateTarget ( path ) ;
186- if ( path === null ) return ;
196+ if ( ! updateTarget ( path ) || path === null ) return ;
187197 // Pending cache/funnel failure is independent of catalog stat or apparent repaired rows.
188198 if ( pendingRetry ) {
189199 const config = gates . loadConfig ( ) ;
@@ -226,7 +236,7 @@ export function startCodexCatalogSelfHeal(options: { port?: number; deps?: Catal
226236 } ) ) . committed ; }
227237 catch { /* Failed attempts retain pending retry and consume the attempt budget. */ }
228238 const selected = catalogPath ( ) ;
229- if ( stopped || released || generation !== entryGeneration || selected === null || ! sameCatalogHealPath ( selected , path ) ) return ;
239+ if ( stopped || released || generation !== entryGeneration || selected === null || compareCatalogHealPaths ( selected , path ) !== "same" ) return ;
230240 last = { at : new Date ( ) . toISOString ( ) , lostNamespaces : lostEnabled . length , committed } ;
231241 if ( committed ) {
232242 heals . push ( clock ( ) ) ;
@@ -241,33 +251,43 @@ export function startCodexCatalogSelfHeal(options: { port?: number; deps?: Catal
241251
242252 const unsubscribe = ( deps . subscribe ?? subscribeCatalogPublication ) ( event => {
243253 if ( stopped ) return ;
254+ const release = event . kind === "native-released" || event . intent === "restore" ;
255+ // Bind publication evidence before comparisons can observe a retargeted alias.
256+ const stillPublished = ! release && event . path !== null ? ( deps . retainPath ?? retainCatalogHealPath ) ( event . path ) : null ;
244257 // Native restoration can remove the journal before notification. Fence the known
245258 // target (and target-less releases) without resolving possibly unavailable evidence.
246- const release = event . kind === "native-released" || event . intent === "restore ";
247- if ( release && ( event . path === null || ( target !== null && sameCatalogHealPath ( event . path , target ) ) ) ) {
259+ const known = event . path !== null && target !== null ? compareCatalogHealPaths ( event . path , target ) : "different ";
260+ if ( release && ( event . path === null || known !== "different" ) ) {
248261 clear ( ) ;
249262 released = true ;
250263 return ;
251264 }
252265 const path = catalogPath ( ) ;
253- if ( event . path !== null && ( target === null || ! sameCatalogHealPath ( event . path , target ) )
254- && ( path === null || ! sameCatalogHealPath ( event . path , path ) ) ) return ;
266+ const selected = event . path !== null && path !== null ? compareCatalogHealPaths ( event . path , path ) : "different" ;
267+ if ( event . path !== null && known === "different" && selected === "different" ) return ;
255268 if ( release ) {
256269 clear ( ) ;
257270 released = true ; // Only a later accepted owner publication can re-arm this lifecycle.
258271 return ;
259272 }
260- if ( running ) return ; // Catalog-success/cache-failure must not accept a baseline.
273+ if ( running ) {
274+ if ( known === "unknown" || selected === "unknown" ) clear ( ) ;
275+ return ; // Catalog-success/cache-failure must not accept a baseline.
276+ }
261277 // A committed owner publication supplies a read-only baseline even while heal writes are gated.
262- if ( event . path === null || path === null || ! sameCatalogHealPath ( event . path , path ) ) return ;
263- updateTarget ( path ) ;
278+ if ( event . path === null || path === null || selected !== "same" || stillPublished === null ) return ;
279+ // Read independently of obsolete evidence, but retire it only after current selection proof.
280+ const seen = read ( event . path , true ) ;
281+ const current = catalogPath ( ) ;
282+ if ( seen ?. namespaces == null || current === null
283+ || compareCatalogHealPaths ( event . path , current ) !== "same" || ! stillPublished ( seen . signature ) ) return ;
284+ clear ( ) ;
285+ target = current ;
286+ baseline = observed = seen ;
264287 released = false ;
265- pendingRetry = null ;
266- recheckAt = 0 ;
267- accept ( event . path ) ;
268288 } ) ;
269289 // Capture immediately after startup sync, before a foreign write can become the first tick.
270- try { updateTarget ( catalogPath ( ) ) ; if ( target !== null ) accept ( target ) ; }
290+ try { const path = catalogPath ( ) ; if ( updateTarget ( path ) && target !== null ) accept ( target ) ; }
271291 catch { /* Unavailable observation can be retried by the scheduled tick. */ }
272292 const schedule = ( ) : void => {
273293 if ( stopped ) return ;
0 commit comments