export type GoalStatus = "active" | "paused" | "blocked" | "budget_limited" | "complete"; export interface GoalBudget { maxIterations?: number; /** @deprecated Token usage is provider-reported and is retained for stored-goal compatibility only. */ maxTokens?: number; maxRuntimeMs?: number; } export interface GoalUsage { iterations: number; tokens: number; } export interface GoalBudgetUpdate { maxIterations?: number; maxRuntimeMs?: number; disabled?: boolean; /** @deprecated Retained for compatibility with previously stored goals. */ maxTokens?: number; } export interface GoalCheckpoint { id: string; scopeId: string; goalId: string; summary: string; evidence?: string; nextStep?: string; blocker?: string; createdAt: string; } export interface GoalEvaluationRecord { id: string; outcome: GoalEvaluation["outcome"]; reason: string; at: string; } export interface AgentGoal { id: string; scopeId: string; /** Legacy goals derive their display name from the objective until first edited. */ name?: string; objective: string; status: GoalStatus; blockedReason?: string; snoozedUntil?: string; budget: GoalBudget; usage: GoalUsage; lastSettledAt?: string; lastEvaluation?: GoalEvaluationRecord; /** Unconsumed lifecycle intent for the next goal-owned turn. */ nextContinuationKind?: Extract; version: number; createdAt: string; updatedAt: string; } export type GoalEvaluation = { outcome: "continue"; reason: string; } | { outcome: "complete"; reason: string; } | { outcome: "blocked"; reason: string; }; export interface GoalTerminalCandidate { outcome: "complete" | "blocked"; reason: string; } export interface GoalTerminalCandidateRecord extends GoalTerminalCandidate { scopeId: string; goalId: string; goalVersion: number; candidateId: string; createdAt: string; } export interface GoalProgress { latestOutput: string; tokenDelta?: number; terminalCandidate?: GoalTerminalCandidate; } export type GoalContinuationClaimState = "claimed" | "deferred" | "started"; export type GoalContinuationKind = "started" | "updated" | "continuation"; export interface GoalPendingEvaluation { scopeId: string; goalId: string; goalVersion: number; evaluationId: string; iterationsDelta: number; progress: GoalProgress; attempt: number; availableAt: string; lastError?: string; createdAt: string; updatedAt: string; } export interface GoalContinuationClaim { scopeId: string; goalId: string; goalVersion: number; claimId: string; state: GoalContinuationClaimState; kind: GoalContinuationKind; reason: string; attempt: number; availableAt: string; expiresAt: string; lastError?: string; createdAt: string; updatedAt: string; } export type GoalDeleteResult = "deleted" | "missing" | "conflict"; export interface GoalStorage { get(scopeId: string): Promise; /** Unfinished goals across every scope, newest activity first. */ listUnfinished(): Promise; create(goal: AgentGoal): Promise; replace(goal: AgentGoal, expectedVersion: number): Promise; updateBudget(goal: AgentGoal, expectedVersion: number): Promise; addCheckpoint(checkpoint: GoalCheckpoint): Promise; listCheckpoints(scopeId: string): Promise; delete(scopeId: string, expectedGoalId: string, expectedVersion: number): Promise; getPendingEvaluation(scopeId: string): Promise; appendPendingEvaluation(pending: GoalPendingEvaluation): Promise; putPendingEvaluation(pending: GoalPendingEvaluation): Promise; replacePendingEvaluation(pending: GoalPendingEvaluation, expectedEvaluationId: string): Promise; deletePendingEvaluation(scopeId: string, expectedEvaluationId: string): Promise; commitEvaluation(goal: AgentGoal, expectedGoalVersion: number, expectedEvaluationId: string): Promise; getTerminalCandidate(scopeId: string): Promise; putTerminalCandidate(candidate: GoalTerminalCandidateRecord): Promise; deleteTerminalCandidate(scopeId: string, expectedCandidateId: string): Promise; getContinuationClaim(scopeId: string): Promise; createContinuationClaim(claim: GoalContinuationClaim): Promise; replaceContinuationClaim(claim: GoalContinuationClaim, expectedClaimId: string): Promise; acknowledgeContinuationClaim(scopeId: string, expectedClaimId: string): Promise; deleteContinuationClaim(scopeId: string, expectedClaimId: string): Promise; close(): void; } export interface GoalEvaluator { evaluate(goal: AgentGoal, progress: GoalProgress): Promise; } export type GoalContinuationResult = { status: "started"; continuationId?: string; } | { status: "busy" | "unavailable" | "rejected"; reason: string; retryAfterMs?: number; }; export interface GoalContinuationRequest { claimId: string; kind: GoalContinuationKind; idempotencyKey: string; expectedGoalVersion: number; reason: string; } export interface GoalContinuation { continueIfIdle(goal: AgentGoal, request: GoalContinuationRequest): Promise; } export interface GoalRetryPolicy { maxAttempts: number; baseDelayMs: number; maxDelayMs: number; } export interface GoalWakeScheduler { schedule(scopeId: string, wakeAt: string, wake: () => void): void; cancel(scopeId: string): void; close(): void; } export type GoalEvent = { type: "goal.created"; goal: AgentGoal; } | { type: "goal.status_changed"; goal: AgentGoal; previousStatus: GoalStatus; } | { type: "goal.progress_accounted"; goal: AgentGoal; tokenDelta: number; } | { type: "goal.budget_changed"; goal: AgentGoal; previousBudget: GoalBudget; } | { type: "goal.updated"; goal: AgentGoal; } | { type: "goal.checkpoint_added"; goal: AgentGoal; checkpoint: GoalCheckpoint; } | { type: "goal.snoozed"; goal: AgentGoal; snoozedUntil: string; } | { type: "goal.snooze_expired"; goal: AgentGoal; } | { type: "goal.evaluated"; goal: AgentGoal; evaluation: GoalEvaluation; } | { type: "goal.auto_continued"; goal: AgentGoal; } | { type: "goal.terminal_candidate_requested"; goal: AgentGoal; candidate: GoalTerminalCandidateRecord; } | { type: "goal.continuation_claimed"; goal: AgentGoal; claim: GoalContinuationClaim; } | { type: "goal.continuation_started"; goal: AgentGoal; claim: GoalContinuationClaim; } | { type: "goal.continuation_deferred"; goal: AgentGoal; claim: GoalContinuationClaim; } | { type: "goal.retry_scheduled"; scopeId: string; goalId: string; operation: "evaluator" | "continuation"; attempt: number; availableAt: string; error: string; claimId?: string; } | { type: "goal.retry_exhausted"; scopeId: string; goalId: string; operation: "evaluator" | "continuation"; attempt: number; error: string; claimId?: string; } | { type: "goal.recovered"; goal: AgentGoal; } | { type: "goal.cleared"; goal: AgentGoal; } | { type: "goal.error"; operation: string; error: string; }; export interface GoalEventSink { record(event: GoalEvent): Promise | void; }