@@ -261,6 +261,9 @@ async function memoryEnabled(binding: CachedBinding): Promise<boolean> {
261261 return ( await memoryStatus ( binding ) ) === "enabled"
262262}
263263
264+ /** A failed read says what failed, so the sync record can classify it. */
265+ type MemoryStatusOutcome = "enabled" | "disabled" | { status : "error" ; error : string }
266+
264267/** Three-way, because a read that cannot reach the service must not be reported
265268 * as "this workspace has no memory" — that reads as success while destroying
266269 * whatever the session already had. */
@@ -273,7 +276,7 @@ async function memoryStatus(
273276 * inherit a positive written under a previous account. */
274277 fresh ?: boolean
275278 } = { } ,
276- ) : Promise < "enabled" | "disabled" | "error" > {
279+ ) : Promise < MemoryStatusOutcome > {
277280 const cached = opts . fresh ? undefined : memoryEnabledCache . get ( binding . datamateId )
278281 if ( cached && Date . now ( ) - cached . checkedAt < MEMORY_ENABLED_TTL_MS ) return "enabled"
279282 try {
@@ -289,7 +292,7 @@ async function memoryStatus(
289292 missingFromListWarned . add ( binding . datamateId )
290293 log . warn ( "workspace missing from the workspace list; memory setting unknown" , { workspace : binding . datamateId } )
291294 }
292- return "error"
295+ return { status : "error" , error : "workspace missing from the workspace list" }
293296 }
294297 missingFromListWarned . delete ( binding . datamateId )
295298 if ( match . memoryEnabled === undefined && ! missingFieldWarned . has ( binding . datamateId ) ) {
@@ -316,7 +319,7 @@ async function memoryStatus(
316319 log . warn ( "could not confirm workspace memory setting" , { err : String ( err ) } )
317320 // Not cached either way: a transient failure should neither disable the
318321 // mirror for a minute nor keep it enabled.
319- return "error"
322+ return { status : "error" , error : String ( err ) }
320323 }
321324}
322325
@@ -1108,7 +1111,7 @@ export async function memoryEnabledForPoller(binding: CachedBinding): Promise<"e
11081111 const ask = ( async ( ) => {
11091112 try {
11101113 const status = await memoryStatus ( binding , { fresh : true } )
1111- const answer = status === "error " ? ( "unknown" as const ) : status
1114+ const answer = typeof status === "string " ? status : ( "unknown" as const )
11121115 pollMemo . set ( key , { at : Date . now ( ) , status : answer } )
11131116 return answer
11141117 } finally {
@@ -1242,8 +1245,10 @@ export async function backfill(
12421245 if ( ! binding )
12431246 return { ok : 0 , failed : 0 , skipped : blocks . length , declined : 0 , deferred : 0 , gated : true , gateReason : "unbound" }
12441247 const status = await memoryStatus ( binding )
1245- if ( status !== "enabled" )
1246- return { ok : 0 , failed : 0 , skipped : blocks . length , declined : 0 , deferred : 0 , gated : true , gateReason : status }
1248+ if ( status !== "enabled" ) {
1249+ const gateReason = typeof status === "string" ? status : status . status
1250+ return { ok : 0 , failed : 0 , skipped : blocks . length , declined : 0 , deferred : 0 , gated : true , gateReason }
1251+ }
12471252 const index = await readIndex ( )
12481253
12491254 const { pending, skipped } = partitionPending ( blocks , binding , index )
@@ -1412,7 +1417,7 @@ type LoadOutcome = (
14121417 }
14131418 | { status : "unlinked" ; /** The server confirmed it, rather than a lookup failing. */ confirmed : boolean }
14141419 | { status : "disabled" }
1415- | { status : "error" }
1420+ | { status : "error" ; /** What failed, for the sync record and its reason class. */ error : string }
14161421) & {
14171422 epoch ?: string
14181423 dir ?: string | null
@@ -1454,12 +1459,12 @@ async function loadWorkspaceMemory(directory?: string): Promise<LoadOutcome> {
14541459 stable = before === epoch
14551460 }
14561461 vouched = epoch
1457- if ( ! stable ) return { status : "error" , epoch, dir, ...stamp }
1462+ if ( ! stable ) return { status : "error" , error : "the workspace link changed while loading" , epoch, dir, ...stamp }
14581463 if ( ! binding ) return { status : "unlinked" , confirmed, epoch, dir, ...stamp }
14591464 const datamateId = binding . datamateId
14601465 resolved = datamateId
14611466 const enabled = await memoryStatus ( binding )
1462- if ( enabled === "error ") return { status : "error" , epoch, dir, datamateId, ...stamp }
1467+ if ( typeof enabled !== "string ") return { status : "error" , error : enabled . error , epoch, dir, datamateId, ...stamp }
14631468 if ( enabled === "disabled" ) return { status : "disabled" , epoch, dir, datamateId, ...stamp }
14641469
14651470 const ownProjectKey = projectKeyFor ( binding )
@@ -1503,7 +1508,7 @@ async function loadWorkspaceMemory(directory?: string): Promise<LoadOutcome> {
15031508 log . warn ( "workspace memory load failed" , { err : String ( err ) } )
15041509 // Stamped like any other outcome: without an epoch the session would reload (and make
15051510 // the prompt wait) on every turn for as long as the service is down.
1506- return { status : "error" , epoch : vouched , dir, datamateId : resolved , ...stamp }
1511+ return { status : "error" , error : String ( err ) , epoch : vouched , dir, datamateId : resolved , ...stamp }
15071512 }
15081513}
15091514
@@ -1549,7 +1554,9 @@ function recordSyncState(outcome: LoadOutcome): void {
15491554 if ( retire ) return SyncState . forget ( dir , "memory" , { isCurrent, before : outcome . observedAt } )
15501555 if ( outcome . datamateId === undefined ) return
15511556 const result =
1552- outcome . status === "loaded" ? { items : outcome . items } : { error : "could not load workspace memory" }
1557+ outcome . status === "loaded"
1558+ ? { items : outcome . items }
1559+ : { error : `could not load workspace memory: ${ "error" in outcome ? outcome . error : outcome . status } ` }
15531560 return SyncState . record ( dir , outcome . datamateId , "memory" , result , {
15541561 isCurrent,
15551562 scope : outcome . scope ,
0 commit comments