import type { KdProduct, ProductProfile } from "../product/profile.ts"; export type KdPhase = "discuss" | "spec" | "plan" | "execute" | "verify" | "ship"; export type KdHarnessMode = "quick" | "normal"; export type KdRisk = "low" | "medium" | "high"; export type KdRunStatus = "active" | "paused" | "done"; export type KdRiskSource = "manual" | "verify" | "ship"; export interface RiskAssessment { level: KdRisk; reason: string; source: KdRiskSource; updatedAt: string; } export interface GateResult { passed: boolean; reason?: string; checkedAt: string; } export interface RepairState { attempts: number; maxAttempts: number; lastFailureEvidence?: string; lastFailureSignature?: string; status: "idle" | "repairing" | "blocked"; goal?: string; updatedAt: string; } export interface KdQuestion { id: string; phase: KdPhase; question: string; reason?: string; contextSummary?: string; sourceRefs?: string[]; factLabel?: string; proposedFactValue?: string; options?: KdQuestionOption[]; choices?: string[]; multiple?: boolean; customAnswer?: boolean; blocking: boolean; status: "open" | "answered"; answer?: string; createdAt: string; answeredAt?: string; } export interface KdQuestionOption { label: string; description?: string; } export interface KdResumeSnapshot { phase: KdPhase; contextSummary: string; sourceRefs?: string[]; gateReason?: string; nextAction: string; pendingQuestionId?: string; status: "open" | "answered" | "archived"; updatedAt: string; } export type KdContextEntryKind = "user-input" | "decision" | "file-finding" | "constraint" | "requirement" | "risk" | "next-action"; export interface KdContextEntry { id: string; phase: KdPhase; kind: KdContextEntryKind; text: string; sourceRefs?: string[]; createdAt: string; } export interface KdWorkingSetFile { path: string; summary: string; source: "tool" | "user" | "artifact"; updatedAt: string; } export interface KdWorkingSet { focus: string; activeGap?: string; currentAction?: string; blockedBy?: string; recentFiles: KdWorkingSetFile[]; forbiddenDrift: string[]; updatedAt: string; } export interface KdToolResultContract { id: string; toolName: string; status: "success" | "blocked" | "failed"; kind: "read" | "search" | "list" | "shell" | "knowledge" | "evidence" | "write" | "other"; summary: string; paths?: string[]; modifiedFiles?: string[]; evidencePaths?: string[]; risk?: string; nextAction?: string; createdAt: string; } export interface KdActionCommit { id: string; phase: KdPhase; focus: string; intent: string; allowed: boolean; reason: string; requiredAction: string; source: "input" | "command" | "tool" | "system"; createdAt: string; } export interface KdWriteTransaction { id: string; path: string; status: "planned" | "blocked" | "written" | "verified"; checks: string[]; reason?: string; createdAt: string; updatedAt: string; } export interface KdSourceAnchorRef { line: number; hash: string; } export interface KdSourceAnchorSnapshot { id: string; path: string; fileHash: string; refs: KdSourceAnchorRef[]; summary?: string; createdAt: string; } export type KdFactStatus = "current" | "superseded" | "rejected"; export type KdFactSource = "question" | "manual"; export interface KdFact { key: string; label: string; value: string; status: KdFactStatus; source: KdFactSource; sourceQuestionId?: string; reason?: string; createdAt: string; updatedAt: string; supersededBy?: string; } export interface ActiveRun { id: string; goal?: string; phase: KdPhase; mode: KdHarnessMode; cwd: string; status?: KdRunStatus; createdAt?: string; updatedAt?: string; product?: KdProduct; version?: string; profile?: ProductProfile; riskAssessment?: RiskAssessment; repair?: RepairState; artifacts: Record; gate: GateResult; questions?: KdQuestion[]; facts?: KdFact[]; contextEntries?: KdContextEntry[]; resumeSnapshot?: KdResumeSnapshot; workingSet?: KdWorkingSet; toolResults?: KdToolResultContract[]; actionCommits?: KdActionCommit[]; writeTransactions?: KdWriteTransaction[]; sourceAnchors?: KdSourceAnchorSnapshot[]; } export const PHASE_ORDER: KdPhase[] = ["discuss", "spec", "plan", "execute", "verify", "ship"]; export const MODE_PHASE_ORDER: Record = { quick: ["execute"], normal: ["discuss", "plan", "execute", "verify"], }; export const HARNESS_MODES: KdHarnessMode[] = ["quick", "normal"]; export const PHASE_ARTIFACTS: Record = { discuss: "CONTEXT.md", spec: "SPEC.md", plan: "PLAN.md", execute: "EXECUTION.md", verify: "VERIFY.md", ship: "SHIP.md", }; export function isKdPhase(value: string): value is KdPhase { return (PHASE_ORDER as readonly string[]).includes(value); } export function isKdHarnessMode(value: string): value is KdHarnessMode { return (HARNESS_MODES as readonly string[]).includes(value); } export function phaseOrderForMode(mode: KdHarnessMode): KdPhase[] { return MODE_PHASE_ORDER[mode]; } export function phaseOrderForRun(run: Pick): KdPhase[] { return phaseOrderForMode(run.mode); } export function phaseIndexForRun(run: Pick, phase: KdPhase): number { return phaseOrderForRun(run).indexOf(phase); } export function phaseIncludedInRun(run: Pick, phase: KdPhase): boolean { return phaseIndexForRun(run, phase) >= 0; } export function nextPhaseForRun(run: Pick, phase: KdPhase = run.phase): KdPhase | undefined { const order = phaseOrderForRun(run); const index = order.indexOf(phase); return index >= 0 ? order[index + 1] : undefined; }