@@ -35,7 +35,8 @@ import {
3535 type CachedBinding ,
3636} from "./state"
3737import { indexKey , readIndex , readIndexEntry , recordIndexEntry } from "./memory-index"
38- import { WorkspaceApi } from "./api-client"
38+ import { WorkspaceApi , WorkspaceApiError } from "./api-client"
39+ import { redact } from "@/altimate/debug/report" // altimate_change — failure text in the sync record
3940import { AltimateApi } from "@/altimate/api/client"
4041import {
4142 LIST_LIMIT ,
@@ -264,6 +265,16 @@ async function memoryEnabled(binding: CachedBinding): Promise<boolean> {
264265/** A failed read says what failed, so the sync record can classify it. */
265266type MemoryStatusOutcome = "enabled" | "disabled" | { status : "error" ; error : string }
266267
268+ // altimate_change start — what failed, for the persisted sync record: the HTTP status when the API
269+ // answered one (so the reason class sees it whatever the detail says), redacted of secrets, URLs
270+ // and the user's identity before it is written.
271+ function failureText ( err : unknown ) : string {
272+ const status = err instanceof WorkspaceApiError ? err . status : undefined
273+ const base = err instanceof Error ? `${ err . name } : ${ err . message } ` : String ( err )
274+ return redact ( status !== undefined ? `HTTP ${ status } ${ base } ` : base )
275+ }
276+ // altimate_change end
277+
267278/** Three-way, because a read that cannot reach the service must not be reported
268279 * as "this workspace has no memory" — that reads as success while destroying
269280 * whatever the session already had. */
@@ -319,7 +330,7 @@ async function memoryStatus(
319330 log . warn ( "could not confirm workspace memory setting" , { err : String ( err ) } )
320331 // Not cached either way: a transient failure should neither disable the
321332 // mirror for a minute nor keep it enabled.
322- return { status : "error" , error : String ( err ) }
333+ return { status : "error" , error : failureText ( err ) }
323334 }
324335}
325336
@@ -677,6 +688,8 @@ async function push(
677688 binding : CachedBinding | null ,
678689 known ?: KnownRecords ,
679690 directory ?: string ,
691+ /** The session that wrote the block, captured before the queue; the context's when absent. */
692+ sessionID ?: string ,
680693) : Promise < PushOutcome > {
681694 const key = indexKey ( {
682695 scope : block . scope ,
@@ -760,7 +773,7 @@ async function push(
760773 }
761774 await MemoryApi . update ( match , block . content , metadata )
762775 await recordIndexEntry ( key , { memoryId : match , contentHash : hash , syncedAt : Date . now ( ) } )
763- if ( binding ) trackContributed ( binding )
776+ if ( binding ) trackContributed ( binding , sessionID )
764777 return "stored"
765778 }
766779
@@ -795,17 +808,17 @@ async function push(
795808 log . warn ( "archiving an extra record produced by one create" , { id : block . id , memoryId : extra } )
796809 await MemoryApi . update ( extra , "" , { ...metadata , archived : "true" , archived_at : new Date ( ) . toISOString ( ) } )
797810 }
798- if ( binding ) trackContributed ( binding )
811+ if ( binding ) trackContributed ( binding , sessionID )
799812 return "stored"
800813}
801814
802815// altimate_change start — a successful write-back is the workspace getting something back
803- function trackContributed ( binding : CachedBinding ) {
816+ function trackContributed ( binding : CachedBinding , sessionID ?: string ) {
804817 try {
805818 Telemetry . track ( {
806819 type : "workspace_memory_contributed" ,
807820 timestamp : Date . now ( ) ,
808- session_id : Telemetry . getContext ( ) . sessionId ,
821+ session_id : sessionID ?? Telemetry . getContext ( ) . sessionId ,
809822 workspace_id : binding . datamateId ,
810823 block_count : 1 ,
811824 } )
@@ -890,6 +903,9 @@ export async function flushPendingMirrors(timeoutMs = 30_000): Promise<void> {
890903 * disabled. */
891904export async function mirrorBlock ( block : MemoryBlock , directory ?: string ) : Promise < void > {
892905 if ( ! isEnabled ( ) ) return
906+ // altimate_change — the writer's session, read before the queue: the context can be another
907+ // session's by the time the mirror lands.
908+ const sessionID = Telemetry . getContext ( ) . sessionId
893909 // Queued BEFORE the binding lookup, not after. Both are async, so resolving
894910 // them first let two operations on one block reach `serialize` in the
895911 // opposite order to the writes that triggered them.
@@ -903,7 +919,7 @@ export async function mirrorBlock(block: MemoryBlock, directory?: string): Promi
903919 const binding = await currentBinding ( directory )
904920 if ( ! binding ) return
905921 if ( ! ( await memoryEnabled ( binding ) ) ) return
906- await push ( block , binding , undefined , directory )
922+ await push ( block , binding , undefined , directory , sessionID )
907923 } )
908924 return tracked ( task )
909925}
@@ -1508,7 +1524,7 @@ async function loadWorkspaceMemory(directory?: string): Promise<LoadOutcome> {
15081524 log . warn ( "workspace memory load failed" , { err : String ( err ) } )
15091525 // Stamped like any other outcome: without an epoch the session would reload (and make
15101526 // the prompt wait) on every turn for as long as the service is down.
1511- return { status : "error" , error : String ( err ) , epoch : vouched , dir, datamateId : resolved , ...stamp }
1527+ return { status : "error" , error : failureText ( err ) , epoch : vouched , dir, datamateId : resolved , ...stamp }
15121528 }
15131529}
15141530
0 commit comments