import type { KvService, KvEntry } from './kv'; import type { Logger, LoopConfig } from '../types'; import type { OpencodeClient } from '@opencode-ai/sdk/v2'; export declare function migrateRalphKeys(kvService: KvService, projectId: string, logger: Logger): void; export declare const MAX_RETRIES = 3; export declare const STALL_TIMEOUT_MS = 60000; export declare const MAX_CONSECUTIVE_STALLS = 5; export declare const DEFAULT_MIN_AUDITS = 1; export declare const RECENT_MESSAGES_COUNT = 5; export declare const DEFAULT_COMPLETION_SIGNAL = "ALL_PHASES_COMPLETE"; export declare function buildCompletionSignalInstructions(signal: string): string; export { LOOP_PERMISSION_RULESET } from '../constants/loop'; /** * Represents the runtime state of an autonomous loop. */ export interface LoopState { active: boolean; sessionId: string; loopName: string; worktreeDir: string; projectDir?: string; worktreeBranch?: string; iteration: number; maxIterations: number; completionSignal: string | null; startedAt: string; prompt?: string; phase: 'coding' | 'auditing'; audit?: boolean; lastAuditResult?: string; errorCount: number; auditCount: number; terminationReason?: string; completedAt?: string; worktree?: boolean; modelFailed?: boolean; sandbox?: boolean; sandboxContainerName?: string; completionSummary?: string; executionModel?: string; auditorModel?: string; /** Cumulative token usage across all iterations. */ totalTokens?: number; /** Cumulative cost in USD. */ totalCostUsd?: number; } export interface LoopService { /** * Gets the active state for a loop by name. * @param name - The loop name. * @returns The loop state if active, null otherwise. */ getActiveState(name: string): LoopState | null; /** * Gets any state (active or completed) for a loop by name. * @param name - The loop name. * @returns The loop state if found, null otherwise. */ getAnyState(name: string): LoopState | null; /** * Updates the state for a loop. * @param name - The loop name. * @param state - The new state to persist. */ setState(name: string, state: LoopState): void; /** * Deletes the state for a loop. * @param name - The loop name. */ deleteState(name: string): void; /** * Registers a session as belonging to a loop. * @param sessionId - The OpenCode session ID. * @param loopName - The loop name to associate. */ registerLoopSession(sessionId: string, loopName: string): void; /** * Resolves the loop name for a session. * @param sessionId - The OpenCode session ID. * @returns The loop name if found, null otherwise. */ resolveLoopName(sessionId: string): string | null; /** * Unregisters a session from its loop. * @param sessionId - The OpenCode session ID. */ unregisterLoopSession(sessionId: string): void; /** * Checks if text contains the completion signal. * @param text - The text to check. * @param promise - The completion signal phrase. * @returns True if the signal is present. */ checkCompletionSignal(text: string, promise: string): boolean; /** * Builds the prompt for continuing a loop iteration. * @param state - The current loop state. * @param auditFindings - Optional audit findings to include. * @returns The continuation prompt string. */ buildContinuationPrompt(state: LoopState, auditFindings?: string): string; /** * Builds the prompt for the auditor agent. * @param state - The current loop state. * @returns The audit prompt string. */ buildAuditPrompt(state: LoopState): string; /** * Lists all currently active loops. * @returns Array of active loop states. */ listActive(): LoopState[]; /** * Lists recently completed loops. * @returns Array of completed loop states. */ listRecent(): LoopState[]; /** * Finds a loop by exact or partial name match. * @param name - The loop name to search for. * @returns The matching loop state or null. */ findByLoopName(name: string): LoopState | null; /** * Finds candidate loops matching a partial name. * @param name - The partial name to search for. * @returns Array of matching loop states. */ findCandidatesByPartialName(name: string): LoopState[]; /** * Gets the configured stall timeout in milliseconds. * @returns Stall timeout value. */ getStallTimeoutMs(): number; /** * Gets the minimum number of audits required. * @returns Minimum audit count. */ getMinAudits(): number; /** * Terminates all active loops. */ terminateAll(): void; /** * Reconciles loops that were active but are now stale. * @returns Number of loops reconciled. */ reconcileStale(): number; /** * Checks if there are outstanding review findings. * @param branch - Optional branch to filter by. * @returns True if findings exist. */ hasOutstandingFindings(branch?: string): boolean; /** * Gets all outstanding review findings. * @param branch - Optional branch to filter by. * @returns Array of KV entries with findings. */ getOutstandingFindings(branch?: string): KvEntry[]; /** * Generates a unique loop name based on a base name. * @param baseName - The desired base name. * @returns A unique loop name. */ generateUniqueLoopName(baseName: string): string; /** * Gets the plan text for a loop by name or session ID. * @param loopName - The loop name. * @param sessionId - The session ID. * @returns The plan text or null. */ getPlanText(loopName: string, sessionId: string): string | null; } /** * Creates a loop service instance for managing autonomous dev loops. * * @param kvService - KV service for persistence. * @param projectId - The current project ID. * @param logger - Logger instance. * @param loopConfig - Optional loop configuration. * @returns A LoopService instance. */ export declare function createLoopService(kvService: KvService, projectId: string, logger: Logger, loopConfig?: LoopConfig): LoopService; /** * Generates a unique worktree name by checking against existing names and appending a numeric suffix if needed. * This is exported for use by TUI and other modules that need to generate unique names without direct loop service access. * * @param baseName - The base name to uniquify * @param existingNames - Array of existing worktree names to check against * @returns A unique name (either the base name or with -1, -2, etc. suffix) */ export declare function generateUniqueName(baseName: string, existingNames: readonly string[]): string; export interface LoopSessionOutput { messages: { text: string; cost: number; tokens: { input: number; output: number; reasoning: number; cacheRead: number; cacheWrite: number; }; }[]; totalCost: number; totalTokens: { input: number; output: number; reasoning: number; cacheRead: number; cacheWrite: number; }; fileChanges: { additions: number; deletions: number; files: number; } | null; } /** * Fetches the output and statistics for a completed loop session. * * @param v2Client - OpenCode v2 API client. * @param sessionId - The session ID to fetch. * @param directory - The working directory. * @param logger - Optional logger for debugging. * @returns Session output including messages, costs, and file changes. */ export declare function fetchSessionOutput(v2Client: OpencodeClient, sessionId: string, directory: string, logger?: Logger): Promise; /** * Run success-criteria commands and return which ones failed. * Each command is run synchronously in the working directory. * Returns an empty array if all criteria pass. */ export declare function checkSuccessCriteria(criteria: { tests?: string; lint?: string; custom?: string[]; }, cwd: string): Array<{ label: string; command: string; error: string; }>; /** * Check whether a loop has exceeded its budget. * Returns a descriptive string if exceeded, null if within budget. */ export declare function checkBudgetExceeded(budget: { maxTokens?: number; maxCostUsd?: number; maxIterations?: number; }, state: { iteration: number; totalTokens?: number; totalCostUsd?: number; }): string | null; //# sourceMappingURL=loop.d.ts.map