1616// Nothing is written to disk: local files are the source of truth, and a cloud
1717// record can have been edited elsewhere by a client that does not preserve this
1818// CLI's metadata.
19+ import { Telemetry } from "@/altimate/telemetry"
1920import { createHash } from "node:crypto"
2021import { Flag as CoreFlag } from "@opencode-ai/core/flag/flag"
2122import { Flag } from "@/flag/flag"
@@ -332,11 +333,7 @@ function stripTrainingMeta(content: string): string {
332333/** Fingerprint of everything about a block that reaches the store. Tags and
333334 * expiry are included because both are mirrored. */
334335function contentHash ( block : MemoryBlock ) : string {
335- const payload = JSON . stringify ( [
336- stripTrainingMeta ( block . content ) ,
337- [ ...block . tags ] . sort ( ) ,
338- block . expires ?? "" ,
339- ] )
336+ const payload = JSON . stringify ( [ stripTrainingMeta ( block . content ) , [ ...block . tags ] . sort ( ) , block . expires ?? "" ] )
340337 // sha-256, not a 32-bit non-cryptographic hash. This value is the single
341338 // gate deciding whether a save is sent at all: on a collision ``push``
342339 // returns "unchanged" and a real edit is silently never mirrored, with no
@@ -656,7 +653,9 @@ async function reapArchivedBlocks(
656653 try {
657654 const gone = syncInternals . removeBlock
658655 ? await syncInternals . removeBlock ( scope , blockId , directory ?? undefined , block . updated )
659- : await ( await import ( "@/memory/store" ) ) . MemoryStore . remove ( scope , blockId , directory ?? undefined , {
656+ : await (
657+ await import ( "@/memory/store" )
658+ ) . MemoryStore . remove ( scope , blockId , directory ?? undefined , {
660659 expectUpdated : block . updated ,
661660 } )
662661 if ( gone ) {
@@ -724,9 +723,7 @@ async function push(
724723 // user recreated under an old id gets a fresh record instead of un-archiving
725724 // the old one (`MemoryApi.update` replaces metadata wholesale, which would
726725 // drop the archived marker).
727- const indexed = existing ?. memoryId
728- ? view . records . find ( ( r ) => r . id === existing . memoryId )
729- : undefined
726+ const indexed = existing ?. memoryId ? view . records . find ( ( r ) => r . id === existing . memoryId ) : undefined
730727 // A record the index names but a COMPLETE record set does not hold is gone —
731728 // deleted in the web app, by another client, or by a wipe. Absence used to
732729 // read as "still ours", so `match` stayed pointing at a dead id and every
@@ -740,7 +737,8 @@ async function push(
740737 // that already has one — the duplicate this function works hardest to avoid.
741738 const indexedGone = existing ?. memoryId !== undefined && indexed === undefined && ! view . truncated
742739 const indexUsable = existing ?. memoryId && ! indexedGone && ( ! indexed || ! isArchived ( indexed ) )
743- const match = ( indexUsable ? existing ?. memoryId : undefined ) ?? view . records . find ( ( r ) => isSameBlock ( r , block , binding ) ) ?. id
740+ const match =
741+ ( indexUsable ? existing ?. memoryId : undefined ) ?? view . records . find ( ( r ) => isSameBlock ( r , block , binding ) ) ?. id
744742 if ( match ) {
745743 // Refuse to move a record backwards. Two machines editing the same block,
746744 // or a stale clone running a sweep, would otherwise overwrite a newer cloud
@@ -759,6 +757,7 @@ async function push(
759757 }
760758 await MemoryApi . update ( match , block . content , metadata )
761759 await recordIndexEntry ( key , { memoryId : match , contentHash : hash , syncedAt : Date . now ( ) } )
760+ if ( binding ) trackContributed ( binding )
762761 return "stored"
763762 }
764763
@@ -793,9 +792,26 @@ async function push(
793792 log . warn ( "archiving an extra record produced by one create" , { id : block . id , memoryId : extra } )
794793 await MemoryApi . update ( extra , "" , { ...metadata , archived : "true" , archived_at : new Date ( ) . toISOString ( ) } )
795794 }
795+ if ( binding ) trackContributed ( binding )
796796 return "stored"
797797}
798798
799+ // altimate_change start — a successful write-back is the workspace getting something back
800+ function trackContributed ( binding : CachedBinding ) {
801+ try {
802+ Telemetry . track ( {
803+ type : "workspace_memory_contributed" ,
804+ timestamp : Date . now ( ) ,
805+ session_id : Telemetry . getContext ( ) . sessionId ,
806+ workspace_id : binding . datamateId ,
807+ block_count : 1 ,
808+ } )
809+ } catch {
810+ // telemetry never blocks a mirror
811+ }
812+ }
813+ // altimate_change end
814+
799815/** Cloud operations for one logical block, run one at a time.
800816 *
801817 * Both callers are fire-and-forget from the store's write and delete paths, so
@@ -892,11 +908,7 @@ export async function mirrorBlock(block: MemoryBlock, directory?: string): Promi
892908/** Archive a block's cloud record rather than deleting it, so the workspace
893909 * keeps the history. Only this client filters the marker — other readers do
894910 * not — so an archived record stays visible elsewhere. */
895- export async function archiveBlock (
896- scope : "global" | "project" ,
897- blockId : string ,
898- directory ?: string ,
899- ) : Promise < void > {
911+ export async function archiveBlock ( scope : "global" | "project" , blockId : string , directory ?: string ) : Promise < void > {
900912 if ( ! isEnabled ( ) ) return
901913 // Queued behind any in-flight mirror for the same block, so a delete cannot
902914 // run before the create it is meant to undo. The binding lookup happens
@@ -915,11 +927,7 @@ export async function archiveBlock(
915927 )
916928}
917929
918- async function archiveNow (
919- scope : "global" | "project" ,
920- blockId : string ,
921- binding : CachedBinding ,
922- ) : Promise < void > {
930+ async function archiveNow ( scope : "global" | "project" , blockId : string , binding : CachedBinding ) : Promise < void > {
923931 const key = indexKey ( {
924932 scope,
925933 blockId,
@@ -1082,9 +1090,7 @@ async function pollMemoKey(binding: CachedBinding): Promise<string> {
10821090 *
10831091 * An "unknown" is never memoized: the next tick should ask again rather than
10841092 * inherit a network blip. */
1085- export async function memoryEnabledForPoller (
1086- binding : CachedBinding ,
1087- ) : Promise < "enabled" | "disabled" | "unknown" > {
1093+ export async function memoryEnabledForPoller ( binding : CachedBinding ) : Promise < "enabled" | "disabled" | "unknown" > {
10881094 // The scoped memo is the poller's only cache. `memoryEnabledCache` is keyed by
10891095 // bare workspace id — right for the write path, which is tenant-bound by its
10901096 // credentials — but consulting it here reopened a 60s cross-tenant window the
@@ -1093,8 +1099,7 @@ export async function memoryEnabledForPoller(
10931099 // minutes per tenant is the price, and `memoryStatus` still warms both.
10941100 const key = await pollMemoKey ( binding )
10951101 const memo = pollMemo . get ( key )
1096- if ( memo && Date . now ( ) - memo . at < ( memo . status === "unknown" ? POLL_UNKNOWN_TTL_MS : POLL_TTL_MS ) )
1097- return memo . status
1102+ if ( memo && Date . now ( ) - memo . at < ( memo . status === "unknown" ? POLL_UNKNOWN_TTL_MS : POLL_TTL_MS ) ) return memo . status
10981103 // The in-flight ask is memoized too, not only the settled answer. Two
10991104 // refreshes overlapping on a cold memo — a remount while a slow one is
11001105 // still out — both saw it empty and both put a request on the wire.
@@ -1229,7 +1234,8 @@ export async function backfill(
12291234 // ``gated`` says the sweep never ran, as opposed to running and storing
12301235 // nothing. A caller recording "this binding is seeded" must be able to tell
12311236 // those apart: memory being off is not a completed seed.
1232- if ( ! isEnabled ( ) ) return { ok : 0 , failed : 0 , skipped : 0 , declined : 0 , deferred : 0 , gated : true , gateReason : "local-off" }
1237+ if ( ! isEnabled ( ) )
1238+ return { ok : 0 , failed : 0 , skipped : 0 , declined : 0 , deferred : 0 , gated : true , gateReason : "local-off" }
12331239 // The bind path passes the binding it just recorded; there is no ambient
12341240 // instance to resolve one from on the `link` subcommand.
12351241 const binding = explicitBinding ?? ( await currentBinding ( ) )
@@ -1259,10 +1265,7 @@ export async function backfill(
12591265 log . info ( "workspace memory backfill starting" , { pending : pending . length , skipped } )
12601266 const result = await runQueue (
12611267 pending ,
1262- ( item ) =>
1263- serialize ( item . block . scope , item . block . id , ( ) =>
1264- push ( item . block , item . binding , known , sweepDirectory ) ,
1265- ) ,
1268+ ( item ) => serialize ( item . block . scope , item . block . id , ( ) => push ( item . block , item . binding , known , sweepDirectory ) ) ,
12661269 BACKFILL_CONCURRENCY ,
12671270 )
12681271 // `skipped` combines blocks filtered before the queue (already synced, or
@@ -1277,7 +1280,10 @@ export async function backfill(
12771280function originLabel ( meta : Record < string , unknown > ) : string | undefined {
12781281 const remote = typeof meta . repo_remote === "string" ? meta . repo_remote : undefined
12791282 if ( remote ) {
1280- const trimmed = remote . replace ( / [ / ] + $ / , "" ) . replace ( / \. g i t $ / , "" ) . replace ( / [ / ] + $ / , "" )
1283+ const trimmed = remote
1284+ . replace ( / [ / ] + $ / , "" )
1285+ . replace ( / \. g i t $ / , "" )
1286+ . replace ( / [ / ] + $ / , "" )
12811287 const last = trimmed . split ( / [ / : ] / ) . pop ( )
12821288 if ( last ) return last
12831289 }
@@ -1291,10 +1297,7 @@ function originLabel(meta: Record<string, unknown>): string | undefined {
12911297
12921298/** Map a cloud record back to an injectable block, or null if it is not a
12931299 * well-formed mirrored block. */
1294- export function toBlock (
1295- record : CloudMemoryRecord ,
1296- ownProjectKey : string | undefined ,
1297- ) : RemoteMemoryBlock | null {
1300+ export function toBlock ( record : CloudMemoryRecord , ownProjectKey : string | undefined ) : RemoteMemoryBlock | null {
12981301 const meta = record . metadata
12991302 if ( ! meta || typeof meta !== "object" ) return null
13001303 const blockId = typeof meta . block_id === "string" ? meta . block_id : undefined
@@ -1368,10 +1371,7 @@ export async function hydrate(sessionID: string): Promise<void> {
13681371
13691372/** Wait for a session's in-flight hydration, capped. Resolves immediately once
13701373 * the fetch has settled, so only the first injection of a session pays. */
1371- export async function whenHydrated (
1372- sessionID : string ,
1373- timeoutMs : number = HYDRATION_WAIT_MS ,
1374- ) : Promise < void > {
1374+ export async function whenHydrated ( sessionID : string , timeoutMs : number = HYDRATION_WAIT_MS ) : Promise < void > {
13751375 const state = sessions . get ( sessionID )
13761376 const pending = state ?. hydration
13771377 if ( ! pending || ! state ) return
0 commit comments