feat: complete TASK-WP3-02 runtime recommendation

This commit is contained in:
suyx
2026-08-03 03:00:12 +08:00
parent d79ac74fdd
commit 827ae912e0
12 changed files with 627 additions and 203 deletions
+21 -15
View File
@@ -5,17 +5,19 @@ function identifier(value) {
return value.replaceAll(/[^A-Za-z0-9_$]/g, "_");
}
function schemaType(schema) {
function schemaType(schema, namedSchemas = []) {
if (!schema || typeof schema !== "object") return "unknown";
if (schema.$ref) return identifier(schema.$ref.split("/").at(-1));
const named = namedSchemas.find(([, candidate]) => JSON.stringify(candidate) === JSON.stringify(schema));
if (named) return identifier(named[0]);
if (Object.hasOwn(schema, "const")) return JSON.stringify(schema.const);
if (schema.enum) return schema.enum.map((value) => JSON.stringify(value)).join(" | ");
if (schema.anyOf) return schema.anyOf.map(schemaType).join(" | ");
if (schema.oneOf) return schema.oneOf.map(schemaType).join(" | ");
if (schema.anyOf) return schema.anyOf.map((item) => schemaType(item, namedSchemas)).join(" | ");
if (schema.oneOf) return schema.oneOf.map((item) => schemaType(item, namedSchemas)).join(" | ");
if (Array.isArray(schema.type)) {
return schema.type.map((type) => schemaType({ ...schema, type })).join(" | ");
return schema.type.map((type) => schemaType({ ...schema, type }, namedSchemas)).join(" | ");
}
if (schema.type === "array") return `Array<${schemaType(schema.items)}>`;
if (schema.type === "array") return `Array<${schemaType(schema.items, namedSchemas)}>`;
if (schema.type === "boolean") return "boolean";
if (schema.type === "integer" || schema.type === "number") return "number";
if (schema.type === "null") return "null";
@@ -23,27 +25,31 @@ function schemaType(schema) {
if (schema.type === "object" || schema.properties) {
const required = new Set(schema.required ?? []);
const fields = Object.entries(schema.properties ?? {}).map(
([name, child]) => ` ${JSON.stringify(name)}${required.has(name) ? "" : "?"}: ${schemaType(child)};`,
([name, child]) => ` ${JSON.stringify(name)}${required.has(name) ? "" : "?"}: ${schemaType(child, namedSchemas)};`,
);
if (!fields.length && schema.additionalProperties && typeof schema.additionalProperties === "object") {
return `Record<string, ${schemaType(schema.additionalProperties, namedSchemas)}>`;
}
if (!fields.length && schema.additionalProperties === true) return "Record<string, unknown>";
return fields.length ? `{\n${fields.join("\n")}\n}` : "Record<string, never>";
}
return "unknown";
}
function operationResult(operation) {
function operationResult(operation, namedSchemas) {
const response = operation.responses?.["200"];
if (!response) return "unknown";
if (response.content) {
const media = response.content["application/json"] ?? response.content["text/event-stream"] ?? Object.values(response.content)[0];
if (media?.schema) return media.schema.format === "binary" ? "Blob" : schemaType(media.schema);
if (media?.schema) return media.schema.format === "binary" ? "Blob" : schemaType(media.schema, namedSchemas);
}
return response.schema ? schemaType(response.schema) : "unknown";
return response.schema ? schemaType(response.schema, namedSchemas) : "unknown";
}
function operationBodyType(operation) {
function operationBodyType(operation, namedSchemas) {
const content = operation.requestBody?.content;
const jsonSchema = content?.["application/json"]?.schema;
if (jsonSchema) return schemaType(jsonSchema);
if (jsonSchema) return schemaType(jsonSchema, namedSchemas);
if (content?.["multipart/form-data"]?.schema) return "FormData";
return undefined;
}
@@ -90,16 +96,16 @@ export function generateClient(input, output) {
const operationList = operations(document);
const builtInTypes = new Set(["Blob", "FormData", "boolean", "number", "string", "unknown"]);
const importedTypes = [...new Set(operationList.flatMap(({ operation }) => [
operationResult(operation),
operationBodyType(operation),
operationResult(operation, schemas),
operationBodyType(operation, schemas),
]).filter((type) => type && !builtInTypes.has(type) && /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(type)))];
const sdk = [
"// Generated from openapi/openapi.json. Do not edit by hand.",
importedTypes.length ? `import type { ${importedTypes.join(", ")} } from "./types.gen.js";` : "",
"export interface ClientOptions { baseUrl?: string; fetch?: typeof globalThis.fetch; headers?: HeadersInit; }",
...operationList.map(({ method, operation, path }) => {
const resultType = operationResult(operation);
const bodyType = operationBodyType(operation);
const resultType = operationResult(operation, schemas);
const bodyType = operationBodyType(operation, schemas);
const requestMediaType = operationRequestMediaType(operation);
if (operationMediaType(operation) === "text/event-stream") {
return `export function ${identifier(operation.operationId)}(options: Pick<ClientOptions, "baseUrl"> = {}): string {\n return \`${"${options.baseUrl ?? \"\"}"}${path}\`;\n}`;
+76
View File
@@ -0,0 +1,76 @@
import { createHash } from "node:crypto";
import { spawnSync } from "node:child_process";
import { existsSync, mkdirSync, writeFileSync, readFileSync } from "node:fs";
import { resolve } from "node:path";
const phaseIndex = process.argv.indexOf("--phase");
const phase = phaseIndex >= 0 ? process.argv[phaseIndex + 1] : "green";
if (!["red", "green"].includes(phase)) throw new Error(`Unsupported phase: ${phase}`);
const runId = process.env.DADA_TDD_RUN_ID ?? `wp3-02-${phase}-${new Date().toISOString().replace(/[^0-9]/g, "")}`;
const runDirectory = resolve("artifacts", "tdd", runId);
const casesDirectory = resolve(runDirectory, "cases");
if (existsSync(runDirectory)) throw new Error(`Evidence run already exists: ${runId}`);
mkdirSync(casesDirectory, { recursive: true });
const cases = [
{ acceptance: ["AC-30", "AC-51"], evidence: ["response.json", "db-diff.json", "screenshots/model-states.png"], id: "TDD-WP3-MDL-003-runtime-recommendation", requirements: ["ADMIN-03", "GEN-15"] },
{ acceptance: ["AC-51"], evidence: ["response.json", "db-diff.json", "external-calls.json", "screenshots/recovery.png"], id: "TDD-WP3-BAL-001-confirmed-recovery", requirements: ["ADMIN-09", "GEN-14", "GEN-15"] },
];
for (const item of cases) mkdirSync(resolve(casesDirectory, item.id), { recursive: true });
const commands = phase === "red"
? [["unit-red", ["exec", "vitest", "run", "tests/unit/wp3-02-runtime-recommendation.test.ts"]], ["integration-red", ["exec", "vitest", "run", "tests/integration/wp3-02-runtime-recovery.test.ts"]]]
: [
["integration", ["test:integration"]],
["worker", ["test:worker"]],
["e2e", ["test:e2e"]],
["unit", ["test:unit"]],
["api", ["test:api"]],
["tdd-trace", ["validate:tdd-trace"]],
];
const environment = {
...process.env,
DADA_EVIDENCE_DIR_RUNTIME: casesDirectory,
DADA_PLAYWRIGHT_OUTPUT_DIR: resolve("test-results", runId, "e2e"),
};
const commandResults = [];
for (const [name, args] of commands) {
const command = `pnpm ${args.join(" ")}`;
const started_at = new Date().toISOString();
const result = spawnSync(process.env.ComSpec ?? "cmd.exe", ["/d", "/s", "/c", command], { encoding: "utf8", env: environment });
if (result.stdout) process.stdout.write(result.stdout);
if (result.stderr) process.stderr.write(result.stderr);
commandResults.push({ command, exit_code: result.status ?? 1, finished_at: new Date().toISOString(), name, started_at });
}
const commandState = phase === "red" ? commandResults.every((result) => result.exit_code !== 0) : commandResults.every((result) => result.exit_code === 0);
const manifest = { path: "tasks.manifest.json", sha256: createHash("sha256").update(readFileSync("tasks.manifest.json")).digest("hex").toUpperCase() };
const commit = spawnSync("git", ["rev-parse", "--short", "HEAD"], { encoding: "utf8" }).stdout.trim();
const dirty = spawnSync("git", ["status", "--porcelain"], { encoding: "utf8" }).stdout.trim().length > 0;
const summaries = [];
for (const item of cases) {
const directory = resolve(casesDirectory, item.id);
writeFileSync(resolve(directory, "commands.json"), `${JSON.stringify({ commands: commandResults, phase, run_id: runId }, null, 2)}\n`);
const evidenceRefs = phase === "red" ? ["red-observation.json"] : item.evidence;
if (phase === "red") writeFileSync(resolve(directory, "red-observation.json"), `${JSON.stringify({
expected_failure: "TASK-WP3-02 had no runtime recommendation derivation and WP2-06 worker runtime used an incompatible schema",
observed_commands: ["pnpm vitest run tests/unit/wp3-02-runtime-recommendation.test.ts", "pnpm vitest run tests/integration/wp3-02-runtime-recovery.test.ts"],
observed_errors: ["recommended_model_id was null", "SqliteError: table model_runtime_availability has no column named gateway_account_ref"],
status: "red_confirmed",
}, null, 2)}\n`);
const missingEvidence = evidenceRefs.filter((file) => !existsSync(resolve(directory, file)));
const targetStatus = phase === "red" ? "red_confirmed" : "passed";
const status = commandState && missingEvidence.length === 0 ? targetStatus : "failed";
writeFileSync(resolve(directory, "result.json"), `${JSON.stringify({
acceptance_criteria: item.acceptance, automation: ["automated"], commit, evidence_refs: evidenceRefs,
manifest, missing_evidence: missingEvidence, phase, requirements: item.requirements,
run_id: runId, status, task_id: "TASK-WP3-02", test_id: item.id, work_package: "WP-3",
worktree_under_test: dirty ? "uncommitted implementation" : "clean committed implementation",
}, null, 2)}\n`);
summaries.push({ missing_evidence: missingEvidence, status, test_id: item.id });
}
const targetStatus = phase === "red" ? "red_confirmed" : "passed";
const status = summaries.every((item) => item.status === targetStatus) ? targetStatus : "failed";
writeFileSync(resolve(runDirectory, "evidence.json"), `${JSON.stringify({ cases: summaries, phase, run_id: runId, status }, null, 2)}\n`);
console.log(JSON.stringify({ cases: summaries, phase, run_id: runId, status }, null, 2));
if (status !== targetStatus) process.exit(1);