/** * Executes a single workflow step through the 3-phase model. * * Phase 1: Main agent execution (with tools) * Phase 2: Report output (Write-only, optional) * Phase 3: Status judgment (no tools, optional) */ import type { AgentWorkflowStep, WorkflowStep, WorkflowState, AgentResponse, Language, FallbackContext, WorkflowConfig, WorkflowWideRule, WorkflowResumePointEntry, CompanionReviewMode, NormalOrTeamLeaderWorkflowStep, ResolvedFacetPool } from '../../models/types.js'; import type { PhaseName, PhasePromptParts, JudgeStageEntry, RuntimeStepResolution, StepProviderInfo, StepRunResult, WorkflowEngineOptions, WorkflowStepExecutionEventContext } from '../types.js'; import type { ProviderUsageSnapshot } from '../../models/response.js'; import type { InjectedReport, PreparedInstruction } from '../instruction/prepared-instruction.js'; import type { DynamicFacetSelectionContext, DynamicFacetSelectorCoordinator } from '../dynamic-facets/dynamicFacetSelectorCoordinator.js'; import type { BasePhaseRunnerContext } from '../phase-runner.js'; import type { OptionsBuilder } from './OptionsBuilder.js'; import type { RunPaths } from '../run/run-paths.js'; import type { StructuredOutputNormalizerRegistry } from './structured-output-normalizer.js'; import type { InstructionBuildTransaction } from './instruction-build-transaction.js'; import type { PullRequestContext } from '../pr-context.js'; import type { TaskReviewScope } from '../review-scope.js'; import { type Phase1Attempt } from './phase1-empty-recovery.js'; import type { CompanionFollowUpResult } from '../companion/fix-loop.js'; import { CompanionStepRuntime, type CompanionDiffBaseline } from '../companion/step-runtime.js'; import type { RunAgentOptions } from '../../../agents/types.js'; import { type CompanionFixPolicy } from '../../models/companion-types.js'; import { type CompletionRetryDiagnostic } from '../completion-retry.js'; import type { LiveInterventionChannel, PreparedLiveInterventionDelivery } from '../live-intervention/types.js'; export interface StepExecutorDeps { readonly optionsBuilder: OptionsBuilder; readonly getCwd: () => string; readonly getProjectCwd: () => string; readonly getReportDir: () => string; readonly getRunPaths: () => RunPaths; readonly getFailureDir: () => string; readonly getLanguage: () => Language | undefined; readonly getInteractive: () => boolean; readonly getWorkflowSteps: () => ReadonlyArray<{ name: string; description?: string; }>; readonly getWorkflowName: () => string; readonly getTask: () => string; readonly getWorkflowDescription: () => string | undefined; readonly getWorkflowRules: () => readonly WorkflowWideRule[] | undefined; readonly getWorkflowCallVars?: () => Readonly> | undefined; readonly getRetryNote: () => string | undefined; readonly getPrContext?: () => PullRequestContext | undefined; /** Changed file set for this task. Recomputed per instruction build (the working tree moves). */ readonly getReviewScope: () => TaskReviewScope; readonly getObservabilityRunId?: () => string | undefined; readonly observabilityEnabled?: () => boolean; readonly sanitizeObservabilityText?: (text: string) => string; readonly getCurrentWorkflowStack?: () => WorkflowResumePointEntry[] | undefined; readonly structuredOutputNormalizers: StructuredOutputNormalizerRegistry; readonly abortSignal?: AbortSignal; readonly getAbortSignal?: () => AbortSignal | undefined; readonly executionProvider: WorkflowEngineOptions['provider']; readonly executionModel: WorkflowEngineOptions['model']; readonly internalAgentSeats?: import('../../models/config-types.js').InternalAgentSeats; readonly emitEvent: (event: string, ...args: unknown[]) => void; /** 実行ループ外の合成ステップの LLM 呼び出しを usage-events へ記録する。 */ readonly recordSynthesizedAgentUsage: (stepName: string, providerInfo: StepProviderInfo, success: boolean, usage: ProviderUsageSnapshot | undefined) => void; readonly getRunId: () => string; readonly getRunPathNamespace: () => readonly string[]; readonly companionEnabled: boolean; readonly companionReviewMode: CompanionReviewMode; readonly companionFixPolicy?: CompanionFixPolicy; readonly companionDefinitions?: WorkflowConfig['companions']; readonly companionProviders?: WorkflowEngineOptions['companionProviders']; readonly companionSelectorProvider?: WorkflowEngineOptions['selectorProvider']; readonly companionDiffReader?: WorkflowEngineOptions['companionDiffReader']; readonly onPhaseStart?: (step: WorkflowStep, phase: 1 | 2 | 3, phaseName: PhaseName, instruction: string, promptParts: PhasePromptParts, phaseExecutionId?: string, iteration?: number) => void; readonly onPhaseComplete?: (step: WorkflowStep, phase: 1 | 2 | 3, phaseName: PhaseName, content: string, status: string, error?: string, phaseExecutionId?: string, iteration?: number) => void; readonly onJudgeStage?: (step: WorkflowStep, phase: 3, phaseName: 'judge', entry: JudgeStageEntry, phaseExecutionId?: string, iteration?: number) => void; readonly dynamicFacetSelectorCoordinator?: DynamicFacetSelectorCoordinator; readonly getFacetPool?: (name: string) => ResolvedFacetPool | undefined; readonly liveIntervention?: LiveInterventionChannel; } /** * 通常 agent ステップを実行前に確定した結果。RunLoop の観測イベント、 * StepExecutor、provider がこの同じ値を共有する。 */ export interface PreparedNormalStepExecution { readonly executableStep: AgentWorkflowStep; readonly phase1Instruction: string; readonly injectedReports: readonly InjectedReport[]; readonly priorStepResponseText?: string; readonly stepIteration: number; readonly liveInterventionDelivery?: PreparedLiveInterventionDelivery; } interface StructuredOutputNormalizationResult { readonly response: AgentResponse; readonly invalidDetail?: string; readonly invalidKind?: 'model_output' | 'schema_config'; readonly invalidIssues?: readonly { readonly path: string; readonly keyword: string; readonly message: string; }[]; } export declare class StepExecutor { private readonly deps; private static isProviderStreamParseFailure; private readonly structuredOutputNormalizers; constructor(deps: StepExecutorDeps); private resolveAbortSignal; private static buildTimestamp; private static buildSnapshotFileName; completeReviewerResponse(input: { readonly step: AgentWorkflowStep; readonly originalInstruction: string; readonly initialResponse: AgentResponse; readonly executeRetry: (instruction: string, sessionId: string | undefined) => Promise; }): Promise<{ readonly response: AgentResponse; readonly reviewerSessionId: string | undefined; readonly diagnostic?: CompletionRetryDiagnostic; }>; normalizeReviewerResponse(step: AgentWorkflowStep, response: AgentResponse, runtime?: RuntimeStepResolution): AgentResponse; private normalizeReviewerResponseWithOrigin; private finalizeObservedNormalAttempt; private finalizeObservedLiveInterventionResponse; private finalizeObservedReviewerAttemptWithOrigin; finalizeObservedReviewerAttempt(input: { readonly eventStep: WorkflowStep; readonly executableStep: AgentWorkflowStep; readonly iteration: number; readonly attempt: Phase1Attempt; readonly response: AgentResponse; readonly runtime?: RuntimeStepResolution; readonly recordUsage?: (success: boolean, usage: AgentResponse['providerUsage']) => void; }): AgentResponse; completeCompanionReview(input: { readonly eventStep: WorkflowStep; readonly executableStep: AgentWorkflowStep; readonly state: WorkflowState; readonly initialResponse: AgentResponse; readonly agentOptions: RunAgentOptions; readonly runtime?: RuntimeStepResolution; readonly companionRuntime: CompanionStepRuntime | undefined; readonly providerInfo: StepProviderInfo; readonly nextSequence: () => number; readonly onSingleFixSettled?: () => void; readonly abortSignal: AbortSignal | undefined; readonly afterPhase1Response?: (response: AgentResponse) => Promise; readonly recordUsage?: (success: boolean, usage: AgentResponse['providerUsage']) => void; }): Promise; private resolveDynamicFacetPool; prepareDynamicFacetStep(step: AgentWorkflowStep, state: WorkflowState, task: string, stepIteration: number, context?: DynamicFacetSelectionContext): Promise; createCompanionRuntime(step: NormalOrTeamLeaderWorkflowStep, task: string, state: WorkflowState, abortSignal?: AbortSignal, options?: { readonly diffBaseline?: CompanionDiffBaseline; }): Promise; createCompanionDiffBaseline(abortSignal?: AbortSignal): CompanionDiffBaseline | undefined; private writeSnapshot; private contextSnapshotDirectory; private writeFacetSnapshot; private ensurePreviousResponseSnapshot; persistPreviousResponseSnapshot(state: WorkflowState, stepName: string, stepIteration: number, content: string): void; buildPhase1Instruction(instruction: string, step: WorkflowStep, runtime?: RuntimeStepResolution): string; prepareNormalStepExecution(step: WorkflowStep, state: WorkflowState, task: string, maxSteps: number | 'infinite', stepIteration: number, runtime?: RuntimeStepResolution): Promise; private prepareLiveInterventionDelivery; private appendLiveInterventionInstruction; /** * 実行ループを通らない合成ステップ * の LLM 呼び出しを usage-events へ記録する。通常ステップは step:complete * イベント経由、parallel / team_leader は recordDelegatedAgentUsage 経由で * 記録されるが、合成ステップの executeAgent 直呼びはどちらの経路にも * 乗らず、トークン集計の死角になっていた。 * * `attemptProviderInfo` は、その呼び出しが実際に使った provider/model。 * report phase の fallback のように attempt ごとに provider が変わる経路では * これを渡さないと、fallback で走った試行を primary として計上してしまう。 * 単発呼び出しでは省略でき、ステップ解決結果を使う。 */ recordSynthesizedAgentUsage(step: WorkflowStep, success: boolean, usage: ProviderUsageSnapshot | undefined, attemptProviderInfo?: StepProviderInfo): void; normalizeStructuredOutput(step: WorkflowStep, response: AgentResponse, runtime?: RuntimeStepResolution): AgentResponse; normalizeStructuredOutputWithDiagnostics(step: WorkflowStep, response: AgentResponse, runtime?: RuntimeStepResolution): StructuredOutputNormalizationResult; private buildStructuredOutputFailureFallback; private resolveStructuredOutputFailureReason; private logStructuredOutputFailure; /** Build Phase 1 instruction from template */ buildInstruction(step: WorkflowStep, stepIteration: number, state: WorkflowState, task: string, maxSteps: number | 'infinite', fallbackContext?: FallbackContext, transaction?: InstructionBuildTransaction): string; prepareInstruction(step: WorkflowStep, stepIteration: number, state: WorkflowState, task: string, maxSteps: number | 'infinite', fallbackContext?: FallbackContext, transaction?: InstructionBuildTransaction): PreparedInstruction; /** * Apply shared post-execution phases (Phase 2/3 + fallback rule evaluation). * * This method is intentionally reusable by non-normal step runners * (e.g., team_leader) so rule/report behavior stays consistent. */ applyPostExecutionPhases(step: WorkflowStep, state: WorkflowState, stepIteration: number, response: AgentResponse, updatePersonaSession: (persona: string, sessionId: string | undefined) => void, runtime?: RuntimeStepResolution, onProviderAttempt?: BasePhaseRunnerContext['onProviderAttempt'], onTerminalOperation?: (terminalOperation: NonNullable) => void, phase2Diagnostic?: string, injectedReports?: readonly InjectedReport[]): Promise; private applyPostExecutionRules; applyPostExecutionRulesOnly(step: WorkflowStep, state: WorkflowState, response: AgentResponse, updatePersonaSession: (persona: string, sessionId: string | undefined) => void, runtime?: RuntimeStepResolution): Promise; /** * Execute a normal (non-parallel) step through all 3 phases. * * Returns the final response (with matchedRuleIndex if a rule matched) * and the instruction used for Phase 1. */ runNormalStep(step: WorkflowStep, state: WorkflowState, task: string, maxSteps: number | 'infinite', updatePersonaSession: (persona: string, sessionId: string | undefined) => void, prebuiltInstruction?: PreparedInstruction, runtime?: RuntimeStepResolution, preparedExecution?: PreparedNormalStepExecution): Promise; private createReportExecutionContext; /** Collect step:report events for each report file that exists */ emitStepReports(step: WorkflowStep, execution: { readonly iteration: number; readonly resumeStepName: string; readonly stepIteration: number; readonly providerInfo: StepProviderInfo; }): void; private reportFiles; /** Check if report file exists and collect for emission */ private checkReportFile; /** Drain collected report files (called by engine after step execution) */ drainReportFiles(): Array<{ step: WorkflowStep; filePath: string; fileName: string; context: WorkflowStepExecutionEventContext; }>; } export {}; //# sourceMappingURL=StepExecutor.d.ts.map