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

@@ -0,0 +1,22 @@
# 2026-10-30 工作流 Schema 导出修复
## 问题与修复
`bun run export:workflow-schema` 原先在导出 `WorkflowDetailSchema.alarm` 时失败:Zod 默认导出解析后的输出结构,但 `alarm` 的字符串分支使用 transform 解析 JSON,无法自动推导函数结果。目录整理前后均可复现。
[导出脚本](../../scripts/generate/export-workflow-schema.ts) 现为详情建立仅用于导出的视图,以现有 `WorkflowAlarmSchema.nullable().optional()` 声明归一化后的告警类型。仍直接复用任务与工作流 Schema,移除 `as any`,使用 Zod 的类型化导出 API。没有修改业务域 Schema、服务端请求或运行时解析行为。
保持原生成文件的输出语义:数字字段已完成强制转换,带默认值的输出字段为必填;`alarm` 输出为告警对象、null 或缺省,输入的 JSON 字符串不是输出类型。未使用全局 `unrepresentable: "any"` 忽略无法表达的约束,新增不可表示类型仍会导致导出失败。
## 实际文件变化
- [export-workflow-schema.ts](../../scripts/generate/export-workflow-schema.ts):声明上述导出视图,恢复命令执行。
- [export-workflow-schema.test.ts](../../scripts/generate/export-workflow-schema.test.ts):在临时工作目录真实执行导出两次,验证可重现、与仓库生成文件一致、递归引用可解析、默认值与输出类型契约,以及运行时字符串/对象/null/无效 JSON 的解析行为。
- [workflow-schema.json](../../specs/workflow-schema.json):重新生成三个独立 Schema。除 `alarm` 补充 null 外,还反映当前代码已有但旧产物未同步的 `inputs`、`$snapshot`、动态延迟节点、字段映射和未知节点保留字段/默认值等结构。
- [README.scripts.md](../../scripts/README.scripts.md):用已修复后的输出语义说明替换当前故障提示;上一轮整理报告保留当时的历史验证结果。
## 验证与边界
实际执行导出成功;回归覆盖重复生成内容一致和三个子 Schema 的本地递归引用。隔离配置和持久化路径的全量测试为 **930 通过、0 失败,121 文件、4453 次断言**;`bun run check`、`bun run check:version` 和 `git diff --check` 通过。日志保存在 Git 忽略目录 `.drafts/workflow-schema-export-fix-20261030/`。
本次修复限于本地导出工具、测试与生成文件;没有执行企业数据操作,也没有把导出改为原始输入契约。JSON Schema 描述可序列化输出结构,不负责执行 Zod 的数据转换函数。
+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",
+322 -204
View File
@@ -101,36 +101,79 @@
}
},
"alarm": {
"type": "object",
"properties": {
"alarmUser": {
"default": [],
"type": "array",
"items": {}
"anyOf": [
{
"type": "object",
"properties": {
"alarmUser": {
"default": [],
"type": "array",
"items": {}
},
"alarmType": {
"default": [],
"type": "array",
"items": {
"type": "string"
}
},
"active": {
"default": true,
"type": "boolean"
},
"alarmScope": {
"default": "",
"type": "string"
}
},
"required": [
"alarmUser",
"alarmType",
"active",
"alarmScope"
],
"additionalProperties": false
},
"alarmType": {
"default": [],
"type": "array",
"items": {
{
"type": "null"
}
]
},
"inputs": {
"type": "array",
"items": {
"type": "object",
"properties": {
"key": {
"type": "string"
},
"value": {}
},
"required": [
"key",
"value"
],
"additionalProperties": false
}
},
"$snapshot": {
"type": "array",
"items": {
"type": "object",
"properties": {
"key": {
"type": "string"
},
"value": {
"type": "string"
}
},
"active": {
"default": true,
"type": "boolean"
},
"alarmScope": {
"default": "",
"type": "string"
}
},
"required": [
"alarmUser",
"alarmType",
"active",
"alarmScope"
],
"additionalProperties": false
"required": [
"key",
"value"
],
"additionalProperties": false
}
}
},
"required": [
@@ -148,7 +191,7 @@
"tasksVersion",
"tasks"
],
"additionalProperties": false,
"additionalProperties": {},
"$defs": {
"__schema0": {
"anyOf": [
@@ -768,36 +811,17 @@
"fields": {
"type": "array",
"items": {
"anyOf": [
{
"type": "object",
"properties": {
"key": {
"type": "string"
}
},
"required": [
"key"
],
"additionalProperties": false
"type": "object",
"properties": {
"key": {
"type": "string"
},
{
"type": "object",
"properties": {
"key": {
"type": "string"
},
"value": {
"type": "string"
}
},
"required": [
"key",
"value"
],
"additionalProperties": false
}
]
"value": {}
},
"required": [
"key"
],
"additionalProperties": {}
}
},
"filters": {
@@ -896,36 +920,17 @@
"fields": {
"type": "array",
"items": {
"anyOf": [
{
"type": "object",
"properties": {
"key": {
"type": "string"
}
},
"required": [
"key"
],
"additionalProperties": false
"type": "object",
"properties": {
"key": {
"type": "string"
},
{
"type": "object",
"properties": {
"key": {
"type": "string"
},
"value": {
"type": "string"
}
},
"required": [
"key",
"value"
],
"additionalProperties": false
}
]
"value": {}
},
"required": [
"key"
],
"additionalProperties": {}
}
},
"filters": {
@@ -1148,7 +1153,8 @@
"type": "string"
},
"type": {
"type": "string"
"type": "string",
"const": "task-dynamic-delay"
},
"label": {
"type": "string"
@@ -1171,6 +1177,68 @@
"status": {
"type": "string"
},
"displayName": {
"type": "string"
},
"sleepTime": {
"anyOf": [
{
"type": "number"
},
{
"type": "string"
}
]
}
},
"required": [
"name",
"type",
"label",
"groupName",
"orderIndex",
"levelIndex",
"status"
],
"additionalProperties": false
},
{
"type": "object",
"properties": {
"name": {
"default": "task",
"type": "string"
},
"type": {
"type": "string"
},
"label": {
"type": "string"
},
"desc": {
"type": "string"
},
"groupName": {
"default": "trigger",
"type": "string"
},
"orderIndex": {
"default": 0,
"type": "number"
},
"levelIndex": {
"default": [
0
],
"type": "array",
"items": {
"type": "number"
}
},
"status": {
"default": "active",
"type": "string"
},
"displayName": {
"type": "string"
}
@@ -1848,36 +1916,17 @@
"fields": {
"type": "array",
"items": {
"anyOf": [
{
"type": "object",
"properties": {
"key": {
"type": "string"
}
},
"required": [
"key"
],
"additionalProperties": false
"type": "object",
"properties": {
"key": {
"type": "string"
},
{
"type": "object",
"properties": {
"key": {
"type": "string"
},
"value": {
"type": "string"
}
},
"required": [
"key",
"value"
],
"additionalProperties": false
}
]
"value": {}
},
"required": [
"key"
],
"additionalProperties": {}
}
},
"filters": {
@@ -1976,36 +2025,17 @@
"fields": {
"type": "array",
"items": {
"anyOf": [
{
"type": "object",
"properties": {
"key": {
"type": "string"
}
},
"required": [
"key"
],
"additionalProperties": false
"type": "object",
"properties": {
"key": {
"type": "string"
},
{
"type": "object",
"properties": {
"key": {
"type": "string"
},
"value": {
"type": "string"
}
},
"required": [
"key",
"value"
],
"additionalProperties": false
}
]
"value": {}
},
"required": [
"key"
],
"additionalProperties": {}
}
},
"filters": {
@@ -2228,7 +2258,8 @@
"type": "string"
},
"type": {
"type": "string"
"type": "string",
"const": "task-dynamic-delay"
},
"label": {
"type": "string"
@@ -2251,6 +2282,68 @@
"status": {
"type": "string"
},
"displayName": {
"type": "string"
},
"sleepTime": {
"anyOf": [
{
"type": "number"
},
{
"type": "string"
}
]
}
},
"required": [
"name",
"type",
"label",
"groupName",
"orderIndex",
"levelIndex",
"status"
],
"additionalProperties": false
},
{
"type": "object",
"properties": {
"name": {
"default": "task",
"type": "string"
},
"type": {
"type": "string"
},
"label": {
"type": "string"
},
"desc": {
"type": "string"
},
"groupName": {
"default": "trigger",
"type": "string"
},
"orderIndex": {
"default": 0,
"type": "number"
},
"levelIndex": {
"default": [
0
],
"type": "array",
"items": {
"type": "number"
}
},
"status": {
"default": "active",
"type": "string"
},
"displayName": {
"type": "string"
}
@@ -3003,36 +3096,17 @@
"fields": {
"type": "array",
"items": {
"anyOf": [
{
"type": "object",
"properties": {
"key": {
"type": "string"
}
},
"required": [
"key"
],
"additionalProperties": false
"type": "object",
"properties": {
"key": {
"type": "string"
},
{
"type": "object",
"properties": {
"key": {
"type": "string"
},
"value": {
"type": "string"
}
},
"required": [
"key",
"value"
],
"additionalProperties": false
}
]
"value": {}
},
"required": [
"key"
],
"additionalProperties": {}
}
},
"filters": {
@@ -3131,36 +3205,17 @@
"fields": {
"type": "array",
"items": {
"anyOf": [
{
"type": "object",
"properties": {
"key": {
"type": "string"
}
},
"required": [
"key"
],
"additionalProperties": false
"type": "object",
"properties": {
"key": {
"type": "string"
},
{
"type": "object",
"properties": {
"key": {
"type": "string"
},
"value": {
"type": "string"
}
},
"required": [
"key",
"value"
],
"additionalProperties": false
}
]
"value": {}
},
"required": [
"key"
],
"additionalProperties": {}
}
},
"filters": {
@@ -3383,7 +3438,8 @@
"type": "string"
},
"type": {
"type": "string"
"type": "string",
"const": "task-dynamic-delay"
},
"label": {
"type": "string"
@@ -3406,6 +3462,68 @@
"status": {
"type": "string"
},
"displayName": {
"type": "string"
},
"sleepTime": {
"anyOf": [
{
"type": "number"
},
{
"type": "string"
}
]
}
},
"required": [
"name",
"type",
"label",
"groupName",
"orderIndex",
"levelIndex",
"status"
],
"additionalProperties": false
},
{
"type": "object",
"properties": {
"name": {
"default": "task",
"type": "string"
},
"type": {
"type": "string"
},
"label": {
"type": "string"
},
"desc": {
"type": "string"
},
"groupName": {
"default": "trigger",
"type": "string"
},
"orderIndex": {
"default": 0,
"type": "number"
},
"levelIndex": {
"default": [
0
],
"type": "array",
"items": {
"type": "number"
}
},
"status": {
"default": "active",
"type": "string"
},
"displayName": {
"type": "string"
}