@@ -182,6 +182,13 @@ function blockUpdatedOf(record: CloudMemoryRecord | undefined): string {
182182 return typeof value === "string" ? value : ""
183183}
184184
185+ /** The record with the newest `block_updated`, the first on a tie. */
186+ function newestRecord ( records : CloudMemoryRecord [ ] ) : CloudMemoryRecord | undefined {
187+ let newest : CloudMemoryRecord | undefined
188+ for ( const record of records ) if ( ! newest || blockUpdatedOf ( record ) > blockUpdatedOf ( newest ) ) newest = record
189+ return newest
190+ }
191+
185192/** ``directory`` is the tree that owned the operation. Fire-and-forget mirrors
186193 * and archives run after their writing context has gone, so resolving the
187194 * binding from the ambient instance can attribute one project's memory to
@@ -751,10 +758,7 @@ async function push(
751758 // that compared remotes as raw strings wrote a second record once its
752759 // binding's cached form of the repo changed.
753760 const copies = view . records . filter ( ( r ) => isSameBlock ( r , block , binding ) )
754- const newest = copies . reduce < CloudMemoryRecord | undefined > (
755- ( best , r ) => ( best && blockUpdatedOf ( best ) >= blockUpdatedOf ( r ) ? best : r ) ,
756- undefined ,
757- )
761+ const newest = newestRecord ( copies )
758762 const match = ( indexUsable ? existing ?. memoryId : undefined ) ?? newest ?. id
759763 if ( match ) {
760764 // Refuse to move a record backwards. Two machines editing the same block,
@@ -764,9 +768,7 @@ async function push(
764768 // needs a conditional update the service does not offer. Checked against
765769 // the newest copy too: the other copies are archived below, so a newer
766770 // one's content would be lost.
767- const remoteUpdated = [ view . records . find ( ( r ) => r . id === match ) , newest ]
768- . map ( blockUpdatedOf )
769- . reduce ( ( a , b ) => ( a > b ? a : b ) )
771+ const remoteUpdated = blockUpdatedOf ( newestRecord ( view . records . filter ( ( r ) => r . id === match || r === newest ) ) )
770772 if ( remoteUpdated > block . updated ) {
771773 log . warn ( "declining to overwrite a newer workspace record with older local content" , {
772774 id : block . id ,
@@ -777,15 +779,9 @@ async function push(
777779 }
778780 await MemoryApi . update ( match , block . content , metadata )
779781 await recordIndexEntry ( key , { memoryId : match , contentHash : hash , syncedAt : Date . now ( ) } )
780- // One live record per block. The other copies are archived as a split
781- // create's extras are below: emptied, under this push's metadata, so any
782- // client that reads one as this block's tombstone also reads `match` as
783- // its live record and keeps the block.
784- for ( const copy of copies ) {
785- if ( copy . id === match ) continue
786- log . warn ( "archiving a second live record of one block" , { id : block . id , memoryId : copy . id } )
787- await MemoryApi . update ( copy . id , "" , { ...metadata , archived : "true" , archived_at : new Date ( ) . toISOString ( ) } )
788- }
782+ // One live record per block.
783+ const others = copies . filter ( ( r ) => r . id !== match ) . map ( ( r ) => r . id )
784+ await archiveDuplicates ( others , block , metadata , "archiving a second live record of one block" )
789785 return "stored"
790786 }
791787
@@ -816,13 +812,25 @@ async function push(
816812 // the metadata we sent. Only one can represent the block; the rest would
817813 // otherwise be injected as duplicates under the same block id, holding text
818814 // the user never wrote.
819- for ( const extra of extras ) {
820- log . warn ( "archiving an extra record produced by one create" , { id : block . id , memoryId : extra } )
821- await MemoryApi . update ( extra , "" , { ...metadata , archived : "true" , archived_at : new Date ( ) . toISOString ( ) } )
822- }
815+ await archiveDuplicates ( extras , block , metadata , "archiving an extra record produced by one create" )
823816 return "stored"
824817}
825818
819+ /** Archive records that duplicate a block's live record: emptied, under the
820+ * metadata the live record was just given. Any client that reads one as the
821+ * block's tombstone then also reads the live record, and keeps the block. */
822+ async function archiveDuplicates (
823+ ids : string [ ] ,
824+ block : MemoryBlock ,
825+ metadata : MirrorMetadata ,
826+ reason : string ,
827+ ) : Promise < void > {
828+ for ( const id of ids ) {
829+ log . warn ( reason , { id : block . id , memoryId : id } )
830+ await MemoryApi . update ( id , "" , { ...metadata , archived : "true" , archived_at : new Date ( ) . toISOString ( ) } )
831+ }
832+ }
833+
826834/** Cloud operations for one logical block, run one at a time.
827835 *
828836 * Both callers are fire-and-forget from the store's write and delete paths, so
0 commit comments