@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,1014 @@
|
|
|
1
|
+
import { canonicalJson, hashArtifactValue } from "./canonical.mjs";
|
|
2
|
+
import {
|
|
3
|
+
normalizeModelScope,
|
|
4
|
+
validateWorkspaceModelScope,
|
|
5
|
+
} from "./model-scope.mjs";
|
|
6
|
+
import { listAllPages } from "./shared.mjs";
|
|
7
|
+
import { actionInterpretations } from "./interpretation-pairs.mjs";
|
|
8
|
+
import { fail, normalizeText, requireClient, uniquePairs } from "./shared.mjs";
|
|
9
|
+
|
|
10
|
+
const SYNC_TASK_TYPES = [0, 2, 19, 31];
|
|
11
|
+
const KEYWORD_SYNC_TASK_TYPES = new Set([0, 31]);
|
|
12
|
+
const DICTIONARY_TASK_TYPE = 2;
|
|
13
|
+
const TASK_PENDING = 0;
|
|
14
|
+
const TASK_STARTED = 1;
|
|
15
|
+
const TASK_SUCCESS = 2;
|
|
16
|
+
const TASK_PAGE_LIMIT = 100;
|
|
17
|
+
const MAX_TASK_PAGES = 10000;
|
|
18
|
+
const BASELINE_SCAN_COUNT = 2;
|
|
19
|
+
const PROBE_KINDS = new Set(["primary", "paraphrase", "contrast"]);
|
|
20
|
+
const PROBE_GATES = new Set(["cut_and_message", "message_only"]);
|
|
21
|
+
const AUTHORITY_GATES = new Map([
|
|
22
|
+
["alias", "cut_and_message"],
|
|
23
|
+
["dimension_value_synonym", "cut_and_message"],
|
|
24
|
+
["semantic_calibration", "cut_and_message"],
|
|
25
|
+
["dataset", "cut_and_message"],
|
|
26
|
+
["knowledge", "message_only"],
|
|
27
|
+
]);
|
|
28
|
+
|
|
29
|
+
export async function captureSyncBaseline(client, workspaceId) {
|
|
30
|
+
requireClient(client);
|
|
31
|
+
const tasksByUid = new Map();
|
|
32
|
+
let serverWatermarkMs = null;
|
|
33
|
+
for (let scan = 0; scan < BASELINE_SCAN_COUNT; scan += 1) {
|
|
34
|
+
const tasks = await listTasks(client, workspaceId);
|
|
35
|
+
for (const task of tasks) {
|
|
36
|
+
if (safeIdentifier(task?.uid)) tasksByUid.set(String(task.uid), task);
|
|
37
|
+
const timestamp = validTaskTimestamp(task);
|
|
38
|
+
if (
|
|
39
|
+
timestamp !== null &&
|
|
40
|
+
(serverWatermarkMs === null || timestamp > serverWatermarkMs)
|
|
41
|
+
)
|
|
42
|
+
serverWatermarkMs = timestamp;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return {
|
|
46
|
+
baselineTaskUids: [...tasksByUid.keys()],
|
|
47
|
+
serverWatermark:
|
|
48
|
+
serverWatermarkMs === null
|
|
49
|
+
? null
|
|
50
|
+
: new Date(serverWatermarkMs).toISOString(),
|
|
51
|
+
capturedAt: new Date().toISOString(),
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export async function waitForFreshDictionary(
|
|
56
|
+
client,
|
|
57
|
+
workspaceId,
|
|
58
|
+
expectation,
|
|
59
|
+
options = {},
|
|
60
|
+
) {
|
|
61
|
+
requireClient(client);
|
|
62
|
+
const baselineTaskUids = baselineUids(expectation);
|
|
63
|
+
const baseline = new Set(baselineTaskUids);
|
|
64
|
+
const mutationAt = mutationTimestamp(expectation);
|
|
65
|
+
const mutationThreshold = validTimestamp(mutationAt);
|
|
66
|
+
const capturedAt = baselineCapturedAt(expectation);
|
|
67
|
+
const capturedThreshold = validTimestamp(capturedAt);
|
|
68
|
+
const serverWatermark = baselineServerWatermark(expectation);
|
|
69
|
+
const serverThreshold = validTimestamp(serverWatermark);
|
|
70
|
+
if (
|
|
71
|
+
mutationThreshold === null ||
|
|
72
|
+
capturedThreshold === null ||
|
|
73
|
+
mutationThreshold < capturedThreshold ||
|
|
74
|
+
(serverWatermark !== undefined &&
|
|
75
|
+
serverWatermark !== null &&
|
|
76
|
+
serverThreshold === null) ||
|
|
77
|
+
(baseline.size > 0 && serverThreshold === null)
|
|
78
|
+
)
|
|
79
|
+
fail(
|
|
80
|
+
"FRESHNESS_EVIDENCE_REQUIRED",
|
|
81
|
+
"valid ordered local timestamps and baseline server-time evidence are required to reject stale synchronization tasks",
|
|
82
|
+
);
|
|
83
|
+
const taskIsFresh = (task) =>
|
|
84
|
+
serverThresholdIsSatisfied(task, baseline.size ? serverThreshold : null);
|
|
85
|
+
const targets = normalizeCoverageTargets(expectation?.targets ?? []);
|
|
86
|
+
const inspected = new Set();
|
|
87
|
+
const coverageByTarget = new Map(
|
|
88
|
+
targets.map((target) => [target.key, new Set()]),
|
|
89
|
+
);
|
|
90
|
+
const coverageCandidates = [];
|
|
91
|
+
const failures = [];
|
|
92
|
+
let dictionary = null;
|
|
93
|
+
const poll = pollOptions(options);
|
|
94
|
+
let firstPoll = true;
|
|
95
|
+
|
|
96
|
+
while (firstPoll || poll.now() < poll.deadlineAt) {
|
|
97
|
+
firstPoll = false;
|
|
98
|
+
const tasks = await listTasks(client, workspaceId);
|
|
99
|
+
const candidates = tasks.filter(
|
|
100
|
+
(task) =>
|
|
101
|
+
task?.uid &&
|
|
102
|
+
!baseline.has(String(task.uid)) &&
|
|
103
|
+
!inspected.has(String(task.uid)) &&
|
|
104
|
+
taskIsFresh(task),
|
|
105
|
+
);
|
|
106
|
+
for (const candidate of candidates) {
|
|
107
|
+
if (isPending(candidate.status)) continue;
|
|
108
|
+
const detail = await client.request(
|
|
109
|
+
"GET",
|
|
110
|
+
`/api/v2/admin-projects/${workspaceId}/async-tasks/${candidate.uid}/`,
|
|
111
|
+
);
|
|
112
|
+
if (isPending(detail?.status)) continue;
|
|
113
|
+
inspected.add(String(candidate.uid));
|
|
114
|
+
if (!taskIsFresh(detail)) continue;
|
|
115
|
+
const taskType = Number(detail?.taskType ?? candidate.taskType);
|
|
116
|
+
if (Number(detail?.status) !== TASK_SUCCESS) {
|
|
117
|
+
failures.push(safeTask(detail, taskType));
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
if (
|
|
121
|
+
taskType === DICTIONARY_TASK_TYPE &&
|
|
122
|
+
isFiniteCount(detail?.result?.keywordsNum)
|
|
123
|
+
)
|
|
124
|
+
dictionary = safeTask(detail, taskType);
|
|
125
|
+
if (KEYWORD_SYNC_TASK_TYPES.has(taskType) && targets.length)
|
|
126
|
+
collectCoverage(
|
|
127
|
+
detail,
|
|
128
|
+
taskType,
|
|
129
|
+
targets,
|
|
130
|
+
coverageByTarget,
|
|
131
|
+
coverageCandidates,
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (dictionary && coverageComplete(targets, coverageByTarget))
|
|
136
|
+
return {
|
|
137
|
+
dictionary,
|
|
138
|
+
coverage: coverageSummary(targets, coverageByTarget),
|
|
139
|
+
coverageCandidates,
|
|
140
|
+
causalAttribution: "outcome_gated",
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
const remainingMs = poll.deadlineAt - poll.now();
|
|
144
|
+
if (remainingMs <= 0) break;
|
|
145
|
+
await poll.sleep(Math.min(poll.pollIntervalMs, remainingMs));
|
|
146
|
+
}
|
|
147
|
+
fail(
|
|
148
|
+
"FRESH_SYNC_NOT_OBSERVED",
|
|
149
|
+
"fresh dictionary and full ready-model coverage were not observed within the polling window",
|
|
150
|
+
{
|
|
151
|
+
mutationAt,
|
|
152
|
+
serverWatermark,
|
|
153
|
+
baselineTaskCount: baselineTaskUids.length,
|
|
154
|
+
dictionary,
|
|
155
|
+
coverage: coverageSummary(targets, coverageByTarget),
|
|
156
|
+
coverageCandidates,
|
|
157
|
+
failures,
|
|
158
|
+
},
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export async function inspectCut(
|
|
163
|
+
client,
|
|
164
|
+
workspaceId,
|
|
165
|
+
question,
|
|
166
|
+
expectation = {},
|
|
167
|
+
) {
|
|
168
|
+
requireClient(client);
|
|
169
|
+
const response = await client.request(
|
|
170
|
+
"POST",
|
|
171
|
+
`/api/v3/account-projects/${workspaceId}/cut/`,
|
|
172
|
+
{ body: { tokens: question } },
|
|
173
|
+
);
|
|
174
|
+
const pairs = normalizePairs(
|
|
175
|
+
Array.isArray(response) ? response : response?.tokens,
|
|
176
|
+
);
|
|
177
|
+
const comparison = compareProps(
|
|
178
|
+
pairs,
|
|
179
|
+
expectation.expectedProps,
|
|
180
|
+
expectation.forbiddenProps,
|
|
181
|
+
);
|
|
182
|
+
return {
|
|
183
|
+
passed: comparison.mismatches.length === 0,
|
|
184
|
+
pairs,
|
|
185
|
+
...comparison.fields,
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export function summarizeMessage(message) {
|
|
190
|
+
const current = objectOf(message?.current);
|
|
191
|
+
const actions = Array.isArray(current.result) ? current.result : [];
|
|
192
|
+
const failed = current.error !== undefined && current.error !== null;
|
|
193
|
+
const summary = {
|
|
194
|
+
...(safeIdentifier(message?.id) ? { id: message.id } : {}),
|
|
195
|
+
...(safeIdentifier(message?.uid) ? { uid: message.uid } : {}),
|
|
196
|
+
state: failed
|
|
197
|
+
? "failed"
|
|
198
|
+
: actions.length
|
|
199
|
+
? "completed"
|
|
200
|
+
: safeTerminalState(current.status),
|
|
201
|
+
actionTypes: actions
|
|
202
|
+
.map((action) => String(action?.type ?? "").trim())
|
|
203
|
+
.filter(Boolean),
|
|
204
|
+
resultCount: actions.length,
|
|
205
|
+
interpretations: failed ? [] : actionInterpretations(actions),
|
|
206
|
+
};
|
|
207
|
+
return summary;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export async function verifyReceipt(
|
|
211
|
+
client,
|
|
212
|
+
receipt,
|
|
213
|
+
verification,
|
|
214
|
+
options = {},
|
|
215
|
+
) {
|
|
216
|
+
requireClient(client);
|
|
217
|
+
if (
|
|
218
|
+
receipt?.threadUid !== undefined ||
|
|
219
|
+
verification?.threadUid !== undefined ||
|
|
220
|
+
options.threadUid !== undefined ||
|
|
221
|
+
hasNestedThreadUid(verification)
|
|
222
|
+
)
|
|
223
|
+
fail(
|
|
224
|
+
"EXISTING_THREAD_FORBIDDEN",
|
|
225
|
+
"verification creates a new current-administrator thread for every probe",
|
|
226
|
+
);
|
|
227
|
+
const workspaceId = receipt?.workspaceId;
|
|
228
|
+
if (workspaceId === undefined || workspaceId === null || workspaceId === "")
|
|
229
|
+
fail("INVALID_RECEIPT", "receipt workspaceId is required");
|
|
230
|
+
const suites = normalizeProbeSuites(verification);
|
|
231
|
+
const suiteResults = [];
|
|
232
|
+
const usedThreadUids = new Set();
|
|
233
|
+
|
|
234
|
+
for (const suite of suites) {
|
|
235
|
+
if (suite.knowledgeTarget)
|
|
236
|
+
await verifyKnowledgeTarget(client, workspaceId, suite.knowledgeTarget);
|
|
237
|
+
const probeResults = [];
|
|
238
|
+
for (const probe of suite.probes) {
|
|
239
|
+
const thread = await client.request(
|
|
240
|
+
"POST",
|
|
241
|
+
`/api/v3/account-projects/${workspaceId}/threads/`,
|
|
242
|
+
{ body: {} },
|
|
243
|
+
);
|
|
244
|
+
if (!safeIdentifier(thread?.uid))
|
|
245
|
+
fail("INVALID_THREAD_RESPONSE", "new thread response is missing a UID");
|
|
246
|
+
const threadUid = String(thread.uid);
|
|
247
|
+
if (usedThreadUids.has(threadUid))
|
|
248
|
+
fail(
|
|
249
|
+
"DUPLICATE_THREAD",
|
|
250
|
+
"each verification probe requires a distinct new thread",
|
|
251
|
+
);
|
|
252
|
+
usedThreadUids.add(threadUid);
|
|
253
|
+
const cut =
|
|
254
|
+
suite.gate === "cut_and_message"
|
|
255
|
+
? await inspectCut(client, workspaceId, probe.question, probe)
|
|
256
|
+
: null;
|
|
257
|
+
const created = await client.request(
|
|
258
|
+
"POST",
|
|
259
|
+
`/api/v3/account-projects/${workspaceId}/threads/${thread.uid}/messages/`,
|
|
260
|
+
{
|
|
261
|
+
body: {
|
|
262
|
+
message: probe.question,
|
|
263
|
+
...(suite.knowledgeTarget?.tuids?.length
|
|
264
|
+
? { extraData: { tuids: suite.knowledgeTarget.tuids } }
|
|
265
|
+
: {}),
|
|
266
|
+
},
|
|
267
|
+
},
|
|
268
|
+
);
|
|
269
|
+
if (!safeIdentifier(created?.id))
|
|
270
|
+
fail(
|
|
271
|
+
"INVALID_MESSAGE_RESPONSE",
|
|
272
|
+
"new message response is missing an ID",
|
|
273
|
+
);
|
|
274
|
+
const message = await waitForMessage(
|
|
275
|
+
client,
|
|
276
|
+
workspaceId,
|
|
277
|
+
thread.uid,
|
|
278
|
+
created.id,
|
|
279
|
+
pollOptions(options),
|
|
280
|
+
);
|
|
281
|
+
const summary = summarizeMessage(message);
|
|
282
|
+
const mismatches = probeMismatches(probe, cut, summary);
|
|
283
|
+
const automatedChecksPassed = mismatches.length === 0;
|
|
284
|
+
const manualConfirmationRequired = suite.gate === "message_only";
|
|
285
|
+
probeResults.push({
|
|
286
|
+
kind: probe.kind,
|
|
287
|
+
passed: automatedChecksPassed && !manualConfirmationRequired,
|
|
288
|
+
automatedChecksPassed,
|
|
289
|
+
manualConfirmationRequired,
|
|
290
|
+
threadUid,
|
|
291
|
+
...(cut === null ? {} : { cut: redactVerificationWords(cut) }),
|
|
292
|
+
message: redactVerificationWords(summary),
|
|
293
|
+
mismatches,
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
const automatedChecksPassed = probeResults.every(
|
|
297
|
+
(probe) => probe.automatedChecksPassed,
|
|
298
|
+
);
|
|
299
|
+
const manualConfirmationRequired = suite.gate === "message_only";
|
|
300
|
+
suiteResults.push({
|
|
301
|
+
caseId: suite.caseId,
|
|
302
|
+
gate: suite.gate,
|
|
303
|
+
...(suite.gate === "message_only"
|
|
304
|
+
? {
|
|
305
|
+
modelScopeStatus: suite.knowledgeTarget
|
|
306
|
+
? "verified"
|
|
307
|
+
: "model_scope_unverified",
|
|
308
|
+
...(suite.knowledgeTarget
|
|
309
|
+
? {
|
|
310
|
+
modelScope: suite.knowledgeTarget.tuids,
|
|
311
|
+
untestedScenes: suite.knowledgeTarget.scenes.filter(
|
|
312
|
+
(scene) => ![1, 5, 14].includes(scene),
|
|
313
|
+
),
|
|
314
|
+
}
|
|
315
|
+
: {}),
|
|
316
|
+
}
|
|
317
|
+
: {}),
|
|
318
|
+
passed: automatedChecksPassed && !manualConfirmationRequired,
|
|
319
|
+
automatedChecksPassed,
|
|
320
|
+
manualConfirmationRequired,
|
|
321
|
+
probes: probeResults,
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
const automatedChecksPassed = suiteResults.every(
|
|
326
|
+
(suite) => suite.automatedChecksPassed,
|
|
327
|
+
);
|
|
328
|
+
const manualCaseIds = suiteResults
|
|
329
|
+
.filter((suite) => suite.manualConfirmationRequired)
|
|
330
|
+
.map((suite) => suite.caseId);
|
|
331
|
+
const manualConfirmationRequired = manualCaseIds.length > 0;
|
|
332
|
+
const status = !automatedChecksPassed
|
|
333
|
+
? "failed"
|
|
334
|
+
: manualConfirmationRequired
|
|
335
|
+
? "manual_confirmation_required"
|
|
336
|
+
: "passed";
|
|
337
|
+
return {
|
|
338
|
+
status,
|
|
339
|
+
passed: status === "passed",
|
|
340
|
+
automatedChecksPassed,
|
|
341
|
+
manualConfirmationRequired,
|
|
342
|
+
manualCaseIds,
|
|
343
|
+
...(safeIdentifier(receipt.receiptHash)
|
|
344
|
+
? { receiptHash: receipt.receiptHash }
|
|
345
|
+
: {}),
|
|
346
|
+
suites: suiteResults,
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
export function bindVerificationToAuthorities(
|
|
351
|
+
authorities,
|
|
352
|
+
verification,
|
|
353
|
+
planSteps = [],
|
|
354
|
+
) {
|
|
355
|
+
if (hasNestedThreadUid(verification))
|
|
356
|
+
fail(
|
|
357
|
+
"EXISTING_THREAD_FORBIDDEN",
|
|
358
|
+
"verification creates a new current-administrator thread for every probe",
|
|
359
|
+
);
|
|
360
|
+
validateVerificationContainer(verification);
|
|
361
|
+
const contrastFor = (caseId) =>
|
|
362
|
+
planSteps.some(
|
|
363
|
+
(step) =>
|
|
364
|
+
Array.isArray(step?.caseIds) &&
|
|
365
|
+
step.caseIds.includes(caseId) &&
|
|
366
|
+
step.requiresContrast === true,
|
|
367
|
+
);
|
|
368
|
+
const normalizedAuthorities = normalizeAuthorities(authorities);
|
|
369
|
+
if (!Object.hasOwn(verification ?? {}, "suites")) {
|
|
370
|
+
if (normalizedAuthorities.length !== 1)
|
|
371
|
+
fail(
|
|
372
|
+
"VERIFICATION_AUTHORITY_MISMATCH",
|
|
373
|
+
"legacy probes require exactly one plan authority",
|
|
374
|
+
);
|
|
375
|
+
const [authority] = normalizedAuthorities;
|
|
376
|
+
return {
|
|
377
|
+
suites: [
|
|
378
|
+
{
|
|
379
|
+
...authority,
|
|
380
|
+
contrastRequired: contrastFor(authority.caseId),
|
|
381
|
+
probes: normalizeProbes(
|
|
382
|
+
verification?.probes,
|
|
383
|
+
authority.gate,
|
|
384
|
+
contrastFor(authority.caseId),
|
|
385
|
+
),
|
|
386
|
+
},
|
|
387
|
+
],
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
if (!Array.isArray(verification.suites) || verification.suites.length === 0)
|
|
392
|
+
fail("INVALID_SUITES", "verification suites must be a non-empty array");
|
|
393
|
+
const supplied = new Map();
|
|
394
|
+
for (const suite of verification.suites) {
|
|
395
|
+
validateVerificationSuite(suite);
|
|
396
|
+
const caseId = String(suite?.caseId ?? "").trim();
|
|
397
|
+
if (!caseId) fail("INVALID_CASE_ID", "each suite requires a caseId");
|
|
398
|
+
if (supplied.has(caseId))
|
|
399
|
+
fail("DUPLICATE_CASE_ID", "suite caseIds must be unique");
|
|
400
|
+
supplied.set(caseId, suite);
|
|
401
|
+
}
|
|
402
|
+
if (
|
|
403
|
+
supplied.size !== normalizedAuthorities.length ||
|
|
404
|
+
normalizedAuthorities.some(({ caseId }) => !supplied.has(caseId))
|
|
405
|
+
)
|
|
406
|
+
fail(
|
|
407
|
+
"VERIFICATION_AUTHORITY_MISMATCH",
|
|
408
|
+
"verification suites must exactly match plan authorities",
|
|
409
|
+
);
|
|
410
|
+
|
|
411
|
+
return {
|
|
412
|
+
suites: normalizedAuthorities.map((authority) => {
|
|
413
|
+
const suite = supplied.get(authority.caseId);
|
|
414
|
+
const suppliedGate = String(suite?.gate ?? "").trim();
|
|
415
|
+
if (Object.hasOwn(suite, "gate") && suppliedGate !== authority.gate)
|
|
416
|
+
fail("INVALID_GATE", "suite gate does not match its plan authority");
|
|
417
|
+
return {
|
|
418
|
+
...authority,
|
|
419
|
+
contrastRequired: contrastFor(authority.caseId),
|
|
420
|
+
probes: normalizeProbes(
|
|
421
|
+
suite?.probes,
|
|
422
|
+
authority.gate,
|
|
423
|
+
contrastFor(authority.caseId),
|
|
424
|
+
),
|
|
425
|
+
};
|
|
426
|
+
}),
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
export function deriveVerificationSyncTargets(plan, receipt) {
|
|
431
|
+
if (!Array.isArray(plan?.steps) || !Array.isArray(receipt?.completedSteps))
|
|
432
|
+
fail(
|
|
433
|
+
"INVALID_SYNC_AUTHORITY",
|
|
434
|
+
"sync targets require a verified plan and receipt",
|
|
435
|
+
);
|
|
436
|
+
const targets = [];
|
|
437
|
+
for (const completed of receipt.completedSteps) {
|
|
438
|
+
const index = completed?.planStepIndex;
|
|
439
|
+
if (!Number.isSafeInteger(index) || index < 0 || index >= plan.steps.length)
|
|
440
|
+
fail(
|
|
441
|
+
"INVALID_SYNC_AUTHORITY",
|
|
442
|
+
"completed step is not bound to a plan step",
|
|
443
|
+
);
|
|
444
|
+
const planned = plan.steps[index];
|
|
445
|
+
if (planned?.kind !== "dataset") continue;
|
|
446
|
+
const name = String(planned.after?.name ?? "").trim();
|
|
447
|
+
const prop = String(planned.after?.columns?.[0] ?? "").trim();
|
|
448
|
+
const readyModelUids = uniqueStrings(planned.after?.tuids);
|
|
449
|
+
if (
|
|
450
|
+
planned.dictionarySyncRequired !== true ||
|
|
451
|
+
completed.kind !== "dataset" ||
|
|
452
|
+
!name ||
|
|
453
|
+
!prop ||
|
|
454
|
+
readyModelUids.length === 0
|
|
455
|
+
)
|
|
456
|
+
fail("INVALID_SYNC_AUTHORITY", "Dataset sync authority is incomplete");
|
|
457
|
+
targets.push({
|
|
458
|
+
id: completed.after?.id ?? planned.after?.id ?? null,
|
|
459
|
+
prop,
|
|
460
|
+
name,
|
|
461
|
+
readyModelUids,
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
return targets;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
function probeMismatches(probe, cut, message) {
|
|
468
|
+
const cutMismatches =
|
|
469
|
+
cut === null
|
|
470
|
+
? []
|
|
471
|
+
: compareProps(cut.pairs, probe.expectedProps, probe.forbiddenProps)
|
|
472
|
+
.mismatches;
|
|
473
|
+
const messageMismatches = compareProps(
|
|
474
|
+
message.interpretations,
|
|
475
|
+
probe.expectedProps,
|
|
476
|
+
probe.forbiddenProps,
|
|
477
|
+
).mismatches;
|
|
478
|
+
const mismatches = [
|
|
479
|
+
...cutMismatches.map((mismatch) => ({ source: "cut", ...mismatch })),
|
|
480
|
+
...messageMismatches.map((mismatch) => ({
|
|
481
|
+
source: "message",
|
|
482
|
+
...mismatch,
|
|
483
|
+
})),
|
|
484
|
+
];
|
|
485
|
+
if (message.state === "failed")
|
|
486
|
+
mismatches.push({ source: "message", type: "message_failed" });
|
|
487
|
+
return mismatches;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
function redactVerificationWords(summary) {
|
|
491
|
+
if (Array.isArray(summary?.pairs))
|
|
492
|
+
return {
|
|
493
|
+
...summary,
|
|
494
|
+
pairs: summary.pairs.map(({ prop }) => ({ prop })),
|
|
495
|
+
};
|
|
496
|
+
if (Array.isArray(summary?.interpretations))
|
|
497
|
+
return {
|
|
498
|
+
...summary,
|
|
499
|
+
interpretations: summary.interpretations.map(({ prop }) => ({ prop })),
|
|
500
|
+
};
|
|
501
|
+
return summary;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
async function listTasks(client, workspaceId) {
|
|
505
|
+
const tasksByUid = new Map();
|
|
506
|
+
for (let page = 0; page < MAX_TASK_PAGES; page += 1) {
|
|
507
|
+
const tasks = await client.request(
|
|
508
|
+
"GET",
|
|
509
|
+
`/api/v2/admin-projects/${workspaceId}/async-tasks/`,
|
|
510
|
+
{
|
|
511
|
+
query: {
|
|
512
|
+
taskType: SYNC_TASK_TYPES.join(","),
|
|
513
|
+
offset: page * TASK_PAGE_LIMIT,
|
|
514
|
+
limit: TASK_PAGE_LIMIT,
|
|
515
|
+
},
|
|
516
|
+
},
|
|
517
|
+
);
|
|
518
|
+
if (!Array.isArray(tasks))
|
|
519
|
+
fail("INVALID_RESPONSE", "synchronization task list is not an array");
|
|
520
|
+
for (const task of tasks)
|
|
521
|
+
if (task?.uid && !tasksByUid.has(task.uid))
|
|
522
|
+
tasksByUid.set(task.uid, task);
|
|
523
|
+
if (tasks.length < TASK_PAGE_LIMIT) return [...tasksByUid.values()];
|
|
524
|
+
}
|
|
525
|
+
fail(
|
|
526
|
+
"TASK_PAGINATION_LIMIT",
|
|
527
|
+
"synchronization task pagination exceeded its safety bound",
|
|
528
|
+
);
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
async function waitForMessage(client, workspaceId, threadUid, messageId, poll) {
|
|
532
|
+
while (true) {
|
|
533
|
+
const message = await client.request(
|
|
534
|
+
"GET",
|
|
535
|
+
`/api/v3/account-projects/${workspaceId}/threads/${threadUid}/messages/${messageId}/`,
|
|
536
|
+
);
|
|
537
|
+
const summary = summarizeMessage(message);
|
|
538
|
+
if (["completed", "failed"].includes(summary.state)) return message;
|
|
539
|
+
const remainingMs = poll.deadlineAt - poll.now();
|
|
540
|
+
if (remainingMs <= 0)
|
|
541
|
+
fail(
|
|
542
|
+
"MESSAGE_POLL_TIMEOUT",
|
|
543
|
+
"DataSeek message did not finish within the polling window",
|
|
544
|
+
{
|
|
545
|
+
id: messageId,
|
|
546
|
+
state: summary.state,
|
|
547
|
+
},
|
|
548
|
+
);
|
|
549
|
+
await poll.sleep(Math.min(poll.pollIntervalMs, remainingMs));
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
function collectCoverage(
|
|
554
|
+
detail,
|
|
555
|
+
taskType,
|
|
556
|
+
targets,
|
|
557
|
+
coverageByTarget,
|
|
558
|
+
coverageCandidates,
|
|
559
|
+
) {
|
|
560
|
+
const results = Array.isArray(detail?.result?.results)
|
|
561
|
+
? detail.result.results
|
|
562
|
+
: [];
|
|
563
|
+
for (const target of targets) {
|
|
564
|
+
const matched = results.filter((result) =>
|
|
565
|
+
resultMatchesTarget(result, target),
|
|
566
|
+
);
|
|
567
|
+
const covered = new Set(
|
|
568
|
+
matched
|
|
569
|
+
.filter(
|
|
570
|
+
(result) =>
|
|
571
|
+
Number(result?.status) === 0 &&
|
|
572
|
+
(!result?.skipped || Object.keys(result.skipped).length === 0),
|
|
573
|
+
)
|
|
574
|
+
.flatMap((result) => modelUids(result?.tuids)),
|
|
575
|
+
);
|
|
576
|
+
if (!covered.size) continue;
|
|
577
|
+
const missingModelUids = target.readyModelUids.filter(
|
|
578
|
+
(uid) => !covered.has(uid),
|
|
579
|
+
);
|
|
580
|
+
if (missingModelUids.length) {
|
|
581
|
+
coverageCandidates.push({
|
|
582
|
+
task: safeTask(detail, taskType),
|
|
583
|
+
targetId: target.id,
|
|
584
|
+
coveredModelUids: [...covered],
|
|
585
|
+
missingModelUids,
|
|
586
|
+
});
|
|
587
|
+
continue;
|
|
588
|
+
}
|
|
589
|
+
coverageByTarget.set(target.key, covered);
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
function resultMatchesTarget(result, target) {
|
|
594
|
+
const comparisons = [];
|
|
595
|
+
const resultId = result?.id ?? result?.dimensionId;
|
|
596
|
+
if (
|
|
597
|
+
target.id !== null &&
|
|
598
|
+
resultId !== undefined &&
|
|
599
|
+
resultId !== null &&
|
|
600
|
+
String(resultId).trim()
|
|
601
|
+
)
|
|
602
|
+
comparisons.push(String(resultId) === String(target.id));
|
|
603
|
+
const resultProp = String(result?.prop ?? "").trim();
|
|
604
|
+
if (target.prop && resultProp) comparisons.push(resultProp === target.prop);
|
|
605
|
+
const resultName = String(result?.name ?? "").trim();
|
|
606
|
+
if (target.name && resultName)
|
|
607
|
+
comparisons.push(normalizeText(resultName) === normalizeText(target.name));
|
|
608
|
+
return comparisons.length > 0 && comparisons.every(Boolean);
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
function normalizeCoverageTargets(targets) {
|
|
612
|
+
if (!Array.isArray(targets))
|
|
613
|
+
fail("INVALID_EXPECTATION", "targets must be an array");
|
|
614
|
+
return targets.map((target, index) => {
|
|
615
|
+
const readyModelUids = uniqueStrings(target?.readyModelUids);
|
|
616
|
+
if (!readyModelUids.length)
|
|
617
|
+
fail(
|
|
618
|
+
"INVALID_EXPECTATION",
|
|
619
|
+
"coverage targets require at least one ready model UID",
|
|
620
|
+
);
|
|
621
|
+
const id = target?.id ?? null;
|
|
622
|
+
const prop = String(target?.prop ?? "").trim();
|
|
623
|
+
const name = String(target?.name ?? target?.primaryName ?? "").trim();
|
|
624
|
+
if (id === null && !prop && !name)
|
|
625
|
+
fail("INVALID_EXPECTATION", "coverage targets require id, prop, or name");
|
|
626
|
+
return {
|
|
627
|
+
id,
|
|
628
|
+
prop,
|
|
629
|
+
name,
|
|
630
|
+
readyModelUids,
|
|
631
|
+
key: `${index}:${id}:${prop}:${name}`,
|
|
632
|
+
};
|
|
633
|
+
});
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
function coverageComplete(targets, coverageByTarget) {
|
|
637
|
+
return targets.every((target) =>
|
|
638
|
+
target.readyModelUids.every((uid) =>
|
|
639
|
+
coverageByTarget.get(target.key).has(uid),
|
|
640
|
+
),
|
|
641
|
+
);
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
function coverageSummary(targets, coverageByTarget) {
|
|
645
|
+
return targets.map((target) => ({
|
|
646
|
+
targetId: target.id,
|
|
647
|
+
coveredModelUids: [...coverageByTarget.get(target.key)],
|
|
648
|
+
readyModelUids: target.readyModelUids,
|
|
649
|
+
}));
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
function safeTask(task, taskType = Number(task?.taskType)) {
|
|
653
|
+
const result = objectOf(task?.result);
|
|
654
|
+
const counts = {};
|
|
655
|
+
const createdAt = validTimestamp(task?.createdAt);
|
|
656
|
+
const updatedAt = validTimestamp(task?.updatedAt);
|
|
657
|
+
for (const key of ["keywordsNum", "count"])
|
|
658
|
+
if (isFiniteCount(result[key])) counts[key] = result[key];
|
|
659
|
+
return {
|
|
660
|
+
uid: task?.uid,
|
|
661
|
+
taskType,
|
|
662
|
+
status: Number(task?.status),
|
|
663
|
+
...(createdAt === null
|
|
664
|
+
? {}
|
|
665
|
+
: { createdAt: new Date(createdAt).toISOString() }),
|
|
666
|
+
...(updatedAt === null
|
|
667
|
+
? {}
|
|
668
|
+
: { updatedAt: new Date(updatedAt).toISOString() }),
|
|
669
|
+
...(Object.keys(counts).length ? { result: counts } : {}),
|
|
670
|
+
};
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
function normalizePairs(tokens) {
|
|
674
|
+
if (!Array.isArray(tokens)) return [];
|
|
675
|
+
return uniquePairs(
|
|
676
|
+
tokens
|
|
677
|
+
.map((token) => ({
|
|
678
|
+
word: String(token?.word ?? "").trim(),
|
|
679
|
+
prop: String(token?.prop ?? "").trim(),
|
|
680
|
+
}))
|
|
681
|
+
.filter(({ word, prop }) => word && prop),
|
|
682
|
+
);
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
function compareProps(pairs, expectedProps = [], forbiddenProps = []) {
|
|
686
|
+
const actual = new Set(pairs.map(({ prop }) => prop));
|
|
687
|
+
const expected = uniqueStrings(expectedProps);
|
|
688
|
+
const forbidden = uniqueStrings(forbiddenProps);
|
|
689
|
+
const missingExpectedProps = expected.filter((prop) => !actual.has(prop));
|
|
690
|
+
const presentForbiddenProps = forbidden.filter((prop) => actual.has(prop));
|
|
691
|
+
return {
|
|
692
|
+
fields: { missingExpectedProps, presentForbiddenProps },
|
|
693
|
+
mismatches: [
|
|
694
|
+
...missingExpectedProps.map((prop) => ({
|
|
695
|
+
type: "missing_expected_prop",
|
|
696
|
+
prop,
|
|
697
|
+
})),
|
|
698
|
+
...presentForbiddenProps.map((prop) => ({
|
|
699
|
+
type: "forbidden_prop_present",
|
|
700
|
+
prop,
|
|
701
|
+
})),
|
|
702
|
+
],
|
|
703
|
+
};
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
function normalizeProbeSuites(verification) {
|
|
707
|
+
validateVerificationContainer(verification);
|
|
708
|
+
if (!Object.hasOwn(verification ?? {}, "suites"))
|
|
709
|
+
return [
|
|
710
|
+
{
|
|
711
|
+
caseId: "legacy",
|
|
712
|
+
gate: "cut_and_message",
|
|
713
|
+
probes: normalizeProbes(verification?.probes),
|
|
714
|
+
},
|
|
715
|
+
];
|
|
716
|
+
if (!Array.isArray(verification.suites) || verification.suites.length === 0)
|
|
717
|
+
fail("INVALID_SUITES", "verification suites must be a non-empty array");
|
|
718
|
+
const suites = verification.suites.map((suite) => {
|
|
719
|
+
validateVerificationSuite(suite, true);
|
|
720
|
+
const caseId = String(suite?.caseId ?? "").trim();
|
|
721
|
+
const gate = String(suite?.gate ?? "").trim();
|
|
722
|
+
if (!caseId) fail("INVALID_CASE_ID", "each suite requires a caseId");
|
|
723
|
+
if (!PROBE_GATES.has(gate))
|
|
724
|
+
fail(
|
|
725
|
+
"INVALID_GATE",
|
|
726
|
+
"suite gate must be cut_and_message or message_only",
|
|
727
|
+
);
|
|
728
|
+
return {
|
|
729
|
+
caseId,
|
|
730
|
+
gate,
|
|
731
|
+
...(suite.knowledgeTarget
|
|
732
|
+
? { knowledgeTarget: suite.knowledgeTarget }
|
|
733
|
+
: {}),
|
|
734
|
+
probes: normalizeProbes(
|
|
735
|
+
suite?.probes,
|
|
736
|
+
gate,
|
|
737
|
+
suite?.contrastRequired ?? true,
|
|
738
|
+
),
|
|
739
|
+
};
|
|
740
|
+
});
|
|
741
|
+
if (new Set(suites.map(({ caseId }) => caseId)).size !== suites.length)
|
|
742
|
+
fail("DUPLICATE_CASE_ID", "suite caseIds must be unique");
|
|
743
|
+
return suites;
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
function normalizeAuthorities(authorities) {
|
|
747
|
+
if (!Array.isArray(authorities) || authorities.length === 0)
|
|
748
|
+
fail(
|
|
749
|
+
"VERIFICATION_AUTHORITY_MISMATCH",
|
|
750
|
+
"verification requires at least one plan authority",
|
|
751
|
+
);
|
|
752
|
+
const seen = new Set();
|
|
753
|
+
return authorities.map((authority) => {
|
|
754
|
+
const caseId = String(authority?.caseId ?? "").trim();
|
|
755
|
+
const kind = String(authority?.kind ?? "").trim();
|
|
756
|
+
const gate = AUTHORITY_GATES.get(kind);
|
|
757
|
+
if (!caseId || seen.has(caseId) || gate === undefined)
|
|
758
|
+
fail(
|
|
759
|
+
"INVALID_AUTHORITIES",
|
|
760
|
+
"plan authorities are invalid for verification",
|
|
761
|
+
);
|
|
762
|
+
seen.add(caseId);
|
|
763
|
+
return {
|
|
764
|
+
caseId,
|
|
765
|
+
gate,
|
|
766
|
+
...(kind === "knowledge" && Object.hasOwn(authority.target ?? {}, "tuids")
|
|
767
|
+
? { knowledgeTarget: structuredClone(authority.target) }
|
|
768
|
+
: {}),
|
|
769
|
+
};
|
|
770
|
+
});
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
function hasNestedThreadUid(verification) {
|
|
774
|
+
const suites = Array.isArray(verification?.suites)
|
|
775
|
+
? verification.suites
|
|
776
|
+
: [{ probes: verification?.probes }];
|
|
777
|
+
return suites.some(
|
|
778
|
+
(suite) =>
|
|
779
|
+
suite?.threadUid !== undefined ||
|
|
780
|
+
(Array.isArray(suite?.probes) &&
|
|
781
|
+
suite.probes.some((probe) => probe?.threadUid !== undefined)),
|
|
782
|
+
);
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
function normalizeProbes(
|
|
786
|
+
probes,
|
|
787
|
+
gate = "cut_and_message",
|
|
788
|
+
contrastRequired = true,
|
|
789
|
+
) {
|
|
790
|
+
if (!Array.isArray(probes))
|
|
791
|
+
fail("INVALID_PROBES", "verification probes must be an array");
|
|
792
|
+
const normalized = probes.map((probe) => {
|
|
793
|
+
assertAllowedFields(
|
|
794
|
+
probe,
|
|
795
|
+
["expectedProps", "forbiddenProps", "kind", "question"],
|
|
796
|
+
"INVALID_PROBES",
|
|
797
|
+
"verification probe",
|
|
798
|
+
);
|
|
799
|
+
const kind = String(probe?.kind ?? "").trim();
|
|
800
|
+
const question = String(probe?.question ?? "").trim();
|
|
801
|
+
const expectedProps = uniqueStrings(probe?.expectedProps);
|
|
802
|
+
const forbiddenProps = uniqueStrings(probe?.forbiddenProps);
|
|
803
|
+
if (!PROBE_KINDS.has(kind))
|
|
804
|
+
fail(
|
|
805
|
+
"INVALID_PROBE_KIND",
|
|
806
|
+
"probe kind must be primary, paraphrase, or contrast",
|
|
807
|
+
);
|
|
808
|
+
if (!question) fail("INVALID_PROBES", "each probe requires a question");
|
|
809
|
+
if (
|
|
810
|
+
gate !== "message_only" &&
|
|
811
|
+
["primary", "paraphrase"].includes(kind) &&
|
|
812
|
+
expectedProps.length === 0
|
|
813
|
+
)
|
|
814
|
+
fail(
|
|
815
|
+
"INVALID_PROBES",
|
|
816
|
+
"primary and paraphrase probes require expected props",
|
|
817
|
+
);
|
|
818
|
+
if (
|
|
819
|
+
gate !== "message_only" &&
|
|
820
|
+
kind === "contrast" &&
|
|
821
|
+
forbiddenProps.length === 0
|
|
822
|
+
)
|
|
823
|
+
fail("INVALID_PROBES", "the contrast probe requires forbidden props");
|
|
824
|
+
return {
|
|
825
|
+
kind,
|
|
826
|
+
question,
|
|
827
|
+
expectedProps,
|
|
828
|
+
forbiddenProps,
|
|
829
|
+
};
|
|
830
|
+
});
|
|
831
|
+
const kinds = new Set(normalized.map(({ kind }) => kind));
|
|
832
|
+
if (!kinds.has("primary") || !kinds.has("paraphrase"))
|
|
833
|
+
fail(
|
|
834
|
+
"INVALID_PROBES",
|
|
835
|
+
"verification requires primary and paraphrase probes",
|
|
836
|
+
);
|
|
837
|
+
if (contrastRequired && !kinds.has("contrast"))
|
|
838
|
+
fail("INVALID_PROBES", "this intervention requires a contrast probe");
|
|
839
|
+
if (kinds.size !== normalized.length)
|
|
840
|
+
fail("INVALID_PROBES", "verification probe kinds must be unique");
|
|
841
|
+
return normalized;
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
function validateVerificationContainer(verification) {
|
|
845
|
+
assertAllowedFields(
|
|
846
|
+
verification,
|
|
847
|
+
["probes", "suites"],
|
|
848
|
+
"INVALID_VERIFICATION",
|
|
849
|
+
"verification input",
|
|
850
|
+
);
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
function validateVerificationSuite(suite, bound = false) {
|
|
854
|
+
assertAllowedFields(
|
|
855
|
+
suite,
|
|
856
|
+
[
|
|
857
|
+
"caseId",
|
|
858
|
+
"contrastRequired",
|
|
859
|
+
"gate",
|
|
860
|
+
"probes",
|
|
861
|
+
...(bound ? ["knowledgeTarget"] : []),
|
|
862
|
+
],
|
|
863
|
+
"INVALID_SUITES",
|
|
864
|
+
"verification suite",
|
|
865
|
+
);
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
function assertAllowedFields(value, allowed, code, label) {
|
|
869
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
870
|
+
fail(code, `${label} must be an object`);
|
|
871
|
+
const allowedFields = new Set(allowed);
|
|
872
|
+
if (Object.keys(value).some((field) => !allowedFields.has(field)))
|
|
873
|
+
fail(code, `${label} contains unsupported fields`);
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
function pollOptions(options) {
|
|
877
|
+
const pollIntervalMs = nonNegativeNumber(options.pollIntervalMs, 2000);
|
|
878
|
+
const maxWaitMs = nonNegativeNumber(options.maxWaitMs, 600000);
|
|
879
|
+
const now = options.now ?? Date.now;
|
|
880
|
+
return {
|
|
881
|
+
pollIntervalMs,
|
|
882
|
+
deadlineAt: now() + maxWaitMs,
|
|
883
|
+
now,
|
|
884
|
+
sleep:
|
|
885
|
+
options.sleep ??
|
|
886
|
+
((milliseconds) =>
|
|
887
|
+
new Promise((resolve) => setTimeout(resolve, milliseconds))),
|
|
888
|
+
};
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
function mutationTimestamp(expectation) {
|
|
892
|
+
return expectation?.mutationAt;
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
function baselineUids(expectation) {
|
|
896
|
+
return uniqueStrings(
|
|
897
|
+
expectation?.baselineTaskUids ?? expectation?.baseline?.baselineTaskUids,
|
|
898
|
+
);
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
function baselineServerWatermark(expectation) {
|
|
902
|
+
return expectation?.serverWatermark ?? expectation?.baseline?.serverWatermark;
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
function baselineCapturedAt(expectation) {
|
|
906
|
+
return expectation?.capturedAt ?? expectation?.baseline?.capturedAt;
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
function validTaskTimestamp(task) {
|
|
910
|
+
return validTimestamp(task?.createdAt);
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
function validTimestamp(value) {
|
|
914
|
+
if (typeof value !== "string" || !value.trim()) return null;
|
|
915
|
+
const timestamp = Date.parse(value);
|
|
916
|
+
return Number.isFinite(timestamp) ? timestamp : null;
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
function serverThresholdIsSatisfied(task, threshold) {
|
|
920
|
+
const createdAt = validTaskTimestamp(task);
|
|
921
|
+
return createdAt !== null && (threshold === null || createdAt > threshold);
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
function isPending(status) {
|
|
925
|
+
return [TASK_PENDING, TASK_STARTED].includes(Number(status));
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
function safeTerminalState(value) {
|
|
929
|
+
const state = String(value ?? "")
|
|
930
|
+
.trim()
|
|
931
|
+
.toLowerCase();
|
|
932
|
+
return ["cancelled", "completed", "failed", "pending", "started"].includes(
|
|
933
|
+
state,
|
|
934
|
+
)
|
|
935
|
+
? state
|
|
936
|
+
: "pending";
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
function safeIdentifier(value) {
|
|
940
|
+
return (
|
|
941
|
+
["string", "number"].includes(typeof value) && String(value).length > 0
|
|
942
|
+
);
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
function modelUids(value) {
|
|
946
|
+
return [value]
|
|
947
|
+
.flat()
|
|
948
|
+
.flatMap((item) => String(item ?? "").split(/[\s,]+/u))
|
|
949
|
+
.map((item) => item.trim())
|
|
950
|
+
.filter(Boolean);
|
|
951
|
+
}
|
|
952
|
+
|
|
953
|
+
function uniqueStrings(value) {
|
|
954
|
+
return [
|
|
955
|
+
...new Set(
|
|
956
|
+
[value]
|
|
957
|
+
.flat()
|
|
958
|
+
.map((item) => String(item ?? "").trim())
|
|
959
|
+
.filter(Boolean),
|
|
960
|
+
),
|
|
961
|
+
];
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
function nonNegativeNumber(value, fallback) {
|
|
965
|
+
const number = Number(value ?? fallback);
|
|
966
|
+
if (!Number.isFinite(number) || number < 0)
|
|
967
|
+
fail(
|
|
968
|
+
"INVALID_POLL_OPTIONS",
|
|
969
|
+
"polling bounds must be finite non-negative numbers",
|
|
970
|
+
);
|
|
971
|
+
return number;
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
function isFiniteCount(value) {
|
|
975
|
+
return typeof value === "number" && Number.isFinite(value) && value >= 0;
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
function objectOf(value) {
|
|
979
|
+
return value && typeof value === "object" && !Array.isArray(value)
|
|
980
|
+
? value
|
|
981
|
+
: {};
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
async function verifyKnowledgeTarget(client, workspaceId, target) {
|
|
985
|
+
const collection = `/api/v3/admin-projects/${workspaceId}/corpus/`;
|
|
986
|
+
let id = target.id;
|
|
987
|
+
if (id === undefined) {
|
|
988
|
+
const rows = await listAllPages(client, collection);
|
|
989
|
+
const matches = rows.filter(
|
|
990
|
+
(row) => normalizeText(row.name) === normalizeText(target.name),
|
|
991
|
+
);
|
|
992
|
+
if (matches.length !== 1)
|
|
993
|
+
fail(
|
|
994
|
+
"KNOWLEDGE_STATE_DRIFT",
|
|
995
|
+
"待验证知识已变化,请回读保存状态并重新生成方案。",
|
|
996
|
+
);
|
|
997
|
+
id = matches[0].id;
|
|
998
|
+
}
|
|
999
|
+
const current = await client.request("GET", `${collection}${id}/`);
|
|
1000
|
+
const actual = {
|
|
1001
|
+
active: current.active,
|
|
1002
|
+
corpusHash: hashArtifactValue(current.corpus),
|
|
1003
|
+
...(target.id === undefined ? {} : { id: current.id }),
|
|
1004
|
+
name: current.name,
|
|
1005
|
+
scenes: current.scenes,
|
|
1006
|
+
tuids: normalizeModelScope(current.tuids, { allowEmpty: true }),
|
|
1007
|
+
};
|
|
1008
|
+
if (canonicalJson(actual) !== canonicalJson(target))
|
|
1009
|
+
fail(
|
|
1010
|
+
"KNOWLEDGE_STATE_DRIFT",
|
|
1011
|
+
"知识正文、用途或模型范围已变化,请回读后重新验证。",
|
|
1012
|
+
);
|
|
1013
|
+
await validateWorkspaceModelScope(client, workspaceId, target.tuids);
|
|
1014
|
+
}
|