@buaa_smat/hometrans 26.0.4 → 26.0.5

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.
Files changed (102) hide show
  1. package/agents/code-reviewer.md +2 -0
  2. package/agents/model-render-worker.md +1 -1
  3. package/i18n/agents/code-reviewer.md +2 -0
  4. package/i18n/agents/model-render-worker.md +1 -1
  5. package/i18n/skills/a2h-app-migrate/SKILL.md +51 -3
  6. package/i18n/skills/a2h-app-migrate/references/workflow.yaml +140 -32
  7. package/i18n/skills/a2h-app-migrate/scripts/dag_graph.mjs +297 -0
  8. package/i18n/skills/a2h-app-migrate/scripts/validate_workflow.mjs +134 -21
  9. package/i18n/skills/a2h-app-migrate/test/test_finalize.mjs +21 -4
  10. package/i18n/skills/a2h-app-migrate/test/test_image_probe.mjs +3 -3
  11. package/i18n/skills/a2h-app-migrate/test/test_resume_plan.mjs +1 -1
  12. package/i18n/skills/a2h-app-migrate/test/test_reuse_static_utg.mjs +1 -1
  13. package/i18n/skills/a2h-app-migrate/test/test_utg_static_done.mjs +12 -5
  14. package/i18n/skills/a2h-arkts-logic-coder/SKILL.md +7 -8
  15. package/i18n/skills/a2h-arkts-logic-coder/references/ISSUES.md +1 -1
  16. package/i18n/skills/a2h-arkts-logic-coder/references/code-generation.md +27 -15
  17. package/i18n/skills/a2h-batch-ui-align/SKILL.md +1 -1
  18. package/i18n/skills/a2h-batch-ui-align/references/cards/widgets.md +4 -0
  19. package/i18n/skills/a2h-batch-ui-align/references/checker-rules.md +36 -0
  20. package/i18n/skills/a2h-batch-ui-align/references/conversion-procedure.md +4 -0
  21. package/i18n/skills/a2h-batch-ui-align/references/mappings/android-to-harmonyOS-ui-layout-mapping-reference.md +4 -0
  22. package/i18n/skills/a2h-batch-ui-align/references/step5-convert.md +3 -3
  23. package/i18n/skills/a2h-batch-ui-align/scripts/static_check/main.ts +8 -0
  24. package/i18n/skills/a2h-batch-ui-align/scripts/static_check/rules/overlay_in_linear.ts +164 -0
  25. package/i18n/skills/a2h-batch-ui-align/scripts/static_check/rules/self_drawn_sheet.ts +105 -0
  26. package/i18n/skills/a2h-batch-ui-align/scripts/static_check/rules/staggered_lanes.ts +127 -0
  27. package/i18n/skills/a2h-batch-ui-align/scripts/static_check/rules/window_size_constant.ts +114 -0
  28. package/i18n/skills/a2h-page-specgen/references/scenario-authoring.md +4 -1
  29. package/i18n/skills/a2h-page-specgen/references/spec-example.md +35 -5
  30. package/i18n/skills/a2h-page-specgen/references/spec-template.md +4 -4
  31. package/i18n/skills/a2h-spec-generate/SKILL.md +3 -1
  32. package/i18n/skills/a2h-spec-generate/references/fusion-task-contract.md +15 -3
  33. package/i18n/skills/a2h-spec-generate/references/review-checklist.md +1 -0
  34. package/i18n/skills/a2h-spec-generate/references/spec-sample-complex.md +16 -0
  35. package/i18n/skills/a2h-spec-generate/references/spec-sample-transfer.md +16 -0
  36. package/i18n/skills/a2h-spec-generate/references/spec-samples.md +5 -1
  37. package/i18n/skills/a2h-spec-generate/references/synthesis-rules.md +21 -1
  38. package/i18n/skills/a2h-spec-generate/references/worker-rules.md +25 -2
  39. package/i18n/skills/a2h-spec-generate/scripts/check_spec.ts +121 -6
  40. package/i18n/skills/a2h-spec-generate/scripts/reqgen/src/fusion/task-contract.ts +2 -1
  41. package/i18n/skills/a2h-spec-generate/scripts/reqgen/test/fusion-behavior-e2e.test.ts +3 -0
  42. package/i18n/skills/a2h-spec-generate/scripts/reqgen/test/fusion-spec.test.ts +16 -2
  43. package/i18n/skills/a2h-spec-generate/test/check-fused-spec.test.mjs +11 -2
  44. package/i18n/skills/a2h-spec-generate/test/check-spec.test.ts +35 -0
  45. package/package.json +1 -1
  46. package/skills/a2h-app-migrate/SKILL.md +53 -5
  47. package/skills/a2h-app-migrate/references/workflow.yaml +140 -32
  48. package/skills/a2h-app-migrate/scripts/assert_dag_complete.mjs +226 -0
  49. package/skills/a2h-app-migrate/scripts/dag_graph.mjs +297 -0
  50. package/skills/a2h-app-migrate/scripts/dag_process_executor.mjs +199 -0
  51. package/skills/a2h-app-migrate/scripts/dag_production_executor.mjs +897 -0
  52. package/skills/a2h-app-migrate/scripts/dag_runner.mjs +1767 -0
  53. package/skills/a2h-app-migrate/scripts/ledger.mjs +15 -2
  54. package/skills/a2h-app-migrate/scripts/lib-yaml-lite.mjs +8 -2
  55. package/skills/a2h-app-migrate/scripts/resume_plan.mjs +3 -8
  56. package/skills/a2h-app-migrate/scripts/validate_workflow.mjs +134 -21
  57. package/skills/a2h-app-migrate/test/test_dag_complete.mjs +222 -0
  58. package/skills/a2h-app-migrate/test/test_dag_production.mjs +670 -0
  59. package/skills/a2h-app-migrate/test/test_dag_runner.mjs +423 -0
  60. package/skills/a2h-app-migrate/test/test_finalize.mjs +21 -4
  61. package/skills/a2h-app-migrate/test/test_image_probe.mjs +1 -1
  62. package/skills/a2h-app-migrate/test/test_resume_plan.mjs +16 -1
  63. package/skills/a2h-app-migrate/test/test_reuse_static_utg.mjs +1 -1
  64. package/skills/a2h-app-migrate/test/test_utg_static_done.mjs +12 -5
  65. package/skills/a2h-arkts-logic-coder/SKILL.md +7 -8
  66. package/skills/a2h-arkts-logic-coder/references/ISSUES.md +1 -1
  67. package/skills/a2h-arkts-logic-coder/references/code-generation.md +27 -15
  68. package/skills/a2h-batch-ui-align/SKILL.md +1 -1
  69. package/skills/a2h-batch-ui-align/references/cards/widgets.md +4 -0
  70. package/skills/a2h-batch-ui-align/references/checker-rules.md +36 -0
  71. package/skills/a2h-batch-ui-align/references/conversion-procedure.md +4 -0
  72. package/skills/a2h-batch-ui-align/references/mappings/android-to-harmonyOS-ui-layout-mapping-reference.md +4 -0
  73. package/skills/a2h-batch-ui-align/references/step5-convert.md +3 -3
  74. package/skills/a2h-batch-ui-align/scripts/group_queue.mjs +49 -3
  75. package/skills/a2h-batch-ui-align/scripts/static_check/core/android.ts +36 -0
  76. package/skills/a2h-batch-ui-align/scripts/static_check/main.ts +8 -0
  77. package/skills/a2h-batch-ui-align/scripts/static_check/registry.ts +8 -0
  78. package/skills/a2h-batch-ui-align/scripts/static_check/rules/overlay_in_linear.ts +164 -0
  79. package/skills/a2h-batch-ui-align/scripts/static_check/rules/self_drawn_sheet.ts +105 -0
  80. package/skills/a2h-batch-ui-align/scripts/static_check/rules/staggered_lanes.ts +127 -0
  81. package/skills/a2h-batch-ui-align/scripts/static_check/rules/window_size_constant.ts +114 -0
  82. package/skills/a2h-batch-ui-align/scripts/tests/arkts_static_check_coolmall_ds1123.test.mjs +433 -0
  83. package/skills/a2h-batch-ui-align/scripts/tests/arkts_static_check_overlay_in_linear.test.mjs +288 -0
  84. package/skills/a2h-batch-ui-align/scripts/tests/group_queue.test.mjs +46 -0
  85. package/skills/a2h-page-specgen/references/scenario-authoring.md +4 -1
  86. package/skills/a2h-page-specgen/references/spec-example.md +35 -5
  87. package/skills/a2h-page-specgen/references/spec-template.md +4 -4
  88. package/skills/a2h-spec-generate/SKILL.md +3 -1
  89. package/skills/a2h-spec-generate/references/fusion-task-contract.md +15 -3
  90. package/skills/a2h-spec-generate/references/review-checklist.md +1 -0
  91. package/skills/a2h-spec-generate/references/spec-sample-complex.md +16 -0
  92. package/skills/a2h-spec-generate/references/spec-sample-transfer.md +16 -0
  93. package/skills/a2h-spec-generate/references/spec-samples.md +5 -1
  94. package/skills/a2h-spec-generate/references/synthesis-rules.md +21 -1
  95. package/skills/a2h-spec-generate/references/worker-rules.md +25 -2
  96. package/skills/a2h-spec-generate/scripts/check-fused-spec.mjs +3 -1
  97. package/skills/a2h-spec-generate/scripts/check_spec.ts +262 -6
  98. package/skills/a2h-spec-generate/scripts/reqgen/src/fusion/task-contract.ts +12 -2
  99. package/skills/a2h-spec-generate/scripts/reqgen/test/fusion-behavior-e2e.test.ts +3 -0
  100. package/skills/a2h-spec-generate/scripts/reqgen/test/fusion-spec.test.ts +16 -2
  101. package/skills/a2h-spec-generate/test/check-fused-spec.test.mjs +54 -3
  102. package/skills/a2h-spec-generate/test/check-spec.test.ts +247 -0
@@ -0,0 +1,670 @@
1
+ import assert from "node:assert/strict";
2
+ import { spawnSync } from "node:child_process";
3
+ import fs from "node:fs";
4
+ import os from "node:os";
5
+ import path from "node:path";
6
+ import test from "node:test";
7
+ import { fileURLToPath } from "node:url";
8
+
9
+ import {
10
+ advance,
11
+ completeAttempt,
12
+ confirmGate,
13
+ loadRunState,
14
+ presentationForTransition,
15
+ rewindGate,
16
+ startRun,
17
+ } from "../scripts/dag_runner.mjs";
18
+ import { buildGraph, readyNodes, rewindAffected } from "../scripts/dag_graph.mjs";
19
+ import { readLedger } from "../scripts/ledger.mjs";
20
+ import { formatReport } from "../scripts/env_preflight.mjs";
21
+ import yaml from "../scripts/lib-yaml-lite.mjs";
22
+
23
+ const HERE = path.dirname(fileURLToPath(import.meta.url));
24
+ const SKILL = path.join(HERE, "..");
25
+ const WORKFLOW = path.join(SKILL, "references", "workflow.yaml");
26
+ const EXECUTOR = path.join(SKILL, "scripts", "dag_production_executor.mjs");
27
+ const RUNNER = path.join(SKILL, "scripts", "dag_runner.mjs");
28
+ const originalWorkflow = yaml.load(fs.readFileSync(WORKFLOW, "utf8"));
29
+
30
+ function writeJson(file, value) {
31
+ fs.mkdirSync(path.dirname(file), { recursive: true });
32
+ fs.writeFileSync(file, JSON.stringify(value, null, 2) + "\n");
33
+ }
34
+
35
+ function flow(value) {
36
+ if (Array.isArray(value)) return `[${value.map(flow).join(", ")}]`;
37
+ if (value && typeof value === "object") {
38
+ return `{${Object.entries(value).map(([key, item]) => `${JSON.stringify(key)}: ${flow(item)}`).join(", ")}}`;
39
+ }
40
+ return JSON.stringify(value ?? null);
41
+ }
42
+
43
+ function writeWorkflow(file, doc) {
44
+ fs.writeFileSync(file, `${Object.entries(doc).map(([key, value]) => `${key}: ${flow(value)}`).join("\n")}\n`);
45
+ }
46
+
47
+ function runProtocol(request) {
48
+ writeJson(request.request_path, request);
49
+ const outcome = spawnSync(process.execPath, [EXECUTOR], {
50
+ env: {
51
+ ...process.env,
52
+ A2H_DAG_REQUEST: request.request_path,
53
+ A2H_DAG_RESULT: request.result_path,
54
+ A2H_DAG_ATTEMPT: request.attempt_id,
55
+ },
56
+ encoding: "utf8",
57
+ });
58
+ assert.equal(outcome.status, 0, outcome.stderr);
59
+ return JSON.parse(fs.readFileSync(request.result_path, "utf8"));
60
+ }
61
+
62
+ function runCallback(request, command, payload) {
63
+ const payloadPath = path.join(path.dirname(request.request_path), `${request.attempt.file_id}.${command}.json`);
64
+ writeJson(payloadPath, payload);
65
+ const outcome = spawnSync(process.execPath, [
66
+ EXECUTOR,
67
+ command,
68
+ "--request", request.request_path,
69
+ command === "answer" ? "--answer" : "--outcome", payloadPath,
70
+ ], { encoding: "utf8" });
71
+ assert.equal(outcome.status, 0, outcome.stderr);
72
+ return JSON.parse(fs.readFileSync(request.result_path, "utf8"));
73
+ }
74
+
75
+ function manualRequest(root, node, runner, vars, nodeDefinition = {}) {
76
+ const dagRunDir = path.join(root, "dag-control");
77
+ const fileId = "attempt-1";
78
+ return {
79
+ run_id: "run-manual",
80
+ node,
81
+ node_id: node,
82
+ node_definition: { id: node, actions: [], ...nodeDefinition },
83
+ node_state: { generation: 0 },
84
+ runner: { action: "production", ...runner },
85
+ invoke: "",
86
+ vars,
87
+ paths: {
88
+ dag_run_dir: dagRunDir,
89
+ ledger: path.join(root, "stage-ledger.jsonl"),
90
+ state_path: path.join(dagRunDir, "dag-run-state.json"),
91
+ },
92
+ attempt_id: `run-manual:${node}:1`,
93
+ attempt: { file_id: fileId },
94
+ request_path: path.join(dagRunDir, "attempts", `${fileId}.request.json`),
95
+ result_path: path.join(dagRunDir, "attempts", `${fileId}.result.json`),
96
+ };
97
+ }
98
+
99
+ function lightweightProductionWorkflow() {
100
+ const doc = structuredClone(originalWorkflow);
101
+ for (const node of doc.nodes) {
102
+ node.success = node.id === "logic.finalize" ? [{ file_exists: "${LOGIC_REPORT}" }] : [];
103
+ if (["bootstrap", "workspace", "script", "snapshot", "adopt_specs"].includes(node.runner.kind)) {
104
+ node.actions = [];
105
+ delete node.precondition;
106
+ }
107
+ if (node.runner.kind === "skill") {
108
+ node.before_run = [];
109
+ node.after_run = [];
110
+ }
111
+ }
112
+ return doc;
113
+ }
114
+
115
+ function productionFixture(doc) {
116
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), "dag-production-"));
117
+ const workflow = path.join(root, "workflow.yaml");
118
+ writeWorkflow(workflow, doc);
119
+ const hmos = path.join(root, "hmos");
120
+ const work = path.join(root, "production-work");
121
+ const runDir = path.join(work, "reqgen", "run-1");
122
+ const dagRunDir = path.join(root, "dag-control");
123
+ const statePath = path.join(dagRunDir, "dag-run-state.json");
124
+ const vars = {
125
+ ANDROID: path.join(root, "android"),
126
+ HMOS: hmos,
127
+ WORK: work,
128
+ RUN_DIR: runDir,
129
+ APK_PATH: path.join(root, "app.apk"),
130
+ ENV_PREFLIGHT_PASSED: true,
131
+ INTERACTION_MODE: "interactive",
132
+ INTERACTIVE_GATES: "review.arch,review.spec_gen,review.ui_batch",
133
+ IMAGE_READ: false,
134
+ };
135
+ return {
136
+ root,
137
+ workflow,
138
+ hmos,
139
+ work,
140
+ runDir,
141
+ dagRunDir,
142
+ statePath,
143
+ ledger: path.join(work, "stage-ledger.jsonl"),
144
+ logicReport: path.join(work, "logic-convert-report.md"),
145
+ logicState: path.join(hmos, ".hometrans", "spec_pipeline_state.json"),
146
+ vars,
147
+ };
148
+ }
149
+
150
+ function startFixture(f) {
151
+ return startRun({
152
+ workflow: f.workflow,
153
+ runDir: f.dagRunDir,
154
+ state: f.statePath,
155
+ profile: "full",
156
+ vars: f.vars,
157
+ });
158
+ }
159
+
160
+ function writeLogicEvidence(f) {
161
+ writeJson(f.logicState, {
162
+ schema: "spec_pipeline_state/v2",
163
+ groups: [{ G: 1, status: "done", chunks: [] }],
164
+ specs: [{ status: "done" }],
165
+ finalization: { done: true, homecheck: "PASS", smoke: "PASS", final_build: "SUCCESS" },
166
+ });
167
+ }
168
+
169
+ test("the real workflow maps every node to a production Runner kind without reuse/skip ambiguity", () => {
170
+ const expectedKinds = {
171
+ bootstrap: "bootstrap",
172
+ "discover.android_index": "script",
173
+ "boot.workspace": "workspace",
174
+ "discover.arch": "skill",
175
+ "discover.utg_static": "skill",
176
+ "review.arch": "gate",
177
+ "discover.utg_snapshot": "snapshot",
178
+ "discover.feature_scope": "host_action",
179
+ "ui.batch": "skill",
180
+ "review.ui_batch": "gate",
181
+ "enrich.spec_gen": "skill",
182
+ "review.spec_gen": "gate",
183
+ "enrich.module_docs": "skill",
184
+ "barrier.l2_enrich": "barrier",
185
+ "adopt.signing": "host_action",
186
+ "adopt.specs_json": "adopt_specs",
187
+ "logic.spec_pipeline": "skill",
188
+ "logic.finalize": "host_action",
189
+ };
190
+ assert.equal(originalWorkflow.nodes.length, Object.keys(expectedKinds).length);
191
+ assert.deepEqual(originalWorkflow.serial_order, Object.keys(expectedKinds));
192
+ for (const node of originalWorkflow.nodes) {
193
+ assert.equal(node.runner.action, "production", node.id);
194
+ assert.equal(node.runner.kind, expectedKinds[node.id], node.id);
195
+ const crossNode = node.id === "discover.utg_static" ? "${REUSE_SPEC_GENERATE}" : "";
196
+ assert.doesNotMatch(String(node.skip_if || "").replace(crossNode, ""), /REUSE_/, node.id);
197
+ }
198
+ assert.deepEqual(
199
+ originalWorkflow.nodes.filter((node) => node.runner.after_terminal === "resume_summary").map((node) => node.id),
200
+ ["enrich.spec_gen", "ui.batch", "logic.spec_pipeline"],
201
+ );
202
+ });
203
+
204
+ test("Runner CLI returns committed presentation events for the host", () => {
205
+ const f = productionFixture(lightweightProductionWorkflow());
206
+ const outcome = spawnSync(process.execPath, [
207
+ RUNNER, "run",
208
+ "--workflow", f.workflow,
209
+ "--run-dir", f.dagRunDir,
210
+ "--profile", "full",
211
+ "--vars", JSON.stringify(f.vars),
212
+ ], { encoding: "utf8" });
213
+ assert.equal(outcome.status, 1, outcome.stderr);
214
+ const result = JSON.parse(outcome.stdout);
215
+ assert.equal(result.status, "waiting_external");
216
+ assert.deepEqual(result.presentation.checklist, originalWorkflow.policy.progress_checklist);
217
+ assert.ok(result.presentation.events.some((event) => event.node === "discover.arch" && event.kind === "doing"));
218
+ assert.ok(result.presentation.events.some((event) => event.node === "bootstrap" && event.kind === "terminal" && event.status === "done"));
219
+ assert.equal(result.presentation.events.find((event) => event.node === "discover.arch").user_facing.doing, originalWorkflow.nodes.find((node) => node.id === "discover.arch").user_facing.doing);
220
+ });
221
+
222
+ test("presentation includes new host notices once and never invents terminal completion", () => {
223
+ const before = {
224
+ nodes: { bootstrap: { status: "waiting_external", current_attempt_id: "boot-1" } },
225
+ attempts: [{ attempt_id: "boot-1", node: "bootstrap" }],
226
+ };
227
+ const after = {
228
+ vars: {},
229
+ nodes: { bootstrap: { status: "done", current_attempt_id: "boot-1" } },
230
+ attempts: [{
231
+ attempt_id: "boot-1",
232
+ node: "bootstrap",
233
+ result: { status: "done", evidence: { notices: [{ action: "tip_image_read_false", text: "Show the multimodal warning." }] } },
234
+ }],
235
+ };
236
+ const shown = presentationForTransition(before, after, originalWorkflow);
237
+ assert.deepEqual(shown.events.map((event) => event.kind), ["notice", "terminal"]);
238
+ assert.equal(shown.events[0].text, "Show the multimodal warning.");
239
+ assert.equal(shown.events[1].status, "done");
240
+ assert.deepEqual(presentationForTransition(after, after, originalWorkflow).events, []);
241
+ });
242
+
243
+ test("bootstrap backfill skips protected PROFILE from real PREFLIGHT_VARS output", () => {
244
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), "dag-production-preflight-profile-"));
245
+ const preflight = path.join(root, "fake-preflight.mjs");
246
+ const output = formatReport({
247
+ exit_code: 0,
248
+ markdown_table: "# 环境预检",
249
+ profile: "full",
250
+ engine_mode: "rich",
251
+ });
252
+ fs.writeFileSync(preflight, `process.stdout.write(${JSON.stringify(output)});\n`);
253
+ const setVars = originalWorkflow.nodes.find((node) => node.id === "bootstrap").actions
254
+ .map((action) => action.set_vars_from_preflight).find(Boolean);
255
+ const request = manualRequest(root, "bootstrap", { kind: "bootstrap" }, { PROFILE: "full" }, {
256
+ actions: [
257
+ { run_env_preflight: { script: preflight, args: {} } },
258
+ { set_vars_from_preflight: setVars },
259
+ ],
260
+ });
261
+
262
+ const result = runProtocol(request);
263
+ assert.equal(result.status, "done", result.reason);
264
+ assert.equal(Object.hasOwn(result.set_vars, "PROFILE"), false);
265
+ assert.equal(result.set_vars.ENGINE_MODE, "rich");
266
+ assert.equal(result.set_vars.ENV_PREFLIGHT_PASSED, true);
267
+ });
268
+
269
+ function spawnProtocol(request) {
270
+ writeJson(request.request_path, request);
271
+ return spawnSync(process.execPath, [EXECUTOR], {
272
+ env: { ...process.env, A2H_DAG_REQUEST: request.request_path, A2H_DAG_RESULT: request.result_path, A2H_DAG_ATTEMPT: request.attempt_id },
273
+ encoding: "utf8",
274
+ });
275
+ }
276
+
277
+ function spawnCallback(request, command, payload) {
278
+ const payloadPath = path.join(path.dirname(request.request_path), `${request.attempt.file_id}.${command}.json`);
279
+ writeJson(payloadPath, payload);
280
+ return spawnSync(process.execPath, [EXECUTOR, command, "--request", request.request_path,
281
+ command === "answer" ? "--answer" : "--outcome", payloadPath], { encoding: "utf8" });
282
+ }
283
+
284
+ test("a pre-workspace implementation gap blocks without production paths or a business ledger", async () => {
285
+ const doc = lightweightProductionWorkflow();
286
+ doc.nodes.find((node) => node.id === "bootstrap").actions = [{ unsupported_test_action: {} }];
287
+ const f = productionFixture(doc);
288
+ const started = startFixture(f);
289
+ assert.equal(started.ledger_started, false);
290
+ assert.equal(fs.existsSync(f.ledger), false);
291
+
292
+ const state = await advance({ state: f.statePath });
293
+ assert.equal(state.status, "blocked", "an executor implementation gap is never a business failure");
294
+ assert.equal(state.nodes.bootstrap.status, "blocked");
295
+ assert.match(state.error.message, /UNSUPPORTED_PRODUCTION_ACTION/);
296
+ assert.equal(state.ledger_started, false);
297
+ assert.deepEqual(state.pending_ledger, []);
298
+ assert.equal(fs.existsSync(f.ledger), false);
299
+ assert.equal(fs.existsSync(f.work), false);
300
+ assert.equal(fs.existsSync(f.hmos), false);
301
+ });
302
+
303
+ test("a pre-workspace production failure writes neither production paths nor a business ledger", async () => {
304
+ const doc = lightweightProductionWorkflow();
305
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), "dag-preflight-fail-"));
306
+ const failing = path.join(root, "preflight-fails.mjs");
307
+ fs.writeFileSync(failing, "process.stderr.write('device missing'); process.exit(1);\n");
308
+ doc.nodes.find((node) => node.id === "bootstrap").actions = [{ run_env_preflight: { script: failing, runner: "node", args: {} } }];
309
+ const f = productionFixture(doc);
310
+ const started = startFixture(f);
311
+ assert.equal(started.ledger_started, false);
312
+ assert.equal(fs.existsSync(f.ledger), false);
313
+
314
+ const state = await advance({ state: f.statePath });
315
+ assert.equal(state.status, "failed");
316
+ assert.match(state.error.message, /PRODUCTION_SCRIPT_FAILED/);
317
+ assert.equal(state.ledger_started, false);
318
+ assert.equal(state.error.ledger_started, false, "the host must be told this run has no ledger records");
319
+ assert.match(state.error.hint, /重新 run/);
320
+ assert.match(state.error.hint, /不要用 run --resume-from/);
321
+ assert.equal(state.pending_ledger.length, 1);
322
+ assert.equal(state.pending_ledger[0].node, "bootstrap");
323
+ assert.equal(fs.existsSync(f.ledger), false);
324
+ assert.equal(fs.existsSync(f.work), false);
325
+ assert.equal(fs.existsSync(f.hmos), false);
326
+ assert.equal(fs.existsSync(f.statePath), true, "only DAG control state is written before preflight");
327
+ });
328
+
329
+ test("executor protocol/config errors exit non-zero without a result; only business verdicts become failed", () => {
330
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), "dag-production-errors-"));
331
+ // Config error on a soft_continue node: must block, not become a soft failure.
332
+ const badSkill = manualRequest(root, "ui.batch", { kind: "skill" }, {}, { skill: "${UNRESOLVED_SKILL}", before_run: [], after_run: [] });
333
+ const crashed = spawnProtocol(badSkill);
334
+ assert.notEqual(crashed.status, 0);
335
+ assert.match(crashed.stderr, /INVALID_SKILL/);
336
+ assert.equal(fs.existsSync(badSkill.result_path), false, "no strict result, so the Runner blocks");
337
+
338
+ // Business verdict: a contract script exiting non-zero stays a strict failed result.
339
+ const script = path.join(root, "fails.mjs");
340
+ fs.writeFileSync(script, "process.exit(3);\n");
341
+ const scriptFail = manualRequest(path.join(root, "b"), "enrich.module_docs", { kind: "skill" }, {}, {
342
+ skill: "a2h-module-docs", before_run: [{ mark_module_docs_start: { script, args: "" } }], after_run: [],
343
+ });
344
+ const failed = runProtocol(scriptFail);
345
+ assert.equal(failed.status, "failed");
346
+ assert.match(failed.reason, /PRODUCTION_SCRIPT_FAILED/);
347
+ });
348
+
349
+ test("callback errors leave the waiting result intact; host ok:false is the only callback business failure", () => {
350
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), "dag-production-callback-"));
351
+ const request = manualRequest(root, "ui.batch", { kind: "skill" }, {}, { skill: "a2h-batch-ui-align", before_run: [], after_run: [] });
352
+ assert.equal(runProtocol(request).status, "waiting_external");
353
+ const waiting = fs.readFileSync(request.result_path, "utf8");
354
+
355
+ const invalid = spawnCallback(request, "complete", { attempt_id: request.attempt_id, ok: true, set_vars: { PROFILE: "full" } });
356
+ assert.notEqual(invalid.status, 0);
357
+ assert.match(invalid.stderr, /INVALID_SET_VARS/);
358
+ assert.equal(fs.readFileSync(request.result_path, "utf8"), waiting, "INVALID_SET_VARS must not be written as failed");
359
+
360
+ const stale = spawnCallback(request, "complete", { attempt_id: "run-manual:ui.batch:0", ok: true });
361
+ assert.notEqual(stale.status, 0);
362
+ assert.match(stale.stderr, /STALE_ATTEMPT/);
363
+ assert.equal(fs.readFileSync(request.result_path, "utf8"), waiting);
364
+
365
+ const hostFailed = runCallback(request, "complete", { attempt_id: request.attempt_id, ok: false, reason: "device lost" });
366
+ assert.equal(hostFailed.status, "failed");
367
+ assert.equal(hostFailed.reason, "device lost");
368
+ });
369
+
370
+ test("skill action cursor resumes only an interrupted attempt, not a business retry", () => {
371
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), "dag-production-cursor-"));
372
+ const countFile = path.join(root, "count.json");
373
+ const counter = path.join(root, "counter.mjs");
374
+ fs.writeFileSync(counter, `
375
+ import fs from "node:fs";
376
+ const file = process.argv[2];
377
+ let count = 0;
378
+ try { count = JSON.parse(fs.readFileSync(file, "utf8")); } catch {}
379
+ fs.writeFileSync(file, JSON.stringify(count + 1));
380
+ `);
381
+ const beforeRun = [{ clear_gate_checkpoint: { script: counter, args: "${COUNT}" } }];
382
+ const first = manualRequest(root, "discover.arch", { kind: "skill" }, { COUNT: countFile }, {
383
+ skill: "a2h-arch-generate",
384
+ before_run: beforeRun,
385
+ after_run: [],
386
+ });
387
+ const waiting = runProtocol(first);
388
+ assert.equal(waiting.status, "waiting_external");
389
+ assert.equal(JSON.parse(fs.readFileSync(countFile, "utf8")), 1);
390
+
391
+ const resumed = structuredClone(first);
392
+ resumed.attempt_id = "run-manual:discover.arch:2";
393
+ resumed.attempt = { file_id: "attempt-2", resume_from_attempt: "attempt-1" };
394
+ resumed.request_path = path.join(root, "dag-control", "attempts", "attempt-2.request.json");
395
+ resumed.result_path = path.join(root, "dag-control", "attempts", "attempt-2.result.json");
396
+ const resumedWaiting = runProtocol(resumed);
397
+ assert.equal(resumedWaiting.status, "waiting_external");
398
+ assert.equal(JSON.parse(fs.readFileSync(countFile, "utf8")), 1, "committed before_run actions are not replayed");
399
+ const completed = runCallback(resumed, "complete", { attempt_id: resumed.attempt_id, ok: true });
400
+ assert.equal(completed.status, "done");
401
+
402
+ const retry = structuredClone(first);
403
+ retry.attempt_id = "run-manual:discover.arch:3";
404
+ retry.attempt = { file_id: "attempt-3" };
405
+ retry.request_path = path.join(root, "dag-control", "attempts", "attempt-3.request.json");
406
+ retry.result_path = path.join(root, "dag-control", "attempts", "attempt-3.result.json");
407
+ assert.equal(runProtocol(retry).status, "waiting_external");
408
+ assert.equal(JSON.parse(fs.readFileSync(countFile, "utf8")), 2, "a new business attempt owns a fresh cursor");
409
+ });
410
+
411
+ test("image-read host assistance uses isolated paths for both HMOS modes and cleans up after callback", () => {
412
+ for (const mode of ["existing_project", "new_project"]) {
413
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), `dag-production-image-${mode}-`));
414
+ const work = path.join(root, "work");
415
+ const android = path.join(root, "android");
416
+ const request = manualRequest(root, "bootstrap", { kind: "bootstrap" }, {
417
+ HMOS_MODE: mode,
418
+ WORK: work,
419
+ ANDROID: android,
420
+ }, {
421
+ actions: [
422
+ { probe_image_read: { skip_when: "false" } },
423
+ { tip_image_read_false: { skip_when: "false", tip: "Show the multimodal warning." } },
424
+ ],
425
+ });
426
+ const waiting = runProtocol(request);
427
+ assert.equal(waiting.status, "waiting_external");
428
+ assert.equal(waiting.wait.kind, "image_read");
429
+ const expectedDir = mode === "existing_project"
430
+ ? path.join(work, "image_probe")
431
+ : path.join(android, ".hometrans-image-probe");
432
+ assert.equal(waiting.wait.probe_dir, expectedDir);
433
+ assert.equal(fs.existsSync(waiting.wait.image), true);
434
+
435
+ const done = runCallback(request, "answer", { attempt_id: request.attempt_id, reply: "NO_IMAGE" });
436
+ assert.equal(done.status, "done");
437
+ assert.equal(done.set_vars.IMAGE_READ, false);
438
+ assert.equal(done.evidence.notices[0].text, "Show the multimodal warning.");
439
+ assert.equal(fs.existsSync(expectedDir), false);
440
+ }
441
+ });
442
+
443
+ test("discover.feature_scope waits for the host under auto and accepts scoped callback variables", () => {
444
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), "dag-production-feature-scope-"));
445
+ const node = originalWorkflow.nodes.find((item) => item.id === "discover.feature_scope");
446
+ const request = manualRequest(root, node.id, { kind: node.runner.kind }, { INTERACTION_MODE: "auto" }, {
447
+ actions: node.actions,
448
+ });
449
+ request.invoke = node.invoke;
450
+
451
+ const waiting = runProtocol(request);
452
+ assert.equal(waiting.status, "waiting_external");
453
+ assert.equal(waiting.wait.kind, "host_action");
454
+ assert.equal(waiting.wait.action, "discover.feature_scope");
455
+ assert.match(waiting.wait.invoke, /auto/);
456
+
457
+ const done = runCallback(request, "complete", {
458
+ attempt_id: request.attempt_id,
459
+ ok: true,
460
+ set_vars: { FEATURE_SCOPE: true, SCOPE_PAGES: 0 },
461
+ });
462
+ assert.equal(done.status, "done");
463
+ assert.equal(done.set_vars.FEATURE_SCOPE, true);
464
+ assert.equal(done.set_vars.SCOPE_PAGES, 0);
465
+ });
466
+
467
+ test("bootstrap reads real migration history without failing the incremental run", () => {
468
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), "dag-production-history-"));
469
+ const hmos = path.join(root, "hmos");
470
+ writeJson(path.join(hmos, ".hometrans", "migration_manifest.json"), {
471
+ schema: "migration_manifest/v1",
472
+ pages: {
473
+ PageB: { ui: { status: "done" } },
474
+ PageA: { ui: { status: "done" } },
475
+ },
476
+ runs: [{}],
477
+ });
478
+ const action = originalWorkflow.nodes.find((node) => node.id === "bootstrap").actions
479
+ .map((item) => item.incremental_history).find(Boolean);
480
+ const request = manualRequest(root, "bootstrap", { kind: "bootstrap" }, {
481
+ PROFILE: "incremental",
482
+ HMOS: hmos,
483
+ SKILLS_ROOT: path.join(SKILL, ".."),
484
+ }, { actions: [{ incremental_history: action }] });
485
+
486
+ const result = runProtocol(request);
487
+ assert.equal(result.status, "done", result.reason);
488
+ assert.equal(result.set_vars.MIGRATED_PAGES, "PageA,PageB");
489
+ });
490
+
491
+ test("bootstrap runs the real incremental diagnosis and keeps its scenario output", () => {
492
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), "dag-production-diagnose-"));
493
+ const work = path.join(root, "work");
494
+ const action = originalWorkflow.nodes.find((node) => node.id === "bootstrap").actions
495
+ .map((item) => item.incremental_diagnose).find(Boolean);
496
+ const request = manualRequest(root, "bootstrap", { kind: "bootstrap" }, {
497
+ PROFILE: "incremental",
498
+ HMOS: path.join(root, "hmos"),
499
+ WORK: work,
500
+ ANDROID: path.join(root, "android"),
501
+ SKILLS_ROOT: path.join(SKILL, ".."),
502
+ }, { actions: [{ incremental_diagnose: action }] });
503
+
504
+ const result = runProtocol(request);
505
+ assert.equal(result.status, "done", result.reason);
506
+ assert.equal(JSON.parse(fs.readFileSync(path.join(work, "diagnose.json"), "utf8")).scenario, 3);
507
+ });
508
+
509
+ test("serial order: boot.workspace (run_start) precedes discover.arch unless scaffold_new serializes discover", () => {
510
+ const graph = buildGraph(originalWorkflow);
511
+ const statuses = { bootstrap: "done", "discover.android_index": "done" };
512
+ assert.deepEqual(readyNodes(graph, statuses, { SERIALIZE_DISCOVER: false }).map((n) => n.id).slice(0, 2), ["boot.workspace", "discover.arch"],
513
+ "existing_project commits run_start before the long arch skill, so an arch hard_stop is in this run's ledger");
514
+ assert.deepEqual(readyNodes(graph, statuses, { SERIALIZE_DISCOVER: true }).map((n) => n.id)[0], "discover.arch",
515
+ "scaffold_new still builds the shell before boot.workspace writes into HMOS");
516
+ assert.ok(!readyNodes(graph, statuses, { SERIALIZE_DISCOVER: true }).some((n) => n.id === "boot.workspace"));
517
+ });
518
+
519
+ test("rewind follows data edges only: review.arch -> discover.arch keeps boot.workspace / discover.utg_static", () => {
520
+ const graph = buildGraph(originalWorkflow);
521
+ // The ordering-only edge is really there, so this would regress if rewind walked conditional_needs again.
522
+ assert.ok(graph.children["discover.arch"].has("boot.workspace"));
523
+ assert.ok(graph.byId.get("boot.workspace").conditional_needs.some((c) => c.add.includes("discover.arch")));
524
+ const affected = rewindAffected(graph, "discover.arch");
525
+ assert.ok(affected.has("discover.arch") && affected.has("review.arch"));
526
+ for (const id of ["boot.workspace", "discover.utg_static", "bootstrap", "discover.android_index"]) {
527
+ assert.ok(!affected.has(id), `${id} must not be reset when rewinding to discover.arch`);
528
+ }
529
+ // Real data consumers downstream of arch are still reset.
530
+ assert.ok(affected.has("ui.batch"));
531
+ for (const gate of originalWorkflow.nodes.filter((n) => n.runner?.kind === "gate" && n.runner.rewind_to)) {
532
+ assert.ok(rewindAffected(graph, gate.runner.rewind_to).has(gate.id), `${gate.id} unreachable from its rewind_to`);
533
+ }
534
+ });
535
+
536
+ test("full fake-host run covers interruption, rewind, stale callbacks, gates, soft failure and unique run_end", async () => {
537
+ const doc = lightweightProductionWorkflow();
538
+ const f = productionFixture(doc);
539
+ writeJson(path.join(f.work, "checkpoints", "spec-gen-reviewed.ok"), {});
540
+ const started = startFixture(f);
541
+ assert.match(started.executor.command, /dag_production_executor\.mjs/);
542
+ assert.equal(started.ledger_started, false);
543
+ assert.equal(fs.existsSync(f.ledger), false);
544
+
545
+ let state = await advance({ state: f.statePath });
546
+ let interrupted = false;
547
+ let rewound = false;
548
+ const visited = [];
549
+ const confirmed = new Set();
550
+
551
+ async function completeExternal(current) {
552
+ const attempt = current.attempts.find((item) => item.attempt_id === current.nodes[current.current_node].current_attempt_id);
553
+ const request = JSON.parse(fs.readFileSync(attempt.request_path, "utf8"));
554
+ visited.push(request.node);
555
+ if (request.node === "discover.arch") {
556
+ for (const name of ["project_design.md", "database_design.md", "preferences_design.md"]) {
557
+ const target = path.join(f.hmos, ".hometrans", name);
558
+ fs.mkdirSync(path.dirname(target), { recursive: true });
559
+ fs.writeFileSync(target, `# ${name}\n`);
560
+ }
561
+ }
562
+ if (request.node === "logic.spec_pipeline") writeLogicEvidence(f);
563
+ if (request.node === "logic.finalize") {
564
+ fs.mkdirSync(path.dirname(f.logicReport), { recursive: true });
565
+ fs.writeFileSync(f.logicReport, "# final report\n");
566
+ }
567
+ const failed = request.node === "ui.batch";
568
+ const result = runCallback(request, "complete", {
569
+ attempt_id: request.attempt_id,
570
+ ok: !failed,
571
+ ...(failed ? { reason: "device lost" } : {}),
572
+ });
573
+ assert.equal(result.status, failed ? "failed" : "done");
574
+ return completeAttempt({ state: f.statePath, attemptId: request.attempt_id });
575
+ }
576
+
577
+ for (let step = 0; step < 60 && !["completed", "completed_with_failures"].includes(state.status); step++) {
578
+ assert.notEqual(state.status, "blocked", JSON.stringify(state.error));
579
+ assert.notEqual(state.status, "failed", JSON.stringify(state.error));
580
+ if (state.status === "waiting_external") {
581
+ if (state.current_node === "discover.arch" && !interrupted) {
582
+ const interruptedState = loadRunState(f.statePath);
583
+ const attempt = interruptedState.attempts.find((item) => item.attempt_id === interruptedState.nodes["discover.arch"].current_attempt_id);
584
+ fs.rmSync(attempt.result_path);
585
+ interruptedState.status = "running";
586
+ interruptedState.nodes["discover.arch"].status = "running";
587
+ attempt.status = "running";
588
+ writeJson(f.statePath, interruptedState);
589
+ state = await advance({ state: f.statePath });
590
+ const attempts = state.attempts.filter((item) => item.node === "discover.arch");
591
+ assert.equal(attempts.length, 2);
592
+ assert.equal(attempts[1].resume_from_attempt, attempts[0].file_id);
593
+ assert.equal(state.status, "waiting_external");
594
+ interrupted = true;
595
+ continue;
596
+ }
597
+ state = await completeExternal(state);
598
+ continue;
599
+ }
600
+ if (state.status === "waiting_human") {
601
+ if (state.current_node === "review.arch" && !rewound) {
602
+ const staleAttempt = state.nodes["discover.arch"].current_attempt_id;
603
+ const kept = ["boot.workspace", "discover.utg_static"];
604
+ const before = Object.fromEntries(kept.map((id) => [id, structuredClone(state.nodes[id])]));
605
+ for (const id of kept) assert.equal(before[id].status, "done", id);
606
+ state = await rewindGate({ state: f.statePath, node: "review.arch" });
607
+ // Redoing the architecture docs must not reset nodes that only wait on discover.arch for ordering.
608
+ for (const id of kept) {
609
+ assert.equal(state.nodes[id].status, "done", `${id} was reset by the review.arch rewind`);
610
+ assert.equal(state.nodes[id].generation || 0, before[id].generation || 0, id);
611
+ assert.equal(state.nodes[id].current_attempt_id, before[id].current_attempt_id, id);
612
+ }
613
+ rewound = true;
614
+ assert.equal(state.current_node, "discover.arch");
615
+ assert.equal(state.status, "waiting_external");
616
+ assert.equal(state.nodes["discover.arch"].generation, 1);
617
+ assert.equal(state.vars.REUSE_DISCOVER_ARCH, false);
618
+ const current = state.attempts.find((item) => item.attempt_id === state.nodes["discover.arch"].current_attempt_id);
619
+ assert.equal(current.resume_from_attempt, undefined, "rewind must not inherit an interrupted cursor");
620
+ await assert.rejects(
621
+ completeAttempt({ state: f.statePath, attemptId: staleAttempt }),
622
+ (error) => error.code === "STALE_ATTEMPT",
623
+ );
624
+ continue;
625
+ }
626
+ confirmed.add(state.current_node);
627
+ state = await confirmGate({ state: f.statePath, node: state.current_node, confirmedBy: "user" });
628
+ continue;
629
+ }
630
+ throw new Error(`Unexpected run status ${state.status}: ${JSON.stringify(state.error)}`);
631
+ }
632
+
633
+ assert.equal(state.status, "completed_with_failures", JSON.stringify(state.error));
634
+ assert.equal(interrupted, true);
635
+ assert.equal(rewound, true);
636
+ assert.deepEqual([...confirmed].sort(), ["review.arch", "review.ui_batch"]);
637
+ assert.equal(state.nodes["ui.batch"].status, "failed");
638
+ assert.equal(state.nodes["review.ui_batch"].status, "done");
639
+ assert.equal(state.nodes["logic.finalize"].status, "done");
640
+ assert.deepEqual(state.pending_after_terminal, []);
641
+ for (const id of ["discover.arch", "discover.utg_static", "ui.batch", "enrich.spec_gen", "enrich.module_docs", "logic.spec_pipeline", "adopt.signing", "logic.finalize"]) {
642
+ assert.ok(visited.includes(id), `${id} did not reach the fake host`);
643
+ }
644
+
645
+ const ledger = readLedger(f.ledger);
646
+ assert.equal(ledger.filter((record) => record.event === "run_start").length, 1);
647
+ assert.equal(ledger.filter((record) => record.event === "run_end").length, 1);
648
+ assert.equal(ledger.at(-1).event, "run_end");
649
+ assert.equal(ledger.at(-1).status, "completed_with_failures");
650
+ assert.deepEqual(ledger.at(-1).failed_nodes, ["ui.batch"]);
651
+ assert.equal(ledger.filter((record) => record.event === "rewind").length, 1);
652
+ for (const id of ["boot.workspace", "discover.utg_static"]) {
653
+ assert.equal(ledger.filter((record) => record.event === "node" && record.node === id).length, 1, `${id} re-ran after the arch rewind`);
654
+ }
655
+ assert.equal(ledger.filter((record) => record.node === "logic.finalize" && record.status === "done").length, 1);
656
+ assert.ok(ledger.some((record) => record.event === "node_start" && record.node === "discover.arch"));
657
+ const executed = ledger.filter((record) => record.event === "node" && record.attempt_id);
658
+ assert.ok(executed.some((record) => record.node === "bootstrap"));
659
+ for (const record of executed) {
660
+ assert.ok(Number.isFinite(Date.parse(record.started_at)), record.node);
661
+ assert.ok(Number.isInteger(record.duration_s) && record.duration_s >= 0, record.node);
662
+ }
663
+ assert.notEqual(state.paths.run_dir, state.paths.production_run_dir);
664
+
665
+ const uiGateAttempt = state.attempts.find((item) => item.attempt_id === state.nodes["review.ui_batch"].current_attempt_id);
666
+ const uiCursor = JSON.parse(fs.readFileSync(path.join(f.dagRunDir, "production", `${uiGateAttempt.file_id}.json`), "utf8"));
667
+ assert.equal(uiCursor.history.some((entry) => entry.action === "capture_audit"), false);
668
+ const uiCheckpoint = JSON.parse(fs.readFileSync(path.join(f.work, "checkpoints", "ui-batch-reviewed.ok"), "utf8"));
669
+ assert.equal(uiCheckpoint.ui_status, "failed");
670
+ });