@dommaker/harness 1.1.1 → 1.2.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 (348) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/README.md +2 -4
  3. package/bin/harness.js +12 -37
  4. package/dist/cli/commands/check.d.ts +2 -2
  5. package/dist/cli/commands/check.d.ts.map +1 -1
  6. package/dist/cli/commands/check.js +7 -32
  7. package/dist/cli/commands/check.js.map +1 -1
  8. package/dist/cli/commands/definitions.d.ts +2 -0
  9. package/dist/cli/commands/definitions.d.ts.map +1 -1
  10. package/dist/cli/commands/definitions.js.map +1 -1
  11. package/dist/cli/commands/init.d.ts.map +1 -1
  12. package/dist/cli/commands/init.js +6 -37
  13. package/dist/cli/commands/init.js.map +1 -1
  14. package/dist/cli/commands/knowledge.d.ts.map +1 -1
  15. package/dist/cli/commands/knowledge.js +1 -12
  16. package/dist/cli/commands/knowledge.js.map +1 -1
  17. package/dist/cli/commands/report.d.ts.map +1 -1
  18. package/dist/cli/commands/report.js +0 -2
  19. package/dist/cli/commands/report.js.map +1 -1
  20. package/dist/cli/commands/status.js +2 -2
  21. package/dist/cli/commands/status.js.map +1 -1
  22. package/dist/context/index.d.ts +9 -6
  23. package/dist/context/index.d.ts.map +1 -1
  24. package/dist/context/index.js +18 -20
  25. package/dist/context/index.js.map +1 -1
  26. package/dist/core/constraints/capabilities-parser.d.ts +3 -2
  27. package/dist/core/constraints/capabilities-parser.d.ts.map +1 -1
  28. package/dist/core/constraints/capabilities-parser.js +25 -47
  29. package/dist/core/constraints/capabilities-parser.js.map +1 -1
  30. package/dist/core/constraints/checker.d.ts +29 -32
  31. package/dist/core/constraints/checker.d.ts.map +1 -1
  32. package/dist/core/constraints/checker.js +34 -126
  33. package/dist/core/constraints/checker.js.map +1 -1
  34. package/dist/core/constraints/checkers/index.d.ts +2 -1
  35. package/dist/core/constraints/checkers/index.d.ts.map +1 -1
  36. package/dist/core/constraints/checkers/index.js +3 -0
  37. package/dist/core/constraints/checkers/index.js.map +1 -1
  38. package/dist/core/constraints/checkers/types.d.ts +25 -0
  39. package/dist/core/constraints/checkers/types.d.ts.map +1 -1
  40. package/dist/core/constraints/checkers/types.js +24 -0
  41. package/dist/core/constraints/checkers/types.js.map +1 -1
  42. package/dist/core/constraints/definitions/guidelines.d.ts.map +1 -1
  43. package/dist/core/constraints/definitions/guidelines.js +0 -2
  44. package/dist/core/constraints/definitions/guidelines.js.map +1 -1
  45. package/dist/core/constraints/definitions/prompts.d.ts.map +1 -1
  46. package/dist/core/constraints/definitions/prompts.js +0 -5
  47. package/dist/core/constraints/definitions/prompts.js.map +1 -1
  48. package/dist/core/constraints/index.d.ts +1 -2
  49. package/dist/core/constraints/index.d.ts.map +1 -1
  50. package/dist/core/constraints/index.js +1 -7
  51. package/dist/core/constraints/index.js.map +1 -1
  52. package/dist/core/constraints/usage-report.d.ts +1 -2
  53. package/dist/core/constraints/usage-report.d.ts.map +1 -1
  54. package/dist/core/constraints/usage-report.js +2 -5
  55. package/dist/core/constraints/usage-report.js.map +1 -1
  56. package/dist/core/effective-constraints.d.ts +24 -4
  57. package/dist/core/effective-constraints.d.ts.map +1 -1
  58. package/dist/core/effective-constraints.js +28 -7
  59. package/dist/core/effective-constraints.js.map +1 -1
  60. package/dist/core/effective-set.d.ts +30 -0
  61. package/dist/core/effective-set.d.ts.map +1 -0
  62. package/dist/core/effective-set.js +35 -0
  63. package/dist/core/effective-set.js.map +1 -0
  64. package/dist/core/index.d.ts +13 -6
  65. package/dist/core/index.d.ts.map +1 -1
  66. package/dist/core/index.js +44 -21
  67. package/dist/core/index.js.map +1 -1
  68. package/dist/core/project-config-loader.d.ts +3 -6
  69. package/dist/core/project-config-loader.d.ts.map +1 -1
  70. package/dist/core/project-config-loader.js +47 -61
  71. package/dist/core/project-config-loader.js.map +1 -1
  72. package/dist/failure/constraint-handler.d.ts +1 -1
  73. package/dist/failure/constraint-handler.js +1 -1
  74. package/dist/gates/checker-gate.d.ts +1 -4
  75. package/dist/gates/checker-gate.d.ts.map +1 -1
  76. package/dist/gates/checker-gate.js +3 -11
  77. package/dist/gates/checker-gate.js.map +1 -1
  78. package/dist/gates/definitions.d.ts +9 -38
  79. package/dist/gates/definitions.d.ts.map +1 -1
  80. package/dist/gates/definitions.js +24 -13
  81. package/dist/gates/definitions.js.map +1 -1
  82. package/dist/gates/effective-gates.d.ts.map +1 -1
  83. package/dist/gates/effective-gates.js +14 -18
  84. package/dist/gates/effective-gates.js.map +1 -1
  85. package/dist/gates/index.d.ts +1 -1
  86. package/dist/gates/index.d.ts.map +1 -1
  87. package/dist/gates/index.js.map +1 -1
  88. package/dist/hooks/bootstrap.d.ts.map +1 -1
  89. package/dist/hooks/bootstrap.js +0 -3
  90. package/dist/hooks/bootstrap.js.map +1 -1
  91. package/dist/index.d.ts +43 -60
  92. package/dist/index.d.ts.map +1 -1
  93. package/dist/index.js +180 -143
  94. package/dist/index.js.map +1 -1
  95. package/dist/knowledge/query.d.ts +15 -0
  96. package/dist/knowledge/query.d.ts.map +1 -1
  97. package/dist/knowledge/query.js +23 -0
  98. package/dist/knowledge/query.js.map +1 -1
  99. package/dist/monitoring/analyzer-base.d.ts +0 -4
  100. package/dist/monitoring/analyzer-base.d.ts.map +1 -1
  101. package/dist/monitoring/analyzer-base.js +0 -21
  102. package/dist/monitoring/analyzer-base.js.map +1 -1
  103. package/dist/monitoring/index.d.ts +6 -12
  104. package/dist/monitoring/index.d.ts.map +1 -1
  105. package/dist/monitoring/index.js +13 -29
  106. package/dist/monitoring/index.js.map +1 -1
  107. package/dist/monitoring/trace-analyzer.d.ts.map +1 -1
  108. package/dist/monitoring/trace-analyzer.js +2 -68
  109. package/dist/monitoring/trace-analyzer.js.map +1 -1
  110. package/dist/monitoring/traces.d.ts +0 -4
  111. package/dist/monitoring/traces.d.ts.map +1 -1
  112. package/dist/monitoring/traces.js +0 -14
  113. package/dist/monitoring/traces.js.map +1 -1
  114. package/dist/presets/index.d.ts +2 -2
  115. package/dist/presets/index.d.ts.map +1 -1
  116. package/dist/presets/index.js +3 -4
  117. package/dist/presets/index.js.map +1 -1
  118. package/dist/presets/standard.d.ts +10 -22
  119. package/dist/presets/standard.d.ts.map +1 -1
  120. package/dist/presets/standard.js +15 -75
  121. package/dist/presets/standard.js.map +1 -1
  122. package/dist/types/constraint.d.ts +1 -29
  123. package/dist/types/constraint.d.ts.map +1 -1
  124. package/dist/types/constraint.js +1 -1
  125. package/dist/types/constraint.js.map +1 -1
  126. package/dist/types/index.d.ts +0 -3
  127. package/dist/types/index.d.ts.map +1 -1
  128. package/dist/types/index.js +0 -4
  129. package/dist/types/index.js.map +1 -1
  130. package/dist/types/project-config.d.ts +4 -8
  131. package/dist/types/project-config.d.ts.map +1 -1
  132. package/dist/types/trace.d.ts +5 -18
  133. package/dist/types/trace.d.ts.map +1 -1
  134. package/package.json +2 -2
  135. package/src/CONTEXT.md +4 -5
  136. package/src/__tests__/checker-extra.test.ts +33 -139
  137. package/src/__tests__/checker.test.ts +0 -376
  138. package/src/__tests__/project-config-loader-extra.test.ts +42 -29
  139. package/src/__tests__/public-exports.test.ts +138 -0
  140. package/src/__tests__/trace-analyzer-extra.test.ts +6 -86
  141. package/src/__tests__/trace-analyzer.test.ts +4 -5
  142. package/src/__tests__/trace-collector.test.ts +0 -11
  143. package/src/__tests__/traces.test.ts +3 -22
  144. package/src/cli/commands/CONTEXT.md +2 -2
  145. package/src/cli/commands/__tests__/check-skip-output.test.ts +10 -7
  146. package/src/cli/commands/__tests__/check.test.ts +29 -69
  147. package/src/cli/commands/__tests__/constraints-report.test.ts +1 -1
  148. package/src/cli/commands/__tests__/knowledge.test.ts +90 -1
  149. package/src/cli/commands/__tests__/registry.test.ts +8 -6
  150. package/src/cli/commands/__tests__/status-extra.test.ts +6 -6
  151. package/src/cli/commands/__tests__/status.test.ts +6 -6
  152. package/src/cli/commands/check.ts +8 -34
  153. package/src/cli/commands/definitions.ts +2 -0
  154. package/src/cli/commands/init.ts +6 -37
  155. package/src/cli/commands/knowledge.ts +1 -16
  156. package/src/cli/commands/report.ts +0 -2
  157. package/src/cli/commands/status.ts +2 -2
  158. package/src/context/index.ts +29 -6
  159. package/src/core/CONTEXT.md +4 -3
  160. package/src/core/__tests__/effective-set.test.ts +80 -0
  161. package/src/core/constraints/capabilities-parser.ts +43 -49
  162. package/src/core/constraints/checker.ts +51 -145
  163. package/src/core/constraints/checkers/index.ts +2 -1
  164. package/src/core/constraints/checkers/types.ts +42 -0
  165. package/src/core/constraints/definitions/guidelines.ts +0 -2
  166. package/src/core/constraints/definitions/prompts.ts +0 -5
  167. package/src/core/constraints/index.ts +2 -17
  168. package/src/core/constraints/usage-report.ts +5 -8
  169. package/src/core/effective-constraints.ts +35 -7
  170. package/src/core/effective-set.ts +56 -0
  171. package/src/core/index.ts +80 -6
  172. package/src/core/project-config-loader.ts +53 -68
  173. package/src/failure/CONTEXT.md +1 -1
  174. package/src/failure/constraint-handler.ts +1 -1
  175. package/src/gates/CONTEXT.md +3 -3
  176. package/src/gates/checker-gate.ts +4 -12
  177. package/src/gates/definitions.ts +27 -50
  178. package/src/gates/effective-gates.ts +14 -21
  179. package/src/gates/index.ts +1 -1
  180. package/src/hooks/bootstrap.ts +0 -3
  181. package/src/index.ts +384 -148
  182. package/src/knowledge/CONTEXT.md +2 -2
  183. package/src/knowledge/__tests__/query.test.ts +64 -0
  184. package/src/knowledge/query.ts +28 -0
  185. package/src/monitoring/CONTEXT.md +7 -13
  186. package/src/monitoring/__tests__/traces.test.ts +0 -10
  187. package/src/monitoring/analyzer-base.ts +0 -22
  188. package/src/monitoring/index.ts +6 -12
  189. package/src/monitoring/trace-analyzer.ts +2 -73
  190. package/src/monitoring/traces.ts +0 -20
  191. package/src/presets/__tests__/standard.test.ts +48 -69
  192. package/src/presets/index.ts +3 -5
  193. package/src/presets/standard.ts +14 -79
  194. package/src/spec/CONTEXT.md +5 -13
  195. package/src/types/constraint.ts +1 -51
  196. package/src/types/index.ts +0 -12
  197. package/src/types/project-config.ts +4 -10
  198. package/src/types/trace.ts +5 -30
  199. package/dist/core/constraints/interceptor.d.ts +0 -39
  200. package/dist/core/constraints/interceptor.d.ts.map +0 -1
  201. package/dist/core/constraints/interceptor.js +0 -166
  202. package/dist/core/constraints/interceptor.js.map +0 -1
  203. package/dist/dashboard/data.d.ts +0 -31
  204. package/dist/dashboard/data.d.ts.map +0 -1
  205. package/dist/dashboard/data.js +0 -81
  206. package/dist/dashboard/data.js.map +0 -1
  207. package/dist/dashboard/index.d.ts +0 -4
  208. package/dist/dashboard/index.d.ts.map +0 -1
  209. package/dist/dashboard/index.js +0 -20
  210. package/dist/dashboard/index.js.map +0 -1
  211. package/dist/dashboard/stats.d.ts +0 -20
  212. package/dist/dashboard/stats.d.ts.map +0 -1
  213. package/dist/dashboard/stats.js +0 -115
  214. package/dist/dashboard/stats.js.map +0 -1
  215. package/dist/dashboard/types.d.ts +0 -110
  216. package/dist/dashboard/types.d.ts.map +0 -1
  217. package/dist/dashboard/types.js +0 -8
  218. package/dist/dashboard/types.js.map +0 -1
  219. package/dist/monitoring/constraint-doctor.d.ts +0 -76
  220. package/dist/monitoring/constraint-doctor.d.ts.map +0 -1
  221. package/dist/monitoring/constraint-doctor.js +0 -189
  222. package/dist/monitoring/constraint-doctor.js.map +0 -1
  223. package/dist/monitoring/diagnosis-rules.d.ts +0 -18
  224. package/dist/monitoring/diagnosis-rules.d.ts.map +0 -1
  225. package/dist/monitoring/diagnosis-rules.js +0 -119
  226. package/dist/monitoring/diagnosis-rules.js.map +0 -1
  227. package/dist/monitoring/knowledge-doctor.d.ts +0 -78
  228. package/dist/monitoring/knowledge-doctor.d.ts.map +0 -1
  229. package/dist/monitoring/knowledge-doctor.js +0 -266
  230. package/dist/monitoring/knowledge-doctor.js.map +0 -1
  231. package/dist/monitoring/knowledge-evolver.d.ts +0 -72
  232. package/dist/monitoring/knowledge-evolver.d.ts.map +0 -1
  233. package/dist/monitoring/knowledge-evolver.js +0 -205
  234. package/dist/monitoring/knowledge-evolver.js.map +0 -1
  235. package/dist/monitoring/performance-analyzer.d.ts +0 -92
  236. package/dist/monitoring/performance-analyzer.d.ts.map +0 -1
  237. package/dist/monitoring/performance-analyzer.js +0 -343
  238. package/dist/monitoring/performance-analyzer.js.map +0 -1
  239. package/dist/monitoring/performance-collector.d.ts +0 -103
  240. package/dist/monitoring/performance-collector.d.ts.map +0 -1
  241. package/dist/monitoring/performance-collector.js +0 -298
  242. package/dist/monitoring/performance-collector.js.map +0 -1
  243. package/dist/safety/index.d.ts +0 -6
  244. package/dist/safety/index.d.ts.map +0 -1
  245. package/dist/safety/index.js +0 -22
  246. package/dist/safety/index.js.map +0 -1
  247. package/dist/safety/input-guardrail.d.ts +0 -36
  248. package/dist/safety/input-guardrail.d.ts.map +0 -1
  249. package/dist/safety/input-guardrail.js +0 -118
  250. package/dist/safety/input-guardrail.js.map +0 -1
  251. package/dist/safety/output-guardrail.d.ts +0 -39
  252. package/dist/safety/output-guardrail.d.ts.map +0 -1
  253. package/dist/safety/output-guardrail.js +0 -147
  254. package/dist/safety/output-guardrail.js.map +0 -1
  255. package/dist/safety/sandbox.d.ts +0 -45
  256. package/dist/safety/sandbox.d.ts.map +0 -1
  257. package/dist/safety/sandbox.js +0 -134
  258. package/dist/safety/sandbox.js.map +0 -1
  259. package/dist/safety/tool-guardrail.d.ts +0 -51
  260. package/dist/safety/tool-guardrail.d.ts.map +0 -1
  261. package/dist/safety/tool-guardrail.js +0 -147
  262. package/dist/safety/tool-guardrail.js.map +0 -1
  263. package/dist/safety/types.d.ts +0 -104
  264. package/dist/safety/types.d.ts.map +0 -1
  265. package/dist/safety/types.js +0 -6
  266. package/dist/safety/types.js.map +0 -1
  267. package/dist/spec/annotation-checker.d.ts +0 -47
  268. package/dist/spec/annotation-checker.d.ts.map +0 -1
  269. package/dist/spec/annotation-checker.js +0 -279
  270. package/dist/spec/annotation-checker.js.map +0 -1
  271. package/dist/types/enforcement.d.ts +0 -42
  272. package/dist/types/enforcement.d.ts.map +0 -1
  273. package/dist/types/enforcement.js +0 -6
  274. package/dist/types/enforcement.js.map +0 -1
  275. package/dist/types/monitoring-types.d.ts +0 -53
  276. package/dist/types/monitoring-types.d.ts.map +0 -1
  277. package/dist/types/monitoring-types.js +0 -10
  278. package/dist/types/monitoring-types.js.map +0 -1
  279. package/dist/types/performance.d.ts +0 -204
  280. package/dist/types/performance.d.ts.map +0 -1
  281. package/dist/types/performance.js +0 -12
  282. package/dist/types/performance.js.map +0 -1
  283. package/dist/verification/index.d.ts +0 -4
  284. package/dist/verification/index.d.ts.map +0 -1
  285. package/dist/verification/index.js +0 -20
  286. package/dist/verification/index.js.map +0 -1
  287. package/dist/verification/loop.d.ts +0 -30
  288. package/dist/verification/loop.d.ts.map +0 -1
  289. package/dist/verification/loop.js +0 -114
  290. package/dist/verification/loop.js.map +0 -1
  291. package/dist/verification/rules-based.d.ts +0 -40
  292. package/dist/verification/rules-based.d.ts.map +0 -1
  293. package/dist/verification/rules-based.js +0 -130
  294. package/dist/verification/rules-based.js.map +0 -1
  295. package/dist/verification/types.d.ts +0 -69
  296. package/dist/verification/types.d.ts.map +0 -1
  297. package/dist/verification/types.js +0 -6
  298. package/dist/verification/types.js.map +0 -1
  299. package/src/__tests__/annotation-checker.test.ts +0 -183
  300. package/src/__tests__/constraint-doctor.test.ts +0 -271
  301. package/src/__tests__/doctor-extra.test.ts +0 -155
  302. package/src/__tests__/extend-exceptions.test.ts +0 -149
  303. package/src/__tests__/interceptor.test.ts +0 -89
  304. package/src/__tests__/performance-analyzer.test.ts +0 -499
  305. package/src/__tests__/performance-collector.test.ts +0 -276
  306. package/src/__tests__/presets.test.ts +0 -65
  307. package/src/core/constraints/__tests__/interceptor.test.ts +0 -267
  308. package/src/core/constraints/interceptor.ts +0 -217
  309. package/src/dashboard/CONTEXT.md +0 -21
  310. package/src/dashboard/__tests__/data.test.ts +0 -111
  311. package/src/dashboard/__tests__/stats.test.ts +0 -159
  312. package/src/dashboard/data.ts +0 -103
  313. package/src/dashboard/index.ts +0 -3
  314. package/src/dashboard/stats.ts +0 -127
  315. package/src/dashboard/types.ts +0 -130
  316. package/src/monitoring/__tests__/knowledge-doctor.test.ts +0 -278
  317. package/src/monitoring/__tests__/knowledge-evolver.test.ts +0 -368
  318. package/src/monitoring/__tests__/performance-analyzer.test.ts +0 -137
  319. package/src/monitoring/__tests__/performance-collector.test.ts +0 -331
  320. package/src/monitoring/constraint-doctor.ts +0 -228
  321. package/src/monitoring/diagnosis-rules.ts +0 -132
  322. package/src/monitoring/knowledge-doctor.ts +0 -322
  323. package/src/monitoring/knowledge-evolver.ts +0 -289
  324. package/src/monitoring/performance-analyzer.ts +0 -400
  325. package/src/monitoring/performance-collector.ts +0 -326
  326. package/src/safety/CONTEXT.md +0 -23
  327. package/src/safety/__tests__/input-guardrail.test.ts +0 -105
  328. package/src/safety/__tests__/output-guardrail.test.ts +0 -108
  329. package/src/safety/__tests__/sandbox.test.ts +0 -158
  330. package/src/safety/__tests__/tool-guardrail.test.ts +0 -127
  331. package/src/safety/index.ts +0 -5
  332. package/src/safety/input-guardrail.ts +0 -133
  333. package/src/safety/output-guardrail.ts +0 -170
  334. package/src/safety/sandbox.ts +0 -148
  335. package/src/safety/tool-guardrail.ts +0 -176
  336. package/src/safety/types.ts +0 -124
  337. package/src/spec/__tests__/annotation-checker.test.ts +0 -294
  338. package/src/spec/annotation-checker.ts +0 -320
  339. package/src/types/enforcement.ts +0 -48
  340. package/src/types/monitoring-types.ts +0 -68
  341. package/src/types/performance.ts +0 -295
  342. package/src/verification/CONTEXT.md +0 -23
  343. package/src/verification/__tests__/loop.test.ts +0 -303
  344. package/src/verification/__tests__/rules-based.test.ts +0 -240
  345. package/src/verification/index.ts +0 -3
  346. package/src/verification/loop.ts +0 -137
  347. package/src/verification/rules-based.ts +0 -152
  348. package/src/verification/types.ts +0 -81
@@ -1,68 +0,0 @@
1
- /**
2
- * 监控域类型(诊断)
3
- *
4
- * 原定义位于 monitoring/constraint-doctor.ts,
5
- * 但被 types/index.ts 反向再导出(类型层依赖业务模块,分层倒挂)。
6
- * 工单 14 将类型定义归位到 types 层,monitoring 改从 types 导入。
7
- */
8
-
9
- import type { ExecutionTrace } from './trace';
10
-
11
- /**
12
- * 诊断结果
13
- */
14
- export interface Diagnosis {
15
- /** 异常 ID */
16
- anomalyId: string;
17
-
18
- /** 约束 ID */
19
- constraintId: string;
20
-
21
- /** 诊断时间 */
22
- diagnosedAt: number;
23
-
24
- /** 根因分析 */
25
- rootCause: {
26
- /** 主要原因 */
27
- primary: string;
28
-
29
- /** 次要原因 */
30
- secondary?: string[];
31
-
32
- /** 相关 traces */
33
- evidence: ExecutionTrace[];
34
- };
35
-
36
- /** 影响评估 */
37
- impact: {
38
- /** 影响程度 */
39
- severity: 'low' | 'medium' | 'high';
40
-
41
- /** 影响范围 */
42
- scope: 'single_project' | 'multiple_projects' | 'team';
43
-
44
- /** 用户影响 */
45
- userImpact: string;
46
- };
47
-
48
- /** 改进建议 */
49
- recommendations: {
50
- /** 建议类型 */
51
- type: 'add_exception' | 'adjust_threshold' | 'modify_constraint' | 'user_training';
52
-
53
- /** 建议内容 */
54
- content: string;
55
-
56
- /** 预期效果 */
57
- expectedOutcome: string;
58
-
59
- /** 实施成本 */
60
- implementationCost: 'low' | 'medium' | 'high';
61
- }[];
62
-
63
- /** 是否需要变更 */
64
- needsChange: boolean;
65
-
66
- /** 紧急程度 */
67
- urgency: 'low' | 'medium' | 'high';
68
- }
@@ -1,295 +0,0 @@
1
- /**
2
- * Performance Trace 类型定义
3
- *
4
- * 轻量设计:只记录核心信息,零 Token 成本
5
- *
6
- * 与 ExecutionTrace 的区别:
7
- * - ExecutionTrace: 记录约束检查结果(业务行为)
8
- * - PerformanceTrace: 记录操作耗时(性能指标)
9
- */
10
-
11
- /**
12
- * 性能追踪记录
13
- *
14
- * 设计原则:
15
- * - 只记录核心字段(零 Token)
16
- * - 文件存储,追加写入
17
- * - 支持多种操作类型
18
- */
19
- export interface PerformanceTrace {
20
- // ========================================
21
- // 核心字段(必须)
22
- // ========================================
23
-
24
- /** 操作类型 */
25
- operation: string;
26
-
27
- /** 时间戳(Unix timestamp) */
28
- timestamp: number;
29
-
30
- /** 耗时(毫秒) */
31
- duration: number;
32
-
33
- /** 结果 */
34
- result: 'ok' | 'exceeded' | 'error';
35
-
36
- // ========================================
37
- // 上下文(可选)
38
- // ========================================
39
-
40
- /** 阈值(毫秒),用于计算是否超阈值 */
41
- threshold?: number;
42
-
43
- /** 项目路径(用于区分多项目) */
44
- projectPath?: string;
45
-
46
- /** 会话 ID(用于追踪同一会话的多次操作) */
47
- sessionId?: string;
48
-
49
- /** 任务 ID */
50
- taskId?: string;
51
-
52
- /** 角色ID(用于角色相关操作) */
53
- roleId?: string;
54
-
55
- /** 额外元数据 */
56
- metadata?: Record<string, unknown>;
57
- }
58
-
59
- /**
60
- * 性能统计汇总
61
- */
62
- export interface PerformanceSummary {
63
- /** 操作类型 */
64
- operation: string;
65
-
66
- /** 统计时间范围 */
67
- timeRange: {
68
- start: number;
69
- end: number;
70
- };
71
-
72
- // ========================================
73
- // 核心统计
74
- // ========================================
75
-
76
- /** 总执行次数 */
77
- totalCalls: number;
78
-
79
- /** 成功次数 */
80
- okCount: number;
81
-
82
- /** 超阈值次数 */
83
- exceededCount: number;
84
-
85
- /** 错误次数 */
86
- errorCount: number;
87
-
88
- // ========================================
89
- // 耗时统计
90
- // ========================================
91
-
92
- /** 平均耗时(毫秒) */
93
- avgDuration: number;
94
-
95
- /** 最大耗时 */
96
- maxDuration: number;
97
-
98
- /** 最小耗时 */
99
- minDuration: number;
100
-
101
- /** P95 耗时 */
102
- p95Duration: number;
103
-
104
- /** P99 耗时 */
105
- p99Duration: number;
106
-
107
- // ========================================
108
- // 比率统计
109
- // ========================================
110
-
111
- /** 成功率 (0-1) */
112
- okRate: number;
113
-
114
- /** 超阈值率 (0-1) */
115
- exceededRate: number;
116
-
117
- /** 错误率 (0-1) */
118
- errorRate: number;
119
-
120
- // ========================================
121
- // 趋势分析
122
- // ========================================
123
-
124
- /** 最近趋势 */
125
- recentTrend: 'stable' | 'rising' | 'falling';
126
-
127
- /** 对比上一周期的变化 */
128
- changeFromLastPeriod?: {
129
- avgDurationDelta: number;
130
- exceededRateDelta: number;
131
- };
132
- }
133
-
134
- /**
135
- * 性能异常
136
- */
137
- export interface PerformanceAnomaly {
138
- /** 异常类型 */
139
- type:
140
- | 'high_avg_duration' // 平均耗时过高
141
- | 'rising_duration' // 耗时趋势上升
142
- | 'high_exceeded_rate' // 超阈值率过高
143
- | 'rising_exceeded_rate' // 超阈值率上升
144
- | 'high_error_rate'; // 错误率过高
145
-
146
- /** 操作类型 */
147
- operation: string;
148
-
149
- /** 异常描述 */
150
- message: string;
151
-
152
- /** 相关数据 */
153
- data: {
154
- currentValue: number;
155
- threshold: number;
156
- trend?: 'rising' | 'falling' | 'stable';
157
- };
158
-
159
- /** 检测时间 */
160
- detectedAt: number;
161
-
162
- /** 建议的下一步 */
163
- suggestedAction?: 'optimize' | 'adjust_threshold' | 'notify_user';
164
- }
165
-
166
- /**
167
- * 性能追踪过滤条件
168
- */
169
- export interface PerformanceTraceFilter {
170
- /** 操作类型(可选) */
171
- operation?: string;
172
-
173
- /** 结果类型(可选) */
174
- result?: 'ok' | 'exceeded' | 'error';
175
-
176
- /** 时间范围(可选) */
177
- timeRange?: {
178
- start: number;
179
- end: number;
180
- };
181
-
182
- /** 项目路径(可选) */
183
- projectPath?: string;
184
-
185
- /** 会话 ID(可选) */
186
- sessionId?: string;
187
-
188
- /** 任务 ID(可选) */
189
- taskId?: string;
190
-
191
- /** 角色 ID(可选) */
192
- roleId?: string;
193
- }
194
-
195
- /**
196
- * PerformanceCollector 配置
197
- */
198
- export interface PerformanceCollectorConfig {
199
- /** 日志文件路径,默认 .harness/logs/performance.log */
200
- logFile?: string;
201
-
202
- /** 最大文件大小(字节),超出则滚动,默认 10MB */
203
- maxFileSize?: number;
204
-
205
- /** 是否启用(默认 true) */
206
- enabled?: boolean;
207
- }
208
-
209
- /**
210
- * PerformanceAnalyzer 配置
211
- */
212
- export interface PerformanceAnalyzerConfig {
213
- /** Summary 文件路径 */
214
- summaryFile?: string;
215
-
216
- /** 周期长度(毫秒),默认 1 小时 */
217
- periodMs?: number;
218
-
219
- /** 异常阈值 */
220
- thresholds?: {
221
- /** 平均耗时阈值(毫秒),超过则告警 */
222
- avgDuration?: number;
223
- /** 超阈值率阈值,默认 0.3 */
224
- exceededRate?: number;
225
- /** 错误率阈值,默认 0.1 */
226
- errorRate?: number;
227
- };
228
- }
229
-
230
- /**
231
- * Token 使用记录
232
- */
233
- export interface TokenUsageRecord {
234
- /** 时间戳 */
235
- timestamp: number;
236
-
237
- /** 会话/会议 ID */
238
- sessionId: string;
239
-
240
- /** 任务 ID(可选) */
241
- taskId?: string;
242
-
243
- /** 角色 ID(可选) */
244
- roleId?: string;
245
-
246
- /** 上下文 Token 数 */
247
- contextTokens: number;
248
-
249
- /** 执行 Token 数 */
250
- executionTokens: number;
251
-
252
- /** 总 Token 数 */
253
- totalTokens: number;
254
-
255
- /** 预算 */
256
- budget: number;
257
-
258
- /** 是否超预算 */
259
- exceeded: boolean;
260
-
261
- /** 项目路径 */
262
- projectPath?: string;
263
- }
264
-
265
- /**
266
- * Token 使用统计
267
- */
268
- export interface TokenUsageSummary {
269
- /** 会话 ID */
270
- sessionId: string;
271
-
272
- /** 时间范围 */
273
- timeRange: {
274
- start: number;
275
- end: number;
276
- };
277
-
278
- /** 总 Token 使用 */
279
- totalTokens: number;
280
-
281
- /** 平均每次 Token 使用 */
282
- avgTokensPerCall: number;
283
-
284
- /** 超预算次数 */
285
- exceededCount: number;
286
-
287
- /** 超预算率 */
288
- exceededRate: number;
289
-
290
- /** 按任务统计 */
291
- byTask?: Map<string, number>;
292
-
293
- /** 按角色统计 */
294
- byRole?: Map<string, number>;
295
- }
@@ -1,23 +0,0 @@
1
- # verification/
2
-
3
- ## 职责
4
- 验证引擎:基于规则的验证 + 循环验证(loop verification)。
5
-
6
- ## 核心导出
7
- - `types.ts` — 验证类型定义
8
- - `rules-based.ts` — 基于规则的验证引擎
9
- - `loop.ts` — 循环验证引擎
10
-
11
- ## 依赖关系
12
- - 依赖 `src/core/constraints/` 约束检查
13
- - 依赖 `src/core/validators/` 验证器
14
- - 经 `src/index.ts` 导出供外部调用方消费(无内置 CLI 命令)
15
-
16
- ## 约定
17
- - 验证规则可配置
18
- - loop 验证用于持续监控场景
19
- - 验证结果包含 pass/fail + 详细信息
20
-
21
- ## 注意事项
22
- - 与 core/validators 配合使用
23
- - loop 验证适用于长期运行的 Agent 会话
@@ -1,303 +0,0 @@
1
- /**
2
- * VerificationLoop 测试
3
- */
4
-
5
- import { VerificationLoop } from '../loop';
6
- import type { VerificationLoopConfig, GatherState } from '../types';
7
-
8
- describe('VerificationLoop', () => {
9
- const baseConfig: VerificationLoopConfig = {
10
- maxRetries: 3,
11
- rules: [
12
- {
13
- id: 'test-rule',
14
- type: 'custom',
15
- name: 'Test Rule',
16
- description: 'Test',
17
- verify: async () => ({ passed: true, ruleId: 'test-rule', duration: 0 }),
18
- },
19
- ],
20
- };
21
-
22
- describe('run', () => {
23
- it('应该在首次通过时返回 passed', async () => {
24
- const loop = new VerificationLoop(baseConfig);
25
- const gather = async (): Promise<GatherState> => ({
26
- changedFiles: [],
27
- metadata: {},
28
- });
29
-
30
- const snapshot = await loop.run({ projectRoot: '.' }, gather);
31
- expect(snapshot.status).toBe('passed');
32
- expect(snapshot.attempt).toBe(1);
33
- expect(snapshot.results.every(r => r.passed)).toBe(true);
34
- });
35
-
36
- it('应该在验证失败时重试', async () => {
37
- let callCount = 0;
38
- const config: VerificationLoopConfig = {
39
- maxRetries: 3,
40
- rules: [
41
- {
42
- id: 'flaky',
43
- type: 'custom',
44
- name: 'Flaky',
45
- description: '',
46
- verify: async () => {
47
- callCount++;
48
- return {
49
- passed: callCount >= 2,
50
- ruleId: 'flaky',
51
- message: callCount < 2 ? 'not yet' : 'ok',
52
- duration: 0,
53
- };
54
- },
55
- },
56
- ],
57
- };
58
-
59
- const loop = new VerificationLoop(config);
60
- const gather = async (): Promise<GatherState> => ({
61
- changedFiles: [],
62
- metadata: {},
63
- });
64
-
65
- const snapshot = await loop.run({ projectRoot: '.' }, gather);
66
- expect(snapshot.status).toBe('passed');
67
- expect(snapshot.attempt).toBe(2);
68
- });
69
-
70
- it('应该在达到最大重试次数后失败', async () => {
71
- const config: VerificationLoopConfig = {
72
- maxRetries: 2,
73
- rules: [
74
- {
75
- id: 'always-fail',
76
- type: 'custom',
77
- name: 'Always Fail',
78
- description: '',
79
- verify: async () => ({
80
- passed: false,
81
- ruleId: 'always-fail',
82
- message: 'nope',
83
- duration: 0,
84
- }),
85
- },
86
- ],
87
- };
88
-
89
- const loop = new VerificationLoop(config);
90
- const gather = async (): Promise<GatherState> => ({
91
- changedFiles: [],
92
- metadata: {},
93
- });
94
-
95
- const snapshot = await loop.run({ projectRoot: '.' }, gather);
96
- expect(snapshot.status).toBe('failed');
97
- expect(snapshot.attempt).toBe(2);
98
- expect(snapshot.lastError).toContain('最大重试次数');
99
- });
100
-
101
- it('failFast 应该在首次失败时立即停止', async () => {
102
- let verifyCount = 0;
103
- const config: VerificationLoopConfig = {
104
- maxRetries: 5,
105
- failFast: true,
106
- rules: [
107
- {
108
- id: 'fail-fast-rule',
109
- type: 'custom',
110
- name: 'Fail Fast',
111
- description: '',
112
- verify: async () => {
113
- verifyCount++;
114
- return {
115
- passed: false,
116
- ruleId: 'fail-fast-rule',
117
- duration: 0,
118
- };
119
- },
120
- },
121
- ],
122
- };
123
-
124
- const loop = new VerificationLoop(config);
125
- const snapshot = await loop.run(
126
- { projectRoot: '.' },
127
- async () => ({ changedFiles: [], metadata: {} }),
128
- );
129
-
130
- expect(snapshot.status).toBe('failed');
131
- expect(snapshot.attempt).toBe(1);
132
- expect(verifyCount).toBe(1);
133
- });
134
-
135
- it('应该在 gather 失败时返回 failed', async () => {
136
- const loop = new VerificationLoop(baseConfig);
137
- const gather = async (): Promise<GatherState> => {
138
- throw new Error('gather failed');
139
- };
140
-
141
- const snapshot = await loop.run({ projectRoot: '.' }, gather);
142
- expect(snapshot.status).toBe('failed');
143
- expect(snapshot.lastError).toContain('Gather 失败');
144
- });
145
-
146
- it('应该在 act 返回 abort 时中止', async () => {
147
- let verifyCount = 0;
148
- const config: VerificationLoopConfig = {
149
- maxRetries: 3,
150
- rules: [
151
- {
152
- id: 'fail-rule',
153
- type: 'custom',
154
- name: 'Fail',
155
- description: '',
156
- verify: async () => {
157
- verifyCount++;
158
- return { passed: false, ruleId: 'fail-rule', duration: 0 };
159
- },
160
- },
161
- ],
162
- };
163
-
164
- const loop = new VerificationLoop(config);
165
- const snapshot = await loop.run(
166
- { projectRoot: '.' },
167
- async () => ({ changedFiles: [], metadata: {} }),
168
- async () => ({ type: 'abort', description: '无法修复' }),
169
- );
170
-
171
- expect(snapshot.status).toBe('failed');
172
- expect(snapshot.lastError).toContain('中止');
173
- // 只执行了 1 次 verify(第 2 轮 act 返回 abort 后未进入 verify)
174
- expect(verifyCount).toBe(1);
175
- });
176
-
177
- it('应该在 act 返回 skip 时跳过', async () => {
178
- const config: VerificationLoopConfig = {
179
- maxRetries: 3,
180
- rules: [
181
- {
182
- id: 'fail-rule',
183
- type: 'custom',
184
- name: 'Fail',
185
- description: '',
186
- verify: async () => ({ passed: false, ruleId: 'fail-rule', duration: 0 }),
187
- },
188
- ],
189
- };
190
-
191
- const loop = new VerificationLoop(config);
192
- const snapshot = await loop.run(
193
- { projectRoot: '.' },
194
- async () => ({ changedFiles: [], metadata: {} }),
195
- async () => ({ type: 'skip', description: '跳过' }),
196
- );
197
-
198
- expect(snapshot.status).toBe('passed');
199
- });
200
-
201
- it('第 1 轮不应该调用 act', async () => {
202
- let actCalled = false;
203
- const loop = new VerificationLoop(baseConfig);
204
-
205
- const snapshot = await loop.run(
206
- { projectRoot: '.' },
207
- async () => ({ changedFiles: [], metadata: {} }),
208
- async () => {
209
- actCalled = true;
210
- return { type: 'retry', description: 'retry' };
211
- },
212
- );
213
-
214
- expect(snapshot.status).toBe('passed');
215
- expect(actCalled).toBe(false);
216
- });
217
-
218
- it('应该在 act 抛出异常时返回 failed', async () => {
219
- const config: VerificationLoopConfig = {
220
- maxRetries: 3,
221
- rules: [
222
- {
223
- id: 'fail-rule',
224
- type: 'custom',
225
- name: 'Fail',
226
- description: '',
227
- verify: async () => ({ passed: false, ruleId: 'fail-rule', duration: 0 }),
228
- },
229
- ],
230
- };
231
-
232
- const loop = new VerificationLoop(config);
233
- const snapshot = await loop.run(
234
- { projectRoot: '.' },
235
- async () => ({ changedFiles: [], metadata: {} }),
236
- async () => { throw new Error('act crashed'); },
237
- );
238
-
239
- expect(snapshot.status).toBe('failed');
240
- expect(snapshot.lastError).toContain('Act 失败');
241
- expect(snapshot.lastError).toContain('act crashed');
242
- });
243
-
244
- it('应该在 verify 函数抛出异常时视为失败并重试', async () => {
245
- const config: VerificationLoopConfig = {
246
- maxRetries: 2,
247
- rules: [
248
- {
249
- id: 'throw-rule',
250
- type: 'custom',
251
- name: 'Throw',
252
- description: '',
253
- verify: async () => { throw new Error('verify crashed'); },
254
- },
255
- ],
256
- };
257
-
258
- const loop = new VerificationLoop(config);
259
- const snapshot = await loop.run(
260
- { projectRoot: '.' },
261
- async () => ({ changedFiles: [], metadata: {} }),
262
- );
263
-
264
- // verifyRule 内部 catch 了异常,返回 passed: false
265
- // 循环重试 maxRetries 次后失败
266
- expect(snapshot.status).toBe('failed');
267
- expect(snapshot.attempt).toBe(2);
268
- expect(snapshot.results[0].message).toContain('verify crashed');
269
- });
270
- });
271
-
272
- describe('snapshot', () => {
273
- it('应该包含完整快照信息', async () => {
274
- const loop = new VerificationLoop(baseConfig);
275
- const snapshot = await loop.run(
276
- { projectRoot: '.' },
277
- async () => ({ changedFiles: [], metadata: {} }),
278
- );
279
-
280
- expect(snapshot.timestamp).toBeDefined();
281
- expect(snapshot.maxRetries).toBe(3);
282
- expect(Array.isArray(snapshot.results)).toBe(true);
283
- });
284
- });
285
-
286
- describe('getters', () => {
287
- it('应该返回当前状态', async () => {
288
- const loop = new VerificationLoop(baseConfig);
289
- expect(loop.getStatus()).toBe('idle');
290
- expect(loop.getAttempt()).toBe(0);
291
- expect(loop.getResults()).toEqual([]);
292
-
293
- await loop.run(
294
- { projectRoot: '.' },
295
- async () => ({ changedFiles: [], metadata: {} }),
296
- );
297
-
298
- expect(loop.getStatus()).toBe('passed');
299
- expect(loop.getAttempt()).toBe(1);
300
- expect(loop.getResults().length).toBeGreaterThan(0);
301
- });
302
- });
303
- });