merge: maestro/tsk_zacmwH3chZpG [tsk_zacmwH3chZpG]
This commit is contained in:
+17
-3
@@ -7,6 +7,7 @@ import type { TaskStatus } from '../model/status.js';
|
||||
import type { Project, Autonomy } from '../model/types.js';
|
||||
import { syncProject, hasTodoJson } from '../sync/todo-sync.js';
|
||||
import { resolvedExecutorModels } from '../executor/models.js';
|
||||
import { classifyComplexity, type ClassifierFn } from '../executor/classify.js';
|
||||
import { mergeBranch } from '../executor/merge.js';
|
||||
import { git, removeWorktree } from '../executor/worktree.js';
|
||||
import { resolveLogo, LOGO_MIME } from './logo.js';
|
||||
@@ -24,6 +25,8 @@ export interface ApiOptions {
|
||||
logger?: boolean;
|
||||
/** Claude 订阅额度查询(测试注入;默认直连 OAuth usage API,60s 缓存) */
|
||||
getUsage?: () => Promise<UsageInfo | null>;
|
||||
/** AUTO 复杂度分类器(测试注入;默认 classifyComplexity,用 sonnet 起一次轻量调用) */
|
||||
classify?: ClassifierFn;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -32,6 +35,7 @@ export interface ApiOptions {
|
||||
*/
|
||||
export function buildServer(opts: ApiOptions): FastifyInstance {
|
||||
const { store } = opts;
|
||||
const classify = opts.classify ?? ((title, description, cwd) => classifyComplexity(title, description, { cwd }));
|
||||
const app = Fastify({ logger: opts.logger ?? false });
|
||||
|
||||
// Store 错误 → 400(业务校验),其余 → 500
|
||||
@@ -156,13 +160,23 @@ export function buildServer(opts: ApiOptions): FastifyInstance {
|
||||
const { id } = req.params as { id: string };
|
||||
const b = req.body as Record<string, unknown>;
|
||||
if (!b?.title) throw new StoreError('title 必填');
|
||||
if (!isComplexity(b.complexity)) throw new StoreError('complexity 必须是 hard|medium|easy');
|
||||
return store.createTask({
|
||||
projectId: id, title: String(b.title), complexity: b.complexity as Complexity,
|
||||
// 'auto' = 由模型判定:先以 medium 落库占位,建任务后异步回填(不阻塞返回)
|
||||
const auto = b.complexity === 'auto';
|
||||
if (!auto && !isComplexity(b.complexity)) throw new StoreError('complexity 必须是 auto|hard|medium|easy');
|
||||
const task = store.createTask({
|
||||
projectId: id, title: String(b.title), complexity: auto ? 'medium' : (b.complexity as Complexity),
|
||||
parentId: b.parentId ? String(b.parentId) : null,
|
||||
priority: b.priority === undefined ? undefined : Number(b.priority),
|
||||
deps: Array.isArray(b.deps) ? (b.deps as string[]) : undefined,
|
||||
});
|
||||
if (auto) {
|
||||
// 异步分类回填:classifyComplexity 自身永不抛错(失败兜底 medium),外层再兜一层防御
|
||||
const cwd = store.getProject(id)?.repoPath ?? process.cwd();
|
||||
void classify(task.title, undefined, cwd)
|
||||
.then(({ complexity, reason }) => store.applyAutoComplexity(task.id, complexity, reason))
|
||||
.catch((e: unknown) => app.log.error(`任务 ${task.id} AUTO 复杂度分类失败(保留占位 medium):${(e as Error).message}`));
|
||||
}
|
||||
return task;
|
||||
});
|
||||
|
||||
app.get('/api/tasks/:id', (req) => {
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
import { nanoid } from 'nanoid';
|
||||
import type { Complexity } from '../model/complexity.js';
|
||||
import { isComplexity } from '../model/complexity.js';
|
||||
import { runClaude } from './cc.js';
|
||||
import { pickClassifierModel } from './models.js';
|
||||
|
||||
/** AUTO 复杂度判定结果:三档之一 + 一句理由(供用户复核) */
|
||||
export interface Classification {
|
||||
complexity: Complexity;
|
||||
reason: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 分类器函数签名(server 注入点;测试传 mock,生产传 classifyComplexity 封装)。
|
||||
* 永不抛错:一切失败折叠成兜底 medium。
|
||||
*/
|
||||
export type ClassifierFn = (title: string, description: string | undefined, cwd: string) => Promise<Classification>;
|
||||
|
||||
/** 解析失败 / 模型不可用时的兜底档 */
|
||||
const FALLBACK: Complexity = 'medium';
|
||||
const CLASSIFY_MAX_TURNS = 2;
|
||||
const CLASSIFY_TIMEOUT_MS = 60_000; // 60s:一次轻量文本判定,超时即兜底
|
||||
|
||||
/** 组装分类提示词:给出三档判据,要求只回 COMPLEXITY + REASON 两行。 */
|
||||
export function buildClassifyPrompt(title: string, description?: string): string {
|
||||
return [
|
||||
'你是任务复杂度分级助手。请根据下述任务,判定它属于 hard / medium / easy 中的哪一档。',
|
||||
'',
|
||||
'## 三档判据',
|
||||
'- easy:单文件或少量机械改动,无需设计、无未知项(如改文案、调参数、加日志)。',
|
||||
'- medium:需要先写改动方案、会跨几处文件,但范围清晰、无需拆解成多个子任务。',
|
||||
'- hard:需要拆解成多个子任务,跨模块/跨子系统,或存在明显未知项与设计取舍。',
|
||||
'',
|
||||
'## 任务',
|
||||
`标题:${title}`,
|
||||
`说明:${description?.trim() || '(无)'}`,
|
||||
'',
|
||||
'## 输出格式(严格两行,不要任何多余内容、不要使用工具)',
|
||||
'COMPLEXITY: <hard|medium|easy>',
|
||||
'REASON: <一句话理由>',
|
||||
].join('\n');
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析模型输出为三档 + 理由:
|
||||
* - 取最后一个匹配 `COMPLEXITY: hard|medium|easy` 的值(大小写/空白宽容);
|
||||
* - REASON 取第一行匹配;
|
||||
* - 解析不到合法档位 → 兜底 medium(理由说明原因,绝不抛错)。
|
||||
*/
|
||||
export function parseClassification(text: string): Classification {
|
||||
let complexity: Complexity | null = null;
|
||||
const re = /COMPLEXITY:\s*(hard|medium|easy)/gi;
|
||||
for (let m = re.exec(text); m !== null; m = re.exec(text)) {
|
||||
complexity = m[1].toLowerCase() as Complexity;
|
||||
}
|
||||
const rm = text.match(/REASON:\s*(.+)/i);
|
||||
const reason = rm ? rm[1].trim() : '';
|
||||
|
||||
if (!complexity || !isComplexity(complexity)) {
|
||||
return {
|
||||
complexity: FALLBACK,
|
||||
reason: reason
|
||||
? `无法解析复杂度档位,兜底 ${FALLBACK}(模型原话:${reason})`
|
||||
: `无法解析模型判定,兜底 ${FALLBACK}`,
|
||||
};
|
||||
}
|
||||
return { complexity, reason: reason || '(模型未给出理由)' };
|
||||
}
|
||||
|
||||
/**
|
||||
* 生产分类器:复用 cc.ts 起一次轻量 headless CC(sonnet 省额度),无工具、限轮限时。
|
||||
* 解析失败 / 超时 / 模型不可用 → 兜底 medium,永不抛错。
|
||||
*/
|
||||
export async function classifyComplexity(
|
||||
title: string,
|
||||
description: string | undefined,
|
||||
opts: { cwd: string },
|
||||
): Promise<Classification> {
|
||||
try {
|
||||
const cc = await runClaude({
|
||||
prompt: buildClassifyPrompt(title, description),
|
||||
cwd: opts.cwd,
|
||||
model: pickClassifierModel(),
|
||||
runId: `classify_${nanoid(10)}`,
|
||||
maxTurns: CLASSIFY_MAX_TURNS,
|
||||
timeoutMs: CLASSIFY_TIMEOUT_MS,
|
||||
permissionMode: 'default',
|
||||
allowedTools: [], // 纯文本判定,不需要任何工具
|
||||
});
|
||||
if (!cc.ok || !cc.finalText.trim()) {
|
||||
return { complexity: FALLBACK, reason: `模型未产出有效判定,兜底 ${FALLBACK}${cc.error ? `(${cc.error})` : ''}` };
|
||||
}
|
||||
return parseClassification(cc.finalText);
|
||||
} catch (e) {
|
||||
return { complexity: FALLBACK, reason: `分类调用异常,兜底 ${FALLBACK}:${(e as Error).message}` };
|
||||
}
|
||||
}
|
||||
@@ -45,6 +45,15 @@ export function resolvedExecutorModels(project: Project): Record<Complexity, str
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* AUTO 复杂度分类器用的模型:复用 executor easy 档(claude-sonnet,省额度),
|
||||
* env MAESTRO_MODEL_EASY 可覆盖。单次轻量调用,不走 project.model。
|
||||
*/
|
||||
export function pickClassifierModel(): string {
|
||||
const [env, fallback] = MODEL_TABLE.executor.easy;
|
||||
return process.env[env]?.trim() || fallback;
|
||||
}
|
||||
|
||||
/** 错误信息是否像“模型不可用”(not_found / invalid / permission 等模式 + 提到 model) */
|
||||
export function isModelError(msg: string): boolean {
|
||||
if (!/model/i.test(msg)) return false;
|
||||
|
||||
@@ -390,6 +390,40 @@ export class Store {
|
||||
return this.getTask(taskId)!;
|
||||
}
|
||||
|
||||
/**
|
||||
* AUTO 复杂度回填:模型判定后设置任务复杂度并记录判定理由(建任务时占位 medium,此处异步回填)。
|
||||
* - 判定档与占位不同且仍可改(COMPLEXITY_EDITABLE)→ 改复杂度 + 重置状态(同 patchTask 落位逻辑,含依赖);
|
||||
* - 理由写入该复杂度对应产出字段(hard→plan / medium→spec / easy→operations)的备注,仅在该字段为空时写,避免覆盖;
|
||||
* - 始终广播 task.updated(payload 带判定档与理由),便于看板/用户复核。
|
||||
*/
|
||||
applyAutoComplexity(taskId: string, complexity: Complexity, reason: string): Task {
|
||||
const row = this.getTaskRow(taskId);
|
||||
if (!row) throw new StoreError(`任务不存在: ${taskId}`);
|
||||
const cur = row.status as TaskStatus;
|
||||
|
||||
let statusChange: { from: TaskStatus; to: TaskStatus } | null = null;
|
||||
if (complexity !== row.complexity && COMPLEXITY_EDITABLE.has(cur)) {
|
||||
const to = this.resolveReady(row, initialNextStatus(complexity));
|
||||
this.db.prepare(`UPDATE tasks SET complexity = ?, status = ?, updated_at = ? WHERE id = ?`)
|
||||
.run(complexity, to, now(), taskId);
|
||||
if (to !== cur) statusChange = { from: cur, to };
|
||||
}
|
||||
|
||||
// 理由写入对应产出字段(仅当为空),便于用户与执行 agent 复核
|
||||
const field = complexity === 'hard' ? 'plan' : complexity === 'medium' ? 'spec' : 'operations';
|
||||
const fresh = this.getTaskRow(taskId)!;
|
||||
if (!fresh[field]) {
|
||||
const note = `> AUTO·模型判定复杂度:${complexity}\n> 理由:${reason}`;
|
||||
this.db.prepare(`UPDATE tasks SET ${field} = ?, updated_at = ? WHERE id = ?`).run(note, now(), taskId);
|
||||
}
|
||||
|
||||
this.emit(row.project_id, taskId, 'task.updated', { auto: 'classify', complexity, reason });
|
||||
if (statusChange) {
|
||||
this.emit(row.project_id, taskId, 'status.changed', { ...statusChange, reason: 'complexity.auto-classified' });
|
||||
}
|
||||
return this.getTask(taskId)!;
|
||||
}
|
||||
|
||||
// ---------- 旧 todo 来源映射(sync 引擎用) ----------
|
||||
setSourceRef(taskId: string, ref: string): void {
|
||||
const row = this.getTaskRow(taskId);
|
||||
|
||||
Reference in New Issue
Block a user