Files
ci/src/core/decision-engine.ts
T
Jon Chery 4a58aa1657 refactor(rebrand): rename & rebrand CI → CIAgent across all source and test files
- Type renames: CIConfig → CIAgentConfig, DEFAULT_CI_CONFIG → DEFAULT_CIAGENT_CONFIG
- Type renames: CiMetadata → CIAgentMetadata, ParsedCiCommit → ParsedCIAgentCommit
- Function renames: initCI → initCIAgent, isCIInitialized → isCIAgentInitialized
- Function renames: extractCiBlock → extractCIAgentBlock, parseCiBlock → parseCIAgentBlock
- Class renames: CiFiles → CIAgentFiles
- Import paths: ci-files.js → ciagent-files.js
- Directory paths: .ci/ → .ciagent/ across all source and test files
- Check names: ".ci directory exists" → ".ciagent directory exists"
- Check names: "CI config valid" → "CIAgent config valid"
- Temp dir names: ci-*-test- → ciagent-*-test-
- CLI examples: "ci init" → "ciagent init"
- Fix deepMerge infinite recursion bug in config.ts
- ---ci---/---/ci--- block markers preserved unchanged
- All 31 test suites, 370 tests passing

---ci---
phase: 1
milestone: v0.5
plan: 07
task: 07-01-01
status: execute
---/ci---
2026-05-29 18:01:13 +00:00

150 lines
4.0 KiB
TypeScript

import { execSync } from "node:child_process";
import { Decision, DecisionCategory, Alternative, confidenceToLevel } from "../types/decisions.js";
import { CIAgentConfig } from "../types/config.js";
import { CommitBuilder, DecisionCommitInput } from "./commit-builder.js";
import { CommitDecision } from "../types/commit-meta.js";
export interface DecisionInput {
decision: string;
rationale: string;
confidence: number;
category: DecisionCategory;
alternatives_considered: Alternative[];
phase?: string;
task?: string;
}
export interface DecisionResult {
decision: Decision;
escalated: boolean;
reason?: string;
commitMessage?: string;
}
export class DecisionEngine {
private config: CIAgentConfig;
private projectPath: string;
private currentPhase: number;
private currentMilestone: string;
private decisionCounter: number;
constructor(config: CIAgentConfig, projectPath: string, milestone: string = "v1.0") {
this.config = config;
this.projectPath = projectPath;
this.currentPhase = 0;
this.currentMilestone = milestone;
this.decisionCounter = 0;
}
setPhase(phase: number): void {
this.currentPhase = phase;
}
setMilestone(milestone: string): void {
this.currentMilestone = milestone;
}
makeDecision(input: DecisionInput): DecisionResult {
const id = `D-${String(++this.decisionCounter).padStart(3, "0")}`;
const threshold = this.config.autonomy.decision_confidence_threshold;
const decision: Decision = {
id,
timestamp: new Date().toISOString(),
decision: input.decision,
rationale: input.rationale,
confidence: input.confidence,
category: input.category,
alternatives_considered: input.alternatives_considered,
human_override: null,
phase: input.phase,
task: input.task,
};
const commitDecision: CommitDecision = {
id,
decision: input.decision,
rationale: input.rationale,
confidence: input.confidence,
alternatives: input.alternatives_considered.map((a) => a.option),
};
const confidenceLevel = confidenceToLevel(input.confidence);
const escalated = input.confidence < threshold;
let commitMessage: string | undefined;
if (this.config.git.auto_commit) {
commitMessage = CommitBuilder.buildDecisionCommit({
phase: this.currentPhase,
milestone: this.currentMilestone,
subject: input.decision,
decisions: [commitDecision],
});
}
if (escalated) {
return {
decision,
escalated: true,
reason: `Confidence ${input.confidence.toFixed(2)} below threshold ${threshold} (${confidenceLevel})`,
commitMessage,
};
}
return { decision, escalated: false, commitMessage };
}
makeHighConfidenceDecision(
decision: string,
rationale: string,
category: DecisionCategory,
alternatives: Alternative[] = []
): DecisionResult {
return this.makeDecision({
decision,
rationale,
confidence: 0.95,
category,
alternatives_considered: alternatives,
});
}
makeMediumConfidenceDecision(
decision: string,
rationale: string,
category: DecisionCategory,
alternatives: Alternative[] = []
): DecisionResult {
return this.makeDecision({
decision,
rationale,
confidence: 0.7,
category,
alternatives_considered: alternatives,
});
}
shouldAutoDecide(confidence: number): boolean {
return confidence >= this.config.autonomy.decision_confidence_threshold;
}
isIrreversibleAction(action: string): boolean {
return this.config.autonomy.escalation_hooks.some((hook) =>
action.toLowerCase().includes(hook.toLowerCase())
);
}
commitDecision(commitMessage: string): boolean {
if (!this.config.git.auto_commit) return false;
try {
execSync(`git add -A && git commit -m "${commitMessage.replace(/"/g, '\\"')}" --allow-empty`, {
cwd: this.projectPath,
stdio: "pipe",
});
return true;
} catch {
return false;
}
}
}