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"
@@ -379,11 +380,7 @@ function stripTrainingMeta(content: string): string {
379380/** Fingerprint of everything about a block that reaches the store. Tags and
380381 * expiry are included because both are mirrored. */
381382function contentHash ( block : MemoryBlock ) : string {
382- const payload = JSON . stringify ( [
383- stripTrainingMeta ( block . content ) ,
384- [ ...block . tags ] . sort ( ) ,
385- block . expires ?? "" ,
386- ] )
383+ const payload = JSON . stringify ( [ stripTrainingMeta ( block . content ) , [ ...block . tags ] . sort ( ) , block . expires ?? "" ] )
387384 // sha-256, not a 32-bit non-cryptographic hash. This value is the single
388385 // gate deciding whether a save is sent at all: on a collision ``push``
389386 // returns "unchanged" and a real edit is silently never mirrored, with no
@@ -702,7 +699,9 @@ async function reapArchivedBlocks(
702699 try {
703700 const gone = syncInternals . removeBlock
704701 ? await syncInternals . removeBlock ( scope , blockId , directory ?? undefined , block . updated )
705- : await ( await import ( "@/memory/store" ) ) . MemoryStore . remove ( scope , blockId , directory ?? undefined , {
702+ : await (
703+ await import ( "@/memory/store" )
704+ ) . MemoryStore . remove ( scope , blockId , directory ?? undefined , {
706705 expectUpdated : block . updated ,
707706 } )
708707 if ( gone ) {
@@ -770,9 +769,7 @@ async function push(
770769 // user recreated under an old id gets a fresh record instead of un-archiving
771770 // the old one (`MemoryApi.update` replaces metadata wholesale, which would
772771 // drop the archived marker).
773- const indexed = existing ?. memoryId
774- ? view . records . find ( ( r ) => r . id === existing . memoryId )
775- : undefined
772+ const indexed = existing ?. memoryId ? view . records . find ( ( r ) => r . id === existing . memoryId ) : undefined
776773 // A record the index names but a COMPLETE record set does not hold is gone —
777774 // deleted in the web app, by another client, or by a wipe. Absence used to
778775 // read as "still ours", so `match` stayed pointing at a dead id and every
@@ -816,6 +813,7 @@ async function push(
816813 const others = copies . filter ( ( r ) => r . id !== match ) . map ( ( r ) => r . id )
817814 await archiveDuplicates ( others , block , metadata , "archiving a second live record of one block" )
818815 await recordIndexEntry ( key , { memoryId : match , contentHash : hash , syncedAt : Date . now ( ) } )
816+ if ( binding ) trackContributed ( binding )
819817 return "stored"
820818 }
821819
@@ -848,6 +846,7 @@ async function push(
848846 // leaves the block unindexed, and the next save finds the extras by identity.
849847 await archiveDuplicates ( extras , block , metadata , "archiving an extra record produced by one create" )
850848 await recordIndexEntry ( key , { memoryId : primary , contentHash : hash , syncedAt : Date . now ( ) } )
849+ if ( binding ) trackContributed ( binding )
851850 return "stored"
852851}
853852
@@ -866,6 +865,22 @@ async function archiveDuplicates(
866865 }
867866}
868867
868+ // altimate_change start — a successful write-back is the workspace getting something back
869+ function trackContributed ( binding : CachedBinding ) {
870+ try {
871+ Telemetry . track ( {
872+ type : "workspace_memory_contributed" ,
873+ timestamp : Date . now ( ) ,
874+ session_id : Telemetry . getContext ( ) . sessionId ,
875+ workspace_id : binding . datamateId ,
876+ block_count : 1 ,
877+ } )
878+ } catch {
879+ // telemetry never blocks a mirror
880+ }
881+ }
882+ // altimate_change end
883+
869884/** Cloud operations for one logical block, run one at a time.
870885 *
871886 * Both callers are fire-and-forget from the store's write and delete paths, so
@@ -992,11 +1007,7 @@ export async function mirrorBlock(block: MemoryBlock, directory?: string): Promi
9921007/** Archive a block's cloud record rather than deleting it, so the workspace
9931008 * keeps the history. Only this client filters the marker — other readers do
9941009 * not — so an archived record stays visible elsewhere. */
995- export async function archiveBlock (
996- scope : "global" | "project" ,
997- blockId : string ,
998- directory ?: string ,
999- ) : Promise < void > {
1010+ export async function archiveBlock ( scope : "global" | "project" , blockId : string , directory ?: string ) : Promise < void > {
10001011 if ( ! isEnabled ( ) ) return
10011012 // Queued behind any in-flight mirror for the same block, so a delete cannot
10021013 // run before the create it is meant to undo. The binding lookup happens
@@ -1015,11 +1026,7 @@ export async function archiveBlock(
10151026 )
10161027}
10171028
1018- async function archiveNow (
1019- scope : "global" | "project" ,
1020- blockId : string ,
1021- binding : CachedBinding ,
1022- ) : Promise < void > {
1029+ async function archiveNow ( scope : "global" | "project" , blockId : string , binding : CachedBinding ) : Promise < void > {
10231030 const key = indexKey ( {
10241031 scope,
10251032 blockId,
@@ -1193,9 +1200,7 @@ async function pollMemoKey(binding: CachedBinding): Promise<string> {
11931200 *
11941201 * An "unknown" is never memoized: the next tick should ask again rather than
11951202 * inherit a network blip. */
1196- export async function memoryEnabledForPoller (
1197- binding : CachedBinding ,
1198- ) : Promise < "enabled" | "disabled" | "unknown" > {
1203+ export async function memoryEnabledForPoller ( binding : CachedBinding ) : Promise < "enabled" | "disabled" | "unknown" > {
11991204 // The scoped memo is the poller's only cache. `memoryEnabledCache` is keyed by
12001205 // bare workspace id — right for the write path, which is tenant-bound by its
12011206 // credentials — but consulting it here reopened a 60s cross-tenant window the
@@ -1204,8 +1209,7 @@ export async function memoryEnabledForPoller(
12041209 // minutes per tenant is the price, and `memoryStatus` still warms both.
12051210 const key = await pollMemoKey ( binding )
12061211 const memo = pollMemo . get ( key )
1207- if ( memo && Date . now ( ) - memo . at < ( memo . status === "unknown" ? POLL_UNKNOWN_TTL_MS : POLL_TTL_MS ) )
1208- return memo . status
1212+ if ( memo && Date . now ( ) - memo . at < ( memo . status === "unknown" ? POLL_UNKNOWN_TTL_MS : POLL_TTL_MS ) ) return memo . status
12091213 // The in-flight ask is memoized too, not only the settled answer. Two
12101214 // refreshes overlapping on a cold memo — a remount while a slow one is
12111215 // still out — both saw it empty and both put a request on the wire.
@@ -1340,7 +1344,8 @@ export async function backfill(
13401344 // ``gated`` says the sweep never ran, as opposed to running and storing
13411345 // nothing. A caller recording "this binding is seeded" must be able to tell
13421346 // those apart: memory being off is not a completed seed.
1343- if ( ! isEnabled ( ) ) return { ok : 0 , failed : 0 , skipped : 0 , declined : 0 , deferred : 0 , gated : true , gateReason : "local-off" }
1347+ if ( ! isEnabled ( ) )
1348+ return { ok : 0 , failed : 0 , skipped : 0 , declined : 0 , deferred : 0 , gated : true , gateReason : "local-off" }
13441349 // The bind path passes the binding it just recorded; there is no ambient
13451350 // instance to resolve one from on the `link` subcommand.
13461351 const binding = explicitBinding ?? ( await currentBinding ( ) )
@@ -1370,10 +1375,7 @@ export async function backfill(
13701375 log . info ( "workspace memory backfill starting" , { pending : pending . length , skipped } )
13711376 const result = await runQueue (
13721377 pending ,
1373- ( item ) =>
1374- serialize ( item . block . scope , item . block . id , ( ) =>
1375- push ( item . block , item . binding , known , sweepDirectory ) ,
1376- ) ,
1378+ ( item ) => serialize ( item . block . scope , item . block . id , ( ) => push ( item . block , item . binding , known , sweepDirectory ) ) ,
13771379 BACKFILL_CONCURRENCY ,
13781380 )
13791381 // `skipped` combines blocks filtered before the queue (already synced, or
@@ -1388,7 +1390,10 @@ export async function backfill(
13881390function originLabel ( meta : Record < string , unknown > ) : string | undefined {
13891391 const remote = typeof meta . repo_remote === "string" ? meta . repo_remote : undefined
13901392 if ( remote ) {
1391- const trimmed = remote . replace ( / [ / ] + $ / , "" ) . replace ( / \. g i t $ / , "" ) . replace ( / [ / ] + $ / , "" )
1393+ const trimmed = remote
1394+ . replace ( / [ / ] + $ / , "" )
1395+ . replace ( / \. g i t $ / , "" )
1396+ . replace ( / [ / ] + $ / , "" )
13921397 const last = trimmed . split ( / [ / : ] / ) . pop ( )
13931398 if ( last ) return last
13941399 }
@@ -1484,10 +1489,7 @@ export async function hydrate(sessionID: string): Promise<void> {
14841489
14851490/** Wait for a session's in-flight hydration, capped. Resolves immediately once
14861491 * the fetch has settled, so only the first injection of a session pays. */
1487- export async function whenHydrated (
1488- sessionID : string ,
1489- timeoutMs : number = HYDRATION_WAIT_MS ,
1490- ) : Promise < void > {
1492+ export async function whenHydrated ( sessionID : string , timeoutMs : number = HYDRATION_WAIT_MS ) : Promise < void > {
14911493 const state = sessions . get ( sessionID )
14921494 const pending = state ?. hydration
14931495 if ( ! pending || ! state ) return
0 commit comments