@arcticdata/dsh-datarc-skills 0.0.0-stage → 26.39.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -2
- package/cordis.patch.yml +4 -0
- package/icon.svg +4 -0
- package/index.js +35 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +37 -4
- package/skills/datarc-access-permissions/SKILL.md +30 -0
- package/skills/datarc-access-permissions/references/api.md +31 -0
- package/skills/datarc-access-permissions/references/write-order.md +16 -0
- package/skills/datarc-analysis-report/SKILL.md +125 -0
- package/skills/datarc-analysis-report/agents/openai.yaml +6 -0
- package/skills/datarc-analysis-report/references/analyst-frameworks.md +215 -0
- package/skills/datarc-analysis-report/references/api-contracts.md +478 -0
- package/skills/datarc-analysis-report/references/direct-card-fallback-design.md +66 -0
- package/skills/datarc-analysis-report/references/human-in-the-loop.md +142 -0
- package/skills/datarc-analysis-report/references/loop-personalization-policy.md +138 -0
- package/skills/datarc-analysis-report/references/manifest-example.json +779 -0
- package/skills/datarc-analysis-report/references/narrative-schema.md +160 -0
- package/skills/datarc-analysis-report/references/question-syntax.md +151 -0
- package/skills/datarc-analysis-report/scripts/run_analysis_report.py +2225 -0
- package/skills/datarc-analysis-report/scripts/run_dataseek_cards.py +1914 -0
- package/skills/datarc-analysis-report/scripts/test_run_analysis_report.py +5295 -0
- package/skills/datarc-data-search/SKILL.md +243 -0
- package/skills/datarc-data-search/scripts/calculate.py +116 -0
- package/skills/datarc-data-search/scripts/data-search.mjs +628 -0
- package/skills/datarc-data-search/scripts/datarc-client.mjs +464 -0
- package/skills/datarc-data-search/scripts/search-result.mjs +211 -0
- package/skills/datarc-dataseek-batch-evaluation/SKILL.md +59 -0
- package/skills/datarc-dataseek-batch-evaluation/references/artifacts.md +36 -0
- package/skills/datarc-dataseek-batch-evaluation/references/equivalence.md +30 -0
- package/skills/datarc-dataseek-batch-evaluation/scripts/datarc-client.mjs +464 -0
- package/skills/datarc-dataseek-batch-evaluation/scripts/dataseek-batch-evaluation.mjs +1020 -0
- package/skills/datarc-dataseek-search/SKILL.md +96 -0
- package/skills/datarc-dataseek-search/scripts/datarc-client.mjs +464 -0
- package/skills/datarc-dataseek-search/scripts/dataseek-search.mjs +257 -0
- package/skills/datarc-dataseek-semantic-debugging/SKILL.md +44 -0
- package/skills/datarc-dataseek-semantic-debugging/agents/openai.yaml +4 -0
- package/skills/datarc-dataseek-semantic-debugging/evals/evals.json +165 -0
- package/skills/datarc-dataseek-semantic-debugging/references/dataset.md +77 -0
- package/skills/datarc-dataseek-semantic-debugging/references/input-and-resolution.md +93 -0
- package/skills/datarc-dataseek-semantic-debugging/references/interventions.md +64 -0
- package/skills/datarc-dataseek-semantic-debugging/references/knowledge-and-prompts.md +47 -0
- package/skills/datarc-dataseek-semantic-debugging/references/rollback.md +11 -0
- package/skills/datarc-dataseek-semantic-debugging/references/verification-and-rollback.md +95 -0
- package/skills/datarc-dataseek-semantic-debugging/scripts/semantic-debugging.mjs +29 -0
- package/skills/datarc-dataseek-testset-generation/SKILL.md +47 -0
- package/skills/datarc-dataseek-testset-generation/scripts/datarc-client.mjs +464 -0
- package/skills/datarc-dataseek-testset-generation/scripts/dataseek-testset-generation.mjs +526 -0
- package/skills/datarc-datasource/SKILL.md +97 -0
- package/skills/datarc-oauth/SKILL.md +47 -0
- package/skills/datarc-oauth/scripts/auth.mjs +130 -0
- package/skills/datarc-oauth/scripts/credentials.mjs +237 -0
- package/skills/datarc-report-add-card/SKILL.md +90 -0
- package/skills/datarc-report-add-card/agents/openai.yaml +4 -0
- package/skills/datarc-report-add-card/scripts/datarc-client.mjs +464 -0
- package/skills/datarc-report-add-card/scripts/report-card.mjs +439 -0
- package/skills/datarc-semantic-alias/SKILL.md +20 -0
- package/skills/datarc-semantic-alias/scripts/alias-plan.mjs +422 -0
- package/skills/datarc-semantic-alias/scripts/alias.mjs +11 -0
- package/skills/datarc-semantic-calibration/SKILL.md +20 -0
- package/skills/datarc-semantic-calibration/scripts/calibration-plan.mjs +683 -0
- package/skills/datarc-semantic-calibration/scripts/calibration.mjs +11 -0
- package/skills/datarc-semantic-core/SKILL.md +12 -0
- package/skills/datarc-semantic-core/scripts/artifact-journal.mjs +830 -0
- package/skills/datarc-semantic-core/scripts/canonical.mjs +48 -0
- package/skills/datarc-semantic-core/scripts/case-model.mjs +608 -0
- package/skills/datarc-semantic-core/scripts/cli.mjs +180 -0
- package/skills/datarc-semantic-core/scripts/datarc-client.mjs +180 -0
- package/skills/datarc-semantic-core/scripts/interpretation-pairs.mjs +57 -0
- package/skills/datarc-semantic-core/scripts/interventions.mjs +693 -0
- package/skills/datarc-semantic-core/scripts/live-resolution.mjs +305 -0
- package/skills/datarc-semantic-core/scripts/model-scope.mjs +49 -0
- package/skills/datarc-semantic-core/scripts/reproduction.mjs +330 -0
- package/skills/datarc-semantic-core/scripts/semantic-cli.mjs +740 -0
- package/skills/datarc-semantic-core/scripts/shared.mjs +312 -0
- package/skills/datarc-semantic-core/scripts/verification.mjs +1014 -0
- package/skills/datarc-semantic-dataset/SKILL.md +20 -0
- package/skills/datarc-semantic-dataset/scripts/dataset-plan.mjs +749 -0
- package/skills/datarc-semantic-dataset/scripts/dataset-preflight.mjs +813 -0
- package/skills/datarc-semantic-dataset/scripts/dataset.mjs +11 -0
- package/skills/datarc-semantic-knowledge/SKILL.md +38 -0
- package/skills/datarc-semantic-knowledge/references/knowledge-drafts.md +61 -0
- package/skills/datarc-semantic-knowledge/references/knowledge-generation.json +16 -0
- package/skills/datarc-semantic-knowledge/scripts/knowledge-draft.mjs +347 -0
- package/skills/datarc-semantic-knowledge/scripts/knowledge-plan.mjs +848 -0
- package/skills/datarc-semantic-knowledge/scripts/knowledge.mjs +31 -0
- package/skills/datarc-semantic-prompt/SKILL.md +20 -0
- package/skills/datarc-semantic-prompt/scripts/prompt-plan.mjs +447 -0
- package/skills/datarc-semantic-prompt/scripts/prompt.mjs +15 -0
- package/skills/datarc-semantic-synonym/SKILL.md +20 -0
- package/skills/datarc-semantic-synonym/scripts/synonym-plan.mjs +688 -0
- package/skills/datarc-semantic-synonym/scripts/synonym.mjs +11 -0
- package/skills/datarc-viz-analysis-report/SKILL.md +108 -0
- package/skills/datarc-viz-analysis-report/assets/LICENSE-echarts.md +8 -0
- package/skills/datarc-viz-analysis-report/assets/echarts.min.js +45 -0
- package/skills/datarc-viz-analysis-report/references/api-contracts.md +163 -0
- package/skills/datarc-viz-analysis-report/references/derivation-log.md +157 -0
- package/skills/datarc-viz-analysis-report/references/evidence-framework.md +76 -0
- package/skills/datarc-viz-analysis-report/references/html-template.md +92 -0
- package/skills/datarc-viz-analysis-report/references/provenance-format.md +56 -0
- package/skills/datarc-viz-analysis-report/references/question-syntax.md +151 -0
- package/skills/datarc-viz-analysis-report/scripts/render_viz_report.py +1708 -0
- package/skills/datarc-viz-analysis-report/scripts/test_render_viz_report.py +964 -0
- package/skills/datarc-workspace-configuration/SKILL.md +59 -0
- package/skills/datarc-workspace-configuration/references/artifacts.md +75 -0
- package/skills/datarc-workspace-configuration/references/verification.md +20 -0
- package/skills/datarc-workspace-configuration/scripts/configuration.mjs +662 -0
- package/skills/datarc-workspace-context/SKILL.md +43 -0
- package/skills/datarc-workspace-context/agents/openai.yaml +4 -0
- package/skills/datarc-workspace-context/scripts/datarc-client.mjs +31 -0
- package/skills/datarc-workspace-context/scripts/query-display.mjs +114 -0
- package/skills/datarc-workspace-context/scripts/workspace-context.mjs +58 -0
- package/skills/datarc-workspace-custom-tables/SKILL.md +89 -0
- package/skills/datarc-workspace-custom-tables/scripts/datarc-client.mjs +165 -0
- package/skills/datarc-workspace-custom-tables/scripts/workspace-custom-tables.mjs +601 -0
- package/skills/datarc-workspace-keywords/SKILL.md +197 -0
- package/skills/datarc-workspace-keywords/agents/openai.yaml +4 -0
- package/skills/datarc-workspace-keywords/references/api-fields.md +79 -0
- package/skills/datarc-workspace-keywords/references/derived-measures.md +177 -0
- package/skills/datarc-workspace-keywords/scripts/datarc-client.mjs +145 -0
- package/skills/datarc-workspace-keywords/scripts/workspace-keywords.mjs +2421 -0
- package/skills/datarc-workspace-membership/SKILL.md +29 -0
- package/skills/datarc-workspace-membership/references/api.md +19 -0
- package/skills/datarc-workspace-modeling/SKILL.md +209 -0
- package/skills/datarc-workspace-modeling/agents/openai.yaml +4 -0
- package/skills/datarc-workspace-modeling/references/join-preview.md +38 -0
- package/skills/datarc-workspace-modeling/references/model-plan.md +300 -0
- package/skills/datarc-workspace-modeling/references/model-tags.md +95 -0
- package/skills/datarc-workspace-modeling/references/modeling-contract.json +29 -0
- package/skills/datarc-workspace-modeling/references/modeling-fixtures.json +2423 -0
- package/skills/datarc-workspace-modeling/scripts/datarc-client.mjs +137 -0
- package/skills/datarc-workspace-modeling/scripts/workspace-model-tags.mjs +238 -0
- package/skills/datarc-workspace-modeling/scripts/workspace-modeling.mjs +1261 -0
- package/skills-index.json +194 -0
|
@@ -0,0 +1,662 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { resolveCredentials } from "../../datarc-oauth/scripts/credentials.mjs";
|
|
3
|
+
import { createHash } from "node:crypto";
|
|
4
|
+
import { spawn } from "node:child_process";
|
|
5
|
+
import { readFile, writeFile, mkdir, rename } from "node:fs/promises";
|
|
6
|
+
import { resolve, join } from "node:path";
|
|
7
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
8
|
+
import { isDeepStrictEqual } from "node:util";
|
|
9
|
+
|
|
10
|
+
const HELP = `configuration.mjs plan --input draft.json --output <new-directory>
|
|
11
|
+
configuration.mjs verify --plan <plan.json> --confirm <planHash> --output <new-directory>
|
|
12
|
+
plan generates plan.json and plan.md without network access.
|
|
13
|
+
verify runs approved query cases, then writes acceptance.json/md and evidence.
|
|
14
|
+
It does not modify workspace configuration or certify configuration readback.`;
|
|
15
|
+
const executors = {
|
|
16
|
+
"data-search": ["datarc-data-search", "data-search.mjs", "search"],
|
|
17
|
+
"dataseek-search": ["datarc-dataseek-search", "dataseek-search.mjs", "ask"],
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export function digest(value) {
|
|
21
|
+
return createHash("sha256")
|
|
22
|
+
.update(JSON.stringify(canonical(value)))
|
|
23
|
+
.digest("hex");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function validatePlan(plan) {
|
|
27
|
+
ensure(
|
|
28
|
+
plan?.schemaVersion === 1,
|
|
29
|
+
"方案版本无效;请按 schemaVersion=1 重新生成",
|
|
30
|
+
);
|
|
31
|
+
for (const key of ["taskId", "goal", "identity", "timeRange"])
|
|
32
|
+
ensure(text(plan[key]), `缺少 ${key};请补齐方案后重试`);
|
|
33
|
+
ensure(
|
|
34
|
+
Number.isSafeInteger(plan.workspaceId) && plan.workspaceId > 0,
|
|
35
|
+
"工作区 ID 无效;请先唯一定位工作区",
|
|
36
|
+
);
|
|
37
|
+
ensure(typeof plan.endpoint === "string", "缺少服务地址;请补齐 endpoint");
|
|
38
|
+
const url = new URL(plan.endpoint);
|
|
39
|
+
ensure(
|
|
40
|
+
["http:", "https:"].includes(url.protocol) &&
|
|
41
|
+
!url.username &&
|
|
42
|
+
!url.password &&
|
|
43
|
+
!url.search &&
|
|
44
|
+
!url.hash,
|
|
45
|
+
"服务地址不应包含凭据、查询或片段;请填写干净的服务入口",
|
|
46
|
+
);
|
|
47
|
+
ensure(
|
|
48
|
+
Array.isArray(plan.changes) &&
|
|
49
|
+
plan.changes.length > 0 &&
|
|
50
|
+
plan.changes.every(
|
|
51
|
+
(c) => text(c.target) && text(c.change) && text(c.impact),
|
|
52
|
+
),
|
|
53
|
+
"请为每项变更填写 target、change 和 impact",
|
|
54
|
+
);
|
|
55
|
+
ensure(
|
|
56
|
+
Array.isArray(plan.questions) && plan.questions.every(text),
|
|
57
|
+
"questions 应为待澄清问题列表;无问题时填写空数组",
|
|
58
|
+
);
|
|
59
|
+
ensure(
|
|
60
|
+
Array.isArray(plan.cases) && plan.cases.length <= 20,
|
|
61
|
+
"cases 应为最多 20 个验收问题;请缩小本次范围",
|
|
62
|
+
);
|
|
63
|
+
ensure(
|
|
64
|
+
plan.cases.length || text(plan.noQueryReason),
|
|
65
|
+
"无查询用例时请填写 noQueryReason,说明为何只需配置回读",
|
|
66
|
+
);
|
|
67
|
+
const ids = new Set();
|
|
68
|
+
for (const c of plan.cases) {
|
|
69
|
+
ensure(
|
|
70
|
+
text(c.id) &&
|
|
71
|
+
/^[a-zA-Z0-9_-]+$/.test(c.id) &&
|
|
72
|
+
!ids.has(c.id.toLowerCase()),
|
|
73
|
+
"验收编号无效或重复(忽略大小写);请使用字母、数字、下划线或短横线,并修改冲突编号,不能仅改变大小写",
|
|
74
|
+
);
|
|
75
|
+
ids.add(c.id.toLowerCase());
|
|
76
|
+
ensure(
|
|
77
|
+
text(c.question) && !c.question.startsWith("--"),
|
|
78
|
+
"验收问题为空或格式无效;请填写原始业务问题",
|
|
79
|
+
);
|
|
80
|
+
ensure(
|
|
81
|
+
Object.hasOwn(executors, c.executor),
|
|
82
|
+
"验收执行器不支持;请选择 data-search 或 dataseek-search",
|
|
83
|
+
);
|
|
84
|
+
ensure(
|
|
85
|
+
typeof c.required === "boolean" && typeof c.reviewRequired === "boolean",
|
|
86
|
+
"请明确 required 和 reviewRequired 布尔值",
|
|
87
|
+
);
|
|
88
|
+
ensure(
|
|
89
|
+
c.executor !== "dataseek-search" || c.reviewRequired,
|
|
90
|
+
"DataSeek 原问题需核对理解及范围;请设置 reviewRequired=true",
|
|
91
|
+
);
|
|
92
|
+
ensure(
|
|
93
|
+
Array.isArray(c.modelUids) &&
|
|
94
|
+
c.modelUids.every(
|
|
95
|
+
(v) =>
|
|
96
|
+
typeof v === "string" &&
|
|
97
|
+
/^(?:[a-f0-9]{32}|[a-f0-9-]{36})$/i.test(v),
|
|
98
|
+
),
|
|
99
|
+
"modelUids 无效;请填写实际模型 UID 列表,无限定时为空",
|
|
100
|
+
);
|
|
101
|
+
ensure(
|
|
102
|
+
c.executor !== "dataseek-search" || !c.modelUids.length,
|
|
103
|
+
"DataSeek 入口不支持强制模型参数;请在问题和人工复核中核对范围,勿伪造模型限制",
|
|
104
|
+
);
|
|
105
|
+
ensure(
|
|
106
|
+
Array.isArray(c.assertions),
|
|
107
|
+
"assertions 应为列表;缺少可靠预期时填写空数组,结果将待确认",
|
|
108
|
+
);
|
|
109
|
+
ensure(
|
|
110
|
+
!c.assertions.length || text(c.expectedSource),
|
|
111
|
+
"请说明预期依据 expectedSource,不能使用本次查询结果自证正确",
|
|
112
|
+
);
|
|
113
|
+
for (const a of c.assertions) {
|
|
114
|
+
ensure(
|
|
115
|
+
typeof a.pointer === "string" &&
|
|
116
|
+
/^\/(?:[^~]|~[01])*$/.test(a.pointer) &&
|
|
117
|
+
/^\/data\/.*\/previews\/\d+\/data(?:\/|$)/.test(a.pointer),
|
|
118
|
+
"断言路径应为预览业务数据的 JSON Pointer;请定位 previews 中的 data,不能只检查技术成功字段",
|
|
119
|
+
);
|
|
120
|
+
ensure(Object.hasOwn(a, "equals"), "缺少 equals 预期值;请补充可信基准");
|
|
121
|
+
if (a.tolerance !== undefined)
|
|
122
|
+
ensure(
|
|
123
|
+
typeof a.equals === "number" &&
|
|
124
|
+
Number.isFinite(a.equals) &&
|
|
125
|
+
Number.isFinite(a.tolerance) &&
|
|
126
|
+
a.tolerance >= 0,
|
|
127
|
+
"数值容差无效;请填写非负有限数值",
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return plan;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export async function generatePlan(draft, output) {
|
|
135
|
+
validatePlan(draft);
|
|
136
|
+
ensure(
|
|
137
|
+
!Object.hasOwn(draft, "planHash"),
|
|
138
|
+
"输入已含 planHash;请从原始草案生成新版本,不要手工覆盖摘要",
|
|
139
|
+
);
|
|
140
|
+
const sealed = { ...draft, planHash: digest(draft) };
|
|
141
|
+
await mkdir(output, { recursive: false, mode: 0o700 });
|
|
142
|
+
await save(join(output, "plan.json"), sealed);
|
|
143
|
+
await writeFile(join(output, "plan.md"), renderPlan(sealed), {
|
|
144
|
+
flag: "wx",
|
|
145
|
+
mode: 0o600,
|
|
146
|
+
});
|
|
147
|
+
return {
|
|
148
|
+
planHash: sealed.planHash,
|
|
149
|
+
document: join(output, "plan.md"),
|
|
150
|
+
blocked: draft.questions.length > 0,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export function assess(c, raw) {
|
|
155
|
+
if (raw?.ok !== true)
|
|
156
|
+
return {
|
|
157
|
+
status: "error",
|
|
158
|
+
reason: raw?.error?.backgroundMayStillRun
|
|
159
|
+
? "本地等待已停止,后台查询可能仍在运行;请先核查任务状态,不要立即重复查询"
|
|
160
|
+
: "查询执行失败;请检查证据中的错误并处理后重试",
|
|
161
|
+
};
|
|
162
|
+
const health = queryHealth(c.executor, raw.data);
|
|
163
|
+
if (health) return health;
|
|
164
|
+
const comparisons = c.assertions.map((a) => {
|
|
165
|
+
const actual = pointer(raw, a.pointer);
|
|
166
|
+
const passed =
|
|
167
|
+
actual !== undefined &&
|
|
168
|
+
(a.tolerance === undefined
|
|
169
|
+
? isDeepStrictEqual(actual, a.equals)
|
|
170
|
+
: typeof actual === "number" &&
|
|
171
|
+
Number.isFinite(actual) &&
|
|
172
|
+
Math.abs(actual - a.equals) <= a.tolerance);
|
|
173
|
+
return {
|
|
174
|
+
pointer: a.pointer,
|
|
175
|
+
matched: passed,
|
|
176
|
+
missing: actual === undefined,
|
|
177
|
+
expected: a.equals,
|
|
178
|
+
...(actual === undefined ? {} : { actual }),
|
|
179
|
+
};
|
|
180
|
+
});
|
|
181
|
+
if (comparisons.some((a) => a.missing))
|
|
182
|
+
return {
|
|
183
|
+
status: "error",
|
|
184
|
+
reason: "证据字段缺失;请检查结果结构与方案路径,变更方案后重新确认",
|
|
185
|
+
comparisons,
|
|
186
|
+
};
|
|
187
|
+
if (comparisons.some((a) => !a.matched))
|
|
188
|
+
return {
|
|
189
|
+
status: "fail",
|
|
190
|
+
reason: "实际结果与方案预期不符;请核对业务口径和数据",
|
|
191
|
+
comparisons,
|
|
192
|
+
};
|
|
193
|
+
if (!comparisons.length || c.reviewRequired)
|
|
194
|
+
return {
|
|
195
|
+
status: "review",
|
|
196
|
+
reason: "查询已完成;请核对业务预期、理解和实际模型范围",
|
|
197
|
+
comparisons,
|
|
198
|
+
};
|
|
199
|
+
return {
|
|
200
|
+
status: "pass",
|
|
201
|
+
reason: "查询完整,所列断言符合预期;仅覆盖方案中的检查项",
|
|
202
|
+
comparisons,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export async function verifyPlan(sealed, confirmation, output, options = {}) {
|
|
207
|
+
const { planHash, ...plan } = sealed;
|
|
208
|
+
validatePlan(plan);
|
|
209
|
+
ensure(
|
|
210
|
+
planHash === digest(plan) && confirmation === planHash,
|
|
211
|
+
"方案已变化或确认不匹配;请重新生成文档并确认当前版本",
|
|
212
|
+
);
|
|
213
|
+
ensure(!plan.questions.length, "方案仍有待澄清问题;请先补齐需求并重新确认");
|
|
214
|
+
const { endpoint, token } = resolveCredentials(options);
|
|
215
|
+
ensure(
|
|
216
|
+
normalizeEndpoint(endpoint) === normalizeEndpoint(plan.endpoint),
|
|
217
|
+
"当前服务与方案不一致;请核对 DATARC_ENDPOINT,不要切换到其他工作区执行",
|
|
218
|
+
);
|
|
219
|
+
if (!options.run && plan.cases.length)
|
|
220
|
+
ensure(
|
|
221
|
+
token,
|
|
222
|
+
"缺少 DATARC_TOKEN;请先按 datarc-oauth 完成认证",
|
|
223
|
+
);
|
|
224
|
+
await mkdir(output, { recursive: false, mode: 0o700 });
|
|
225
|
+
const run = options.run ?? runQuery;
|
|
226
|
+
const now = options.now ?? Date.now;
|
|
227
|
+
const deadline = now() + 30 * 60 * 1000;
|
|
228
|
+
const report = {
|
|
229
|
+
taskId: plan.taskId,
|
|
230
|
+
planHash,
|
|
231
|
+
workspaceId: plan.workspaceId,
|
|
232
|
+
endpoint: plan.endpoint,
|
|
233
|
+
identity: plan.identity,
|
|
234
|
+
identitySource: "caller-declared",
|
|
235
|
+
timeRange: plan.timeRange,
|
|
236
|
+
startedAt: new Date().toISOString(),
|
|
237
|
+
configurationStatus: "not_assessed",
|
|
238
|
+
userAcceptance: "pending",
|
|
239
|
+
results: [],
|
|
240
|
+
};
|
|
241
|
+
const persist = async () => {
|
|
242
|
+
report.counts = Object.fromEntries(
|
|
243
|
+
["pass", "fail", "error", "review", "not_run", "running"].map((s) => [
|
|
244
|
+
s,
|
|
245
|
+
report.results.filter((r) => r.status === s).length,
|
|
246
|
+
]),
|
|
247
|
+
);
|
|
248
|
+
const required = report.results.filter((r) => r.required);
|
|
249
|
+
report.status = !plan.cases.length
|
|
250
|
+
? "not_applicable"
|
|
251
|
+
: required.some((r) => ["fail", "error"].includes(r.status))
|
|
252
|
+
? "failed"
|
|
253
|
+
: required.some((r) => r.status !== "pass") ||
|
|
254
|
+
report.results.length !== plan.cases.length
|
|
255
|
+
? "incomplete"
|
|
256
|
+
: required.length
|
|
257
|
+
? "passed"
|
|
258
|
+
: "review";
|
|
259
|
+
// Always write JSON first so an interrupted render still leaves evidence.
|
|
260
|
+
await save(join(output, "acceptance.json"), report);
|
|
261
|
+
await writeFile(join(output, "acceptance.md"), renderReport(plan, report), {
|
|
262
|
+
mode: 0o600,
|
|
263
|
+
});
|
|
264
|
+
};
|
|
265
|
+
report.results = plan.cases.map((c) => ({
|
|
266
|
+
id: c.id,
|
|
267
|
+
question: c.question,
|
|
268
|
+
required: c.required,
|
|
269
|
+
status: "not_run",
|
|
270
|
+
reason: "尚未运行;请检查执行记录",
|
|
271
|
+
}));
|
|
272
|
+
await persist();
|
|
273
|
+
for (const [index, c] of plan.cases.entries()) {
|
|
274
|
+
if (now() >= deadline) {
|
|
275
|
+
report.results[index].reason =
|
|
276
|
+
"总时限已到;请检查已完成结果,再决定是否重新验收";
|
|
277
|
+
continue;
|
|
278
|
+
}
|
|
279
|
+
let raw;
|
|
280
|
+
report.results[index] = {
|
|
281
|
+
...report.results[index],
|
|
282
|
+
status: "running",
|
|
283
|
+
reason: "查询执行中;若进程已中断,后台可能仍在运行,请先核查任务再补跑",
|
|
284
|
+
};
|
|
285
|
+
await persist();
|
|
286
|
+
try {
|
|
287
|
+
raw = await run(plan, c, Math.min(300000, deadline - now()));
|
|
288
|
+
} catch {
|
|
289
|
+
raw = {
|
|
290
|
+
ok: false,
|
|
291
|
+
error: {
|
|
292
|
+
code: "RUNNER_ERROR",
|
|
293
|
+
stopRun: true,
|
|
294
|
+
backgroundMayStillRun: true,
|
|
295
|
+
message: "查询进程异常;请核对依赖、网络与后台任务后重试",
|
|
296
|
+
},
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
raw = sanitizeErrors(raw, token, true);
|
|
300
|
+
const file = `${c.id}.evidence.json`;
|
|
301
|
+
const evidence = {
|
|
302
|
+
taskId: plan.taskId,
|
|
303
|
+
planHash,
|
|
304
|
+
caseId: c.id,
|
|
305
|
+
workspaceId: plan.workspaceId,
|
|
306
|
+
executor: c.executor,
|
|
307
|
+
question: c.question,
|
|
308
|
+
executedAt: new Date().toISOString(),
|
|
309
|
+
raw,
|
|
310
|
+
};
|
|
311
|
+
await save(join(output, file), evidence);
|
|
312
|
+
report.results[index] = {
|
|
313
|
+
...report.results[index],
|
|
314
|
+
...assess(c, raw),
|
|
315
|
+
evidence: file,
|
|
316
|
+
evidenceHash: digest(evidence),
|
|
317
|
+
};
|
|
318
|
+
await persist();
|
|
319
|
+
if (raw?.error?.stopRun) break;
|
|
320
|
+
}
|
|
321
|
+
await persist();
|
|
322
|
+
return {
|
|
323
|
+
status: report.status,
|
|
324
|
+
counts: report.counts,
|
|
325
|
+
document: join(output, "acceptance.md"),
|
|
326
|
+
configurationStatus: report.configurationStatus,
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
export async function runQuery(plan, c, timeoutMs) {
|
|
331
|
+
const { token } = resolveCredentials();
|
|
332
|
+
const [folder, script, command] = executors[c.executor];
|
|
333
|
+
const scriptPath = fileURLToPath(
|
|
334
|
+
new URL(`../../${folder}/scripts/${script}`, import.meta.url),
|
|
335
|
+
);
|
|
336
|
+
const args = [
|
|
337
|
+
scriptPath,
|
|
338
|
+
command,
|
|
339
|
+
"--workspace-id",
|
|
340
|
+
String(plan.workspaceId),
|
|
341
|
+
"--question",
|
|
342
|
+
c.question,
|
|
343
|
+
"--max-wait-ms",
|
|
344
|
+
String(timeoutMs),
|
|
345
|
+
];
|
|
346
|
+
for (const uid of c.modelUids) args.push("--model-uid", uid);
|
|
347
|
+
return new Promise((resolveResult) => {
|
|
348
|
+
const child = spawn(process.execPath, args, {
|
|
349
|
+
shell: false,
|
|
350
|
+
windowsHide: true,
|
|
351
|
+
env: {
|
|
352
|
+
...process.env,
|
|
353
|
+
DATARC_ENDPOINT: normalizeEndpoint(plan.endpoint),
|
|
354
|
+
DATARC_TOKEN: token,
|
|
355
|
+
},
|
|
356
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
357
|
+
});
|
|
358
|
+
let chunks = [],
|
|
359
|
+
size = 0,
|
|
360
|
+
stopped;
|
|
361
|
+
const stop = (code) => {
|
|
362
|
+
stopped = code;
|
|
363
|
+
child.kill();
|
|
364
|
+
};
|
|
365
|
+
const cancel = () => stop("CANCELLED");
|
|
366
|
+
process.once("SIGINT", cancel);
|
|
367
|
+
process.once("SIGTERM", cancel);
|
|
368
|
+
const cleanup = () => {
|
|
369
|
+
clearTimeout(timer);
|
|
370
|
+
process.removeListener("SIGINT", cancel);
|
|
371
|
+
process.removeListener("SIGTERM", cancel);
|
|
372
|
+
};
|
|
373
|
+
const timer = setTimeout(() => stop("TIMEOUT"), timeoutMs);
|
|
374
|
+
child.stdout.on("data", (chunk) => {
|
|
375
|
+
size += chunk.length;
|
|
376
|
+
if (size > 8 * 1024 * 1024) stop("OUTPUT_LIMIT");
|
|
377
|
+
else chunks.push(chunk);
|
|
378
|
+
});
|
|
379
|
+
// stderr can contain service details; do not copy it into the user report.
|
|
380
|
+
child.stderr.resume();
|
|
381
|
+
child.on("error", () => {
|
|
382
|
+
cleanup();
|
|
383
|
+
resolveResult({
|
|
384
|
+
ok: false,
|
|
385
|
+
error: { code: "START_FAILED", stopRun: true },
|
|
386
|
+
});
|
|
387
|
+
});
|
|
388
|
+
child.on("close", (code) => {
|
|
389
|
+
cleanup();
|
|
390
|
+
if (stopped)
|
|
391
|
+
return resolveResult({
|
|
392
|
+
ok: false,
|
|
393
|
+
error: {
|
|
394
|
+
code: stopped,
|
|
395
|
+
stopRun: true,
|
|
396
|
+
backgroundMayStillRun: true,
|
|
397
|
+
message: "本地等待已停止,后台任务可能仍在运行;请核查后再发起查询",
|
|
398
|
+
},
|
|
399
|
+
});
|
|
400
|
+
try {
|
|
401
|
+
const raw = JSON.parse(
|
|
402
|
+
Buffer.concat(chunks)
|
|
403
|
+
.toString("utf8")
|
|
404
|
+
.replace(/^\uFEFF/, ""),
|
|
405
|
+
);
|
|
406
|
+
if (code !== 0 || raw?.ok !== true)
|
|
407
|
+
return resolveResult({
|
|
408
|
+
ok: false,
|
|
409
|
+
error: {
|
|
410
|
+
code: "QUERY_FAILED",
|
|
411
|
+
message: "查询失败;请检查诊断证据中的原因并处理后重试",
|
|
412
|
+
...safeDiagnostic(
|
|
413
|
+
raw?.error && typeof raw.error === "object"
|
|
414
|
+
? raw.error
|
|
415
|
+
: {
|
|
416
|
+
message:
|
|
417
|
+
raw?.error ?? "查询进程异常退出;请核对依赖和服务状态",
|
|
418
|
+
},
|
|
419
|
+
token,
|
|
420
|
+
),
|
|
421
|
+
stopRun: true,
|
|
422
|
+
exitCode: code,
|
|
423
|
+
},
|
|
424
|
+
});
|
|
425
|
+
resolveResult(raw);
|
|
426
|
+
} catch {
|
|
427
|
+
resolveResult({
|
|
428
|
+
ok: false,
|
|
429
|
+
error: {
|
|
430
|
+
code: "INVALID_OUTPUT",
|
|
431
|
+
stopRun: true,
|
|
432
|
+
message: "查询未返回有效 JSON;请检查搜索技能依赖后重试",
|
|
433
|
+
},
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
});
|
|
437
|
+
});
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
function queryHealth(executor, data) {
|
|
441
|
+
const invalid = {
|
|
442
|
+
status: "error",
|
|
443
|
+
reason: "查询包含失败、未执行分支或无数据卡;请检查完整结果后再验收",
|
|
444
|
+
};
|
|
445
|
+
const validResults = (results) =>
|
|
446
|
+
Array.isArray(results) &&
|
|
447
|
+
results.length > 0 &&
|
|
448
|
+
results.every(
|
|
449
|
+
(r) =>
|
|
450
|
+
r &&
|
|
451
|
+
!r.error &&
|
|
452
|
+
Array.isArray(r.previews) &&
|
|
453
|
+
r.previews.length > 0 &&
|
|
454
|
+
r.previews.every((p) => p && !p.error && Object.hasOwn(p, "data")),
|
|
455
|
+
);
|
|
456
|
+
if (executor === "data-search")
|
|
457
|
+
return ["success", "empty"].includes(data?.status) &&
|
|
458
|
+
validResults(data.results) &&
|
|
459
|
+
data.results.every((r) => ["success", "empty"].includes(r.status))
|
|
460
|
+
? null
|
|
461
|
+
: invalid;
|
|
462
|
+
const actionsValid = (actions) =>
|
|
463
|
+
Array.isArray(actions) &&
|
|
464
|
+
actions.length > 0 &&
|
|
465
|
+
actions.every((a) => {
|
|
466
|
+
if (!a || a.error) return false;
|
|
467
|
+
if (["answer", "search"].includes(a.type)) return validResults(a.results);
|
|
468
|
+
const children =
|
|
469
|
+
a.type === "parallel"
|
|
470
|
+
? a.children
|
|
471
|
+
: a.type === "chained"
|
|
472
|
+
? a.steps
|
|
473
|
+
: null;
|
|
474
|
+
return (
|
|
475
|
+
Array.isArray(children) &&
|
|
476
|
+
children.length > 0 &&
|
|
477
|
+
children.every(
|
|
478
|
+
(child) => child && !child.error && actionsValid(child.actions),
|
|
479
|
+
)
|
|
480
|
+
);
|
|
481
|
+
});
|
|
482
|
+
return actionsValid(data?.actions) ? null : invalid;
|
|
483
|
+
}
|
|
484
|
+
function sanitizeErrors(value, token, envelope = false) {
|
|
485
|
+
if (Array.isArray(value))
|
|
486
|
+
return value.map((item) => sanitizeErrors(item, token));
|
|
487
|
+
if (!value || typeof value !== "object") return value;
|
|
488
|
+
return Object.fromEntries(
|
|
489
|
+
Object.entries(value).map(([key, item]) => [
|
|
490
|
+
key,
|
|
491
|
+
key === "error"
|
|
492
|
+
? safeDiagnostic(item, token)
|
|
493
|
+
: // Only the CLI envelope's data contains query structure. Nested data
|
|
494
|
+
// fields are business payloads (including preview rows), not diagnostics.
|
|
495
|
+
key === "data" && !envelope
|
|
496
|
+
? item
|
|
497
|
+
: sanitizeErrors(item, token),
|
|
498
|
+
]),
|
|
499
|
+
);
|
|
500
|
+
}
|
|
501
|
+
function safeDiagnostic(value, token) {
|
|
502
|
+
if (Array.isArray(value))
|
|
503
|
+
return value.map((item) => safeDiagnostic(item, token));
|
|
504
|
+
if (value && typeof value === "object")
|
|
505
|
+
return Object.fromEntries(
|
|
506
|
+
Object.entries(value).map(([key, item]) => [
|
|
507
|
+
key,
|
|
508
|
+
/^(?:password|passwd|pwd|token|access[_-]?token|refresh[_-]?token|authorization|cookie|set-cookie|secret|api[_-]?key|sql|prompt|databaseurl|connectionstring)$/i.test(
|
|
509
|
+
key,
|
|
510
|
+
)
|
|
511
|
+
? "***"
|
|
512
|
+
: safeDiagnostic(item, token),
|
|
513
|
+
]),
|
|
514
|
+
);
|
|
515
|
+
if (typeof value !== "string") return value;
|
|
516
|
+
return (token ? value.replaceAll(token, "***") : value)
|
|
517
|
+
.replace(/:\/\/[^\s/@]+:[^\s/@]+@/g, "://***@")
|
|
518
|
+
.replace(/\b(Bearer|Basic)\s+[A-Za-z0-9._~+\/-]+=*/gi, "$1 ***")
|
|
519
|
+
.replace(
|
|
520
|
+
/((?:password|passwd|pwd|token|authorization|secret|api[_-]?key)\s*[:=]\s*)(?:"[^"]*"|'[^']*'|[^\s,;]+)/gi,
|
|
521
|
+
"$1***",
|
|
522
|
+
);
|
|
523
|
+
}
|
|
524
|
+
function canonical(value) {
|
|
525
|
+
if (Array.isArray(value)) return value.map(canonical);
|
|
526
|
+
if (value && typeof value === "object")
|
|
527
|
+
return Object.fromEntries(
|
|
528
|
+
Object.keys(value)
|
|
529
|
+
.sort()
|
|
530
|
+
.map((k) => [k, canonical(value[k])]),
|
|
531
|
+
);
|
|
532
|
+
return value;
|
|
533
|
+
}
|
|
534
|
+
function pointer(value, path) {
|
|
535
|
+
for (const key of path
|
|
536
|
+
.slice(1)
|
|
537
|
+
.split("/")
|
|
538
|
+
.map((k) => k.replaceAll("~1", "/").replaceAll("~0", "~"))) {
|
|
539
|
+
if (
|
|
540
|
+
value === null ||
|
|
541
|
+
typeof value !== "object" ||
|
|
542
|
+
!Object.hasOwn(value, key)
|
|
543
|
+
)
|
|
544
|
+
return undefined;
|
|
545
|
+
value = value[key];
|
|
546
|
+
}
|
|
547
|
+
return value;
|
|
548
|
+
}
|
|
549
|
+
function text(value) {
|
|
550
|
+
return typeof value === "string" && value.trim().length > 0;
|
|
551
|
+
}
|
|
552
|
+
function ensure(condition, message) {
|
|
553
|
+
if (!condition) throw new Error(message);
|
|
554
|
+
}
|
|
555
|
+
function normalizeEndpoint(value) {
|
|
556
|
+
return String(value ?? "")
|
|
557
|
+
.split("#", 1)[0]
|
|
558
|
+
.replace(/\/+$/, "");
|
|
559
|
+
}
|
|
560
|
+
function md(value) {
|
|
561
|
+
return String(value ?? "")
|
|
562
|
+
.replaceAll("&", "&")
|
|
563
|
+
.replaceAll("<", "<")
|
|
564
|
+
.replaceAll(">", ">")
|
|
565
|
+
.replace(/[\\`*_{}\[\]()#!|]/g, "\\$&")
|
|
566
|
+
.replace(/[\r\n]+/g, " ");
|
|
567
|
+
}
|
|
568
|
+
async function save(path, value) {
|
|
569
|
+
const temporary = path + ".tmp";
|
|
570
|
+
await writeFile(temporary, JSON.stringify(value, null, 2) + "\n", {
|
|
571
|
+
mode: 0o600,
|
|
572
|
+
});
|
|
573
|
+
await rename(temporary, path);
|
|
574
|
+
}
|
|
575
|
+
function renderPlan(p) {
|
|
576
|
+
return `# 配置方案:${md(p.goal)}\n\n工作区:${p.workspaceId}|服务:${md(p.endpoint)}\n\n任务:${md(p.taskId)}|方案指纹:${p.planHash}\n\n验证身份(声明):${md(p.identity)}|时间口径:${md(p.timeRange)}\n\n## 计划变更\n\n${p.changes.map((c) => `- ${md(c.target)}:${md(c.change)};影响:${md(c.impact)}`).join("\n")}\n\n## 待澄清\n\n${p.questions.length ? p.questions.map((q) => `- ${md(q)}`).join("\n") : "无。复杂配置请确认本版本后执行;子技能专门确认门槛仍保留。"}\n\n## 验收计划\n\n${p.cases.map((c) => `### ${md(c.id)}:${md(c.question)}\n\n执行器:${c.executor};${c.required ? "必需" : "可选"};人工复核:${c.reviewRequired ? "需要" : "无需"};模型:${md(c.modelUids.join(", ") || "未强制限定")}\n\n预期依据:${md(c.expectedSource || "缺少可靠基准,待业务确认")}\n\n${c.assertions.map((a) => `- ${md(a.pointer)} = ${md(JSON.stringify(a.equals))}${a.tolerance === undefined ? "" : `,容差 ${a.tolerance}`}`).join("\n")}`).join("\n\n") || md(p.noQueryReason)}\n\n## 执行边界\n\n依赖和配置回读由对应业务技能执行;查询会创建会话、异步任务,链式问题可能创建临时数据集。本工具不修改配置、不提供备份恢复。发现未知写入结果或并发变化时停止,先核对现状。\n`;
|
|
577
|
+
}
|
|
578
|
+
function renderReport(p, r) {
|
|
579
|
+
const labels = {
|
|
580
|
+
passed: "必需查询检查通过",
|
|
581
|
+
failed: "未通过",
|
|
582
|
+
incomplete: "未完成",
|
|
583
|
+
not_applicable: "不适用",
|
|
584
|
+
pass: "通过",
|
|
585
|
+
fail: "不符合预期",
|
|
586
|
+
error: "执行报错",
|
|
587
|
+
review: "待业务复核",
|
|
588
|
+
not_run: "未运行",
|
|
589
|
+
running: "执行中",
|
|
590
|
+
};
|
|
591
|
+
const short = (value) => md(JSON.stringify(value)?.slice(0, 180) ?? "缺失");
|
|
592
|
+
return `# 配置查询验收报告\n\n查询验收:${labels[r.status]};配置保存与生效:本工具未核验;业务验收:待用户确认。\n\n工作区:${r.workspaceId}|身份(声明):${md(r.identity)}|时间:${md(r.timeRange)}\n\n方案指纹:${r.planHash}\n\n${Object.entries(
|
|
593
|
+
r.counts,
|
|
594
|
+
)
|
|
595
|
+
.map(([s, n]) => `${labels[s]}: ${n}`)
|
|
596
|
+
.join(
|
|
597
|
+
";",
|
|
598
|
+
)}\n\n## 逐项结果\n\n${r.results.map((x) => `- **${md(x.id)} / ${labels[x.status]}** ${md(x.question)}:${md(x.reason)}${x.evidence ? ` [证据](${x.evidence})` : ""}\n${(x.comparisons ?? []).map((a) => ` - ${md(a.pointer)}:预期 ${short(a.expected)};实际 ${short(a.actual)}。`).join("\n")}`).join("\n") || md(p.noQueryReason)}\n\n## 下一步\n\n请结合业务技能的配置回读与就绪结果交付;待复核项核对语义、模型和业务基准,失败项处理后重新验收,未运行项说明原因。长结果仅显示摘要,完整值见证据;预览不能证明全量唯一性或其他用户权限。报告不会自动确认业务验收。\n`;
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
export async function main(argv) {
|
|
602
|
+
if (!argv.length || argv.includes("--help")) return { help: HELP };
|
|
603
|
+
const [command, ...rest] = argv;
|
|
604
|
+
ensure(
|
|
605
|
+
["plan", "verify"].includes(command),
|
|
606
|
+
"未知命令;请运行 --help 查看用法",
|
|
607
|
+
);
|
|
608
|
+
const args = {};
|
|
609
|
+
const allowed =
|
|
610
|
+
command === "plan"
|
|
611
|
+
? ["--input", "--output"]
|
|
612
|
+
: ["--plan", "--confirm", "--output"];
|
|
613
|
+
for (let i = 0; i < rest.length; i += 2) {
|
|
614
|
+
ensure(
|
|
615
|
+
allowed.includes(rest[i]) &&
|
|
616
|
+
!Object.hasOwn(args, rest[i]) &&
|
|
617
|
+
text(rest[i + 1]) &&
|
|
618
|
+
!rest[i + 1].startsWith("--"),
|
|
619
|
+
"参数缺失、重复或未知;请运行 --help 核对",
|
|
620
|
+
);
|
|
621
|
+
args[rest[i]] = rest[i + 1];
|
|
622
|
+
}
|
|
623
|
+
ensure(
|
|
624
|
+
allowed.every((k) => text(args[k])),
|
|
625
|
+
"缺少必填参数;请运行 --help 核对",
|
|
626
|
+
);
|
|
627
|
+
const input = JSON.parse(
|
|
628
|
+
(
|
|
629
|
+
await readFile(
|
|
630
|
+
resolve(args[command === "plan" ? "--input" : "--plan"]),
|
|
631
|
+
"utf8",
|
|
632
|
+
)
|
|
633
|
+
).replace(/^\uFEFF/, ""),
|
|
634
|
+
);
|
|
635
|
+
const output = resolve(args["--output"]);
|
|
636
|
+
return command === "plan"
|
|
637
|
+
? generatePlan(input, output)
|
|
638
|
+
: verifyPlan(input, args["--confirm"], output);
|
|
639
|
+
}
|
|
640
|
+
if (
|
|
641
|
+
process.argv[1] &&
|
|
642
|
+
import.meta.url === pathToFileURL(resolve(process.argv[1])).href
|
|
643
|
+
) {
|
|
644
|
+
try {
|
|
645
|
+
process.stdout.write(
|
|
646
|
+
JSON.stringify({ ok: true, data: await main(process.argv.slice(2)) }) +
|
|
647
|
+
"\n",
|
|
648
|
+
);
|
|
649
|
+
} catch (error) {
|
|
650
|
+
process.stdout.write(
|
|
651
|
+
JSON.stringify({
|
|
652
|
+
ok: false,
|
|
653
|
+
error: {
|
|
654
|
+
message: error.message,
|
|
655
|
+
nextAction:
|
|
656
|
+
"检查输入及输出目录;输出目录必须是尚不存在的新目录,避免覆盖已有证据",
|
|
657
|
+
},
|
|
658
|
+
}) + "\n",
|
|
659
|
+
);
|
|
660
|
+
process.exitCode = 1;
|
|
661
|
+
}
|
|
662
|
+
}
|