feat: Phase1 核心——模型/状态机 + SQLite Store(守卫+审批闸) + REST/WS API + daemon

- src/model: 复杂度分级、16 状态状态机、实体类型
- src/store: better-sqlite3 接 schema,transition 受 canTransition 守卫,
  decide 审批闸(reject 必带改进意见),事件订阅广播,nextExecutable
- src/api: Fastify REST + ws 事件广播(/ws)
- src/daemon: maestrod 入口(env 配置,默认 ~/.maestro :4517)
- test: 9 个生命周期单测全过;typecheck/build 干净;REST+WS 端到端实跑验证

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
wangjia
2026-06-12 20:44:45 +08:00
commit 3172718a94
18 changed files with 2757 additions and 0 deletions
+23
View File
@@ -0,0 +1,23 @@
/** 任务复杂度:Hard / Medium / Easy(对应旧 todo skill 的 tier 1/2/3 */
export type Complexity = 'hard' | 'medium' | 'easy';
export const COMPLEXITY_ORDER: Record<Complexity, number> = { hard: 1, medium: 2, easy: 3 };
export const COMPLEXITY_LABEL: Record<Complexity, string> = { hard: 'Hard', medium: 'Medium', easy: 'Easy' };
/** 旧 todo skill 的 tier(1/2/3) ↔ 复杂度,用于迁移导入 */
export const TIER_TO_COMPLEXITY: Record<number, Complexity> = { 1: 'hard', 2: 'medium', 3: 'easy' };
export const COMPLEXITY_TO_TIER: Record<Complexity, number> = { hard: 1, medium: 2, easy: 3 };
/** 前置闸:执行前是否需要用户确认(Hard / Medium 需要,Easy 不需要) */
export function requiresPreGate(c: Complexity): boolean {
return c === 'hard' || c === 'medium';
}
/** Hard 必须先进 plan 分析 + 任务拆解 */
export function mustDecompose(c: Complexity): boolean {
return c === 'hard';
}
export function isComplexity(v: unknown): v is Complexity {
return v === 'hard' || v === 'medium' || v === 'easy';
}
+95
View File
@@ -0,0 +1,95 @@
import type { Complexity } from './complexity.js';
/** 任务执行状态机(设计见 DESIGN.md §5 */
export type TaskStatus =
| 'init' // 新建,未分析 / 未定方案
| 'analyzing' // Hardplan 分析 + 拆解中
| 'plan_review' // Hard)拆解完成,待 accept
| 'decomposed' // (Hard)已拆解,作为容器,进度由子任务汇总
| 'speccing' // Medium)写方案中
| 'spec_review' // Medium)方案完成,待 accept
| 'ready' // 可执行叶子
| 'blocked' // 依赖未满足
| 'queued' // 编排器已领取,等 worker 槽位
| 'executing' // agent 在 worktree 跑 headless Claude Code
| 'exec_review' // 执行完成,改动在分支,待审 / 合
| 'failed' // 执行失败
| 'needs_attention' // 失败超阈值 / 冲突 / 歧义,需人工
| 'done' // 已合并 / 已接受
| 'paused'
| 'cancelled';
export const STATUS_LABEL: Record<TaskStatus, string> = {
init: '新建',
analyzing: '分析拆解中',
plan_review: '待确认拆解',
decomposed: '已拆解',
speccing: '写方案中',
spec_review: '待确认方案',
ready: '可执行',
blocked: '被依赖阻塞',
queued: '排队中',
executing: '执行中',
exec_review: '待审/合',
failed: '失败',
needs_attention: '需人工',
done: '完成',
paused: '暂停',
cancelled: '取消',
};
/** 审批闸类型 */
export type GateKind = 'plan' | 'spec' | 'exec';
/** 哪些状态在等待用户审批,以及对应的闸类型 */
export const GATE_STATUS: Partial<Record<TaskStatus, GateKind>> = {
plan_review: 'plan',
spec_review: 'spec',
exec_review: 'exec',
};
export function gateOf(s: TaskStatus): GateKind | null {
return GATE_STATUS[s] ?? null;
}
export const TERMINAL_STATUS: ReadonlySet<TaskStatus> = new Set<TaskStatus>(['done', 'cancelled']);
/** 合法状态流转表:from → 允许的 to[] */
export const TRANSITIONS: Record<TaskStatus, TaskStatus[]> = {
init: ['analyzing', 'speccing', 'ready', 'cancelled'], // 按复杂度分流
analyzing: ['plan_review', 'cancelled', 'paused'],
plan_review: ['decomposed', 'analyzing', 'cancelled'], // accept / reject(+意见)
decomposed: ['done', 'analyzing', 'cancelled', 'paused'], // 子全 done / 重拆
speccing: ['spec_review', 'cancelled', 'paused'],
spec_review: ['ready', 'speccing', 'cancelled'], // accept / reject(+意见)
ready: ['blocked', 'queued', 'speccing', 'analyzing', 'cancelled', 'paused'],
blocked: ['ready', 'cancelled', 'paused'],
queued: ['executing', 'ready', 'cancelled', 'paused'],
executing: ['exec_review', 'failed', 'cancelled'],
exec_review: ['done', 'ready', 'failed', 'cancelled'], // accept(合并) / reject(返工)
failed: ['queued', 'needs_attention', 'cancelled'], // 重试 / 升级
needs_attention: ['ready', 'queued', 'analyzing', 'speccing', 'cancelled', 'paused'],
done: [],
paused: ['ready', 'analyzing', 'speccing', 'queued', 'cancelled'],
cancelled: [],
};
export function canTransition(from: TaskStatus, to: TaskStatus): boolean {
return TRANSITIONS[from]?.includes(to) ?? false;
}
/** 新建任务按复杂度决定从 init 进入的下一个状态 */
export function initialNextStatus(c: Complexity): TaskStatus {
if (c === 'hard') return 'analyzing'; // 强制分析 + 拆解
if (c === 'medium') return 'speccing'; // 写方案
return 'ready'; // Easy:写完 operations 即可执行
}
/** 可被执行器领取的状态(叶子 + 依赖满足由调用方另判) */
export function isExecutable(s: TaskStatus): boolean {
return s === 'ready';
}
export function isGate(s: TaskStatus): boolean {
return s in GATE_STATUS;
}
+92
View File
@@ -0,0 +1,92 @@
import type { Complexity } from './complexity.js';
import type { TaskStatus, GateKind } from './status.js';
export type Id = string;
/** 层级上限:一般 ≤3 层,极复杂 ≤4 层 */
export const DEFAULT_MAX_DEPTH = 3;
export const HARD_MAX_DEPTH = 4;
/** 自动执行边界(Easy 任务由后台执行器自动跑到什么程度) */
export type Autonomy = 'manual' | 'auto-easy' | 'auto-approved';
export interface Project {
id: Id;
name: string;
repoPath: string;
defaultBranch: string;
verifyCmd: string | null; // 执行后的校验命令(build/test/lint
autonomy: Autonomy;
model: string | null;
concurrency: number; // 每项目并发执行上限
status: 'active' | 'paused';
createdAt: string;
}
export interface ApprovalRecord {
gate: GateKind;
action: 'accept' | 'reject';
actor: string;
reason: string | null; // reject 必填(改进意见)
at: string;
}
export interface TaskResult {
branch: string | null;
worktree: string | null;
diffSummary: string | null;
commits: string[];
prUrl: string | null;
}
export interface Task {
id: Id;
projectId: Id;
parentId: Id | null;
depth: number; // 1..4
title: string;
complexity: Complexity;
status: TaskStatus;
priority: number;
deps: Id[]; // 兄弟依赖(同层)
// 三选一产出(按复杂度)
plan: string | null; // Hard:分析 + 拆解说明
spec: string | null; // Medium:改动内容 + 为什么
operations: string | null; // Easy:将执行的操作
approvals: ApprovalRecord[];
result: TaskResult | null;
assignee: 'agent' | 'human' | null;
createdAt: string;
updatedAt: string;
}
export type RunKind = 'planner' | 'executor';
export type RunStatus = 'started' | 'succeeded' | 'failed' | 'cancelled';
export interface Run {
id: Id;
taskId: Id;
kind: RunKind; // planner(产出拆解/方案)| executor(执行改动)
worktree: string | null;
branch: string | null;
status: RunStatus;
startedAt: string;
endedAt: string | null;
transcriptRef: string | null; // agent 转录日志文件路径
claudeSessionId: string | null;
error: string | null;
}
export type EventType =
| 'task.created' | 'task.updated' | 'status.changed'
| 'approval.requested' | 'approval.granted' | 'approval.rejected'
| 'run.started' | 'run.finished';
export interface Event {
id: Id;
projectId: Id;
taskId: Id | null;
type: EventType;
payload: Record<string, unknown>;
at: string;
}