// Shared internal types and small pure helpers used across the bug-investigation pipeline stages
// (the model loop, the verify stage, the report stages). Kept private to the module — none of these
// are re-exported from index.ts. Splitting them out keeps each pipeline file under the LOC limit
// while leaving a single source of truth for the resolved BugRunState and the loop result shape.
import { createBugEventEmitter } from "./emit.js";
import { DEFAULT_BUG_WORKFLOW_LIMITS, } from "./types.js";
// A no-op sink used when the caller injects none. emit is synchronous (ADR-0004 EventSink contract).
export const NO_OP_SINK = { emit: () => undefined };
// The zero-progress loop used to assemble a cancelled/failed report before the model loop ran.
export const EMPTY_BUG_LOOP = {
    accepted: undefined,
    investigationOnly: undefined,
    modelCallCount: 0,
    patchRetryCount: 0,
    lastRejectionCode: undefined,
};
export function resolveBugLimits(input) {
    return { ...DEFAULT_BUG_WORKFLOW_LIMITS, ...input.limits };
}
// The #6 PatchLimits view derived from the resolved workflow limits (D6 bound 1). Passed into
// validatePatch/applyPatch via their `limits` override seam — #6's defaults stay untouched.
export function patchLimitsFrom(limits) {
    return {
        maxFilesChanged: limits.maxFilesChanged,
        maxChangedLines: limits.maxChangedLines,
        maxPatchBytes: limits.maxPatchBytes,
    };
}
export function buildBugRunState(input, deps, fingerprint) {
    const now = deps.now ?? Date.now;
    const idSource = deps.idSource ?? (() => crypto.randomUUID());
    return {
        input,
        deps,
        limits: resolveBugLimits(input),
        signal: deps.signal ?? new AbortController().signal,
        now,
        emitter: createBugEventEmitter(deps.sink ?? NO_OP_SINK, idSource(), fingerprint, now),
        startedAt: now(),
        progress: { modelCallCount: 0, patchRetryCount: 0 },
        memoryPromptText: undefined,
    };
}
// UI-renderable next actions for the report. Pure. `elevated` lists changed manifest/config paths
// that warrant elevated review (D6).
export function nextActionsFor(applied, files, elevated) {
    const first = files[0] ?? "the proposed fix";
    const base = applied
        ? [`Review the applied fix in ${first}`, "Run `keiko verify` to confirm the suite is green"]
        : [`Review the proposed fix for ${first}`, "Re-run with --apply to write the fix and verify"];
    if (elevated.length > 0) {
        return [
            ...base,
            `This fix modifies build/manifest configuration (${elevated.join(", ")}) — review with elevated scrutiny before applying`,
        ];
    }
    return base;
}
// The next-actions list for the investigation-only outcome (no patch produced).
export function investigationNextActions() {
    return [
        "No safe fix was proposed; review the root-cause hypothesis and uncertainty",
        "Provide more evidence (full failing output, stack trace, or suspected files) and re-run",
    ];
}
