Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405/** Immutable goal controller transitions and semantic effects. @module */
import { formatGoalFooter, } from './footer.ts';import { buildGoalMessage, } from './message.ts';import { ABSENT_GOAL_STATE, reduceGoalEvent,} from './reducer.ts';import type { ActiveGoalState, GoalControllerState, GoalControllerTransition, GoalGenerationId, GoalGenerationRotatedEvent, GoalMessageMarker, GoalRunId, GoalRuntimeEpoch, GoalStartBoundary, GoalState,} from './types.ts';
/** Create fresh controller for one extension runtime epoch. @param runtimeEpoch - runtime-instance identity @returns absent live controller @example ```ts const controller = createGoalController('runtime-1'); ``` */function createGoalController(runtimeEpoch: GoalRuntimeEpoch,): GoalControllerState { return { goal: ABSENT_GOAL_STATE, runtimeEpoch, settlementSequence: 0, shutdown: false, };}
/** Restore exact selected-branch state without triggering a model turn. @param controller - current runtime controller @param goal - reduced selected-branch state @returns restored controller and footer effects @example ```ts restoreGoalController({ controller, goal }); ``` */function restoreGoalController( { controller, goal, }: { readonly controller: GoalControllerState; readonly goal: GoalState; },): GoalControllerTransition { return { controller: { goal, runtimeEpoch: controller.runtimeEpoch, settlementSequence: controller.settlementSequence, shutdown: false, }, effects: goal.phase === 'active' ? [{ type: 'set_footer', text: formatGoalFooter(goal.objective,), },] : [{ type: 'clear_footer', },], };}
/** Start new run or atomically supersede active or terminal record. @param controller - current controller @param objective - exact normalized objective @param runId - fresh run identity @param generationId - fresh generation identity @param startBoundary - stable reviewer start marker @param marker - unique kickoff message marker @param timestamp - ISO transition timestamp @param isIdle - whether Pi can start turn now @param hasPendingMessages - whether human input already owns next turn @returns next controller with persist, footer, and kickoff effects @example ```ts startGoal({ controller, objective, runId, generationId, startBoundary, marker, timestamp, isIdle: true, hasPendingMessages: false }); ``` */function startGoal( { controller, objective, runId, generationId, startBoundary, marker, timestamp, isIdle, hasPendingMessages, }: { readonly controller: GoalControllerState; readonly objective: string; readonly runId: GoalRunId; readonly generationId: GoalGenerationId; readonly startBoundary: GoalStartBoundary; readonly marker: GoalMessageMarker; readonly timestamp: string; readonly isIdle: boolean; readonly hasPendingMessages: boolean; },): GoalControllerTransition { /** Existing run superseded atomically by start event. */ const supersededRunId = controller.goal .phase === 'absent' ? undefined : controller.goal .runId; /** Persisted atomic start event. */ const event = { kind: 'run_started', runId, generationId, objective, startedAt: timestamp, startBoundary, continuationSequence: 0, transitionedAt: timestamp, ...(supersededRunId === undefined ? {} : { supersededRunId, }), } as const; /** Active state derived through same reducer used during reconstruction. */ const goal = reduceGoalEvent({ state: controller.goal, event, },); if (goal.phase !== 'active') throw new Error('Goal start event did not reduce to active state',); /** Visible kickoff custom message. */ const message = buildGoalMessage({ goal, kind: 'kickoff', continuationSequence: 0, marker, },); /** Whether kickoff may enter Pi immediately. */ const sendImmediately = isIdle && (!hasPendingMessages); return { controller: { goal, runtimeEpoch: controller.runtimeEpoch, settlementSequence: controller.settlementSequence, shutdown: false, ...(sendImmediately ? {} : { pendingKickoff: { runId, generationId, runtimeEpoch: controller.runtimeEpoch, marker, }, }), }, effects: [ ...(controller.goal .phase === 'absent' ? [] : [{ type: 'clear_footer' as const, },]), { type: 'persist', event, }, { type: 'set_footer', text: formatGoalFooter(objective,), }, ...(sendImmediately ? [{ type: 'send_message' as const, message, triggerTurn: true as const, },] : []), { type: 'log', level: 'debug', message: supersededRunId === undefined ? `started goal run ${runId}` : `started goal run ${runId}, superseding ${supersededRunId}`, }, ], };}
/** Rotate active generation during runtime restoration or tree navigation. @param controller - controller restored from selected branch @param generationId - fresh generation identity @param timestamp - ISO rotation timestamp @param cause - lifecycle reason requiring stale-callback invalidation @returns rotation persistence and footer effects, or terminal no-op @example ```ts rotateGoalGeneration({ controller, generationId, timestamp, cause: 'runtime_restore' }); ``` */function rotateGoalGeneration( { controller, generationId, timestamp, cause, }: { readonly controller: GoalControllerState; readonly generationId: GoalGenerationId; readonly timestamp: string; readonly cause: GoalGenerationRotatedEvent['cause']; },): GoalControllerTransition { if (controller.goal .phase !== 'active') { return { controller, effects: [], }; } /** Current active state before rotation. */ const previous = controller.goal; /** Persisted generation rotation. */ const event: GoalGenerationRotatedEvent = { kind: 'generation_rotated', runId: previous.runId, previousGenerationId: previous.generationId, generationId, continuationSequence: previous.continuationSequence, transitionedAt: timestamp, cause, }; /** Active state with fresh generation. */ const goal = reduceGoalEvent({ state: previous, event, },); return { controller: { goal, runtimeEpoch: controller.runtimeEpoch, settlementSequence: controller.settlementSequence, shutdown: controller.shutdown, }, effects: [ { type: 'persist', event, }, { type: 'set_footer', text: formatGoalFooter(previous.objective,), }, { type: 'log', level: 'debug', message: `rotated goal generation for ${previous.runId} after ${cause}`, }, ], };}
/** Clear current active or terminal record without aborting Pi turn. @param controller - current controller @param timestamp - ISO clear timestamp @returns clear tombstone or idempotent informational no-op @example ```ts clearGoal({ controller, timestamp }); ``` */function clearGoal( { controller, timestamp, }: { readonly controller: GoalControllerState; readonly timestamp: string; },): GoalControllerTransition { if (controller.goal .phase === 'absent') { return { controller: { goal: controller.goal, runtimeEpoch: controller.runtimeEpoch, settlementSequence: controller.settlementSequence, shutdown: controller.shutdown, }, effects: [{ type: 'notify', level: 'info', message: 'No goal is active.', },], }; } /** Current record cleared by matching tombstone. */ const current = controller.goal; /** Persisted clear tombstone. */ const event = { kind: 'run_cleared', runId: current.runId, generationId: current.generationId, clearedAt: timestamp, } as const; return { controller: { goal: ABSENT_GOAL_STATE, runtimeEpoch: controller.runtimeEpoch, settlementSequence: controller.settlementSequence, shutdown: controller.shutdown, }, effects: [ { type: 'persist', event, }, { type: 'clear_footer', }, { type: 'notify', level: 'info', message: `Goal cleared: ${current.objective}`, }, { type: 'log', level: 'debug', message: `cleared goal run ${current.runId}`, }, ], };}
export { clearGoal, createGoalController, restoreGoalController, rotateGoalGeneration, startGoal,};