feat(cli): 分离 hose-aipro 独立命令体系并升级至 v0.9.0

1. 架构对称性与声明式注册表抽取:
   - 新增 src/cli/aipro-registry.ts,声明式定义 AIPRO_COMMAND_REGISTRY 及其别名映射与 mutating 突变检查
   - 重构 src/aipro-cli.ts,移除过程式 switch-case 分发,改为查表式声明分发,与 hose-use 的 CLI_COMMAND_REGISTRY 保持 100% 对称
   - 在 src/cli/registry.ts 统一重导出 aipro-registry,巩固其作为顶层声明注册表的单一事实源地位
   - 增加 aipro-cli 测试用例,强制校验 COMMAND_METADATA["hose-aipro"] 与声明式注册表双向一致性

2. 双 Skill 独立打包与内嵌部署:
   - 注册两个独立技能:skills/hose-use 与 skills/hose-aipro,互相关联引用
   - src/packaging/embedded-skill.ts 增加双 Skill 编译期嵌入与 installSkillFiles 全量同步
   - hose-use / hose-aipro 下的 skill status/dump/install 完全对齐

3. 守护服务交叉治理:
   - src/cli/service.ts 支持 ServiceTargetName ("hose-use" | "hose-aipro"),默认端口 8888 与 8889
   - 支持 hose-use aipro <start|stop|...> 与 hose-aipro hose-use <start|stop|...> 双向服务纳管
   - 误触非本领域业务命令时提供友好互导拦截

4. 审批领域数据层防御加固:
   - 迁移 004:approval_runs 增加 short_id 唯一约束
   - 增加 allocateUniqueShortId 冲突重试,多步写操作纳入事务

5. 版本升级至 v0.9.0:
   - package.json、README.md、specs、.zcode-plugin 及两套 Skill 同步更新至 0.9.0
   - bump-version 脚本适配多 Skill 自动递增
This commit is contained in:
gitea committed 2026-11-03 19:16:12 +08:00
1 parent c3e39fe77a
commit 362acfba58
38 files changed
+1380 -690

No files matched your search

+68 -44
View File
@@ -298,9 +298,25 @@ export class ApprovalRepository {
// ===================== 审批运行与要点清单 (Runs & Findings) =====================
/**
* 生成库内唯一的 6 位短 ID。
* 迁移层已用 UPPER(short_id) 唯一索引兜底,这里再显式探测重试,
* 避免极小概率碰撞直接把会话写到同一个短 ID 上。
*/
private allocateUniqueShortId(maxAttempts = 20): string {
for (let i = 0; i < maxAttempts; i++) {
const candidate = generateShortRunId();
const hit = this.db
.query("SELECT 1 FROM approval_runs WHERE UPPER(short_id) = UPPER(?) LIMIT 1")
.get(candidate);
if (!hit) return candidate;
}
throw new BusinessError("生成唯一审批会话短 ID 失败(连续碰撞),请稍后重试");
}
public createApprovalRun(input: CreateApprovalRunInput): ApprovalRunRecord {
const id = crypto.randomUUID();
const shortId = input.shortId ?? generateShortRunId();
const shortId = input.shortId ?? this.allocateUniqueShortId();
const now = Date.now();
const expiresAt = input.expiresAt ?? (now + 6 * 3600 * 1000); // 6 小时有效期
@@ -335,28 +351,31 @@ export class ApprovalRepository {
const existing = this.getLatestRunBySn(input.documentSn, input.corpId ?? undefined);
const now = Date.now();
if (existing) {
// 清理旧要点,重置运行记录
this.db.run("DELETE FROM approval_findings WHERE run_id = ?", [existing.id]);
this.db.run(
`
// 清理旧要点与重置运行记录必须原子完成,避免删了旧要点却没更新主记录
const reset = this.db.transaction(() => {
this.db.run("DELETE FROM approval_findings WHERE run_id = ?", [existing.id]);
this.db.run(
`
UPDATE approval_runs
SET expires_at = ?, flow_id = ?, corp_id = ?, scope = ?, decision = ?, decision_summary = ?, matched_rules_count = ?, failed_rules_count = ?, document_snapshot = ?, updated_at = ?
WHERE id = ?
`,
[
input.expiresAt ?? (now + 6 * 3600 * 1000),
(input.flowId !== undefined ? input.flowId : existing.flowId) ?? null,
(input.corpId !== undefined ? input.corpId : existing.corpId) ?? null,
(input.scope !== undefined ? input.scope : existing.scope) ?? null,
input.decision ?? "PENDING",
(input.decisionSummary !== undefined ? input.decisionSummary : null) ?? null,
input.matchedRulesCount ?? 0,
input.failedRulesCount ?? 0,
(input.documentSnapshot !== undefined ? input.documentSnapshot : existing.documentSnapshot) ?? null,
now,
existing.id,
]
);
[
input.expiresAt ?? (now + 6 * 3600 * 1000),
(input.flowId !== undefined ? input.flowId : existing.flowId) ?? null,
(input.corpId !== undefined ? input.corpId : existing.corpId) ?? null,
(input.scope !== undefined ? input.scope : existing.scope) ?? null,
input.decision ?? "PENDING",
(input.decisionSummary !== undefined ? input.decisionSummary : null) ?? null,
input.matchedRulesCount ?? 0,
input.failedRulesCount ?? 0,
(input.documentSnapshot !== undefined ? input.documentSnapshot : existing.documentSnapshot) ?? null,
now,
existing.id,
]
);
});
reset();
return this.getRunById(existing.id)!;
}
@@ -491,39 +510,44 @@ export class ApprovalRepository {
throw new NotFoundError(`审批运行记录不存在: ${runId}`);
}
// 校验必须在事务外先行完成:任何非法状态都不应触发旧要点被删除
for (const finding of findings) {
if (!isValidFindingCheckStatus(finding.checkStatus ?? "PENDING")) throw new ValidationError("核验状态必须为 PENDING、PASS、FAIL 或 SKIP");
}
// 重新挂载时清空旧要点
this.db.run("DELETE FROM approval_findings WHERE run_id = ?", [runId]);
const now = Date.now();
const insertStmt = this.db.prepare(`
INSERT INTO approval_findings (
id, run_id, rule_id, clause_text, strictness, check_status, evidence, reasoning, created_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
`);
for (const f of findings) {
const id = crypto.randomUUID();
insertStmt.run(
id,
runId,
f.ruleId ?? null,
f.clauseText.trim(),
f.strictness,
f.checkStatus ?? "PENDING",
f.evidence ?? null,
f.reasoning ?? null,
now
// 清空旧要点 + 批量写入 + 回写计数必须原子完成,避免中途失败留下半残清单
const persist = this.db.transaction(() => {
this.db.run("DELETE FROM approval_findings WHERE run_id = ?", [runId]);
const insertStmt = this.db.prepare(`
INSERT INTO approval_findings (
id, run_id, rule_id, clause_text, strictness, check_status, evidence, reasoning, created_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
`);
for (const f of findings) {
insertStmt.run(
crypto.randomUUID(),
runId,
f.ruleId ?? null,
f.clauseText.trim(),
f.strictness,
f.checkStatus ?? "PENDING",
f.evidence ?? null,
f.reasoning ?? null,
now
);
}
this.db.run(
"UPDATE approval_runs SET matched_rules_count = ?, updated_at = ? WHERE id = ?",
[findings.length, now, runId]
);
}
});
this.db.run(
"UPDATE approval_runs SET matched_rules_count = ?, updated_at = ? WHERE id = ?",
[findings.length, now, runId]
);
persist();
return this.getFindingsByRunId(runId);
}
+12 -4
View File
@@ -91,8 +91,16 @@ export class ApprovalService {
matchedSummary = allScopes.find((s) => /差旅|出差/i.test(s.scope));
}
// 默认回退至第一个场景
matchedSummary = matchedSummary ?? allScopes[0];
// 严禁静默回退到首个场景:用错规则集审单据(如差旅规则审办公费)比直接报错危险得多
if (!matchedSummary) {
const available = allScopes.map((s) => s.scope).join("、");
if (options.scope !== undefined) {
throw new NotFoundError(`指定场景 [${options.scope}] 不存在或无效,当前可用场景: ${available}`);
}
throw new ValidationError(
`无法为单据 [${documentSn}] 自动匹配适用场景,请通过 --scope 显式指定。当前可用场景: ${available}`
);
}
scopeDetail = this.repo.getScopeDetail(matchedSummary.index);
}
@@ -181,7 +189,7 @@ export class ApprovalService {
throw new ApprovalGatewayBlockedError(
`[审批链路阻断] 单据 [${documentSn}] 尚未注入任何审核要点清单(要点列表为空)。\n` +
`在没有注入审核要点的情况下,无法进行事实核验!\n` +
`请先执行: hose-use approval audit points ${documentSn} 注入审核要点清单。`
`请先执行: hose-aipro points ${documentSn} 注入审核要点清单。`
);
}
@@ -225,7 +233,7 @@ export class ApprovalService {
throw new ApprovalGatewayBlockedError(
`[审批链路阻断] 单据 [${documentSn}] 尚未注入任何明细级审核要点(要点列表为空)!\n` +
`网关卡点生效:审核要点未注入前,审批链路物理切断,绝对禁止进入审批决策!\n` +
`请先执行: hose-use approval audit points ${documentSn} 注入审核要点。`
`请先执行: hose-aipro points ${documentSn} 注入审核要点。`
);
}