@ninthpalace/cocos-test 0.0.0-stage → 0.1.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.
Files changed (325) hide show
  1. package/README.md +339 -2
  2. package/assets/cocos-sdk/cocos-sdk.js +1942 -0
  3. package/assets/cocos-sdk/manifest.json +5 -0
  4. package/assets/templates/environment/fields.json +35 -0
  5. package/assets/templates/repo-example/README.md +23 -0
  6. package/assets/templates/repo-example/cases/.gitkeep +1 -0
  7. package/assets/templates/repo-example/game/load-env.ts +4 -0
  8. package/assets/templates/repo-example/game/resources.ts +6 -0
  9. package/assets/templates/repo-example/game/setup.ts +30 -0
  10. package/assets/templates/repo-example/gitignore.template +10 -0
  11. package/assets/templates/repo-example/knowledge/navigation/index.json +5 -0
  12. package/assets/templates/repo-example/midscene.config.ts +20 -0
  13. package/assets/templates/repo-example/nct.config.ts +18 -0
  14. package/assets/templates/repo-example/nodes/index.ts +3 -0
  15. package/assets/templates/repo-example/package.json +10 -0
  16. package/assets/templates/repo-example/pnpm-workspace.yaml +3 -0
  17. package/assets/templates/repo-example/reports/.gitkeep +1 -0
  18. package/assets/templates/repo-example/scripts/generate-node-spec.mjs +19 -0
  19. package/assets/templates/repo-example/tsconfig.json +4 -0
  20. package/assets/templates/report/template.html +16 -0
  21. package/dist/cli/commands/case-worker.d.ts +2 -0
  22. package/dist/cli/commands/case-worker.d.ts.map +1 -0
  23. package/dist/cli/commands/case-worker.js +47 -0
  24. package/dist/cli/commands/case-worker.js.map +1 -0
  25. package/dist/cli/commands/case.d.ts +3 -0
  26. package/dist/cli/commands/case.d.ts.map +1 -0
  27. package/dist/cli/commands/case.js +76 -0
  28. package/dist/cli/commands/case.js.map +1 -0
  29. package/dist/cli/commands/knowledge-worker.d.ts +2 -0
  30. package/dist/cli/commands/knowledge-worker.d.ts.map +1 -0
  31. package/dist/cli/commands/knowledge-worker.js +14 -0
  32. package/dist/cli/commands/knowledge-worker.js.map +1 -0
  33. package/dist/cli/commands/knowledge.d.ts +3 -0
  34. package/dist/cli/commands/knowledge.d.ts.map +1 -0
  35. package/dist/cli/commands/knowledge.js +69 -0
  36. package/dist/cli/commands/knowledge.js.map +1 -0
  37. package/dist/cli/commands/navigation.d.ts +3 -0
  38. package/dist/cli/commands/navigation.d.ts.map +1 -0
  39. package/dist/cli/commands/navigation.js +98 -0
  40. package/dist/cli/commands/navigation.js.map +1 -0
  41. package/dist/cli/commands/repl.d.ts +3 -0
  42. package/dist/cli/commands/repl.d.ts.map +1 -0
  43. package/dist/cli/commands/repl.js +81 -0
  44. package/dist/cli/commands/repl.js.map +1 -0
  45. package/dist/cli/commands/report-worker.d.ts +2 -0
  46. package/dist/cli/commands/report-worker.d.ts.map +1 -0
  47. package/dist/cli/commands/report-worker.js +11 -0
  48. package/dist/cli/commands/report-worker.js.map +1 -0
  49. package/dist/cli/commands/report.d.ts +3 -0
  50. package/dist/cli/commands/report.d.ts.map +1 -0
  51. package/dist/cli/commands/report.js +65 -0
  52. package/dist/cli/commands/report.js.map +1 -0
  53. package/dist/cli/commands/task-worker.d.ts +2 -0
  54. package/dist/cli/commands/task-worker.d.ts.map +1 -0
  55. package/dist/cli/commands/task-worker.js +12 -0
  56. package/dist/cli/commands/task-worker.js.map +1 -0
  57. package/dist/cli/commands/task.d.ts +3 -0
  58. package/dist/cli/commands/task.d.ts.map +1 -0
  59. package/dist/cli/commands/task.js +155 -0
  60. package/dist/cli/commands/task.js.map +1 -0
  61. package/dist/cli/index.d.ts +3 -0
  62. package/dist/cli/index.d.ts.map +1 -0
  63. package/dist/cli/index.js +19 -0
  64. package/dist/cli/index.js.map +1 -0
  65. package/dist/cli/output/run.d.ts +18 -0
  66. package/dist/cli/output/run.d.ts.map +1 -0
  67. package/dist/cli/output/run.js +45 -0
  68. package/dist/cli/output/run.js.map +1 -0
  69. package/dist/cli/program.d.ts +3 -0
  70. package/dist/cli/program.d.ts.map +1 -0
  71. package/dist/cli/program.js +152 -0
  72. package/dist/cli/program.js.map +1 -0
  73. package/dist/config.d.ts +42 -0
  74. package/dist/config.d.ts.map +1 -0
  75. package/dist/config.js +59 -0
  76. package/dist/config.js.map +1 -0
  77. package/dist/core/authoring/session.d.ts +27 -0
  78. package/dist/core/authoring/session.d.ts.map +1 -0
  79. package/dist/core/authoring/session.js +161 -0
  80. package/dist/core/authoring/session.js.map +1 -0
  81. package/dist/core/poco/agent.d.ts +57 -0
  82. package/dist/core/poco/agent.d.ts.map +1 -0
  83. package/dist/core/poco/agent.js +116 -0
  84. package/dist/core/poco/agent.js.map +1 -0
  85. package/dist/core/poco/conditions.d.ts +50 -0
  86. package/dist/core/poco/conditions.d.ts.map +1 -0
  87. package/dist/core/poco/conditions.js +25 -0
  88. package/dist/core/poco/conditions.js.map +1 -0
  89. package/dist/core/poco/driver.d.ts +60 -0
  90. package/dist/core/poco/driver.d.ts.map +1 -0
  91. package/dist/core/poco/driver.js +247 -0
  92. package/dist/core/poco/driver.js.map +1 -0
  93. package/dist/core/poco/errors.d.ts +5 -0
  94. package/dist/core/poco/errors.d.ts.map +1 -0
  95. package/dist/core/poco/errors.js +10 -0
  96. package/dist/core/poco/errors.js.map +1 -0
  97. package/dist/core/poco/hierarchy.d.ts +109 -0
  98. package/dist/core/poco/hierarchy.d.ts.map +1 -0
  99. package/dist/core/poco/hierarchy.js +131 -0
  100. package/dist/core/poco/hierarchy.js.map +1 -0
  101. package/dist/core/poco/index.d.ts +11 -0
  102. package/dist/core/poco/index.d.ts.map +1 -0
  103. package/dist/core/poco/index.js +4 -0
  104. package/dist/core/poco/index.js.map +1 -0
  105. package/dist/core/poco/input.d.ts +25 -0
  106. package/dist/core/poco/input.d.ts.map +1 -0
  107. package/dist/core/poco/input.js +135 -0
  108. package/dist/core/poco/input.js.map +1 -0
  109. package/dist/core/poco/proxy.d.ts +43 -0
  110. package/dist/core/poco/proxy.d.ts.map +1 -0
  111. package/dist/core/poco/proxy.js +88 -0
  112. package/dist/core/poco/proxy.js.map +1 -0
  113. package/dist/core/poco/sdk.d.ts +137 -0
  114. package/dist/core/poco/sdk.d.ts.map +1 -0
  115. package/dist/core/poco/sdk.js +104 -0
  116. package/dist/core/poco/sdk.js.map +1 -0
  117. package/dist/core/poco/session.d.ts +25 -0
  118. package/dist/core/poco/session.d.ts.map +1 -0
  119. package/dist/core/poco/session.js +2 -0
  120. package/dist/core/poco/session.js.map +1 -0
  121. package/dist/core/project/env.d.ts +6 -0
  122. package/dist/core/project/env.d.ts.map +1 -0
  123. package/dist/core/project/env.js +21 -0
  124. package/dist/core/project/env.js.map +1 -0
  125. package/dist/core/project/error.d.ts +9 -0
  126. package/dist/core/project/error.d.ts.map +1 -0
  127. package/dist/core/project/error.js +35 -0
  128. package/dist/core/project/error.js.map +1 -0
  129. package/dist/core/project/load.d.ts +26 -0
  130. package/dist/core/project/load.d.ts.map +1 -0
  131. package/dist/core/project/load.js +93 -0
  132. package/dist/core/project/load.js.map +1 -0
  133. package/dist/core/project/node-catalog.d.ts +4 -0
  134. package/dist/core/project/node-catalog.d.ts.map +1 -0
  135. package/dist/core/project/node-catalog.js +14 -0
  136. package/dist/core/project/node-catalog.js.map +1 -0
  137. package/dist/core/project/node-metadata.d.ts +18 -0
  138. package/dist/core/project/node-metadata.d.ts.map +1 -0
  139. package/dist/core/project/node-metadata.js +28 -0
  140. package/dist/core/project/node-metadata.js.map +1 -0
  141. package/dist/core/project/readiness.d.ts +81 -0
  142. package/dist/core/project/readiness.d.ts.map +1 -0
  143. package/dist/core/project/readiness.js +69 -0
  144. package/dist/core/project/readiness.js.map +1 -0
  145. package/dist/core/project/resources.d.ts +17 -0
  146. package/dist/core/project/resources.d.ts.map +1 -0
  147. package/dist/core/project/resources.js +81 -0
  148. package/dist/core/project/resources.js.map +1 -0
  149. package/dist/core/reporting/contract.d.ts +37 -0
  150. package/dist/core/reporting/contract.d.ts.map +1 -0
  151. package/dist/core/reporting/contract.js +2 -0
  152. package/dist/core/reporting/contract.js.map +1 -0
  153. package/dist/core/reporting/evidence.d.ts +5 -0
  154. package/dist/core/reporting/evidence.d.ts.map +1 -0
  155. package/dist/core/reporting/evidence.js +66 -0
  156. package/dist/core/reporting/evidence.js.map +1 -0
  157. package/dist/core/reporting/offline-preview.d.ts +2 -0
  158. package/dist/core/reporting/offline-preview.d.ts.map +1 -0
  159. package/dist/core/reporting/offline-preview.js +45 -0
  160. package/dist/core/reporting/offline-preview.js.map +1 -0
  161. package/dist/core/reporting/offline-types.d.ts +67 -0
  162. package/dist/core/reporting/offline-types.d.ts.map +1 -0
  163. package/dist/core/reporting/offline-types.js +2 -0
  164. package/dist/core/reporting/offline-types.js.map +1 -0
  165. package/dist/core/reporting/page.d.ts +11 -0
  166. package/dist/core/reporting/page.d.ts.map +1 -0
  167. package/dist/core/reporting/page.js +63 -0
  168. package/dist/core/reporting/page.js.map +1 -0
  169. package/dist/core/reporting/recorder.d.ts +4 -0
  170. package/dist/core/reporting/recorder.d.ts.map +1 -0
  171. package/dist/core/reporting/recorder.js +329 -0
  172. package/dist/core/reporting/recorder.js.map +1 -0
  173. package/dist/core/reporting/session-report.d.ts +19 -0
  174. package/dist/core/reporting/session-report.d.ts.map +1 -0
  175. package/dist/core/reporting/session-report.js +226 -0
  176. package/dist/core/reporting/session-report.js.map +1 -0
  177. package/dist/core/testing/calls.d.ts +25 -0
  178. package/dist/core/testing/calls.d.ts.map +1 -0
  179. package/dist/core/testing/calls.js +144 -0
  180. package/dist/core/testing/calls.js.map +1 -0
  181. package/dist/core/testing/contracts.d.ts +110 -0
  182. package/dist/core/testing/contracts.d.ts.map +1 -0
  183. package/dist/core/testing/contracts.js +104 -0
  184. package/dist/core/testing/contracts.js.map +1 -0
  185. package/dist/core/testing/replay.d.ts +44 -0
  186. package/dist/core/testing/replay.d.ts.map +1 -0
  187. package/dist/core/testing/replay.js +89 -0
  188. package/dist/core/testing/replay.js.map +1 -0
  189. package/dist/core/testing/result.d.ts +73 -0
  190. package/dist/core/testing/result.d.ts.map +1 -0
  191. package/dist/core/testing/result.js +51 -0
  192. package/dist/core/testing/result.js.map +1 -0
  193. package/dist/core/testing/session.d.ts +49 -0
  194. package/dist/core/testing/session.d.ts.map +1 -0
  195. package/dist/core/testing/session.js +82 -0
  196. package/dist/core/testing/session.js.map +1 -0
  197. package/dist/core/workspace/artifacts.d.ts +2 -0
  198. package/dist/core/workspace/artifacts.d.ts.map +1 -0
  199. package/dist/core/workspace/artifacts.js +17 -0
  200. package/dist/core/workspace/artifacts.js.map +1 -0
  201. package/dist/core/workspace/files.d.ts +5 -0
  202. package/dist/core/workspace/files.d.ts.map +1 -0
  203. package/dist/core/workspace/files.js +53 -0
  204. package/dist/core/workspace/files.js.map +1 -0
  205. package/dist/core/workspace/identity.d.ts +10 -0
  206. package/dist/core/workspace/identity.d.ts.map +1 -0
  207. package/dist/core/workspace/identity.js +70 -0
  208. package/dist/core/workspace/identity.js.map +1 -0
  209. package/dist/core/workspace/task.d.ts +69 -0
  210. package/dist/core/workspace/task.d.ts.map +1 -0
  211. package/dist/core/workspace/task.js +81 -0
  212. package/dist/core/workspace/task.js.map +1 -0
  213. package/dist/core/workspace/yaml.d.ts +10 -0
  214. package/dist/core/workspace/yaml.d.ts.map +1 -0
  215. package/dist/core/workspace/yaml.js +49 -0
  216. package/dist/core/workspace/yaml.js.map +1 -0
  217. package/dist/index.d.ts +30 -0
  218. package/dist/index.d.ts.map +1 -0
  219. package/dist/index.js +19 -0
  220. package/dist/index.js.map +1 -0
  221. package/dist/nodes/create.d.ts +31 -0
  222. package/dist/nodes/create.d.ts.map +1 -0
  223. package/dist/nodes/create.js +99 -0
  224. package/dist/nodes/create.js.map +1 -0
  225. package/dist/nodes/index.d.ts +9 -0
  226. package/dist/nodes/index.d.ts.map +1 -0
  227. package/dist/nodes/index.js +5 -0
  228. package/dist/nodes/index.js.map +1 -0
  229. package/dist/nodes/input.d.ts +10 -0
  230. package/dist/nodes/input.d.ts.map +1 -0
  231. package/dist/nodes/input.js +22 -0
  232. package/dist/nodes/input.js.map +1 -0
  233. package/dist/nodes/jev/index.d.ts +36 -0
  234. package/dist/nodes/jev/index.d.ts.map +1 -0
  235. package/dist/nodes/jev/index.js +141 -0
  236. package/dist/nodes/jev/index.js.map +1 -0
  237. package/dist/nodes/poco/actions.d.ts +175 -0
  238. package/dist/nodes/poco/actions.d.ts.map +1 -0
  239. package/dist/nodes/poco/actions.js +119 -0
  240. package/dist/nodes/poco/actions.js.map +1 -0
  241. package/dist/nodes/poco/index.d.ts +637 -0
  242. package/dist/nodes/poco/index.d.ts.map +1 -0
  243. package/dist/nodes/poco/index.js +254 -0
  244. package/dist/nodes/poco/index.js.map +1 -0
  245. package/dist/nodes/report.d.ts +17 -0
  246. package/dist/nodes/report.d.ts.map +1 -0
  247. package/dist/nodes/report.js +14 -0
  248. package/dist/nodes/report.js.map +1 -0
  249. package/dist/repl.d.ts +12 -0
  250. package/dist/repl.d.ts.map +1 -0
  251. package/dist/repl.js +125 -0
  252. package/dist/repl.js.map +1 -0
  253. package/dist/report/style.css +2 -0
  254. package/dist/report/template.html +280 -0
  255. package/dist/services/case/index.d.ts +22 -0
  256. package/dist/services/case/index.d.ts.map +1 -0
  257. package/dist/services/case/index.js +238 -0
  258. package/dist/services/case/index.js.map +1 -0
  259. package/dist/services/env/index.d.ts +34 -0
  260. package/dist/services/env/index.d.ts.map +1 -0
  261. package/dist/services/env/index.js +168 -0
  262. package/dist/services/env/index.js.map +1 -0
  263. package/dist/services/env/inspect.d.ts +2 -0
  264. package/dist/services/env/inspect.d.ts.map +1 -0
  265. package/dist/services/env/inspect.js +43 -0
  266. package/dist/services/env/inspect.js.map +1 -0
  267. package/dist/services/knowledge/index.d.ts +67 -0
  268. package/dist/services/knowledge/index.d.ts.map +1 -0
  269. package/dist/services/knowledge/index.js +163 -0
  270. package/dist/services/knowledge/index.js.map +1 -0
  271. package/dist/services/navigation/contracts.d.ts +76 -0
  272. package/dist/services/navigation/contracts.d.ts.map +1 -0
  273. package/dist/services/navigation/contracts.js +67 -0
  274. package/dist/services/navigation/contracts.js.map +1 -0
  275. package/dist/services/navigation/index.d.ts +82 -0
  276. package/dist/services/navigation/index.d.ts.map +1 -0
  277. package/dist/services/navigation/index.js +304 -0
  278. package/dist/services/navigation/index.js.map +1 -0
  279. package/dist/services/point/index.d.ts +13 -0
  280. package/dist/services/point/index.d.ts.map +1 -0
  281. package/dist/services/point/index.js +42 -0
  282. package/dist/services/point/index.js.map +1 -0
  283. package/dist/services/project/index.d.ts +43 -0
  284. package/dist/services/project/index.d.ts.map +1 -0
  285. package/dist/services/project/index.js +194 -0
  286. package/dist/services/project/index.js.map +1 -0
  287. package/dist/services/project/inspect.d.ts +2 -0
  288. package/dist/services/project/inspect.d.ts.map +1 -0
  289. package/dist/services/project/inspect.js +29 -0
  290. package/dist/services/project/inspect.js.map +1 -0
  291. package/dist/services/project/policy.d.ts +12 -0
  292. package/dist/services/project/policy.d.ts.map +1 -0
  293. package/dist/services/project/policy.js +33 -0
  294. package/dist/services/project/policy.js.map +1 -0
  295. package/dist/services/report/catalog.d.ts +4 -0
  296. package/dist/services/report/catalog.d.ts.map +1 -0
  297. package/dist/services/report/catalog.js +6 -0
  298. package/dist/services/report/catalog.js.map +1 -0
  299. package/dist/services/report/index.d.ts +71 -0
  300. package/dist/services/report/index.d.ts.map +1 -0
  301. package/dist/services/report/index.js +334 -0
  302. package/dist/services/report/index.js.map +1 -0
  303. package/dist/services/skill/index.d.ts +14 -0
  304. package/dist/services/skill/index.d.ts.map +1 -0
  305. package/dist/services/skill/index.js +110 -0
  306. package/dist/services/skill/index.js.map +1 -0
  307. package/dist/services/task/index.d.ts +111 -0
  308. package/dist/services/task/index.d.ts.map +1 -0
  309. package/dist/services/task/index.js +81 -0
  310. package/dist/services/task/index.js.map +1 -0
  311. package/docs/examples/select-item.html +7 -0
  312. package/docs/examples/select-item.ts +34 -0
  313. package/docs/node-development.md +62 -0
  314. package/package.json +106 -3
  315. package/sdk/cocos-runtime/README.md +52 -0
  316. package/sdk/cocos-runtime/agents.md +24 -0
  317. package/skills/nct-rookie/SKILL.md +112 -0
  318. package/skills/nct-rookie/agents/openai.yaml +4 -0
  319. package/skills/nct-rookie/references/knowledge.md +48 -0
  320. package/skills/nct-rookie/references/navigation.md +22 -0
  321. package/skills/nct-rookie/references/repl.md +169 -0
  322. package/skills/nct-rookie/references/testing.md +123 -0
  323. package/skills/nct-rookie/references/yaml.md +82 -0
  324. package/skills/nct-rookie/scripts/repl-host.mjs +167 -0
  325. package/src/agents.md +75 -0
@@ -0,0 +1,167 @@
1
+ import { spawn } from 'node:child_process';
2
+ import { randomUUID } from 'node:crypto';
3
+ import { join, resolve } from 'node:path';
4
+
5
+ const TAIL_LIMIT = 16_000;
6
+ const LINE_LIMIT = 1_048_576;
7
+
8
+ function hostError(code, message) {
9
+ return Object.assign(new Error(message), { code });
10
+ }
11
+
12
+ function deferred() {
13
+ let resolve;
14
+ let reject;
15
+ const promise = new Promise((yes, no) => { resolve = yes; reject = no; });
16
+ void promise.catch(() => undefined);
17
+ return { promise, resolve, reject };
18
+ }
19
+
20
+ function wait(promise, timeoutMs, signal) {
21
+ if (!Number.isFinite(timeoutMs) || timeoutMs <= 0) throw new TypeError('timeoutMs must be positive.');
22
+ return new Promise((resolve, reject) => {
23
+ let timer;
24
+ const finish = (error, value) => {
25
+ clearTimeout(timer);
26
+ signal?.removeEventListener('abort', abort);
27
+ if (error) reject(error);
28
+ else resolve(value);
29
+ };
30
+ const abort = () => finish(signal.reason ?? hostError('REPL_RECEIVE_ABORTED', 'Reception was cancelled.'));
31
+ promise.then((value) => finish(undefined, value), (error) => finish(error));
32
+ if (signal?.aborted) abort();
33
+ else {
34
+ signal?.addEventListener('abort', abort, { once: true });
35
+ timer = setTimeout(() => finish(hostError('REPL_RECEIVE_TIMEOUT',
36
+ 'No terminal frame received; keep this host and call receive(), never resend the code.')), timeoutMs);
37
+ }
38
+ });
39
+ }
40
+
41
+ /** Own one native nct REPL. This adapter receives frames; it never executes Nodes itself. */
42
+ export function startReplHost({ cwd, args = [], env, nodeExecutable = 'node', timeoutMs = 30_000 }) {
43
+ if (!Number.isFinite(timeoutMs) || timeoutMs <= 0) throw new TypeError('timeoutMs must be positive.');
44
+ const projectDir = resolve(cwd);
45
+ const cli = join(projectDir, 'node_modules/@ninthpalace/cocos-test/dist/cli/index.js');
46
+ const child = spawn(nodeExecutable, [cli, 'repl', ...args], {
47
+ cwd: projectDir, env, stdio: ['pipe', 'pipe', 'pipe'], windowsHide: true,
48
+ });
49
+ let output = '';
50
+ let errors = '';
51
+ let lineBuffer = '';
52
+ let identity;
53
+ let batch = 0;
54
+ let pending = { kind: 'ready', result: deferred() };
55
+ let receiving = false;
56
+ let closing = false;
57
+ let ended = false;
58
+ let fatal;
59
+ const exit = deferred();
60
+ const fail = (error) => {
61
+ fatal ??= error;
62
+ pending?.result.reject(error);
63
+ };
64
+ child.stdout.setEncoding('utf8');
65
+ child.stderr.setEncoding('utf8');
66
+ child.stderr.on('data', (chunk) => { errors = (errors + chunk).slice(-TAIL_LIMIT); });
67
+ child.on('error', (error) => { fail(error); exit.reject(error); });
68
+ child.stdin.on('error', fail);
69
+ child.on('close', (code, signal) => {
70
+ ended = true;
71
+ fail(hostError('REPL_EXITED', `REPL closed before another frame (code ${code}, signal ${signal}).\n${errors}`));
72
+ exit.resolve({ code, signal });
73
+ });
74
+ child.stdout.on('data', (chunk) => {
75
+ output = (output + chunk).slice(-TAIL_LIMIT);
76
+ lineBuffer += chunk;
77
+ let newline;
78
+ while ((newline = lineBuffer.indexOf('\n')) !== -1) {
79
+ const line = lineBuffer.slice(0, newline).replace(/\r$/, '').replace(/^(?:> )+/, '');
80
+ lineBuffer = lineBuffer.slice(newline + 1);
81
+ if (line.length > LINE_LIMIT) {
82
+ fail(hostError('REPL_PROTOCOL', 'REPL output line exceeded the reception limit.'));
83
+ continue;
84
+ }
85
+ const marker = pending?.kind === 'ready' ? 'NCT_READY '
86
+ : pending?.kind === 'run' ? 'NCT_DONE ' : pending?.marker;
87
+ if (!marker || !line.startsWith(marker)) continue;
88
+ try {
89
+ const frame = JSON.parse(line.slice(marker.length));
90
+ if (pending.kind === 'ready') {
91
+ if (typeof frame.sessionId !== 'string') throw new Error('Missing sessionId.');
92
+ identity = frame;
93
+ } else if (pending.kind === 'run' &&
94
+ (frame.sessionId !== identity.sessionId || frame.batch !== pending.batch)) continue;
95
+ pending.result.resolve(frame);
96
+ } catch (error) {
97
+ fail(hostError('REPL_PROTOCOL', `Invalid REPL frame: ${error.message}`));
98
+ }
99
+ }
100
+ if (lineBuffer.length > LINE_LIMIT) {
101
+ lineBuffer = '';
102
+ fail(hostError('REPL_PROTOCOL', 'Incomplete REPL output line exceeded the reception limit.'));
103
+ }
104
+ });
105
+
106
+ async function receive({ timeoutMs: limit = timeoutMs, signal } = {}) {
107
+ if (receiving) throw hostError('REPL_BUSY', 'Another reception is active.');
108
+ if (!pending) throw hostError('REPL_IDLE', 'No submitted code is awaiting a frame.');
109
+ const request = pending;
110
+ receiving = true;
111
+ try {
112
+ const frame = await wait(request.result.promise, limit, signal);
113
+ pending = undefined;
114
+ return frame;
115
+ } finally {
116
+ receiving = false;
117
+ }
118
+ }
119
+
120
+ function submit(code, request, options) {
121
+ if (fatal) throw fatal;
122
+ if (closing || ended) throw hostError('REPL_CLOSED', 'The REPL is closing or closed.');
123
+ if (pending) throw hostError('REPL_BUSY', 'Receive the previous frame before submitting more code.');
124
+ if (!identity) throw hostError('REPL_NOT_READY', 'Await ready before submitting code.');
125
+ if (typeof code !== 'string' || !code.trim() || /[\r\n]/.test(code))
126
+ throw new TypeError('Submit one non-empty physical JavaScript line.');
127
+ if (options?.signal?.aborted) throw options.signal.reason;
128
+ if (options?.timeoutMs !== undefined && (!Number.isFinite(options.timeoutMs) || options.timeoutMs <= 0))
129
+ throw new TypeError('timeoutMs must be positive.');
130
+ pending = { ...request, result: deferred() };
131
+ const result = receive(options);
132
+ child.stdin.write(`${code}\n`);
133
+ return result;
134
+ }
135
+
136
+ return {
137
+ ready: receive(),
138
+ receive,
139
+ run(code, options) {
140
+ const next = batch + 1;
141
+ const result = submit(code, { kind: 'run', batch: next }, options);
142
+ batch = next;
143
+ return result;
144
+ },
145
+ execute(code, options) {
146
+ const marker = `NCT_REPL_${randomUUID()}:`;
147
+ return submit(`${code}; console.log(${JSON.stringify(marker)} + 'true')`, { kind: 'execute', marker }, options);
148
+ },
149
+ async evaluate(expression, options) {
150
+ const marker = `NCT_REPL_${randomUUID()}:`;
151
+ const code = `try { console.log(${JSON.stringify(marker)} + JSON.stringify({ok:true,value:await (${expression})})); } catch(e) { console.log(${JSON.stringify(marker)} + JSON.stringify({ok:false,error:String(e?.message ?? e)})); }`;
152
+ const result = await submit(code, { kind: 'evaluate', marker }, options);
153
+ if (!result.ok) throw hostError('REPL_EVALUATION_ERROR', result.error);
154
+ return result.value;
155
+ },
156
+ async close(options = {}) {
157
+ if (!closing && !ended) {
158
+ closing = true;
159
+ // EOF cancels an outstanding Case; do not queue .exit behind top-level await.
160
+ child.stdin.end(pending ? undefined : '.exit\n');
161
+ }
162
+ return wait(exit.promise, options.timeoutMs ?? timeoutMs, options.signal);
163
+ },
164
+ get session() { return identity; },
165
+ get transcript() { return `${output}\n${errors}`; },
166
+ };
167
+ }
package/src/agents.md ADDED
@@ -0,0 +1,75 @@
1
+ # Cocos Test 源码分层约定
2
+
3
+ 本文件适用于 `packages/cocos-test/src/`,与仓库根目录 `agents.md` 一起使用。公开入口与依赖以包的 `package.json` 为准。
4
+
5
+ ## 目录与职责
6
+
7
+ ```text
8
+ src/
9
+ ├─ index.ts # 普通 API 与类型的公开导出门面
10
+ ├─ repl.ts # REPL run 的终态摘要展示适配
11
+ ├─ cli/ # Commander 和进程适配
12
+ │ ├─ index.ts # 进程入口
13
+ │ ├─ program.ts # 组装命令树和公共选项
14
+ │ ├─ commands/ # 按命令组注册及适配
15
+ │ └─ output/ # CLI 文本、JSON 和错误展示
16
+ ├─ services/ # 对调用方有意义的业务操作与流程编排
17
+ │ ├─ project/ # 创建与接入游戏测试工程
18
+ │ ├─ env/ # 环境初始化与校验
19
+ │ ├─ task/ # 测试任务创建、查询及离线导出
20
+ │ ├─ point/ # 测试点全文件及资料引用校验
21
+ │ ├─ report/ # 离线报告、派生会话报告及能力快照编排
22
+ │ ├─ navigation/ # 导航索引契约、只读查询与资产检查
23
+ │ ├─ knowledge/ # 独立 WeKnora 查询与来源结果,不创建游戏资源
24
+ │ ├─ skill/ # 随包 Skill 安装
25
+ │ └─ case/ # 原生 YAML 静态检查与独立回放
26
+ ├─ nodes/ # 数组菜单及通用 Poco、Jev Node 导出
27
+ └─ core/ # 按职责组织的共享基础能力
28
+ ├─ project/ # 工程配置、路径与执行项目解析
29
+ ├─ workspace/ # 任务和运行目录及其持久化
30
+ ├─ authoring/ # openSession、发现 Node、原生步骤执行与关闭
31
+ ├─ testing/ # 测试点/结论契约、操作记录及结论保存
32
+ ├─ reporting/ # 动作报告源、离线阅读数据及 Midscene 隔离预览
33
+ └─ poco/ # Page 后端、查询代理、等待、输入与扩展
34
+ ```
35
+
36
+ 工程模板位于包级 `assets/templates/repo-example/`,不放入 `src/`;模板随包发布。上面的分组是起点,后续按真实功能和复用调整,不为尚不存在的能力预建接口或实现。CLI 命令组变大时可再按动作拆分;基础能力按职责命名,不把 `core/` 或 `utils/` 作为杂物目录。
37
+
38
+ ## 依赖与调用
39
+
40
+ - CLI 和普通 API 共用 `services/` 与必要的核心能力;`index.ts` 是公开导出门面,不为每个函数增加无意义的包装。不预建没有实现的工具或 Agent 入口。
41
+ - task create 复用 services/project 的同一次隔离配置检查采集 Node 能力,core/project/node-catalog 只从已加载注册表构建快照;任务落盘后调用报告入口发布,不执行 setup。快照或入口发布失败返回 warnings,保留任务与旧快照;报告默认仍不加载配置。
42
+ - `services/` 组合 `core/` 完成完整操作。简单操作可以直接在 service 实现;`core/` 不反向导入 services 或 CLI。
43
+ - 知识服务只用 loadNctConfig,不调用完整 loadGameProject;环境合并共用 core/project/env,Shell 优先且不修改宿主环境。CLI 隔离配置日志,普通 API 不接管输出;无知识 Node、远端 Agent 会话或自动检索历史,详见 [知识查询](../../../docs/design/knowledge-query.md)。
44
+ - 测试点由外部 Agent 或人工直接编辑每任务唯一的 `test-points.yaml`,规则与示例在随包 Skill。nct 提供全文件及资料引用校验;saveTestResult({taskId}, result) 在 task 目录显式替换当前完整结论及测试点快照,不加载配置、不依赖会话状态。证据相对任务目录,可跨本任务 sessions/replays,不读取旧 session 结论。详细边界见 [测试记录](../../../docs/design/testing-records.md)。
45
+ - `Session` 管理一次直接测试的资源、原生步骤与生命周期,可绑定持久任务;只记录事实与证据,不持有任务结论、不自动判定产品缺陷。每次 run 是独立 Case,不延迟 Node teardown;共享页面不代表跨调用共享 Case 级资源。浏览器会话不会跨进程延续。
46
+ - `repl.ts` 的 createReplRunner 仅适配终端展示,复用原生 run;输出一行 NCT_DONE,数据有界且省略显式可见,select 直接接收原生 Case,不覆盖状态,异常不自动重试。lastResult 仅保留最近原生结果,不新增历史存储。终端输出留在该适配层,不下沉核心;不建立展示保护层或第二套 Session 执行接口。
47
+ - `cli/commands/repl.ts` 在前台创建 Session,使用 Node 自带 REPL 预置 nct/session/run;就绪输出 NCT_READY,.exit/EOF/进程信号时关闭原会话。cwd 切换、信号与终端生命周期只在 CLI;不改写用户 JavaScript、不提供后台连接或另建执行记录,完整交互合同见随包 references/repl.md。
48
+ - 测试 Agent 只使用注册 Node 观察、输入及断言;默认通过同步 listNodes 查看全部有效名称和说明,再按需 describeNode 获取 schema,列表不转换 schema。模板的 nodes 脚本仅生成可选会话外 Node Spec,执行与报告不读取它,不复制维护 Poco API 清单。开发者 Page/Agent 资源不是另一套测试协议;Session 不提供 Poco。
49
+ - `core/project/load.ts` 统一按消费工程加载配置、有效 Node 和固定变量;`node-metadata.ts` 共用元数据、排序和 schema 转换规则,供会话与报告复用,不管理生命周期或另载框架;`resources.ts` 通过官方 ProjectRuntime 管理工程与嵌套 fileSetup 资源。`services/case` 复用官方采集、文档执行及报告公开 API,完整 YAML 回放不依赖创作会话。CLI 在隔离子进程运行可信工程模块,将其 stdout/stderr 转入诊断流,保持 `--json` 的单对象输出。
50
+ - 用例静态检查和独立回放可用于已有正式用例,不应依赖创作会话;三者共享适用的校验和执行能力,但分别报告结果。
51
+ - core/testing/calls 只读持久调用事实;core/reporting/session-report 使用公开 Midscene 组装器派生会话 single-html,不加载配置、不执行 Node、不新增持久映射或中间目录。每个原生 call 是一个 Case;仅 started、异常或损坏记录明确提示,不伪造 Case 或资源生命周期成功。v3 证据为 {file, step?},步骤校验 phase/stepIndex。展示元数据在导出时派生,前端不重复解析 call JSON;原生 Case/步骤 hash 适配须用实际版本浏览器验证。
52
+ - 工作区 task/session/replay ID 统一使用北京时间(UTC+8)的 yyyyMMdd-HHmmss 加六位随机后缀,目录名直接等于 ID;不保留 UUID/旧会话目录兼容。core/workspace/identity.ts 统一排他创建和列表,仅 task 查找解析唯一前缀;session/replay 按完整 ID 读取,任务内两者并列。replay.json 在资源准备前记录 running,保留终态/异常分类,task show 不加载配置即可列出会话与回放,报告引用仍拒绝越界及符号链接。
53
+ - 导航资产固定为游戏工程 knowledge/navigation/index.json 与单 Case 原生 YAML。services/navigation 的查询与检查只读文件,不加载配置、不解析变量、不认证业务可达;完整原生契约与独立回放复用 services/case,不维护第二套步骤 schema 或执行器。索引直接维护,不自动摘取、生成缓存或同步来源用例。
54
+ - CLI 只负责参数适配、输出、退出码和信号。核心返回结构化结果或分类错误,不直接写 stdout/stderr、调用 `process.exit()` 或注册进程信号。
55
+ - Commander 仅由 CLI 加载。根入口不因导入而启动浏览器或创建会话;可选 Midscene peer 在使用对应能力时加载。
56
+ - 原生步骤由所选执行项目注册的官方 Node schema 校验;本包输入契约和错误定义靠近所属能力,确有跨模块复用时再共享。Page、Agent、密钥等运行时对象不进入模型可见 schema。
57
+ - 自定义 Node 的公共输入由 nodes/input.ts 统一维护,通过 /nodes 导出的 withCommonNodeInput 包装业务 schema,保留对象校验;执行入口用 withoutCommonNodeInput 剥离后才交给底层业务。desc 在运行时可选,正式用例/导航及 hooks 的填写要求见 Skill 的 references/yaml.md;不扩展官方 Node 或原生 $ 元数据。
58
+ - 自定义 Node 通过 /nodes 的 withNodeReport 接入官方报告,Poco 和复合业务共用同一 reporting;不要另建 Node 级 HTML 或把业务阶段伪装成 AI 动作。任务离线阅读器是聚合资料的独立展示层,不替代原生执行报告。Poco 只以类型方式依赖 reporting/contract,不加载 Midscene;动作截图在最终目标/SDK/绑定核对之前采集,查询与断言仅记录结果现场,采集故障不得重发输入或替换业务异常。快照在 I/O 前固定截点,详见 [报告设计](../../../docs/design/node-reporting.md)。
59
+ - SDK 源码位于包级 `sdk/cocos-runtime/`,构建产物随包发布;`core/poco/driver.ts` 提供底层宿主入口 `createPoco(page, options)`,`nodes/poco/` 按官方 Node 契约薄适配输入、查询、等待、断言与诊断,不重复实现驱动。actions.ts 集中输入动作 schema 和执行函数,供 Poco Node 与 Jev 共用,不是另一份注册表。游戏工程显式注册 Node,业务分组按需导入;核心不依赖注册模块。
60
+ - 页内 Cocos SDK 只读取游戏结构、组件属性和可交互信息,负责当前目标与命中点的只读检查,不修改游戏属性、派发输入或调用业务回调。`core/poco` 的 `proxy.ts` 描述目标,`driver.ts` 组织等待及动作策略,`agent.ts` 组合具体 Page 后端与共享队列,`session.ts` 定义受控会话及 reader 类型,`hierarchy.ts` 负责结构解释,`input.ts` 统一真实输入与清理,`sdk.ts` 管理协议、加载和注入。Page 是唯一设备后端,不预建多端接入。
61
+ - `poco.hierarchy` 提供观察、结构快照和指纹;`poco.extend` 提供整段排队的扩展会话,内部只用 session,不重入公开队列。session 继承 Poco 的取消信号,所有读取、点击及拖动核对同一 SDK 实例;回调结束后失效,必须等待操作完成。同一 Page 的不同 Poco 共用队列。业务断言及产品规则留在 Node,不因观察成功而判定业务成功。
62
+ - `createPoco` 提供 Poco 风格的多属性目标查询、等待及主要输入动作,从包根或 `/poco` 导出;方法保留 Poco 名称和秒制时间参数,输入前仍须唯一命中。页内协议提供原值查询和命中检查,包括可编辑与密码字段文本;支持范围和差异见包 README。无旧客户端兼容入口。
63
+ - 动作先做就绪检查,仅对明确的输入前暂态失败轮询,默认 150ms;默认无动作后固定等待,click 的显式 0 覆盖默认间隔。业务结果优先显式条件等待。默认观察精简候选,局部 describe 包含深度省略及分页标记;不要把输出精简误称为免遍历整个场景。
64
+ - `hierarchy.check` 在一次同步采集中检查多个 selector,Poco Node 的等待每轮共用一次现场;就绪错误按原条件顺序处理。滚动通过 SDK prepareScroll 合并尺寸与命中准备,保留报告截图前预检及截图后最终核对。页内事实仅在调用内按需读取和复用,局部候选不免全局遮挡检查,完整诊断仍由 SDK 生成。
65
+ - `nodes/create.ts` 按显式数组选择 Poco/Jev/原生 Midscene,菜单只组合定义,不是另一份注册表;省略或 [] 不注册,all 单独表示完整组,错误选项加载时失败。运行设置与资源映射放在第二参数,默认延迟读取 context.page/agent。专项工厂保留,空跨游戏/地区发布入口不保留。游戏业务和列表策略及其测试在消费仓库维护,通过公开 /nodes、/poco 接入;开发入口见包级 docs/node-development.md。description 明确适用条件、副作用、完成条件及是否使用 AI。Node 和业务流程复用普通函数,不互调 Node;重名及覆盖沿用官方注册表。受控执行见 [能力扩展](../../../docs/design/cocos-extensions.md)。
66
+ - `nodes/jev` 的 act 直接调用 Midscene aiAct,受控动作将视觉点绑定至唯一 Poco selector 并在输入前复核,失败取消并保留 started/sent 动作;输入文本须来自显式 allowlist。报告保留原生规划和 Poco 输入,不认证稳定 selector 或保存测试结论。细则与验证范围见 [Jev Node](../../../docs/design/jev-node.md)。
67
+
68
+ ## CLI 与 JSON
69
+
70
+ - `program.ts` 组装 Commander 根命令及子命令;`commands/` 按命令组组织,action 只转换输入并调用共享操作。异步命令使用 `parseAsync()`,公共选项可通过 `optsWithGlobals()` 获取。
71
+ - `--json` 是 CLI 输出选项,不传给 service 改变业务行为。service 返回结构化事实,CLI 分别格式化为人类输出或机器输出。
72
+ - 业务命令执行阶段的 JSON 输出为一个对象,包含 `command`、`ok`、`projectDir`、`executionProject`、`taskId`、`runId`、`data` 和 `error`;不包含 `schemaVersion`。字段及命令结果契约见 `docs/design/nct-commands.md`。成功时 `error` 为 `null`;失败时保留分类错误和可用部分成果,并返回非零退出码。
73
+ - JSON 模式 stdout 只输出一个合法对象;进度和诊断写 stderr。验证第三方输出不污染 stdout。操作记录、错误、变量快照和证据路径保留原值,不使用内容遮盖或字段脱敏。不要把 JSON 协议隔离、输出摘要或路径完整性检查当成内容保护。
74
+ - Commander 在业务 action 执行前拒绝未知命令或无效参数时,stdout 为空,stderr 提示帮助,不承诺 JSON。帮助与版本保持文本;env edit 和 repl 是不支持 --json 的交互命令。
75
+ - 除 env edit 和 repl 外的业务动作均支持命令后 --json。case/repl 可通过 --task 复用任务变量与必填检查,临时运行可用 --config/--project/--vars;不创建隐式任务。task check 加载配置但不输出变量值;task list/show、point validate 及 navigation 命令不加载配置或变量值;report generate 仅在 --include-nodes 时加载可信配置且不执行 setup;session.json 的资源状态与业务结论分开。