import type { ContextDatabase } from "../../features/magic-context/storage";
import { resolveOrdinalsForModule } from "./module-wire";
import { type RawMessageOrdinalAnchor, type RawMessageParts } from "./read-session-raw";
import type { MessageLike, TagNormalizationTarget } from "./tag-messages";
export type ShadowDecisionClass = "defer" | "soft" | "hard";
export interface ShadowPassInputs {
    now_ms: number;
    model_key: string | null;
    usage: {
        input_tokens: number;
        limit: number;
    };
    effective_execute_threshold: number;
    history_budget_tokens: number;
    cache_ttl: string;
    provider_error?: string;
    /** True when the newest assistant message is still streaming. */
    mid_turn: boolean;
    /** Session-mode hint consumed by the module's session-mode machinery. */
    is_subagent?: boolean;
}
export interface ShadowTransformDecision {
    class: ShadowDecisionClass;
    marker_state: {
        marker_message_id?: string;
        advanced_this_pass: boolean;
    };
    materialize_reason?: string | null;
    emergency?: boolean;
}
export interface ShadowDeclaredTrim {
    flat_boundary_id: string;
    boundary_bare_message_id: string;
    boundary_absolute_ordinal: number;
    next_absolute_ordinal: number;
}
export interface ShadowTransformPass {
    sessionId: string;
    isSubagent: boolean;
    db: ContextDatabase;
    projectRoot: string;
    projectPath?: string;
    inputMessages: MessageLike[];
    outputMessages: MessageLike[];
    normalizationTargets: readonly TagNormalizationTarget[];
    passInputs: ShadowPassInputs;
    tsDecision: ShadowTransformDecision;
    declaredTrimBefore: ShadowDeclaredTrim | null;
}
export interface ShadowTransport {
    call(args: {
        sessionId: string;
        projectRoot: string;
        method: "shadow_reset" | "state_sync" | "shadow_transform" | "transform" | "session.status" | "session.flush" | "session.recomp" | "session.wrapup" | "todo_state.set";
        body: unknown;
        signal?: AbortSignal;
    }): Promise<unknown>;
    closeSession?(sessionId: string): void;
}
export interface ShadowSender {
    enqueue(pass: ShadowTransformPass): void;
    resetSession(sessionId: string, reason: string): void;
    clearSession(sessionId: string): void;
    getStats(sessionId: string): Readonly<ShadowSenderCounters>;
    getQueueDepth(sessionId: string): number;
}
interface ShadowSenderCounters {
    enqueued: number;
    dropped_oldest: number;
    send_failures: number;
    send_timeouts: number;
    connection_skips: number;
    ordinal_unresolved: number;
    ordinal_mismatch: number;
    state_sync_rejects: number;
    generation_rejects: number;
    resets_sent: number;
    transforms_sent: number;
    seed_budget_exceeded: number;
    parked: number;
}
interface SessionQueueState {
    queue: ShadowWorkItem[];
    running: boolean;
    initialized: boolean;
    shadowGeneration: number;
    lastAckedSeq: number;
    lastAckedWatermarks: ShadowWatermarks | null;
    idOrdinalMemoGeneration: number;
    idOrdinalMemo: Map<string, number>;
    idOrdinalMemoAnchor: RawMessageOrdinalAnchor | null;
    idOrdinalMemoStoredCount: number | null;
    idOrdinalMemoCanonicalCount: number;
    requireResetReason: string | null;
    blockedUntilReset: boolean;
    seedPassPending: boolean;
    skipped: boolean;
    reseedAttempts: number;
    lastReseedAttemptMs: number | null;
    reseedAwaitingSuccess: boolean;
    seedStartedAtMs: number | null;
    seedBudgetSpentMs: number;
    resetReasons: string[];
    sendFailureClass: string | null;
    consecutiveSendFailures: number;
    parkedReason: "send_failure" | "reset_repeat" | null;
    parked: boolean;
    counters: ShadowSenderCounters;
}
type ShadowWorkItem = {
    kind: "pass";
    pass: ShadowTransformPass;
} | {
    kind: "reset";
    sessionId: string;
    reason: string;
    projectRoot?: string;
    db?: ContextDatabase;
    projectPath?: string;
};
interface PreparedShadowPass extends ShadowTransformPass {
    annotatedInput: unknown[];
    shadowTsOutput?: unknown[];
    shadowNormalizations?: ShadowNormalizationRecord[];
    declaredTrim: ShadowDeclaredTrim | null;
}
interface ShadowWatermarks {
    compartment_sequence: number;
    memory_id: number;
    m0_mutation_id: number;
    memory_mutation_id: number;
    last_todo_state_hash: string;
}
interface ShadowWorkspacePayload {
    fingerprint: string;
    members: Array<{
        project_path: string;
        share_categories: string[];
    }>;
}
interface ShadowStateSyncPayload {
    method: "state_sync";
    params: {
        shadow_generation: number;
        expected_shadow_seq: number;
        seed_id?: string;
        seed_generation?: number;
        seed_batch_index?: number;
        seed_batch_total?: number;
        seed_complete?: boolean;
        seed_boundary_id?: string | null;
        compartments: unknown[];
        memories: unknown[];
        memory_mutations: unknown[];
        user_profile: string[];
        workspace?: ShadowWorkspacePayload | null;
        last_todo_state?: string;
        acked_watermarks?: ShadowWatermarks;
    };
    watermarks: ShadowWatermarks;
    wireBatches?: ShadowStateSyncPayload[];
}
declare function flatWireBodyBytes(payload: ShadowStateSyncPayload): number;
declare function buildPagedSeedPayloads(args: {
    shadowGeneration: number;
    expectedShadowSeq: number;
    seedId: string;
    seedBoundaryId: string | null;
    compartments: unknown[];
    memories: unknown[];
    memoryMutations: unknown[];
    userProfile: string[];
    workspace: ShadowWorkspacePayload | null;
    lastTodoState: string;
    watermarks: ShadowWatermarks;
}): ShadowStateSyncPayload[];
declare function createSessionQueueState(): SessionQueueState;
export interface ShadowNormalizationRecord {
    kind: "tag_prefix" | "ctx_search_hint" | "summary_message";
    message_id: string | null;
    part_index: number;
    field: string;
    tag_number?: number;
    removed: string;
}
export declare function denormalizeShadowOutput(args: {
    db: ContextDatabase;
    sessionId: string;
    outputMessages: MessageLike[];
    normalizationTargets: readonly TagNormalizationTarget[];
}): {
    ts_output: unknown[];
    normalizations: ShadowNormalizationRecord[];
};
export declare function resolveOrdinalsForShadow(args: {
    sessionId: string;
    messages: MessageLike[];
    generation: number;
    memoGeneration: number;
    memo: Map<string, number>;
    memoAnchor?: RawMessageOrdinalAnchor | null;
    memoStoredCount?: number | null;
    memoCanonicalCount?: number;
}): Promise<Awaited<ReturnType<typeof resolveOrdinalsForModule>>>;
export declare function flatBlockIdForRawMessage(messageId: string, raw: RawMessageParts | undefined, edge: "start" | "end"): string;
export declare function resolveDeclaredTrimForShadow(args: {
    db: ContextDatabase;
    sessionId: string;
    state?: SessionQueueState;
}): ShadowDeclaredTrim | null;
declare function buildStateSyncPayload(args: {
    state: SessionQueueState;
    pass: Pick<ShadowTransformPass, "db" | "sessionId" | "projectPath" | "passInputs"> & {
        declaredTrim?: ShadowDeclaredTrim | null;
    };
    force: boolean;
    shouldAbortSeed?: () => boolean;
    beforeSerializeCompartment?: () => void;
    yieldEveryCompartments?: number;
    seedId?: string;
}): Promise<ShadowStateSyncPayload | null | "m0_mutation" | "mismatch" | "unresolved" | "seed_budget">;
declare function toFlatWireBody(payload: {
    method: string;
    params: Record<string, unknown>;
}): unknown;
declare function buildShadowResetBody(args: {
    state: SessionQueueState;
    reason: string;
}): {
    method: "shadow_reset";
    params: Record<string, unknown>;
};
declare function buildShadowTransformBody(args: {
    pass: PreparedShadowPass;
    state: SessionQueueState;
}): {
    method: string;
    params: Record<string, unknown>;
};
declare function buildPagedTransformPayloads(body: Record<string, unknown>): Record<string, unknown>[];
export declare function createShadowSender(options?: {
    transport?: ShadowTransport;
    now?: () => number;
    reseedCooldownMs?: number;
    reseedAttemptCap?: number;
    sendTimeoutMs?: number;
    queueMaxDepth?: number;
    seedBudgetMs?: number;
    seedClock?: () => number;
    beforeSerializeCompartment?: () => void;
    seedYieldEveryCompartments?: number;
    onSeedBudgetExceeded?: (message: string) => void;
}): ShadowSender;
export declare class SubcShadowTransport implements ShadowTransport {
    private readonly connectionFile;
    private readonly moduleId;
    private readonly requestTimeoutMs;
    private readonly routeSessionPrefix;
    private client;
    private routes;
    private activeSession;
    private nextProbeMs;
    private backoffMs;
    private connectionGeneration;
    constructor(connectionFile?: string, moduleId?: string, requestTimeoutMs?: number, routeSessionPrefix?: string);
    call(args: {
        sessionId: string;
        projectRoot: string;
        method: "shadow_reset" | "state_sync" | "shadow_transform" | "transform" | "session.status" | "session.flush" | "session.recomp" | "session.wrapup" | "todo_state.set" | "agent_drops.append";
        body: unknown;
        signal?: AbortSignal;
    }): Promise<unknown>;
    closeSession(sessionId: string): void;
    private ensureRoute;
    private ensureConnected;
    private invalidateConnection;
}
export declare const __shadowSenderTest: {
    MAX_FACADE_FRAME_BYTES: number;
    SHADOW_SEED_BATCH_MAX_BYTES: number;
    SHADOW_TRANSFORM_PAGE_MAX_BYTES: number;
    SubcShadowTransport: typeof SubcShadowTransport;
    buildPagedSeedPayloads: typeof buildPagedSeedPayloads;
    buildShadowResetBody: typeof buildShadowResetBody;
    buildShadowTransformBody: typeof buildShadowTransformBody;
    buildPagedTransformPayloads: typeof buildPagedTransformPayloads;
    buildStateSyncPayload: typeof buildStateSyncPayload;
    flatWireBodyBytes: typeof flatWireBodyBytes;
    createSessionQueueState: typeof createSessionQueueState;
    denormalizeShadowOutput: typeof denormalizeShadowOutput;
    flatBlockIdForRawMessage: typeof flatBlockIdForRawMessage;
    resolveDeclaredTrimForShadow: typeof resolveDeclaredTrimForShadow;
    resolveOrdinalsForShadow: typeof resolveOrdinalsForShadow;
    toFlatWireBody: typeof toFlatWireBody;
};
export {};
//# sourceMappingURL=shadow-sender.d.ts.map