import type { V2Store } from './store-facade.js'; /** * A segment selected for extraction. Contains everything the caller needs to * fetch unextracted events and write the marker afterward. * * Unextracted events in this segment: * `seq > fromSeqExclusive AND seq >= from_seq AND seq <= toSeqInclusive` */ export interface SelectedSegment { /** Which session this binding belongs to. */ session_id: string; /** The `from_seq` of the binding row — start of this segment. */ from_seq: number; /** * `last_extracted_seq` from `segment_extractions` (0 if no marker exists). * Callers use `seq > fromSeqExclusive` to skip already-extracted events. * Note: this may be less than `from_seq` for never-extracted segments * that are not the first segment of a session. */ fromSeqExclusive: number; /** * Upper bound of this segment (inclusive): * - closed segment: `to_seq` * - open segment: `MAX(seq)` of events for this session_id */ toSeqInclusive: number; } export interface SelectOptions { /** The session_id of the currently-active session (open binding). */ currentSessionId: string; /** Only consider bindings for this project_uid. */ projectUid: string; /** * Minimum unextracted event count for the CURRENT segment to be eligible. * Backlog segments only need `unextracted > 0`. * Default: 20. */ cadence?: number; /** * Maximum number of segments to return. Current segment counts toward cap. * Default: 10. */ cap?: number; } /** * Select segments eligible for checkpoint extraction this run. * * Selection rules (deterministic, pure read): * 1. Scope to `projectUid` only. * 2. For each candidate compute `unextracted = #events with seq > last_extracted_seq` * within the segment's `[from_seq .. toSeqInclusive]` range. * 3. Current segment (`session_id === currentSessionId AND to_seq IS NULL`): * eligible iff `unextracted >= cadence`. * 4. Backlog segments (all others in the project): eligible iff `unextracted > 0`. * 5. Segments with `unextracted === 0` are skipped entirely. * 6. Order: current first (if eligible), then backlog by `sessions.updated_at DESC`, * tiebroken by `(session_id, from_seq)` ASC for determinism. * 7. Return at most `cap` segments total. */ export declare function selectSegmentsForExtraction(store: V2Store, opts: SelectOptions): SelectedSegment[];