@dommaker/harness 1.1.1 → 1.2.1

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 (373) hide show
  1. package/CHANGELOG.md +17 -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 +8 -33
  7. package/dist/cli/commands/check.js.map +1 -1
  8. package/dist/cli/commands/constraints-report.js +1 -1
  9. package/dist/cli/commands/constraints-report.js.map +1 -1
  10. package/dist/cli/commands/constraints-retire.d.ts +6 -3
  11. package/dist/cli/commands/constraints-retire.d.ts.map +1 -1
  12. package/dist/cli/commands/constraints-retire.js +34 -25
  13. package/dist/cli/commands/constraints-retire.js.map +1 -1
  14. package/dist/cli/commands/definitions.d.ts +2 -0
  15. package/dist/cli/commands/definitions.d.ts.map +1 -1
  16. package/dist/cli/commands/definitions.js.map +1 -1
  17. package/dist/cli/commands/init.d.ts +21 -1
  18. package/dist/cli/commands/init.d.ts.map +1 -1
  19. package/dist/cli/commands/init.js +118 -40
  20. package/dist/cli/commands/init.js.map +1 -1
  21. package/dist/cli/commands/knowledge.d.ts.map +1 -1
  22. package/dist/cli/commands/knowledge.js +1 -12
  23. package/dist/cli/commands/knowledge.js.map +1 -1
  24. package/dist/cli/commands/report.d.ts.map +1 -1
  25. package/dist/cli/commands/report.js +0 -2
  26. package/dist/cli/commands/report.js.map +1 -1
  27. package/dist/cli/commands/status.js +2 -2
  28. package/dist/cli/commands/status.js.map +1 -1
  29. package/dist/context/index.d.ts +9 -6
  30. package/dist/context/index.d.ts.map +1 -1
  31. package/dist/context/index.js +18 -20
  32. package/dist/context/index.js.map +1 -1
  33. package/dist/core/constraints/capabilities-parser.d.ts +3 -2
  34. package/dist/core/constraints/capabilities-parser.d.ts.map +1 -1
  35. package/dist/core/constraints/capabilities-parser.js +25 -47
  36. package/dist/core/constraints/capabilities-parser.js.map +1 -1
  37. package/dist/core/constraints/checker.d.ts +29 -32
  38. package/dist/core/constraints/checker.d.ts.map +1 -1
  39. package/dist/core/constraints/checker.js +34 -126
  40. package/dist/core/constraints/checker.js.map +1 -1
  41. package/dist/core/constraints/checkers/governance-presence.d.ts +20 -0
  42. package/dist/core/constraints/checkers/governance-presence.d.ts.map +1 -0
  43. package/dist/core/constraints/checkers/governance-presence.js +76 -0
  44. package/dist/core/constraints/checkers/governance-presence.js.map +1 -0
  45. package/dist/core/constraints/checkers/index.d.ts +2 -1
  46. package/dist/core/constraints/checkers/index.d.ts.map +1 -1
  47. package/dist/core/constraints/checkers/index.js +5 -0
  48. package/dist/core/constraints/checkers/index.js.map +1 -1
  49. package/dist/core/constraints/checkers/types.d.ts +25 -0
  50. package/dist/core/constraints/checkers/types.d.ts.map +1 -1
  51. package/dist/core/constraints/checkers/types.js +24 -0
  52. package/dist/core/constraints/checkers/types.js.map +1 -1
  53. package/dist/core/constraints/definitions/guidelines.d.ts.map +1 -1
  54. package/dist/core/constraints/definitions/guidelines.js +21 -2
  55. package/dist/core/constraints/definitions/guidelines.js.map +1 -1
  56. package/dist/core/constraints/definitions/prompts.d.ts.map +1 -1
  57. package/dist/core/constraints/definitions/prompts.js +0 -5
  58. package/dist/core/constraints/definitions/prompts.js.map +1 -1
  59. package/dist/core/constraints/index.d.ts +1 -2
  60. package/dist/core/constraints/index.d.ts.map +1 -1
  61. package/dist/core/constraints/index.js +1 -7
  62. package/dist/core/constraints/index.js.map +1 -1
  63. package/dist/core/constraints/injection-drift.d.ts +29 -4
  64. package/dist/core/constraints/injection-drift.d.ts.map +1 -1
  65. package/dist/core/constraints/injection-drift.js +52 -20
  66. package/dist/core/constraints/injection-drift.js.map +1 -1
  67. package/dist/core/constraints/usage-report.d.ts +1 -2
  68. package/dist/core/constraints/usage-report.d.ts.map +1 -1
  69. package/dist/core/constraints/usage-report.js +2 -5
  70. package/dist/core/constraints/usage-report.js.map +1 -1
  71. package/dist/core/effective-constraints.d.ts +24 -4
  72. package/dist/core/effective-constraints.d.ts.map +1 -1
  73. package/dist/core/effective-constraints.js +28 -7
  74. package/dist/core/effective-constraints.js.map +1 -1
  75. package/dist/core/effective-set.d.ts +30 -0
  76. package/dist/core/effective-set.d.ts.map +1 -0
  77. package/dist/core/effective-set.js +35 -0
  78. package/dist/core/effective-set.js.map +1 -0
  79. package/dist/core/index.d.ts +13 -6
  80. package/dist/core/index.d.ts.map +1 -1
  81. package/dist/core/index.js +44 -21
  82. package/dist/core/index.js.map +1 -1
  83. package/dist/core/project-config-loader.d.ts +3 -6
  84. package/dist/core/project-config-loader.d.ts.map +1 -1
  85. package/dist/core/project-config-loader.js +47 -61
  86. package/dist/core/project-config-loader.js.map +1 -1
  87. package/dist/failure/constraint-handler.d.ts +1 -1
  88. package/dist/failure/constraint-handler.js +1 -1
  89. package/dist/gates/checker-gate.d.ts +1 -4
  90. package/dist/gates/checker-gate.d.ts.map +1 -1
  91. package/dist/gates/checker-gate.js +3 -11
  92. package/dist/gates/checker-gate.js.map +1 -1
  93. package/dist/gates/definitions.d.ts +9 -38
  94. package/dist/gates/definitions.d.ts.map +1 -1
  95. package/dist/gates/definitions.js +24 -13
  96. package/dist/gates/definitions.js.map +1 -1
  97. package/dist/gates/effective-gates.d.ts.map +1 -1
  98. package/dist/gates/effective-gates.js +14 -18
  99. package/dist/gates/effective-gates.js.map +1 -1
  100. package/dist/gates/index.d.ts +1 -1
  101. package/dist/gates/index.d.ts.map +1 -1
  102. package/dist/gates/index.js.map +1 -1
  103. package/dist/hooks/bootstrap.d.ts.map +1 -1
  104. package/dist/hooks/bootstrap.js +0 -3
  105. package/dist/hooks/bootstrap.js.map +1 -1
  106. package/dist/index.d.ts +43 -60
  107. package/dist/index.d.ts.map +1 -1
  108. package/dist/index.js +180 -143
  109. package/dist/index.js.map +1 -1
  110. package/dist/knowledge/query.d.ts +15 -0
  111. package/dist/knowledge/query.d.ts.map +1 -1
  112. package/dist/knowledge/query.js +23 -0
  113. package/dist/knowledge/query.js.map +1 -1
  114. package/dist/monitoring/analyzer-base.d.ts +0 -4
  115. package/dist/monitoring/analyzer-base.d.ts.map +1 -1
  116. package/dist/monitoring/analyzer-base.js +0 -21
  117. package/dist/monitoring/analyzer-base.js.map +1 -1
  118. package/dist/monitoring/index.d.ts +6 -12
  119. package/dist/monitoring/index.d.ts.map +1 -1
  120. package/dist/monitoring/index.js +13 -29
  121. package/dist/monitoring/index.js.map +1 -1
  122. package/dist/monitoring/trace-analyzer.d.ts.map +1 -1
  123. package/dist/monitoring/trace-analyzer.js +2 -68
  124. package/dist/monitoring/trace-analyzer.js.map +1 -1
  125. package/dist/monitoring/traces.d.ts +0 -4
  126. package/dist/monitoring/traces.d.ts.map +1 -1
  127. package/dist/monitoring/traces.js +0 -14
  128. package/dist/monitoring/traces.js.map +1 -1
  129. package/dist/presets/index.d.ts +2 -2
  130. package/dist/presets/index.d.ts.map +1 -1
  131. package/dist/presets/index.js +3 -4
  132. package/dist/presets/index.js.map +1 -1
  133. package/dist/presets/standard.d.ts +10 -22
  134. package/dist/presets/standard.d.ts.map +1 -1
  135. package/dist/presets/standard.js +15 -75
  136. package/dist/presets/standard.js.map +1 -1
  137. package/dist/types/constraint.d.ts +1 -29
  138. package/dist/types/constraint.d.ts.map +1 -1
  139. package/dist/types/constraint.js +1 -1
  140. package/dist/types/constraint.js.map +1 -1
  141. package/dist/types/index.d.ts +0 -3
  142. package/dist/types/index.d.ts.map +1 -1
  143. package/dist/types/index.js +0 -4
  144. package/dist/types/index.js.map +1 -1
  145. package/dist/types/project-config.d.ts +4 -8
  146. package/dist/types/project-config.d.ts.map +1 -1
  147. package/dist/types/trace.d.ts +5 -18
  148. package/dist/types/trace.d.ts.map +1 -1
  149. package/package.json +2 -2
  150. package/src/CONTEXT.md +4 -5
  151. package/src/__tests__/checker-extra.test.ts +33 -139
  152. package/src/__tests__/checker.test.ts +0 -376
  153. package/src/__tests__/governance-presence.test.ts +129 -0
  154. package/src/__tests__/iron-laws.test.ts +7 -7
  155. package/src/__tests__/project-config-loader-extra.test.ts +42 -29
  156. package/src/__tests__/public-exports.test.ts +138 -0
  157. package/src/__tests__/trace-analyzer-extra.test.ts +6 -86
  158. package/src/__tests__/trace-analyzer.test.ts +4 -5
  159. package/src/__tests__/trace-collector.test.ts +0 -11
  160. package/src/__tests__/traces.test.ts +3 -22
  161. package/src/cli/commands/CONTEXT.md +6 -3
  162. package/src/cli/commands/__tests__/check-skip-output.test.ts +10 -7
  163. package/src/cli/commands/__tests__/check.test.ts +29 -69
  164. package/src/cli/commands/__tests__/constraints-report.test.ts +3 -3
  165. package/src/cli/commands/__tests__/constraints-retire.test.ts +45 -4
  166. package/src/cli/commands/__tests__/init-injection.test.ts +202 -0
  167. package/src/cli/commands/__tests__/knowledge.test.ts +90 -1
  168. package/src/cli/commands/__tests__/registry.test.ts +8 -6
  169. package/src/cli/commands/__tests__/status-extra.test.ts +6 -6
  170. package/src/cli/commands/__tests__/status.test.ts +6 -6
  171. package/src/cli/commands/__tests__/sync-docs-agents.test.ts +28 -0
  172. package/src/cli/commands/check.ts +9 -35
  173. package/src/cli/commands/constraints-report.ts +1 -1
  174. package/src/cli/commands/constraints-retire.ts +38 -27
  175. package/src/cli/commands/definitions.ts +2 -0
  176. package/src/cli/commands/init.ts +129 -40
  177. package/src/cli/commands/knowledge.ts +1 -16
  178. package/src/cli/commands/report.ts +0 -2
  179. package/src/cli/commands/status.ts +2 -2
  180. package/src/context/index.ts +29 -6
  181. package/src/core/CONTEXT.md +4 -3
  182. package/src/core/__tests__/effective-set.test.ts +80 -0
  183. package/src/core/constraints/__tests__/injection-drift.test.ts +90 -0
  184. package/src/core/constraints/capabilities-parser.ts +43 -49
  185. package/src/core/constraints/checker.ts +51 -145
  186. package/src/core/constraints/checkers/governance-presence.ts +74 -0
  187. package/src/core/constraints/checkers/index.ts +4 -1
  188. package/src/core/constraints/checkers/types.ts +42 -0
  189. package/src/core/constraints/definitions/guidelines.ts +22 -2
  190. package/src/core/constraints/definitions/prompts.ts +0 -5
  191. package/src/core/constraints/index.ts +2 -17
  192. package/src/core/constraints/injection-drift.ts +60 -21
  193. package/src/core/constraints/usage-report.ts +5 -8
  194. package/src/core/effective-constraints.ts +35 -7
  195. package/src/core/effective-set.ts +56 -0
  196. package/src/core/index.ts +80 -6
  197. package/src/core/project-config-loader.ts +53 -68
  198. package/src/failure/CONTEXT.md +1 -1
  199. package/src/failure/constraint-handler.ts +1 -1
  200. package/src/gates/CONTEXT.md +3 -3
  201. package/src/gates/checker-gate.ts +4 -12
  202. package/src/gates/definitions.ts +27 -50
  203. package/src/gates/effective-gates.ts +14 -21
  204. package/src/gates/index.ts +1 -1
  205. package/src/hooks/bootstrap.ts +0 -3
  206. package/src/index.ts +384 -148
  207. package/src/knowledge/CONTEXT.md +2 -2
  208. package/src/knowledge/__tests__/query.test.ts +64 -0
  209. package/src/knowledge/query.ts +28 -0
  210. package/src/monitoring/CONTEXT.md +7 -13
  211. package/src/monitoring/__tests__/traces.test.ts +0 -10
  212. package/src/monitoring/analyzer-base.ts +0 -22
  213. package/src/monitoring/index.ts +6 -12
  214. package/src/monitoring/trace-analyzer.ts +2 -73
  215. package/src/monitoring/traces.ts +0 -20
  216. package/src/presets/__tests__/standard.test.ts +48 -69
  217. package/src/presets/index.ts +3 -5
  218. package/src/presets/standard.ts +14 -79
  219. package/src/spec/CONTEXT.md +5 -13
  220. package/src/types/constraint.ts +1 -51
  221. package/src/types/index.ts +0 -12
  222. package/src/types/project-config.ts +4 -10
  223. package/src/types/trace.ts +5 -30
  224. package/dist/core/constraints/interceptor.d.ts +0 -39
  225. package/dist/core/constraints/interceptor.d.ts.map +0 -1
  226. package/dist/core/constraints/interceptor.js +0 -166
  227. package/dist/core/constraints/interceptor.js.map +0 -1
  228. package/dist/dashboard/data.d.ts +0 -31
  229. package/dist/dashboard/data.d.ts.map +0 -1
  230. package/dist/dashboard/data.js +0 -81
  231. package/dist/dashboard/data.js.map +0 -1
  232. package/dist/dashboard/index.d.ts +0 -4
  233. package/dist/dashboard/index.d.ts.map +0 -1
  234. package/dist/dashboard/index.js +0 -20
  235. package/dist/dashboard/index.js.map +0 -1
  236. package/dist/dashboard/stats.d.ts +0 -20
  237. package/dist/dashboard/stats.d.ts.map +0 -1
  238. package/dist/dashboard/stats.js +0 -115
  239. package/dist/dashboard/stats.js.map +0 -1
  240. package/dist/dashboard/types.d.ts +0 -110
  241. package/dist/dashboard/types.d.ts.map +0 -1
  242. package/dist/dashboard/types.js +0 -8
  243. package/dist/dashboard/types.js.map +0 -1
  244. package/dist/monitoring/constraint-doctor.d.ts +0 -76
  245. package/dist/monitoring/constraint-doctor.d.ts.map +0 -1
  246. package/dist/monitoring/constraint-doctor.js +0 -189
  247. package/dist/monitoring/constraint-doctor.js.map +0 -1
  248. package/dist/monitoring/diagnosis-rules.d.ts +0 -18
  249. package/dist/monitoring/diagnosis-rules.d.ts.map +0 -1
  250. package/dist/monitoring/diagnosis-rules.js +0 -119
  251. package/dist/monitoring/diagnosis-rules.js.map +0 -1
  252. package/dist/monitoring/knowledge-doctor.d.ts +0 -78
  253. package/dist/monitoring/knowledge-doctor.d.ts.map +0 -1
  254. package/dist/monitoring/knowledge-doctor.js +0 -266
  255. package/dist/monitoring/knowledge-doctor.js.map +0 -1
  256. package/dist/monitoring/knowledge-evolver.d.ts +0 -72
  257. package/dist/monitoring/knowledge-evolver.d.ts.map +0 -1
  258. package/dist/monitoring/knowledge-evolver.js +0 -205
  259. package/dist/monitoring/knowledge-evolver.js.map +0 -1
  260. package/dist/monitoring/performance-analyzer.d.ts +0 -92
  261. package/dist/monitoring/performance-analyzer.d.ts.map +0 -1
  262. package/dist/monitoring/performance-analyzer.js +0 -343
  263. package/dist/monitoring/performance-analyzer.js.map +0 -1
  264. package/dist/monitoring/performance-collector.d.ts +0 -103
  265. package/dist/monitoring/performance-collector.d.ts.map +0 -1
  266. package/dist/monitoring/performance-collector.js +0 -298
  267. package/dist/monitoring/performance-collector.js.map +0 -1
  268. package/dist/safety/index.d.ts +0 -6
  269. package/dist/safety/index.d.ts.map +0 -1
  270. package/dist/safety/index.js +0 -22
  271. package/dist/safety/index.js.map +0 -1
  272. package/dist/safety/input-guardrail.d.ts +0 -36
  273. package/dist/safety/input-guardrail.d.ts.map +0 -1
  274. package/dist/safety/input-guardrail.js +0 -118
  275. package/dist/safety/input-guardrail.js.map +0 -1
  276. package/dist/safety/output-guardrail.d.ts +0 -39
  277. package/dist/safety/output-guardrail.d.ts.map +0 -1
  278. package/dist/safety/output-guardrail.js +0 -147
  279. package/dist/safety/output-guardrail.js.map +0 -1
  280. package/dist/safety/sandbox.d.ts +0 -45
  281. package/dist/safety/sandbox.d.ts.map +0 -1
  282. package/dist/safety/sandbox.js +0 -134
  283. package/dist/safety/sandbox.js.map +0 -1
  284. package/dist/safety/tool-guardrail.d.ts +0 -51
  285. package/dist/safety/tool-guardrail.d.ts.map +0 -1
  286. package/dist/safety/tool-guardrail.js +0 -147
  287. package/dist/safety/tool-guardrail.js.map +0 -1
  288. package/dist/safety/types.d.ts +0 -104
  289. package/dist/safety/types.d.ts.map +0 -1
  290. package/dist/safety/types.js +0 -6
  291. package/dist/safety/types.js.map +0 -1
  292. package/dist/spec/annotation-checker.d.ts +0 -47
  293. package/dist/spec/annotation-checker.d.ts.map +0 -1
  294. package/dist/spec/annotation-checker.js +0 -279
  295. package/dist/spec/annotation-checker.js.map +0 -1
  296. package/dist/types/enforcement.d.ts +0 -42
  297. package/dist/types/enforcement.d.ts.map +0 -1
  298. package/dist/types/enforcement.js +0 -6
  299. package/dist/types/enforcement.js.map +0 -1
  300. package/dist/types/monitoring-types.d.ts +0 -53
  301. package/dist/types/monitoring-types.d.ts.map +0 -1
  302. package/dist/types/monitoring-types.js +0 -10
  303. package/dist/types/monitoring-types.js.map +0 -1
  304. package/dist/types/performance.d.ts +0 -204
  305. package/dist/types/performance.d.ts.map +0 -1
  306. package/dist/types/performance.js +0 -12
  307. package/dist/types/performance.js.map +0 -1
  308. package/dist/verification/index.d.ts +0 -4
  309. package/dist/verification/index.d.ts.map +0 -1
  310. package/dist/verification/index.js +0 -20
  311. package/dist/verification/index.js.map +0 -1
  312. package/dist/verification/loop.d.ts +0 -30
  313. package/dist/verification/loop.d.ts.map +0 -1
  314. package/dist/verification/loop.js +0 -114
  315. package/dist/verification/loop.js.map +0 -1
  316. package/dist/verification/rules-based.d.ts +0 -40
  317. package/dist/verification/rules-based.d.ts.map +0 -1
  318. package/dist/verification/rules-based.js +0 -130
  319. package/dist/verification/rules-based.js.map +0 -1
  320. package/dist/verification/types.d.ts +0 -69
  321. package/dist/verification/types.d.ts.map +0 -1
  322. package/dist/verification/types.js +0 -6
  323. package/dist/verification/types.js.map +0 -1
  324. package/src/__tests__/annotation-checker.test.ts +0 -183
  325. package/src/__tests__/constraint-doctor.test.ts +0 -271
  326. package/src/__tests__/doctor-extra.test.ts +0 -155
  327. package/src/__tests__/extend-exceptions.test.ts +0 -149
  328. package/src/__tests__/interceptor.test.ts +0 -89
  329. package/src/__tests__/performance-analyzer.test.ts +0 -499
  330. package/src/__tests__/performance-collector.test.ts +0 -276
  331. package/src/__tests__/presets.test.ts +0 -65
  332. package/src/core/constraints/__tests__/interceptor.test.ts +0 -267
  333. package/src/core/constraints/interceptor.ts +0 -217
  334. package/src/dashboard/CONTEXT.md +0 -21
  335. package/src/dashboard/__tests__/data.test.ts +0 -111
  336. package/src/dashboard/__tests__/stats.test.ts +0 -159
  337. package/src/dashboard/data.ts +0 -103
  338. package/src/dashboard/index.ts +0 -3
  339. package/src/dashboard/stats.ts +0 -127
  340. package/src/dashboard/types.ts +0 -130
  341. package/src/monitoring/__tests__/knowledge-doctor.test.ts +0 -278
  342. package/src/monitoring/__tests__/knowledge-evolver.test.ts +0 -368
  343. package/src/monitoring/__tests__/performance-analyzer.test.ts +0 -137
  344. package/src/monitoring/__tests__/performance-collector.test.ts +0 -331
  345. package/src/monitoring/constraint-doctor.ts +0 -228
  346. package/src/monitoring/diagnosis-rules.ts +0 -132
  347. package/src/monitoring/knowledge-doctor.ts +0 -322
  348. package/src/monitoring/knowledge-evolver.ts +0 -289
  349. package/src/monitoring/performance-analyzer.ts +0 -400
  350. package/src/monitoring/performance-collector.ts +0 -326
  351. package/src/safety/CONTEXT.md +0 -23
  352. package/src/safety/__tests__/input-guardrail.test.ts +0 -105
  353. package/src/safety/__tests__/output-guardrail.test.ts +0 -108
  354. package/src/safety/__tests__/sandbox.test.ts +0 -158
  355. package/src/safety/__tests__/tool-guardrail.test.ts +0 -127
  356. package/src/safety/index.ts +0 -5
  357. package/src/safety/input-guardrail.ts +0 -133
  358. package/src/safety/output-guardrail.ts +0 -170
  359. package/src/safety/sandbox.ts +0 -148
  360. package/src/safety/tool-guardrail.ts +0 -176
  361. package/src/safety/types.ts +0 -124
  362. package/src/spec/__tests__/annotation-checker.test.ts +0 -294
  363. package/src/spec/annotation-checker.ts +0 -320
  364. package/src/types/enforcement.ts +0 -48
  365. package/src/types/monitoring-types.ts +0 -68
  366. package/src/types/performance.ts +0 -295
  367. package/src/verification/CONTEXT.md +0 -23
  368. package/src/verification/__tests__/loop.test.ts +0 -303
  369. package/src/verification/__tests__/rules-based.test.ts +0 -240
  370. package/src/verification/index.ts +0 -3
  371. package/src/verification/loop.ts +0 -137
  372. package/src/verification/rules-based.ts +0 -152
  373. package/src/verification/types.ts +0 -81
@@ -1,170 +0,0 @@
1
- /**
2
- * 输出护栏
3
- *
4
- * 在最终输出前检查:
5
- * - 敏感信息审查(API keys, passwords, tokens)
6
- * - 代码质量检查
7
- * - 知识引用完整性
8
- */
9
-
10
- import type { OutputSafetyCheckResult, OutputViolation, OutputGuardrailConfig } from './types';
11
-
12
- const DEFAULT_SENSITIVE_PATTERNS: RegExp[] = [
13
- // API keys
14
- /(?:api[_-]?key|apikey)\s*[:=]\s*['"]?([a-zA-Z0-9_-]{20,})['"]?/gi,
15
- // AWS
16
- /(?:AKIA|ASIA)[A-Z0-9]{16}/g,
17
- // Generic secrets
18
- /(?:secret|password|passwd|token|auth)\s*[:=]\s*['"]?([^\s'"]{8,})['"]?/gi,
19
- // Private keys
20
- /-----BEGIN\s+(RSA\s+)?PRIVATE\s+KEY-----/g,
21
- // GitHub tokens
22
- /gh[ps]_[A-Za-z0-9_]{36,}/g,
23
- // Bearer tokens
24
- /Bearer\s+[A-Za-z0-9_\-.]{20,}/g,
25
- // Connection strings
26
- /(?:mongodb|postgres|mysql|redis):\/\/[^\s'"]+/gi,
27
- ];
28
-
29
- export class OutputGuardrail {
30
- private sensitivePatterns: RegExp[];
31
- private checkKnowledgeRefs: boolean;
32
-
33
- constructor(config?: OutputGuardrailConfig) {
34
- this.sensitivePatterns = config?.sensitivePatterns ?? DEFAULT_SENSITIVE_PATTERNS;
35
- this.checkKnowledgeRefs = config?.checkKnowledgeRefs ?? false;
36
- }
37
-
38
- /**
39
- * 检查输出安全性
40
- */
41
- check(content: string, knowledgeRefIds?: string[]): OutputSafetyCheckResult {
42
- const violations: OutputViolation[] = [];
43
-
44
- // 敏感信息检测
45
- violations.push(...this.checkSensitiveInfo(content));
46
-
47
- // 代码质量检查
48
- violations.push(...this.checkQuality(content));
49
-
50
- // 知识引用完整性
51
- if (this.checkKnowledgeRefs && knowledgeRefIds) {
52
- violations.push(...this.checkKnowledgeRefs_(
53
- content,
54
- knowledgeRefIds,
55
- ));
56
- }
57
-
58
- const sanitized = violations.length > 0
59
- ? this.sanitize(content, violations)
60
- : undefined;
61
-
62
- return {
63
- safe: violations.length === 0,
64
- violations,
65
- sanitizedContent: sanitized,
66
- };
67
- }
68
-
69
- /**
70
- * 检测敏感信息
71
- */
72
- private checkSensitiveInfo(content: string): OutputViolation[] {
73
- const violations: OutputViolation[] = [];
74
-
75
- for (const pattern of this.sensitivePatterns) {
76
- // Reset lastIndex for global patterns
77
- pattern.lastIndex = 0;
78
- let match: RegExpExecArray | null;
79
- while ((match = pattern.exec(content)) !== null) {
80
- const line = this.getLineNumber(content, match.index);
81
- violations.push({
82
- type: 'sensitive_info',
83
- severity: 'high',
84
- description: `检测到敏感信息: ${match[0].slice(0, 30)}...`,
85
- location: { line, column: match.index - content.lastIndexOf('\n', match.index) - 1 },
86
- matchedPattern: match[0],
87
- });
88
- }
89
- }
90
-
91
- return violations;
92
- }
93
-
94
- /**
95
- * 代码质量检查(简单启发式)
96
- */
97
- private checkQuality(content: string): OutputViolation[] {
98
- const violations: OutputViolation[] = [];
99
-
100
- // 检查 TODO/FIXME/HACK
101
- const todoPattern = /\b(TODO|FIXME|HACK|XXX)\b/gi;
102
- let match: RegExpExecArray | null;
103
- while ((match = todoPattern.exec(content)) !== null) {
104
- const line = this.getLineNumber(content, match.index);
105
- violations.push({
106
- type: 'quality',
107
- severity: 'low',
108
- description: `代码包含 ${match[1]} 标记`,
109
- location: { line, column: match.index - content.lastIndexOf('\n', match.index) - 1 },
110
- });
111
- }
112
-
113
- // 检查 console.log 残留
114
- const consolePattern = /console\.(log|debug|info)\(/g;
115
- while ((match = consolePattern.exec(content)) !== null) {
116
- const line = this.getLineNumber(content, match.index);
117
- violations.push({
118
- type: 'quality',
119
- severity: 'low',
120
- description: `代码包含 ${match[0].slice(0, -1)} 调用`,
121
- location: { line, column: match.index - content.lastIndexOf('\n', match.index) - 1 },
122
- });
123
- }
124
-
125
- return violations;
126
- }
127
-
128
- /**
129
- * 检查知识引用完整性
130
- */
131
- private checkKnowledgeRefs_(content: string, referencedIds: string[]): OutputViolation[] {
132
- const violations: OutputViolation[] = [];
133
-
134
- for (const id of referencedIds) {
135
- if (!content.includes(id)) {
136
- violations.push({
137
- type: 'knowledge_missing',
138
- severity: 'medium',
139
- description: `引用的知识条目 ${id} 未出现在输出中`,
140
- });
141
- }
142
- }
143
-
144
- return violations;
145
- }
146
-
147
- /**
148
- * 清理敏感信息
149
- */
150
- private sanitize(content: string, violations: OutputViolation[]): string {
151
- let sanitized = content;
152
-
153
- for (const violation of violations) {
154
- if (violation.type === 'sensitive_info' && violation.matchedPattern) {
155
- // Escape special regex characters in the matched string
156
- const escaped = violation.matchedPattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
157
- sanitized = sanitized.replace(new RegExp(escaped, 'g'), '[REDACTED]');
158
- }
159
- }
160
-
161
- return sanitized;
162
- }
163
-
164
- /**
165
- * 获取行号
166
- */
167
- private getLineNumber(content: string, index: number): number {
168
- return content.slice(0, index).split('\n').length;
169
- }
170
- }
@@ -1,148 +0,0 @@
1
- /**
2
- * Sandbox 安全级别管理
3
- *
4
- * 四级权限模型:
5
- * Level 1: 只读文件系统,无网络,无 shell
6
- * Level 2: 可写限定目录(.harness/),无网络
7
- * Level 3: 可写项目目录,受限网络(白名单)
8
- * Level 4: 完全权限,需用户确认
9
- */
10
-
11
- import type { SandboxLevel, SandboxConfig, SandboxCheckResult } from './types';
12
-
13
- const LEVEL_DESCRIPTIONS: Record<SandboxLevel, string> = {
14
- 1: '只读文件系统,无网络,无 shell',
15
- 2: '可写限定目录(.harness/),无网络',
16
- 3: '可写项目目录,受限网络(白名单)',
17
- 4: '完全权限,需用户确认',
18
- };
19
-
20
- const DEFAULT_WRITABLE_DIRS = ['.harness/'];
21
-
22
- export class Sandbox {
23
- private config: SandboxConfig;
24
-
25
- constructor(config?: Partial<SandboxConfig>) {
26
- this.config = {
27
- level: config?.level ?? 3,
28
- writableDirs: config?.writableDirs ?? DEFAULT_WRITABLE_DIRS,
29
- allowedHosts: config?.allowedHosts ?? [],
30
- requiresConfirmation: config?.requiresConfirmation ?? (config?.level === 4),
31
- };
32
- }
33
-
34
- getLevel(): SandboxLevel {
35
- return this.config.level;
36
- }
37
-
38
- getDescription(): string {
39
- return LEVEL_DESCRIPTIONS[this.config.level];
40
- }
41
-
42
- /**
43
- * 检查当前级别是否满足操作所需级别
44
- */
45
- check(requiredLevel: SandboxLevel): SandboxCheckResult {
46
- const allowed = this.config.level >= requiredLevel;
47
- return {
48
- allowed,
49
- reason: allowed ? undefined : `当前 Level ${this.config.level} 不满足操作所需的 Level ${requiredLevel}`,
50
- currentLevel: this.config.level,
51
- requiredLevel,
52
- };
53
- }
54
-
55
- /**
56
- * 检查文件写入是否允许
57
- */
58
- checkFileWrite(filePath: string): SandboxCheckResult {
59
- if (this.config.level >= 3) {
60
- return { allowed: true, currentLevel: this.config.level, requiredLevel: 3 };
61
- }
62
-
63
- if (this.config.level >= 2) {
64
- const inWritableDir = this.config.writableDirs?.some(dir => filePath.startsWith(dir));
65
- if (inWritableDir) {
66
- return { allowed: true, currentLevel: this.config.level, requiredLevel: 2 };
67
- }
68
- return {
69
- allowed: false,
70
- reason: `Level 2 只允许写入 ${this.config.writableDirs?.join(', ')}`,
71
- currentLevel: this.config.level,
72
- requiredLevel: 3,
73
- };
74
- }
75
-
76
- return {
77
- allowed: false,
78
- reason: 'Level 1 禁止写入任何文件',
79
- currentLevel: this.config.level,
80
- requiredLevel: 2,
81
- };
82
- }
83
-
84
- /**
85
- * 检查网络访问是否允许
86
- */
87
- checkNetworkAccess(host: string): SandboxCheckResult {
88
- if (this.config.level >= 4) {
89
- return { allowed: true, currentLevel: this.config.level, requiredLevel: 4 };
90
- }
91
-
92
- if (this.config.level >= 3) {
93
- const allowed = this.config.allowedHosts?.includes(host) ?? false;
94
- return {
95
- allowed,
96
- reason: allowed ? undefined : `Host ${host} 不在白名单中`,
97
- currentLevel: this.config.level,
98
- requiredLevel: 3,
99
- };
100
- }
101
-
102
- return {
103
- allowed: false,
104
- reason: `Level ${this.config.level} 禁止网络访问`,
105
- currentLevel: this.config.level,
106
- requiredLevel: 3,
107
- };
108
- }
109
-
110
- /**
111
- * 检查 shell 命令是否允许
112
- */
113
- checkShellAccess(): SandboxCheckResult {
114
- if (this.config.level >= 3) {
115
- return { allowed: true, currentLevel: this.config.level, requiredLevel: 3 };
116
- }
117
- return {
118
- allowed: false,
119
- reason: `Level ${this.config.level} 禁止 shell 访问`,
120
- currentLevel: this.config.level,
121
- requiredLevel: 3,
122
- };
123
- }
124
-
125
- /**
126
- * 检查是否需要用户确认
127
- */
128
- needsConfirmation(): boolean {
129
- return this.config.requiresConfirmation ?? false;
130
- }
131
-
132
- /**
133
- * 升级到指定级别
134
- */
135
- upgradeTo(level: SandboxLevel): void {
136
- this.config.level = level;
137
- if (level === 4) {
138
- this.config.requiresConfirmation = true;
139
- }
140
- }
141
-
142
- /**
143
- * 获取当前配置
144
- */
145
- getConfig(): SandboxConfig {
146
- return { ...this.config };
147
- }
148
- }
@@ -1,176 +0,0 @@
1
- /**
2
- * 工具护栏
3
- *
4
- * 每次工具调用前检查:
5
- * - 命令黑名单
6
- * - Sandbox 级别检查
7
- * - 速率限制
8
- */
9
-
10
- import type {
11
- ToolCheckResult,
12
- ToolViolation,
13
- ToolGuardrailConfig,
14
- RateLimitState,
15
- SandboxLevel,
16
- } from './types';
17
- import { Sandbox } from './sandbox';
18
-
19
- const DEFAULT_BLACKLISTED_COMMANDS = [
20
- 'rm -rf /',
21
- 'rm -rf /*',
22
- 'mkfs',
23
- 'dd if=',
24
- ':(){:|:&};:', // fork bomb
25
- 'chmod -R 777 /',
26
- '| sh',
27
- '| bash',
28
- 'eval(',
29
- 'exec(',
30
- ];
31
-
32
- const DEFAULT_RATE_LIMIT = 60; // per minute
33
-
34
- export class ToolGuardrail {
35
- private blacklistedCommands: string[];
36
- private rateLimit: number;
37
- private toolSandboxLevels: Record<string, SandboxLevel>;
38
- private rateLimitState: Map<string, RateLimitState>;
39
- private sandbox: Sandbox;
40
-
41
- constructor(sandbox: Sandbox, config?: ToolGuardrailConfig) {
42
- this.sandbox = sandbox;
43
- this.blacklistedCommands = config?.blacklistedCommands ?? DEFAULT_BLACKLISTED_COMMANDS;
44
- this.rateLimit = config?.rateLimit ?? DEFAULT_RATE_LIMIT;
45
- this.toolSandboxLevels = config?.toolSandboxLevels ?? {};
46
- this.rateLimitState = new Map();
47
- }
48
-
49
- /**
50
- * 检查工具调用是否允许
51
- */
52
- check(toolName: string, command?: string): ToolCheckResult {
53
- const violations: ToolViolation[] = [];
54
-
55
- // 命令黑名单检查
56
- if (command) {
57
- violations.push(...this.checkBlacklist(toolName, command));
58
- }
59
-
60
- // Sandbox 级别检查
61
- violations.push(...this.checkSandboxLevel(toolName));
62
-
63
- // 速率限制检查
64
- violations.push(...this.checkRateLimit(toolName));
65
-
66
- return {
67
- allowed: violations.length === 0,
68
- violations,
69
- };
70
- }
71
-
72
- /**
73
- * 检查命令黑名单
74
- */
75
- private checkBlacklist(toolName: string, command: string): ToolViolation[] {
76
- const violations: ToolViolation[] = [];
77
- const lower = command.toLowerCase();
78
-
79
- for (const blocked of this.blacklistedCommands) {
80
- if (lower.includes(blocked.toLowerCase())) {
81
- violations.push({
82
- type: 'blacklist',
83
- severity: 'high',
84
- description: `命令包含被阻止的模式: ${blocked}`,
85
- toolName,
86
- });
87
- }
88
- }
89
-
90
- return violations;
91
- }
92
-
93
- /**
94
- * 检查 sandbox 级别
95
- */
96
- private checkSandboxLevel(toolName: string): ToolViolation[] {
97
- const violations: ToolViolation[] = [];
98
- const requiredLevel = this.toolSandboxLevels[toolName];
99
-
100
- if (requiredLevel !== undefined) {
101
- const result = this.sandbox.check(requiredLevel);
102
- if (!result.allowed) {
103
- violations.push({
104
- type: 'sandbox',
105
- severity: 'high',
106
- description: result.reason ?? `工具 ${toolName} 需要 Level ${requiredLevel}`,
107
- toolName,
108
- });
109
- }
110
- }
111
-
112
- return violations;
113
- }
114
-
115
- /**
116
- * 检查速率限制
117
- */
118
- private checkRateLimit(toolName: string): ToolViolation[] {
119
- const violations: ToolViolation[] = [];
120
- const now = Date.now();
121
- const state = this.rateLimitState.get(toolName);
122
-
123
- if (!state) {
124
- this.rateLimitState.set(toolName, { count: 1, windowStart: now });
125
- return violations;
126
- }
127
-
128
- // 重置窗口(1 分钟)
129
- if (now - state.windowStart > 60000) {
130
- state.count = 1;
131
- state.windowStart = now;
132
- return violations;
133
- }
134
-
135
- state.count++;
136
-
137
- if (state.count > this.rateLimit) {
138
- violations.push({
139
- type: 'rate_limit',
140
- severity: 'medium',
141
- description: `工具 ${toolName} 超过速率限制 (${this.rateLimit}/min)`,
142
- toolName,
143
- });
144
- }
145
-
146
- return violations;
147
- }
148
-
149
- /**
150
- * 添加黑名单命令
151
- */
152
- addBlacklistedCommand(command: string): void {
153
- this.blacklistedCommands.push(command);
154
- }
155
-
156
- /**
157
- * 设置工具 sandbox 级别要求
158
- */
159
- setToolSandboxLevel(toolName: string, level: SandboxLevel): void {
160
- this.toolSandboxLevels[toolName] = level;
161
- }
162
-
163
- /**
164
- * 重置速率限制状态
165
- */
166
- resetRateLimits(): void {
167
- this.rateLimitState.clear();
168
- }
169
-
170
- /**
171
- * 获取 sandbox 实例
172
- */
173
- getSandbox(): Sandbox {
174
- return this.sandbox;
175
- }
176
- }
@@ -1,124 +0,0 @@
1
- /**
2
- * Safety Guardrails 类型定义
3
- */
4
-
5
- // ── Sandbox ──────────────────────────────────────────────
6
-
7
- /** Sandbox 安全级别 */
8
- export type SandboxLevel = 1 | 2 | 3 | 4;
9
-
10
- /** Sandbox 配置 */
11
- export interface SandboxConfig {
12
- level: SandboxLevel;
13
- /** 允许写入的目录(Level 2+) */
14
- writableDirs?: string[];
15
- /** 允许的网络白名单(Level 3) */
16
- allowedHosts?: string[];
17
- /** 需要用户确认的操作(Level 4) */
18
- requiresConfirmation?: boolean;
19
- }
20
-
21
- /** Sandbox 权限检查结果 */
22
- export interface SandboxCheckResult {
23
- allowed: boolean;
24
- reason?: string;
25
- currentLevel: SandboxLevel;
26
- requiredLevel: SandboxLevel;
27
- }
28
-
29
- // ── Input Guardrail ──────────────────────────────────────
30
-
31
- /** 输入检查结果 */
32
- export interface InputCheckResult {
33
- safe: boolean;
34
- violations: InputViolation[];
35
- }
36
-
37
- /** 输入违规 */
38
- export interface InputViolation {
39
- type: 'injection' | 'intent' | 'permission';
40
- severity: 'low' | 'medium' | 'high';
41
- description: string;
42
- matchedPattern?: string;
43
- }
44
-
45
- /** 输入护栏配置 */
46
- export interface InputGuardrailConfig {
47
- /** 注入检测模式 */
48
- injectionPatterns?: RegExp[];
49
- /** 禁止的意图关键词 */
50
- blockedIntents?: string[];
51
- /** 最大输入长度 */
52
- maxInputLength?: number;
53
- }
54
-
55
- // ── Output Guardrail ─────────────────────────────────────
56
-
57
- /** 输出检查结果 */
58
- export interface OutputSafetyCheckResult {
59
- safe: boolean;
60
- violations: OutputViolation[];
61
- sanitizedContent?: string;
62
- }
63
-
64
- /** 输出违规 */
65
- export interface OutputViolation {
66
- type: 'sensitive_info' | 'quality' | 'knowledge_missing';
67
- severity: 'low' | 'medium' | 'high';
68
- description: string;
69
- location?: { line: number; column: number };
70
- matchedPattern?: string;
71
- }
72
-
73
- /** 输出护栏配置 */
74
- export interface OutputGuardrailConfig {
75
- /** 敏感信息模式(API keys, passwords, etc.) */
76
- sensitivePatterns?: RegExp[];
77
- /** 代码质量最低分数 */
78
- minQualityScore?: number;
79
- /** 是否检查知识引用完整性 */
80
- checkKnowledgeRefs?: boolean;
81
- }
82
-
83
- // ── Tool Guardrail ───────────────────────────────────────
84
-
85
- /** 工具检查结果 */
86
- export interface ToolCheckResult {
87
- allowed: boolean;
88
- violations: ToolViolation[];
89
- }
90
-
91
- /** 工具违规 */
92
- export interface ToolViolation {
93
- type: 'blacklist' | 'sandbox' | 'rate_limit';
94
- severity: 'low' | 'medium' | 'high';
95
- description: string;
96
- toolName?: string;
97
- }
98
-
99
- /** 工具护栏配置 */
100
- export interface ToolGuardrailConfig {
101
- /** 命令黑名单 */
102
- blacklistedCommands?: string[];
103
- /** 每分钟最大调用次数 */
104
- rateLimit?: number;
105
- /** 各工具的 sandbox 级别要求 */
106
- toolSandboxLevels?: Record<string, SandboxLevel>;
107
- }
108
-
109
- /** 速率限制状态 */
110
- export interface RateLimitState {
111
- count: number;
112
- windowStart: number;
113
- }
114
-
115
- // ── Aggregate ────────────────────────────────────────────
116
-
117
- /** 安全检查汇总 */
118
- export interface SafetyCheckResult {
119
- safe: boolean;
120
- inputCheck: InputCheckResult;
121
- outputCheck: OutputSafetyCheckResult;
122
- toolCheck: ToolCheckResult;
123
- timestamp: string;
124
- }