fix: 恢复工作流 JSON Schema 导出

在导出视图中明确 alarm 转换后的可空对象结构,保留业务解析行为和输出契约。
重新生成 Schema,补齐真实导出、可重现性与递归引用回归,更新说明和修复报告。

验证:930 项测试通过,类型与版本检查通过,导出脚本及新增测试单独类型检查通过。
This commit is contained in:
gitea committed 2026-10-30 14:24:32 +08:00
1 parent c71b20c25b
commit 01fcebdb1c
5 files changed
+415 -210

No files matched your search

+5 -3
View File
@@ -28,7 +28,7 @@
| `bun run scripts/release/verify-release.ts <Tag>` | [verify-release.ts](./release/verify-release.ts) | 只读验证发布 Tag、当前提交、主分支祖先关系与版本 |
| `bun run openapi:bundle` | [bundle-openapi.ts](./generate/bundle-openapi.ts) | `specs/hose-open-api/` YAML → `specs/openapi.json` |
| `bun run openapi:generate-raw` | [generate-raw-openapi.ts](./generate/generate-raw-openapi.ts) | 生成 `specs/openapi.raw.json`;`openapi:all` 依次生成两份规范 |
| `bun run export:workflow-schema` | [export-workflow-schema.ts](./generate/export-workflow-schema.ts) | 从 Zod 导出 `specs/workflow-schema.json` |
| `bun run export:workflow-schema` | [export-workflow-schema.ts](./generate/export-workflow-schema.ts) | 从 Zod 导出归一化后的工作流结构到 `specs/workflow-schema.json` |
| `bun run dev:status` / `dev:check` | [dev-hygiene.ts](./dev/dev-hygiene.ts) | 检查开发环境、安装与技能同步状态 |
| `bun run dev:clean` | [dev-hygiene.ts](./dev/dev-hygiene.ts) | 默认给出清理计划;执行清理前检查参数与计划 |
| `bun run dev:override` | [dev-override.ts](./dev/dev-override.ts) | 构建并覆盖本机全局命令,同步个人 `~/.agents/skills/hose-use`;`dev:link` 为同一入口 |
@@ -44,6 +44,8 @@
- 不在本目录保存业务工作流脚本、客户载荷或临时生成文件;产品 Skill 中的 `./scripts/*.mvel` 等是调用者工作目录示例。
- 提交前执行 `bun test`、`bun run check`、`bun run check:version`;构建相关调整还需验证二进制和插件的实际产物。
## 已知限制
## 工作流 Schema 的语义
`export:workflow-schema` 当前会因业务 Zod Schema 包含 transform 而报 `Transforms cannot be represented in JSON Schema`。目录迁移前的脚本也能复现;本轮保留导出逻辑和既有生成文件,后续需明确导出输入或输出结构后单独修复。
`workflow-schema.json` 的 `schemas` 分别保存 WorkflowDetail、WorkflowTask 和 Workflow 的独立 JSON Schema,描述 Zod 解析完成后的输出,不是服务端原始输入;每份 Schema 的递归引用相对于该份 Schema 解析。带默认值的输出字段保持必填,数字转换后仍为 number。
`alarm` 可从 JSON 字符串归一化为告警对象,也可为 null 或缺省。导出视图显式使用同一个 `WorkflowAlarmSchema` 描述对象输出;不会尝试把字符串解析函数转换为 JSON Schema,也不会改动域层的输入兼容和解析行为。新增其他无法表示的转换时仍会报错,避免默默丢失约束。
@@ -0,0 +1,56 @@
import { expect, test } from "bun:test";
import { spawnSync } from "node:child_process";
import { mkdtempSync, readFileSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join, resolve } from "node:path";
import { WorkflowDetailSchema } from "../../src/domains/platform/types.js";
test("工作流 Schema 导出可重现,保留归一化输出与有效递归引用", () => {
const dir = mkdtempSync(join(tmpdir(), "hose-workflow-schema-"));
try {
const script = resolve(import.meta.dir, "export-workflow-schema.ts");
const generate = () => {
const result = spawnSync(process.execPath, [script], { cwd: dir, encoding: "utf8" });
expect(result.status, result.stderr).toBe(0);
return readFileSync(join(dir, "specs/workflow-schema.json"), "utf8");
};
const first = generate();
expect(generate()).toBe(first);
expect(first).toBe(readFileSync(resolve(import.meta.dir, "../../specs/workflow-schema.json"), "utf8"));
const document = JSON.parse(first);
expect(Object.keys(document.schemas)).toEqual(["WorkflowDetail", "WorkflowTask", "Workflow"]);
const detail = document.schemas.WorkflowDetail;
expect(detail.properties.state.type).toBe("number");
expect(detail.required).toContain("tasks");
expect(detail.required).toContain("state");
expect(detail.required).not.toContain("alarm");
const alarm = detail.properties.alarm.anyOf;
expect(alarm.map((branch: { type: string }) => branch.type)).toEqual(["object", "null"]);
expect(alarm[0].properties.active.type).toBe("boolean");
expect(alarm[0].required).toContain("alarmUser");
for (const input of [{ active: false }, '{"active":false}']) {
const normalized = WorkflowDetailSchema.parse({ id: "fixture", state: "1", alarm: input });
expect(normalized.state).toBe(1);
expect(normalized.alarm).toEqual({ active: false, alarmUser: [], alarmType: [], alarmScope: "" });
}
expect(WorkflowDetailSchema.parse({ id: "fixture", alarm: null }).alarm).toBeNull();
expect(WorkflowDetailSchema.parse({ id: "fixture", alarm: "invalid JSON" }).alarm).toBeUndefined();
for (const schema of Object.values(document.schemas)) {
let refs = 0;
const visit = (node: any): void => {
if (!node || typeof node !== "object") return;
if (typeof node.$ref === "string") {
expect(node.$ref === "#" || node.$ref.startsWith("#/")).toBe(true);
const target = node.$ref === "#" ? schema : node.$ref.slice(2).split("/").reduce((value: any, key: string) => value?.[key.replace(/~1/g, "/").replace(/~0/g, "~")], schema);
expect(target).toBeDefined();
refs++;
}
Object.values(node).forEach(visit);
};
visit(schema);
expect(refs).toBeGreaterThan(0);
}
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
+10 -3
View File
@@ -1,6 +1,8 @@
import { writeFileSync, mkdirSync } from "fs";
import { join, dirname } from "path";
import { z } from "zod";
import {
WorkflowAlarmSchema,
WorkflowDetailSchema,
WorkflowTaskSchema,
WorkflowSchema,
@@ -14,9 +16,14 @@ function writeJson(path: string, data: unknown): void {
writeFileSync(path, JSON.stringify(data, null, 2));
}
const workflowDetailJsonSchema = (WorkflowDetailSchema as any).toJSONSchema();
const workflowTaskJsonSchema = (WorkflowTaskSchema as any).toJSONSchema();
const workflowJsonSchema = (WorkflowSchema as any).toJSONSchema();
// 导出运行时归一化后的结构:alarm 的 JSON 字符串已解析为对象或 undefined。
// JSON Schema 无法推导 transform 函数,因而只在导出视图中声明其输出,保留域层解析逻辑。
const workflowDetailOutputSchema = WorkflowDetailSchema.extend({
alarm: WorkflowAlarmSchema.nullable().optional(),
});
const workflowDetailJsonSchema = z.toJSONSchema(workflowDetailOutputSchema);
const workflowTaskJsonSchema = z.toJSONSchema(WorkflowTaskSchema);
const workflowJsonSchema = z.toJSONSchema(WorkflowSchema);
writeJson(OUTPUT_FILE, {
$schema: "https://json-schema.org/draft/2020-12/schema",