MCPcopy Create free account
hub / github.com/Noumena-Network/code / restoreAttributionStateFromSnapshots

Function restoreAttributionStateFromSnapshots

src/utils/commitAttribution.ts:840–870  ·  view source on GitHub ↗
(
  snapshots: AttributionSnapshotMessage[],
)

Source from the content-addressed store, hash-verified

838 * Restore attribution state from snapshot messages.
839 */
840export function restoreAttributionStateFromSnapshots(
841 snapshots: AttributionSnapshotMessage[],
842): AttributionState {
843 const state = createEmptyAttributionState()
844
845 // Snapshots are full-state dumps (see stateToSnapshotMessage), not deltas.
846 // The last snapshot has the most recent count for every path — fileStates
847 // never shrinks. Iterating and SUMMING counts across snapshots causes
848 // quadratic growth on restore (837 snapshots × 280 files → 1.15 quadrillion
849 // "chars" tracked for a 5KB file over a 5-day session).
850 const lastSnapshot = snapshots[snapshots.length - 1]
851 if (!lastSnapshot) {
852 return state
853 }
854
855 state.surface = lastSnapshot.surface
856 for (const [path, fileState] of Object.entries(lastSnapshot.fileStates)) {
857 state.fileStates.set(path, fileState)
858 }
859
860 // Restore prompt counts from the last snapshot (most recent state)
861 state.promptCount = lastSnapshot.promptCount ?? 0
862 state.promptCountAtLastCommit = lastSnapshot.promptCountAtLastCommit ?? 0
863 state.permissionPromptCount = lastSnapshot.permissionPromptCount ?? 0
864 state.permissionPromptCountAtLastCommit =
865 lastSnapshot.permissionPromptCountAtLastCommit ?? 0
866 state.escapeCount = lastSnapshot.escapeCount ?? 0
867 state.escapeCountAtLastCommit = lastSnapshot.escapeCountAtLastCommit ?? 0
868
869 return state
870}
871
872/**
873 * Restore attribution state from log snapshots on session resume.

Callers 2

Calls 3

entriesMethod · 0.80
setMethod · 0.80

Tested by

no test coverage detected