/** * Sync puller (FEAT-407, ADR-011) — cloud/remote-device -> local merge. * The mirror image of shipper.ts: shipper is local -> cloud (push, ADR-003, * one-way); this module is cloud -> local (pull, additive, no push-side * changes — ADR-011 point (e): "synthetic conflict events ride the existing * shipper", no new push-side code). * * Merge shape (ADR-011 "Implications for FEAT-407"): * 1. dedup-insert pulled events by event_id (AC-2 idempotent replay), * 2. scoped projection-recompute (conflict-resolve.ts) for exactly the * atom_ids that received a NEW event this round, * 3. persist the watermark/cursor in the SAME transaction as (1)+(2), so * a crash mid-pull cannot advance the watermark past events that were * not actually durably merged (AC-4: resumable, no gap, no re-pull * from zero). * * WIRE CONTRACT CAVEAT (be conservative — no live SaaS repo to consult): * astramem-sync@1 (ADR-003) ships `memory_events` metadata ONLY — no atom * body (text/type/hash/scope). That is sufficient for push (the cloud * ledger materializes state from events it already trusts) but a `create` * event pulled onto a device that has never seen this atom_id has nothing * to materialize a `memories` row FROM. Real cross-service pull will need a * cloud-side answer to "where does the atom body come from" that this repo * does not currently define (ADR-003 explicitly scoped pull out: "NOT in * v1: bidirectional sync, cross-device pull"). Rather than block on that, * this module defines `atom_snapshot` as an OPTIONAL, additive field on the * pulled event carrying just enough of the atom body to materialize a new * row locally. This is a LOCAL, FEAT-407-side design choice, not a verified * live cloud contract — flagged explicitly in the FEAT-407 completion * report. `PullFetcher` is injectable specifically so tests can simulate * this without a real cloud. */ import type { DB } from '../storage/db.js'; import type { MemoryScope, MemoryType } from '../contracts/memory.js'; import { type WireSyncEvent } from './shipper.js'; /** Minimal atom body needed to materialize a brand-new `memories` row on pull. See file header caveat. */ export interface AtomSnapshot { type: MemoryType; text: string; normalized_text: string; hash: string; scope: MemoryScope; repo?: string | null; project?: string | null; branch?: string | null; agent?: string | null; session_id?: string | null; importance?: number; confidence?: number; source_hash?: string | null; evidence?: string | null; } export interface RemotePullEvent extends WireSyncEvent { /** Required on pull (unlike push, where it's additive-optional) — it is the dedup key merge correctness depends on. */ event_id: string; /** Present only for 'create' events the sender knows the puller may not have seen yet. See file header caveat. */ atom_snapshot?: AtomSnapshot | null; } export interface PullBatch { events: RemotePullEvent[]; /** Opaque resume token. null means "no further pages" (nothing beyond this batch). */ next_cursor: string | null; } /** Injectable — the real implementation talks to the cloud; tests supply an in-memory fake event source. */ export type PullFetcher = (cursor: string | null, limit: number) => Promise; export interface PullOpts { db: DB; fetcher: PullFetcher; batchSize?: number; } export interface PullOnceResult { /** Events returned by the fetcher this round (before dedup). */ fetched: number; /** Events actually newly inserted (post dedup-by-event_id). */ inserted: number; /** Distinct atom_ids whose projection was recomputed this round. */ atomsRecomputed: number; /** Cursor persisted at the end of this round. */ cursor: string | null; /** true when the fetcher returned nothing to pull. */ idle: boolean; } /** Current persisted pull watermark, or null if pull has never run. */ export declare function getPullWatermark(db: DB): string | null; /** * One pull round: fetch a batch from `cursor`, dedup-insert by event_id, * materialize any brand-new atoms, scoped-recompute touched atoms' * projections, and persist the new watermark — all durably: the fetch * itself is outside any transaction (network I/O can't hold a DB lock * across a hung socket), but the merge + watermark advance is one atomic * transaction (AC-4: a crash between fetch and merge simply re-fetches the * same batch next round, which is safe because merge is idempotent). */ export declare function pullOnce(opts: PullOpts): Promise; export interface DrainPullResult { rounds: number; totalFetched: number; totalInserted: number; cursor: string | null; } /** * Drain every available page: calls pullOnce repeatedly until the fetcher * reports idle (or a `next_cursor` of null after a non-empty batch). Bounds * the number of rounds so a misbehaving fetcher can't loop forever. */ export declare function drainPull(opts: PullOpts, maxRounds?: number): Promise; /** * Real HTTP pull fetcher: GET /sync/pull?cursor=&limit=. NOT exercised * against a live cloud in this slice's tests (no SaaS repo available * locally) — see the completion report's "requires-live-cloud" section. * Response shape assumed to mirror SyncEnvelope's event shape plus * `next_cursor`; this is this daemon's expectation of the pull contract, * not a verified cloud implementation. */ export declare function httpPullFetcher(url: string, token: string, fetchImpl?: typeof fetch): PullFetcher;