import { type SessionCompactionPolicy } from "@rudderhq/agent-runtime-utils";
import type { Db } from "@rudderhq/db";
import { agents, heartbeatRuns, issues } from "@rudderhq/db";
import type { HeartbeatRecoveryTrigger } from "@rudderhq/shared";
import type { AgentRuntimeExecutionResult, AgentRuntimeSessionCodec, UsageSummary } from "../../agent-runtimes/index.js";
import { type ResolvedWorkspaceForRun } from "../agent-run-context.js";
export { prioritizeProjectWorkspaceCandidatesForRun, type ResolvedWorkspaceForRun } from "../agent-run-context.js";
import type { ParsedIssueAssigneeAgentRuntimeOverrides, UsageTotals, WakeupOptions } from "./heartbeat.core.js";
declare const ISSUE_PASSIVE_FOLLOWUP_FAILURE_REASON: string, ISSUE_REVIEW_CLOSEOUT_FAILURE_REASON: string;
export type ResumeSessionRow = {
    sessionParamsJson: Record<string, unknown> | null;
    sessionDisplayId: string | null;
    lastRunId: string | null;
};
export declare function buildExplicitResumeSessionOverride(input: {
    resumeFromRunId: string;
    resumeRunSessionIdBefore: string | null;
    resumeRunSessionIdAfter: string | null;
    taskSession: ResumeSessionRow | null;
    sessionCodec: AgentRuntimeSessionCodec;
}): {
    sessionDisplayId: string | null;
    sessionParams: Record<string, unknown> | null;
} | null;
export declare function normalizeUsageTotals(usage: UsageSummary | null | undefined): UsageTotals | null;
export declare function readRawUsageTotals(usageJson: unknown): UsageTotals | null;
export declare function deriveNormalizedUsageDelta(current: UsageTotals | null, previous: UsageTotals | null): UsageTotals | null;
export declare function formatCount(value: number | null | undefined): string;
export declare function parseSessionCompactionPolicy(agent: typeof agents.$inferSelect): SessionCompactionPolicy;
export declare function resolveRuntimeSessionParamsForWorkspace(input: {
    orgId: string;
    agent: {
        id: string;
        name?: string | null;
        workspaceKey?: string | null;
    };
    previousSessionParams: Record<string, unknown> | null;
    resolvedWorkspace: ResolvedWorkspaceForRun;
}): {
    sessionParams: Record<string, unknown> | null;
    warning: string | null;
};
export declare function parseIssueAssigneeAgentRuntimeOverrides(raw: unknown): ParsedIssueAssigneeAgentRuntimeOverrides | null;
export declare function deriveTaskKey(contextSnapshot: Record<string, unknown> | null | undefined, payload: Record<string, unknown> | null | undefined): string | null;
export declare function shouldResetTaskSessionForWake(contextSnapshot: Record<string, unknown> | null | undefined): boolean;
export declare function formatRuntimeWorkspaceWarningLog(warning: string): {
    stream: "stdout";
    chunk: string;
};
export declare function describeSessionResetReason(contextSnapshot: Record<string, unknown> | null | undefined): "forceFreshSession was requested" | "wake reason is issue_assigned" | null;
export declare function deriveCommentId(contextSnapshot: Record<string, unknown> | null | undefined, payload: Record<string, unknown> | null | undefined): string | null;
export declare function enrichWakeContextSnapshot(input: {
    contextSnapshot: Record<string, unknown>;
    reason: string | null;
    source: WakeupOptions["source"];
    triggerDetail: WakeupOptions["triggerDetail"] | null;
    payload: Record<string, unknown> | null;
}): {
    contextSnapshot: Record<string, unknown>;
    issueIdFromPayload: string | null;
    commentIdFromPayload: string | null;
    taskKey: string | null;
    wakeCommentId: string | null;
};
export declare function mergeCoalescedContextSnapshot(existingRaw: unknown, incoming: Record<string, unknown>): Record<string, unknown>;
export declare function issueCommentAuthorKind(comment: {
    authorAgentId?: string | null;
    authorUserId?: string | null;
}): "agent" | "system" | "user";
export declare function issueCommentAuthorLabel(comment: {
    authorAgentId?: string | null;
    authorUserId?: string | null;
    authorAgentName?: string | null;
    authorUserName?: string | null;
}): string;
export declare function buildDeferredWakePayload(payload: Record<string, unknown> | null, contextSnapshot: Record<string, unknown>, issueId?: string | null): Record<string, unknown>;
export declare function readDeferredWakeContext(payloadRaw: unknown): Record<string, unknown>;
export declare function readDeferredWakePayload(payloadRaw: unknown): {
    [x: string]: unknown;
};
export declare function deriveDeferredWakeTaskKey(payloadRaw: unknown): string | null;
export declare function hydrateWakeContextSnapshot(db: Db, orgId: string, contextSnapshot: Record<string, unknown>): Promise<void>;
export declare function firstNonEmptyLine(value: unknown): string | null;
export declare function deriveRecoveryFailureKind(run: typeof heartbeatRuns.$inferSelect): string;
export declare function deriveRecoveryFailureSummary(run: typeof heartbeatRuns.$inferSelect): string;
export declare function mergeMissingRecoveryContextFields(target: Record<string, unknown>, source: Record<string, unknown>): void;
export declare function hydrateRecoveryBaseContextSnapshot(run: typeof heartbeatRuns.$inferSelect, getRunById: (runId: string) => Promise<typeof heartbeatRuns.$inferSelect | null>): Promise<{
    [x: string]: unknown;
}>;
export declare function buildRecoveryContextSnapshot(input: {
    baseContextSnapshot: Record<string, unknown>;
    run: typeof heartbeatRuns.$inferSelect;
    recoveryTrigger: HeartbeatRecoveryTrigger;
    wakeReason: string;
    wakeSource: string;
    triggerDetail: NonNullable<WakeupOptions["triggerDetail"]>;
}): Record<string, unknown>;
export type PassiveFollowupIssueRow = Pick<typeof issues.$inferSelect, "id" | "orgId" | "identifier" | "title" | "description" | "status" | "priority" | "projectId" | "assigneeAgentId" | "reviewerAgentId" | "reviewerUserId">;
export type PassiveFollowupContext = {
    originRunId: string;
    previousRunId: string | null;
    attempt: number;
    maxAttempts: number;
    reason: typeof ISSUE_PASSIVE_FOLLOWUP_FAILURE_REASON;
    queuedAt: string | null;
};
export type ReviewCloseoutContext = {
    originRunId: string;
    previousRunId: string | null;
    attempt: number;
    maxAttempts: number;
    reason: typeof ISSUE_REVIEW_CLOSEOUT_FAILURE_REASON;
};
export type PassiveIssueClosureOutcome = {
    kind: "none";
    reason: string;
} | {
    kind: "queued";
    run: typeof heartbeatRuns.$inferSelect;
    issue: PassiveFollowupIssueRow;
    originRunId: string;
    previousRunId: string;
    attempt: number;
    requestedAt: Date;
} | {
    kind: "operator_review";
    issue: PassiveFollowupIssueRow;
    originRunId: string;
    previousRunId: string;
    attempts: number;
    reason: typeof ISSUE_PASSIVE_FOLLOWUP_FAILURE_REASON;
} | {
    kind: "reviewer_convergence";
    issue: PassiveFollowupIssueRow;
    originRunId: string;
    previousRunId: string;
    attempts: number;
    reason: typeof ISSUE_PASSIVE_FOLLOWUP_FAILURE_REASON;
} | {
    kind: "reviewer_closeout";
    issue: PassiveFollowupIssueRow;
    originRunId: string;
    previousRunId: string;
    attempts: number;
    maxAttempts: number;
    reason: typeof ISSUE_REVIEW_CLOSEOUT_FAILURE_REASON;
} | {
    kind: "reviewer_closeout_operator_review";
    issue: PassiveFollowupIssueRow;
    originRunId: string;
    previousRunId: string;
    attempts: number;
    maxAttempts: number;
    reason: typeof ISSUE_REVIEW_CLOSEOUT_FAILURE_REASON;
};
export declare function normalizePassiveFollowupContext(raw: unknown): PassiveFollowupContext | null;
export declare function normalizeReviewCloseoutContext(raw: unknown): ReviewCloseoutContext | null;
export declare function passiveFollowupCooldownMs(attempt: number): number;
export declare function issueHasReviewer(issue: Pick<PassiveFollowupIssueRow, "reviewerAgentId" | "reviewerUserId">): boolean;
export declare function isAgentEligibleForTimerContinuation(agent: typeof agents.$inferSelect): boolean;
export declare function hasCredibleTimerContinuation(input: {
    agent: typeof agents.$inferSelect;
    policy: {
        enabled: boolean;
        intervalSec: number;
    };
    run: typeof heartbeatRuns.$inferSelect;
    now: Date;
}): boolean;
export declare function buildPassiveFollowupContextSnapshot(input: {
    run: typeof heartbeatRuns.$inferSelect;
    issue: PassiveFollowupIssueRow;
    originRunId: string;
    attempt: number;
    now: Date;
}): {
    reviewGate?: {
        reviewerAgentId: string | null;
        reviewerUserId: string | null;
        closeOutRequirement: string;
    } | undefined;
    issueId: string;
    taskId: string;
    taskKey: string;
    projectId: string | undefined;
    wakeReason: string;
    wakeSource: string;
    wakeTriggerDetail: string;
    issue: {
        id: string;
        title: string;
        description: string | null;
        status: string;
        priority: string;
    };
    passiveFollowup: {
        originRunId: string;
        previousRunId: string;
        attempt: number;
        maxAttempts: number;
        reason: string;
        queuedAt: string;
    };
};
export declare function runTaskKey(run: typeof heartbeatRuns.$inferSelect): string | null;
export declare function isSameTaskScope(left: string | null, right: string | null): boolean;
export declare function isTrackedLocalChildProcessAdapter(agentRuntimeType: string): boolean;
export declare function isProcessAlive(pid: number | null | undefined): boolean;
export declare function waitForProcessExit(pid: number, timeoutMs: number): Promise<boolean>;
export declare function terminateOrphanedProcess(pid: number): Promise<{
    stillAlive: boolean;
    terminationSignal: NodeJS.Signals | null;
    error: string | null;
}>;
export declare function truncateDisplayId(value: string | null | undefined, max?: number): string | null;
export declare function normalizeAgentNameKey(value: string | null | undefined): string | null;
export declare const defaultSessionCodec: AgentRuntimeSessionCodec;
export declare function getAgentRuntimeSessionCodec(agentRuntimeType: string): AgentRuntimeSessionCodec;
export declare function normalizeSessionParams(params: Record<string, unknown> | null | undefined): Record<string, unknown> | null;
export declare function resolveNextSessionState(input: {
    codec: AgentRuntimeSessionCodec;
    adapterResult: AgentRuntimeExecutionResult;
    previousParams: Record<string, unknown> | null;
    previousDisplayId: string | null;
    previousLegacySessionId: string | null;
}): {
    params: Record<string, unknown> | null;
    displayId: string | null;
    legacySessionId: string | null;
};
//# sourceMappingURL=heartbeat.sessions.d.ts.map