Skip to content

Save and restore results ​

encodeRun validates a result and returns a bundle whose json field contains a segmented gzip JSON envelope and checksum. Persist that JSON value or serialize it as a string. decodeRun accepts the envelope, bundle, JSON string, or canonical result; segmented payloads are checksum-verified.

Use RunStore to connect application-owned storage:

ts
import { loadRun, saveRun, type RunResult, type RunStore } from '@grabm/abm'

// Demonstration only: replace this volatile map with host-owned storage.
export function createMemoryStore(): RunStore {
  const records = new Map<string, unknown>()
  return {
    async save(bundle) {
      const id = bundle.checksum
      records.set(id, structuredClone(bundle.json))
      return { id, checksum: bundle.checksum }
    },
    async load(id) {
      if (!records.has(id)) throw new Error(`Run not found: ${id}`)
      return structuredClone(records.get(id))
    },
  }
}
export async function saveAndRestore(result: RunResult) {
  const store = createMemoryStore()
  const reference = await saveRun(store, result)
  return loadRun(store, reference.id)
}

The example store is volatile and uses the checksum as its ID. A production host chooses its IDs, durability, authentication, limits, and retention. saveRun encodes before saving; loadRun decodes and validates the store's response.

Encoding uses bounded batches, but the result and compressed output still consume memory. Plan storage and resource limits for your population size. The library does not install a database or promise constant memory usage.

Saved data must match the current schema. There is no migration or compatibility layer: regenerate incompatible results from the original source. Every run includes needOutcomes (an empty array when no needs were authored), and the codec requires all result arrays rather than guessing missing data.

For backend execution and partial replay, use stored datasets. Archive saving keeps the full run in memory; dataset replay loads independent windows and inspection pages. Choose storage separately from execution location.

Compressed archive decoding shares repeated short strings through a bounded, per-decode dictionary (at most 65,536 entries, with strings longer than 256 UTF-16 code units excluded). This preserves primitive values and does not share mutable arrays or records. It does not change the archive format, CRC checks or canonical checksum. The decoded graph and result arrays still require memory proportional to their size; a bounded string dictionary is not a regional memory guarantee.

Documentation for the grABM library.