SPB Git

spb/khaelor Public

KHAELOR — a terminal-native autonomous engineering agent powered by Anthropic.

TypeScript 82.9% HTML 14.9% CSS 1.1% JavaScript 0.7%
1.8 KB · 55 lines typescript
Raw Blame History
1/**2 * KHAELOR3 * File: src/phases/state.ts4 * Description: Pure fold of phase events into the current phase-gate state (v2 design §1).5 *6 * Author: Simon-Pierre Boucher7 * Contact: contact@spboucher.ai8 */910import type { DesignArtifact, DurableEvent, Phase } from "../session/index.js";1112export interface PhaseState {13  /** Current phase; sessions without any phase event are in "understand". */14  phase: Phase;15  /** Artifact submitted but not yet approved/rejected. */16  pendingArtifact: { artifactId: string; artifact: DesignArtifact } | null;17  /** The artifact id whose approval unlocked implement, when one exists. */18  approvedArtifactId: string | null;19  /** True once a phase.approved{phase:"design"} exists in the session. */20  designApproved: boolean;21}2223/** Fold the durable stream into the phase-gate decision state. */24export function foldPhaseState(events: readonly DurableEvent[]): PhaseState {25  let phase: Phase = "understand";26  let pending: PhaseState["pendingArtifact"] = null;27  let approvedArtifactId: string | null = null;28  let designApproved = false;2930  for (const event of events) {31    switch (event.type) {32      case "phase.entered":33        phase = event.payload.phase;34        break;35      case "phase.artifact":36        pending = { artifactId: event.payload.artifactId, artifact: event.payload.artifact };37        break;38      case "phase.approved":39        if (event.payload.phase === "design") {40          designApproved = true;41          approvedArtifactId = event.payload.artifactId ?? pending?.artifactId ?? null;42          pending = null;43        }44        break;45      case "phase.rejected":46        pending = null;47        break;48      default:49        break;50    }51  }5253  return { phase, pendingArtifact: pending, approvedArtifactId, designApproved };54}55