/** * Bulk-promote + payload backfill so existing local memories can sync to the * cloud (`astramem-local sync backfill`). * * Two obstacles this closes: * 1. Scope filter (ADR-009): only team/org atoms ship. Private never does. * `applyBackfill` promotes private → the chosen tier so they become * shippable. IRREVERSIBLE — scope is upward-only. * 2. Empty replicas: `create` events written before the payload fix * (astramem-local#86) carry `payload_json = NULL`, so they materialize * with empty content in the cloud. We backfill content onto every * null-payload, not-yet-shipped create event. * * Already-synced (synced_at set) events are left alone by `applyBackfill` — * the cloud dedups by atom_id, so re-shipping them was historically a no-op. * * astramem-local#97 / memory#629 close the remaining gap: create events that * shipped BEFORE #86 landed already carry `synced_at` but an empty cloud * replica (their payload was null at ship time). The cloud now upserts empty * content onto a re-shipped create (memory#629, merged), so * `planReship`/`applyReship` below backfill the payload onto those * already-synced rows AND reset `synced_at = NULL` so the shipper re-sends * them — the cloud upsert then fills in the previously-empty replica. * Scope-safe (team/org atoms only, matching ADR-009) and idempotent (a row * that already carries a payload is never touched, so re-running reships * nothing new). */ import type { DB } from '../storage/db.js'; import type { MemoryScope } from '../contracts/index.js'; export interface BackfillOptions { /** Promotion target for private atoms. Upward-only. */ scope: Extract; /** Restrict to a single local project (memories.project); null = all. */ project?: string | null; } export interface BackfillPlan { scope: MemoryScope; project: string | null; /** Private atoms that would be promoted (and thus start shipping). */ privateToPromote: number; /** Atoms already team/org (already shippable). */ alreadyShareable: number; /** Null-payload, unsynced create events that would be backfilled. */ createEventsToBackfill: number; } export interface BackfillResult { promoted: number; backfilled: number; } /** Read-only: count what an apply would touch. Safe to run against a live DB. */ export declare function planBackfill(db: DB, opts: BackfillOptions): BackfillPlan; /** * Apply the promotion + backfill in one transaction. IRREVERSIBLE. * 1. Promote private → target FIRST, so the payload backfill reads the * final scope and the create event materializes at the right cloud tier. * 2. Backfill content onto null-payload, unsynced create events for every * now-shareable atom. */ export declare function applyBackfill(db: DB, opts: BackfillOptions): BackfillResult; export interface ReshipOptions { /** Restrict to one shareable tier; omit to cover both team and org. */ scope?: Extract; /** Restrict to a single local project (memories.project); null = all. */ project?: string | null; } export interface ReshipPlan { scope: Extract | 'team+org'; project: string | null; /** * Already-synced, null-payload team/org create events that would be * backfilled + re-queued for shipping. Distinct from `BackfillPlan`'s * `createEventsToBackfill`, which only ever counts UNsynced rows. */ createEventsToBackfillAndReship: number; } export interface ReshipResult { reshipped: number; } /** * Read-only: count what an `applyReship` would touch. Safe to run against a * live DB. */ export declare function planReship(db: DB, opts: ReshipOptions): ReshipPlan; /** * Backfill payload onto already-synced, null-payload team/org create events * AND reset their `synced_at` to NULL so the shipper re-sends them * (astramem-local#97 / memory#629). One transaction. * * Idempotent: the WHERE clause requires `payload_json IS NULL`, so a row * this already touched (payload now non-null) is never matched again — a * second call reships nothing new. Scope-safe: only atoms currently * team/org are eligible; private atoms are never reset or reshipped here * (use `applyBackfill` to promote them first). */ export declare function applyReship(db: DB, opts: ReshipOptions): ReshipResult;