@danceiny/gotry 0.0.1-rc.21 → 0.0.1-rc.24

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 (327) hide show
  1. package/README.md +102 -194
  2. package/README.zh-CN.md +115 -210
  3. package/bin/gotry-backend.js +53 -0
  4. package/bin/gotry-bootstrap.js +466 -35
  5. package/bin/gotry-inner.js +255 -36
  6. package/bin/gotry-process-liveness.d.ts +27 -0
  7. package/bin/gotry-process-liveness.js +97 -0
  8. package/bin/gotry-runtime-resolution.d.ts +1 -1
  9. package/bin/gotry-runtime-resolution.js +2 -2
  10. package/client/client.js +174 -0
  11. package/cordis.gotry-patch.yml +70 -93
  12. package/dist/capabilities/agent-reach-deep.js +45 -122
  13. package/dist/capabilities/agent-reach.js +63 -148
  14. package/dist/capabilities/anything.js +89 -75
  15. package/dist/capabilities/artifacts.js +123 -104
  16. package/dist/capabilities/channel-health.js +65 -45
  17. package/dist/capabilities/channel-registry.js +108 -87
  18. package/dist/capabilities/doctor.js +357 -235
  19. package/dist/capabilities/effect.js +223 -309
  20. package/dist/capabilities/fact-log.js +28 -14
  21. package/dist/capabilities/flyai.js +245 -156
  22. package/dist/capabilities/ground-transfer.js +509 -0
  23. package/dist/capabilities/hbcli.js +144 -148
  24. package/dist/capabilities/hotelbyte-transaction.js +194 -0
  25. package/dist/capabilities/incident-log.js +131 -61
  26. package/dist/capabilities/model-override.js +30 -10
  27. package/dist/capabilities/opensky.js +54 -43
  28. package/dist/capabilities/resilience.js +50 -32
  29. package/dist/capabilities/session/action-cache.js +50 -44
  30. package/dist/capabilities/session/adapters/ctrip-flight.js +119 -62
  31. package/dist/capabilities/session/adapters/ctrip-hotel.js +125 -110
  32. package/dist/capabilities/session/adapters/dida-portal.js +206 -0
  33. package/dist/capabilities/session/adapters/meituan-local.js +35 -54
  34. package/dist/capabilities/session/adapters/rail-12306.js +169 -112
  35. package/dist/capabilities/session/benchmark.js +85 -149
  36. package/dist/capabilities/session/extension-bridge.js +320 -259
  37. package/dist/capabilities/session/extension-channel.js +57 -76
  38. package/dist/capabilities/session/extension-distribution.js +115 -119
  39. package/dist/capabilities/session/extract.js +31 -21
  40. package/dist/capabilities/session/golden-score.js +55 -33
  41. package/dist/capabilities/session/health-watch.js +146 -154
  42. package/dist/capabilities/session/read-guard.js +55 -47
  43. package/dist/capabilities/session/static-flight-golden.js +49 -36
  44. package/dist/capabilities/session/transport.js +54 -61
  45. package/dist/capabilities/session/wizard.js +63 -93
  46. package/dist/capabilities/session-consent.js +143 -47
  47. package/dist/capabilities/session-login.js +95 -126
  48. package/dist/capabilities/session-search.js +391 -253
  49. package/dist/capabilities/sponsor-plugin.js +341 -0
  50. package/dist/capabilities/visa-policy.js +87 -73
  51. package/dist/capabilities/weather.js +163 -235
  52. package/dist/scripts/action-cache-tests.js +24 -59
  53. package/dist/scripts/agent-planning-turn-deadline-e2e.js +91 -142
  54. package/dist/scripts/agent-planning-turn-deadline-tests.js +80 -176
  55. package/dist/scripts/agent-reach-deep-tests.js +30 -40
  56. package/dist/scripts/agent-reach-tests.js +26 -18
  57. package/dist/scripts/agent-reach-wrapper-tests.js +46 -50
  58. package/dist/scripts/anything-tests.js +58 -62
  59. package/dist/scripts/artifact-client-contract-tests.js +69 -0
  60. package/dist/scripts/artifacts-capability-tests.js +235 -0
  61. package/dist/scripts/async-collect.js +26 -15
  62. package/dist/scripts/benchmark-environment-bridge-e2e.js +287 -684
  63. package/dist/scripts/benchmark-environment-bridge-tests.js +818 -3123
  64. package/dist/scripts/booking-copilot-availability-ledger-binding-tests.js +66 -279
  65. package/dist/scripts/booking-copilot-availability-policy-tests.js +153 -1257
  66. package/dist/scripts/booking-copilot-bin-proof-tests.js +22 -45
  67. package/dist/scripts/booking-copilot-crossrepo-fixture-server.js +64 -76
  68. package/dist/scripts/booking-copilot-dsh-core-proof-tests.js +69 -199
  69. package/dist/scripts/booking-copilot-dsh-planner-proof-tests.js +393 -759
  70. package/dist/scripts/booking-copilot-dsh-plugin-proof-tests.js +33 -75
  71. package/dist/scripts/booking-copilot-event-sequence-concurrency-proof-tests.js +60 -110
  72. package/dist/scripts/booking-copilot-gap-code-contract-proof-tests.js +28 -66
  73. package/dist/scripts/booking-copilot-operation-ledger-concurrency-proof-tests.js +72 -271
  74. package/dist/scripts/booking-copilot-receipt-ledger-concurrency-proof-tests.js +73 -275
  75. package/dist/scripts/booking-copilot-runtime-proof-tests.js +784 -3398
  76. package/dist/scripts/booking-copilot-server-proof-tests.js +63 -168
  77. package/dist/scripts/booking-copilot-startup-proof-tests.js +38 -124
  78. package/dist/scripts/booking-executor-tests.js +80 -0
  79. package/dist/scripts/booking-recovery-chain-tests.js +182 -0
  80. package/dist/scripts/booking-saga-tests.js +61 -123
  81. package/dist/scripts/booking-surface-contract-proof-tests.js +152 -1167
  82. package/dist/scripts/bootstrap-tests.js +1383 -128
  83. package/dist/scripts/build-changelog.js +89 -130
  84. package/dist/scripts/build-metrics-report.js +135 -143
  85. package/dist/scripts/changelog-tests.js +35 -82
  86. package/dist/scripts/channel-probe-tests.js +44 -116
  87. package/dist/scripts/channel-probe.js +84 -167
  88. package/dist/scripts/channel-registry-tests.js +97 -185
  89. package/dist/scripts/companion-tests.js +25 -70
  90. package/dist/scripts/dida-cookie-probe.js +87 -0
  91. package/dist/scripts/dida-sw-probe.js +114 -0
  92. package/dist/scripts/diff-test.js +17 -12
  93. package/dist/scripts/doctor-tests.js +428 -120
  94. package/dist/scripts/dsh-artifact-e2e.js +150 -0
  95. package/dist/scripts/dsh-artifact-web-e2e.js +443 -0
  96. package/dist/scripts/dsh-http-proxy-sse-proof.js +90 -0
  97. package/dist/scripts/dsh-runtime-closure-tests.js +110 -153
  98. package/dist/scripts/dsh-runtime-closure.js +75 -63
  99. package/dist/scripts/dsh-session-v3-migration-proof.js +106 -0
  100. package/dist/scripts/dsh-subprocess-local-proof.js +149 -0
  101. package/dist/scripts/dsh-target-closure-proof.js +178 -0
  102. package/dist/scripts/effect-tests.js +165 -552
  103. package/dist/scripts/engine-run.js +1 -3
  104. package/dist/scripts/engine-tests.js +16 -10
  105. package/dist/scripts/evaluation-cadence-tests.js +69 -242
  106. package/dist/scripts/evaluation-contract-tests.js +193 -499
  107. package/dist/scripts/extension-distribution-cli.js +19 -15
  108. package/dist/scripts/extension-distribution-tests.js +98 -211
  109. package/dist/scripts/extension-tests.js +366 -389
  110. package/dist/scripts/fact-gate-tests.js +725 -263
  111. package/dist/scripts/fixtures/extension-bridge-unref-child.mjs +21 -0
  112. package/dist/scripts/flight-hotel-anchor-363-registered-e2e.js +169 -0
  113. package/dist/scripts/flight-malformed-tests.js +159 -0
  114. package/dist/scripts/flyai-tests.js +325 -68
  115. package/dist/scripts/g5-guard-tests.js +131 -0
  116. package/dist/scripts/g5-guard.js +189 -0
  117. package/dist/scripts/gen-lunar-anchors.js +106 -0
  118. package/dist/scripts/gotry-backend-kernel-tests.js +79 -0
  119. package/dist/scripts/ground-transfer-tests.js +956 -0
  120. package/dist/scripts/hbcli-e2e-tests.js +71 -90
  121. package/dist/scripts/hbcli-live-optin-tests.js +96 -0
  122. package/dist/scripts/hbcli-release-contract-tests.js +122 -0
  123. package/dist/scripts/hbcli-tests.js +41 -115
  124. package/dist/scripts/health-watch-cli.js +42 -28
  125. package/dist/scripts/hotel-date-gate-tests.js +496 -0
  126. package/dist/scripts/hotelbyte-reconcile-tests.js +141 -0
  127. package/dist/scripts/hotelbyte-spawn-e2e-tests.js +373 -0
  128. package/dist/scripts/i18n-tests.js +16 -9
  129. package/dist/scripts/incident-tests.js +281 -87
  130. package/dist/scripts/issue-194-job-id-guard-tests.js +106 -0
  131. package/dist/scripts/issue-233-cancel-refund-commission-tests.js +392 -0
  132. package/dist/scripts/issue-271-liveness-tests.js +547 -0
  133. package/dist/scripts/issue-289-model-retry-real-tests.js +296 -0
  134. package/dist/scripts/issue-289-model-retry-tests.js +441 -0
  135. package/dist/scripts/issue-289-web-retry-e2e.js +630 -0
  136. package/dist/scripts/issue-338-home-city-e2e.js +577 -0
  137. package/dist/scripts/issue343-real-entry-e2e.js +160 -0
  138. package/dist/scripts/journey-tests.js +22 -27
  139. package/dist/scripts/kernel-manifest-evidence.js +109 -0
  140. package/dist/scripts/kernel-manifest-gate.js +126 -0
  141. package/dist/scripts/kernel-manifest-tests.js +212 -0
  142. package/dist/scripts/kernel-manifest-trace-entry.js +7 -0
  143. package/dist/scripts/kernel-manifest-trace-hook.mjs +28 -0
  144. package/dist/scripts/kernel-manifest.js +369 -0
  145. package/dist/scripts/ledger-repair-apply-tests.js +316 -0
  146. package/dist/scripts/ledger-repair-plan-tests.js +375 -0
  147. package/dist/scripts/ledger-tests.js +325 -340
  148. package/dist/scripts/ledger-workflow-crash.js +10 -20
  149. package/dist/scripts/map-tools-vendor-package-proof.js +206 -134
  150. package/dist/scripts/memory-capture-tests.js +242 -61
  151. package/dist/scripts/memory-decay-tests.js +18 -52
  152. package/dist/scripts/memory-lifecycle-tests.js +657 -0
  153. package/dist/scripts/memory-lifecycle.js +196 -0
  154. package/dist/scripts/memory-metrics.js +24 -12
  155. package/dist/scripts/memory-value-report.js +474 -222
  156. package/dist/scripts/memory-value-tests.js +328 -0
  157. package/dist/scripts/metrics-report-tests.js +62 -199
  158. package/dist/scripts/model-override-e2e.js +66 -105
  159. package/dist/scripts/nightly-evidence-tests.js +64 -89
  160. package/dist/scripts/nightly-evidence.js +108 -111
  161. package/dist/scripts/nudge-digest.js +39 -28
  162. package/dist/scripts/onboarding-tests.js +63 -88
  163. package/dist/scripts/opensky-check.js +18 -13
  164. package/dist/scripts/opensky-tests.js +28 -28
  165. package/dist/scripts/persona-surface-guard-tests.js +37 -12
  166. package/dist/scripts/pnpm-dsh-closure-proof.js +2 -5
  167. package/dist/scripts/policy-anchor-359-registered-e2e.js +214 -0
  168. package/dist/scripts/price-drift-tests.js +126 -227
  169. package/dist/scripts/price-drift-watch.js +209 -253
  170. package/dist/scripts/probe-poi-tests.js +52 -45
  171. package/dist/scripts/product-metrics.js +187 -273
  172. package/dist/scripts/publish-preverify.js +47 -41
  173. package/dist/scripts/realtime-pricing-tests.js +37 -76
  174. package/dist/scripts/replay-async.js +20 -18
  175. package/dist/scripts/replay-real.js +16 -18
  176. package/dist/scripts/replay.js +52 -82
  177. package/dist/scripts/session-attach-diagnose.js +16 -12
  178. package/dist/scripts/session-attach-poc.js +34 -37
  179. package/dist/scripts/session-benchmark.js +66 -147
  180. package/dist/scripts/session-dida-live-e2e.js +151 -0
  181. package/dist/scripts/session-extract-tests.js +14 -35
  182. package/dist/scripts/session-login.js +14 -13
  183. package/dist/scripts/session-tests.js +545 -583
  184. package/dist/scripts/sf-live-benchmark.js +92 -110
  185. package/dist/scripts/sf-live-cli-tests.js +2 -9
  186. package/dist/scripts/sf-soft-score-tests.js +19 -60
  187. package/dist/scripts/sf-summary-tests.js +149 -0
  188. package/dist/scripts/sf-summary.js +466 -84
  189. package/dist/scripts/skeleton-check.js +16 -14
  190. package/dist/scripts/skeleton-integration-test.js +11 -10
  191. package/dist/scripts/skills-contract-tests.js +51 -46
  192. package/dist/scripts/smoke-session-gate-tests.js +12 -20
  193. package/dist/scripts/smoke.js +541 -422
  194. package/dist/scripts/sponsor-reuse-tests.js +262 -0
  195. package/dist/scripts/state-cli-tests.js +226 -101
  196. package/dist/scripts/state-cli.js +549 -212
  197. package/dist/scripts/static-golden-tests.js +46 -180
  198. package/dist/scripts/time-eval-tests.js +210 -192
  199. package/dist/scripts/travel-timeline-tests.js +29 -93
  200. package/dist/scripts/turn-handoff-collect-tests.js +46 -93
  201. package/dist/scripts/turn-handoff-collect.js +71 -99
  202. package/dist/scripts/turn-policy-tests.js +28 -29
  203. package/dist/scripts/typed-contract-canary.js +77 -162
  204. package/dist/scripts/tz-resolver-sanity.js +50 -0
  205. package/dist/scripts/tz-resolver-tests.js +584 -0
  206. package/dist/scripts/unified-tests.js +23 -19
  207. package/dist/scripts/visa-policy-tests.js +31 -44
  208. package/dist/scripts/weather-tests.js +121 -543
  209. package/dist/scripts/wish-channel-gate-tests.js +29 -86
  210. package/dist/scripts/write-gate-tests.js +522 -0
  211. package/dist/scripts/z3-lifecycle-fault-tests.js +188 -0
  212. package/dist/scripts/z3-lifecycle-tests.js +170 -0
  213. package/dist/scripts/z3-race-repeat-tests.js +39 -0
  214. package/dist/scripts/z3-race-tests.js +18 -12
  215. package/dist/src/artifact-gate.js +652 -221
  216. package/dist/src/backend/kernel.js +91 -0
  217. package/dist/src/backend/modules/booking-copilot.js +55 -0
  218. package/dist/src/backend/modules/booking-executor.js +124 -0
  219. package/dist/src/backend/modules/session-search.js +194 -0
  220. package/dist/src/benchmark-agent-conformance.js +296 -262
  221. package/dist/src/benchmark-environment-bridge.js +298 -413
  222. package/dist/src/benchmark-headless-child-diagnostics.js +93 -100
  223. package/dist/src/benchmark-tool-isolation.js +94 -67
  224. package/dist/src/bookable-facts.js +299 -174
  225. package/dist/src/booking-saga.js +64 -120
  226. package/dist/src/booking-surface/availability-policy.js +443 -576
  227. package/dist/src/booking-surface/cancel-refund-commission.js +379 -0
  228. package/dist/src/booking-surface/contracts.js +24 -80
  229. package/dist/src/booking-surface/dsh-planner.js +510 -281
  230. package/dist/src/booking-surface/dsh-plugin.js +17 -1
  231. package/dist/src/booking-surface/error-codes.js +82 -10
  232. package/dist/src/booking-surface/index.js +0 -3
  233. package/dist/src/booking-surface/profile.js +10 -54
  234. package/dist/src/booking-surface/recovery-chain.js +367 -0
  235. package/dist/src/booking-surface/runtime.js +765 -891
  236. package/dist/src/booking-surface/server.js +200 -251
  237. package/dist/src/booking-surface/startup.js +58 -54
  238. package/dist/src/booking-surface/validation.js +150 -218
  239. package/dist/src/bridge.js +20 -13
  240. package/dist/src/companions.js +48 -74
  241. package/dist/src/contracts.js +21 -43
  242. package/dist/src/dsh-llm.js +82 -120
  243. package/dist/src/engine.js +253 -239
  244. package/dist/src/evaluation-cadence.js +72 -209
  245. package/dist/src/evaluation-contracts.js +220 -843
  246. package/dist/src/flight-pack-adapter.js +54 -0
  247. package/dist/src/flight-pack-contract.js +2 -0
  248. package/dist/src/gotry-backend.js +70 -0
  249. package/dist/src/hotel-date-gate.js +118 -0
  250. package/dist/src/i18n.js +18 -7
  251. package/dist/src/index.js +1425 -1629
  252. package/dist/src/journey.js +132 -81
  253. package/dist/src/ledger-repair-apply.js +538 -0
  254. package/dist/src/ledger-repair-plan.js +463 -0
  255. package/dist/src/loop.js +257 -166
  256. package/dist/src/memory-capture.js +180 -32
  257. package/dist/src/memory-decay.js +30 -14
  258. package/dist/src/memory-lifecycle.js +1310 -0
  259. package/dist/src/memory-utility.js +27 -40
  260. package/dist/src/mock-llm.js +36 -61
  261. package/dist/src/model.js +81 -36
  262. package/dist/src/realtime-pricing.js +63 -72
  263. package/dist/src/slot-spec.js +84 -106
  264. package/dist/src/state-ledger.js +310 -471
  265. package/dist/src/subagent-job-id-guard.js +58 -0
  266. package/dist/src/time-anchor.js +231 -33
  267. package/dist/src/tool-packet.js +20 -8
  268. package/dist/src/travel-slots.js +58 -35
  269. package/dist/src/travel-timeline.js +44 -63
  270. package/dist/src/turn-deadline.js +121 -127
  271. package/dist/src/turn-policy.js +61 -89
  272. package/dist/src/tz-resolver.js +143 -0
  273. package/dist/src/unified.js +504 -381
  274. package/dist/src/wish-pool.js +39 -14
  275. package/dist/src/write-gate.js +495 -0
  276. package/dist/src/z3-shared.js +273 -7
  277. package/extension/README.md +4 -3
  278. package/extension/background.js +125 -14
  279. package/extension/content-bridge.js +23 -1
  280. package/extension/content-main.js +15 -4
  281. package/extension/manifest.json +16 -8
  282. package/package.json +251 -220
  283. package/ts/capabilities/agent-reach.ts +4 -4
  284. package/ts/capabilities/anything.ts +1 -1
  285. package/ts/capabilities/artifacts.ts +46 -9
  286. package/ts/capabilities/flyai.ts +156 -37
  287. package/ts/capabilities/hbcli.ts +1 -1
  288. package/ts/capabilities/incident-log.ts +88 -59
  289. package/ts/capabilities/session/adapters/ctrip-flight.ts +99 -36
  290. package/ts/capabilities/session/adapters/dida-portal.ts +250 -0
  291. package/ts/capabilities/session/adapters/rail-12306.ts +123 -24
  292. package/ts/capabilities/session/extension-bridge.ts +226 -101
  293. package/ts/capabilities/session/extension-channel.ts +22 -9
  294. package/ts/capabilities/session/wizard.ts +2 -2
  295. package/ts/capabilities/session-consent.ts +98 -14
  296. package/ts/capabilities/session-login.ts +1 -0
  297. package/ts/capabilities/session-search.ts +315 -31
  298. package/ts/dsh-runtime/vendor/README.md +8 -4
  299. package/ts/dsh-runtime/vendor/dsh-map-tools/README.en.md +7 -1
  300. package/ts/dsh-runtime/vendor/dsh-map-tools/README.md +5 -3
  301. package/ts/dsh-runtime/vendor/dsh-map-tools/lib/settings-ns.js +1 -1
  302. package/ts/dsh-runtime/vendor/dsh-map-tools/lib/types/settings-ns.d.ts +1 -1
  303. package/ts/dsh-runtime/vendor/dsh-map-tools/package.json +6 -6
  304. package/ts/package.json +236 -16
  305. package/ts/scripts/map-tools-vendor-package-proof.ts +152 -31
  306. package/ts/scripts/state-cli.ts +418 -51
  307. package/ts/src/artifact-gate.ts +561 -15
  308. package/ts/src/benchmark-environment-bridge.ts +37 -20
  309. package/ts/src/bookable-facts.ts +183 -7
  310. package/ts/src/booking-surface/cancel-refund-commission.ts +488 -0
  311. package/ts/src/booking-surface/dsh-planner.ts +240 -32
  312. package/ts/src/booking-surface/dsh-plugin.js +17 -1
  313. package/ts/src/booking-surface/error-codes.ts +75 -0
  314. package/ts/src/booking-surface/recovery-chain.ts +409 -0
  315. package/ts/src/booking-surface/server.ts +84 -51
  316. package/ts/src/booking-surface/validation.ts +2 -0
  317. package/ts/src/contracts.ts +12 -2
  318. package/ts/src/dsh-llm.ts +4 -8
  319. package/ts/src/index.ts +609 -90
  320. package/ts/src/loop.ts +116 -7
  321. package/ts/src/memory-capture.ts +227 -3
  322. package/ts/src/mock-llm.ts +4 -8
  323. package/ts/src/model.ts +88 -2
  324. package/ts/src/state-ledger.ts +47 -29
  325. package/ts/src/subagent-job-id-guard.ts +72 -0
  326. package/ts/src/time-anchor.ts +217 -13
  327. package/ts/src/unified.ts +316 -54
@@ -0,0 +1,328 @@
1
+ /**
2
+ * M4 memory value scorer contract tests(issue #223).
3
+ *
4
+ * Focus:
5
+ * - frozen M4 thresholds (N>=5, median reduction >=0.5) cannot be weakened
6
+ * - public synthetic fixture keeps correct calculations but never closes M4
7
+ * - observed_private is only a candidate without a human source-review attestation
8
+ * - manual attestation is bound to the current scoring payload digest
9
+ * - root/nested unknown fields and PII-like values are rejected without echoing attacker keys/values
10
+ * - all id refs use hmac-sha256:<64lowerhex>; chronology/waits/duplicates/P4/preference guards fail closed
11
+ */
12
+ import assert from 'node:assert/strict';
13
+ import { spawnSync } from 'node:child_process';
14
+ import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
15
+ import { join } from 'node:path';
16
+ import { tmpdir } from 'node:os';
17
+ import { memoryValueSummaryDigest, scoreMemoryValue } from './memory-value-report.js';
18
+ const fixture = JSON.parse(readFileSync('data/memory-value-fixture.json', 'utf8'));
19
+ const hmac = (n) => `hmac-sha256:${n.toString(16).padStart(64, '0')}`;
20
+ let passed = 0;
21
+ function ok(label) {
22
+ passed += 1;
23
+ console.log(` ok ${label}`);
24
+ }
25
+ function clone(value) {
26
+ return structuredClone(value);
27
+ }
28
+ function asText(value) {
29
+ return JSON.stringify(value);
30
+ }
31
+ function assertInvalid(value, label) {
32
+ const report = scoreMemoryValue(value);
33
+ assert.equal(report.contract_valid, false, label);
34
+ assert.equal(report.exit_ready, false, label);
35
+ }
36
+ function observedCopy() {
37
+ const payload = clone(fixture);
38
+ payload.evidence_kind = 'observed_private';
39
+ return payload;
40
+ }
41
+ function manualAttestation(payload) {
42
+ return {
43
+ schema_version: 'memory_value_source_review.v1',
44
+ state: 'manual_attested',
45
+ reviewed_at: '2026-09-08T00:00:00Z',
46
+ reviewer_ref: hmac(9001),
47
+ attestation_ref: hmac(9002),
48
+ private_source_digest_sha256: 'a'.repeat(64),
49
+ reviewed_summary_digest_sha256: memoryValueSummaryDigest(payload),
50
+ checks: {
51
+ raw_private_material_excluded: true,
52
+ paired_cohort_source_reviewed: true,
53
+ summary_matches_private_source: true,
54
+ no_synthetic_or_test_subjects: true,
55
+ },
56
+ };
57
+ }
58
+ function attest(payload) {
59
+ payload.source_review = manualAttestation(payload);
60
+ return payload;
61
+ }
62
+ function setPairDurations(payload, firstMs, returningMs) {
63
+ for (const [index, pair] of payload.pairs.entries()) {
64
+ const firstStartedAt = Date.UTC(2026, 8, 1 + index, 10, 0, 0, 0);
65
+ const returningStartedAt = Date.UTC(2026, 8, 8 + index, 10, 0, 0, 0);
66
+ pair.first.started_at = new Date(firstStartedAt).toISOString();
67
+ pair.first.completed_at = new Date(firstStartedAt + firstMs).toISOString();
68
+ pair.first.external_waits = [];
69
+ pair.returning.started_at = new Date(returningStartedAt).toISOString();
70
+ pair.returning.completed_at = new Date(returningStartedAt + returningMs).toISOString();
71
+ pair.returning.external_waits = [];
72
+ }
73
+ }
74
+ function runCli(input) {
75
+ const root = mkdtempSync(join(tmpdir(), 'gotry-memory-value-'));
76
+ const path = join(root, 'input.json');
77
+ writeFileSync(path, JSON.stringify(input), 'utf8');
78
+ const result = spawnSync('npx', ['tsx', 'scripts/memory-value-report.ts', path], {
79
+ cwd: process.cwd(),
80
+ encoding: 'utf8',
81
+ });
82
+ let report;
83
+ try {
84
+ report = JSON.parse(result.stdout);
85
+ }
86
+ finally {
87
+ rmSync(root, { recursive: true, force: true });
88
+ }
89
+ return { status: result.status, stdout: result.stdout, stderr: result.stderr, report };
90
+ }
91
+ function runMalformedCli(contents) {
92
+ const root = mkdtempSync(join(tmpdir(), 'privacy-sentinel-path-'));
93
+ const path = join(root, 'privacy-sentinel-input.json');
94
+ writeFileSync(path, contents, 'utf8');
95
+ const result = spawnSync('npx', ['tsx', 'scripts/memory-value-report.ts', path], {
96
+ cwd: process.cwd(),
97
+ encoding: 'utf8',
98
+ });
99
+ rmSync(root, { recursive: true, force: true });
100
+ return { status: result.status, stdout: result.stdout, stderr: result.stderr };
101
+ }
102
+ // ---- 1. synthetic fixture: metric-positive but business-ineligible ----
103
+ const synthetic = scoreMemoryValue(fixture);
104
+ assert.equal(synthetic.contract_valid, true);
105
+ assert.equal(synthetic.cohort.eligible_pair_count, 5);
106
+ assert.deepEqual(synthetic.cohort.first_active_seconds, { p50: 720, p75: 900 });
107
+ assert.deepEqual(synthetic.cohort.returning_active_seconds, { p50: 360, p75: 450 });
108
+ assert.deepEqual(synthetic.cohort.paired_reduction_ratio, { p50: 0.5, p75: 0.5 });
109
+ assert.equal(synthetic.cohort.minimum_pair_count_for_exit, 5);
110
+ assert.equal(synthetic.cohort.target_median_reduction_ratio, 0.5);
111
+ assert.equal(synthetic.cohort.sample_size_met, true);
112
+ assert.equal(synthetic.cohort.target_met, true);
113
+ assert.equal(synthetic.experience_reflux.baseline, 0.5);
114
+ assert.equal(synthetic.preference_assertions.traceable_ratio, 1);
115
+ assert.equal(synthetic.preference_assertions.hard_filter_violation_count, 0);
116
+ assert.equal(synthetic.p4.contract_met, true);
117
+ assert.equal(synthetic.source.source_review.contract_met, false);
118
+ assert.equal(synthetic.exit_evidence_eligible, false);
119
+ assert.equal(synthetic.exit_ready, false);
120
+ ok('synthetic fixture N=5/0.5 指标通过但不可作为 M4 Exit 证据');
121
+ // ---- 2. legal manual_attested observed-private positive path ----
122
+ const manualPositive = attest(observedCopy());
123
+ const manualPositiveReport = scoreMemoryValue(manualPositive);
124
+ assert.equal(manualPositiveReport.contract_valid, true);
125
+ assert.equal(manualPositiveReport.source.source_review.contract_met, true);
126
+ assert.equal(manualPositiveReport.source.source_review.provenance_verified_by_scorer, false);
127
+ assert.equal(manualPositiveReport.source.source_review.reviewed_summary_digest_sha256, memoryValueSummaryDigest(manualPositive));
128
+ assert.equal(manualPositiveReport.source.source_review.current_summary_digest_sha256, memoryValueSummaryDigest(manualPositive));
129
+ assert.equal(manualPositiveReport.source.source_review.reviewer_ref, hmac(9001));
130
+ assert.equal(manualPositiveReport.source.source_review.attestation_ref, hmac(9002));
131
+ assert.equal(manualPositiveReport.experience_reflux.real_evidence, true);
132
+ assert.equal(manualPositiveReport.exit_evidence_eligible, true);
133
+ assert.equal(manualPositiveReport.exit_ready, true);
134
+ ok('合法 manual_attested observed_private 在数字门槛与人工声明齐备时可达 ready');
135
+ // ---- 3. observed_private without provenance remains candidate despite passing metrics ----
136
+ const candidate = observedCopy();
137
+ candidate.source_review = {
138
+ schema_version: 'memory_value_source_review.v1',
139
+ state: 'candidate',
140
+ reviewed_at: null,
141
+ reviewer_ref: null,
142
+ attestation_ref: null,
143
+ private_source_digest_sha256: null,
144
+ reviewed_summary_digest_sha256: null,
145
+ checks: {
146
+ raw_private_material_excluded: true,
147
+ paired_cohort_source_reviewed: true,
148
+ summary_matches_private_source: true,
149
+ no_synthetic_or_test_subjects: false,
150
+ },
151
+ };
152
+ const candidateReport = scoreMemoryValue(candidate);
153
+ assert.equal(candidateReport.contract_valid, true);
154
+ assert.equal(candidateReport.cohort.sample_size_met, true);
155
+ assert.equal(candidateReport.cohort.target_met, true);
156
+ assert.equal(candidateReport.source.evidence_kind, 'observed_private');
157
+ assert.equal(candidateReport.source.source_review.required_for_exit, true);
158
+ assert.equal(candidateReport.source.source_review.contract_met, false);
159
+ assert.equal(candidateReport.source.source_review.provenance_verified_by_scorer, false);
160
+ assert.equal(candidateReport.experience_reflux.real_evidence, false);
161
+ assert.equal(candidateReport.exit_evidence_eligible, false);
162
+ assert.equal(candidateReport.exit_ready, false);
163
+ ok('observed_private 缺人工 source-review attestation 时只可作为 candidate');
164
+ // ---- 4. attestation digest binds the current scoring payload ----
165
+ const staleAttestation = attest(observedCopy());
166
+ staleAttestation.pairs[0].returning.completed_at = '2026-08-08T10:07:00Z';
167
+ const staleAttestationReport = scoreMemoryValue(staleAttestation);
168
+ assert.equal(staleAttestationReport.contract_valid, false);
169
+ assert.equal(staleAttestationReport.exit_ready, false);
170
+ assert.match(asText(staleAttestationReport.errors), /reviewed_summary_digest_sha256/);
171
+ assert.notEqual(staleAttestation.source_review.reviewed_summary_digest_sha256, memoryValueSummaryDigest(staleAttestation));
172
+ ok('manual attestation 的 reviewed_summary_digest 绑定本次评分 payload,改 summary 后旧 attestation 失效');
173
+ // ---- 5. raw ratio threshold: report may round to 0.5, comparison must not ----
174
+ const exactHalf = observedCopy();
175
+ setPairDurations(exactHalf, 2_000_000, 1_000_000);
176
+ attest(exactHalf);
177
+ const exactHalfReport = scoreMemoryValue(exactHalf);
178
+ assert.equal(exactHalfReport.contract_valid, true);
179
+ assert.equal(exactHalfReport.cohort.paired_reduction_ratio.p50, 0.5);
180
+ assert.equal(exactHalfReport.cohort.target_met, true);
181
+ assert.equal(exactHalfReport.exit_ready, true);
182
+ const justUnderHalf = observedCopy();
183
+ setPairDurations(justUnderHalf, 2_000_001, 1_000_001);
184
+ attest(justUnderHalf);
185
+ const justUnderHalfReport = scoreMemoryValue(justUnderHalf);
186
+ assert.equal(justUnderHalfReport.contract_valid, true);
187
+ assert.equal(justUnderHalfReport.cohort.paired_reduction_ratio.p50, 0.5);
188
+ assert.equal(justUnderHalfReport.cohort.target_met, false);
189
+ assert.equal(justUnderHalfReport.exit_ready, false);
190
+ ok('raw ratio 比较:exact 50% 通过,49.999975% 即使展示 round 到 50% 仍未达标');
191
+ // ---- 6. original exploit: self-declared observed, N=1, target=0, email ids, undeclared fields ----
192
+ const exploit = clone(fixture);
193
+ exploit.evidence_kind = 'observed_private';
194
+ delete exploit.source_review;
195
+ exploit.attacker_note = 'self-declared observed';
196
+ exploit.user_email = 'victim@example.com';
197
+ exploit.measurement_policy.minimum_pair_count_for_exit = 1;
198
+ exploit.measurement_policy.target_median_reduction_ratio = 0;
199
+ exploit.pairs = [exploit.pairs[0]];
200
+ exploit.pairs[0].subject_ref = 'victim@example.com';
201
+ exploit.pairs[0].returning.raw_transcript = 'PII leak field';
202
+ const exploitReport = scoreMemoryValue(exploit);
203
+ assert.equal(exploitReport.contract_valid, false);
204
+ assert.equal(exploitReport.exit_ready, false);
205
+ const exploitText = asText(exploitReport);
206
+ assert.doesNotMatch(exploitText, /victim@example\.com/);
207
+ assert.doesNotMatch(exploitText, /attacker_note/);
208
+ assert.doesNotMatch(exploitText, /raw_transcript/);
209
+ assert.doesNotMatch(exploitText, /PII leak field/);
210
+ ok('原反例红:observed 自报+降阈+明文邮箱+未知字段 fail-closed 且不回显攻击者 key/value');
211
+ // ---- 7. frozen policy and HMAC id format ----
212
+ const relaxedPolicy = clone(fixture);
213
+ relaxedPolicy.measurement_policy.minimum_pair_count_for_exit = 1;
214
+ relaxedPolicy.measurement_policy.target_median_reduction_ratio = 0.49;
215
+ assertInvalid(relaxedPolicy, 'weakened M4 threshold must be invalid');
216
+ const nonHmacIds = clone(fixture);
217
+ nonHmacIds.pairs[0].pair_id = 'pair-a';
218
+ nonHmacIds.pairs[0].subject_ref = 'user@example.com';
219
+ nonHmacIds.pairs[0].first.flow_id = 'flow-a-first';
220
+ nonHmacIds.experience_reflux_events[0].experience_id = 'experience-a';
221
+ nonHmacIds.experience_reflux_events[0].evidence_ref = 'fixture:evidence';
222
+ nonHmacIds.preference_assertions[0].assertion_id = 'preference-a';
223
+ nonHmacIds.preference_assertions[0].evidence_ref = 'raw evidence ref';
224
+ assertInvalid(nonHmacIds, 'all ids/refs must be hmac-sha256 pseudonyms');
225
+ ok('M4 阈值冻结且 subject/flow/pair/experience/assertion/evidence ref 均要求 HMAC');
226
+ // ---- 8. strict timestamps, first/return chronology, and wait interval guard ----
227
+ const invalidDate = clone(fixture);
228
+ invalidDate.pairs[0].first.started_at = '2026-02-30T10:00:00Z';
229
+ assertInvalid(invalidDate, 'invalid calendar date must be rejected');
230
+ const nonIsoDate = clone(fixture);
231
+ nonIsoDate.pairs[0].first.started_at = '2026-08-01 10:00:00';
232
+ assertInvalid(nonIsoDate, 'non-ISO timestamp must be rejected');
233
+ const reversedReturn = clone(fixture);
234
+ reversedReturn.pairs[0].returning.started_at = '2026-07-31T10:00:00Z';
235
+ reversedReturn.pairs[0].returning.completed_at = '2026-07-31T10:08:00Z';
236
+ reversedReturn.pairs[0].returning.external_waits = [];
237
+ assertInvalid(reversedReturn, 'returning flow must follow first flow');
238
+ const outsideWait = clone(fixture);
239
+ outsideWait.pairs[0].returning.external_waits[0].started_at = '2026-08-08T09:59:00Z';
240
+ assertInvalid(outsideWait, 'external wait must stay inside flow');
241
+ const overlappingWait = clone(fixture);
242
+ overlappingWait.pairs[0].returning.external_waits.push({
243
+ code: 'tool_latency',
244
+ started_at: '2026-08-08T10:05:00Z',
245
+ completed_at: '2026-08-08T10:07:00Z',
246
+ });
247
+ assertInvalid(overlappingWait, 'external waits must not overlap');
248
+ ok('严格 ISO 日期、首返时序、等待区间与不重叠守卫');
249
+ // ---- 9. duplicate pair/subject/flow and reflux/assertion pairing; preference evidence_ref may be reused ----
250
+ const duplicatePair = clone(fixture);
251
+ duplicatePair.pairs[1].pair_id = duplicatePair.pairs[0].pair_id;
252
+ assertInvalid(duplicatePair, 'pair ids must be unique');
253
+ const duplicateSubject = clone(fixture);
254
+ duplicateSubject.pairs[1].subject_ref = duplicateSubject.pairs[0].subject_ref;
255
+ assertInvalid(duplicateSubject, 'subjects must be unique');
256
+ const duplicateFlow = clone(fixture);
257
+ duplicateFlow.pairs[1].first.flow_id = duplicateFlow.pairs[0].first.flow_id;
258
+ assertInvalid(duplicateFlow, 'flow ids must be globally unique');
259
+ const duplicateReflux = clone(fixture);
260
+ duplicateReflux.experience_reflux_events.push({
261
+ experience_id: duplicateReflux.experience_reflux_events[0].experience_id,
262
+ kind: duplicateReflux.experience_reflux_events[0].kind,
263
+ evidence_ref: hmac(604),
264
+ });
265
+ assertInvalid(duplicateReflux, 'same experience/kind reflux event must not duplicate');
266
+ const unpairedVerified = clone(fixture);
267
+ unpairedVerified.experience_reflux_events.push({ experience_id: hmac(599), kind: 'verified_outcome', evidence_ref: hmac(605) });
268
+ assertInvalid(unpairedVerified, 'verified outcome must have recalled event');
269
+ const duplicateAssertion = clone(fixture);
270
+ duplicateAssertion.preference_assertions[1].assertion_id = duplicateAssertion.preference_assertions[0].assertion_id;
271
+ assertInvalid(duplicateAssertion, 'assertion ids must be unique');
272
+ const sharedPreferenceEvidence = clone(fixture);
273
+ sharedPreferenceEvidence.preference_assertions[1].evidence_ref = sharedPreferenceEvidence.preference_assertions[0].evidence_ref;
274
+ const sharedPreferenceEvidenceReport = scoreMemoryValue(sharedPreferenceEvidence);
275
+ assert.equal(sharedPreferenceEvidenceReport.contract_valid, true);
276
+ assert.equal(sharedPreferenceEvidenceReport.preference_assertions.contract_met, true);
277
+ ok('重复 pair/subject/flow、回流重复/配对、断言重复 fail-closed;preference evidence_ref 可合法复用');
278
+ // ---- 10. explicit hard_filter bool, consumer enum, and P4 bool/state ----
279
+ const hardFilter = clone(fixture);
280
+ hardFilter.preference_assertions[0].hard_filter = true;
281
+ const hardFilterReport = scoreMemoryValue(hardFilter);
282
+ assert.equal(hardFilterReport.contract_valid, true);
283
+ assert.equal(hardFilterReport.preference_assertions.hard_filter_violation_count, 1);
284
+ assert.equal(hardFilterReport.preference_assertions.contract_met, false);
285
+ assert.equal(hardFilterReport.exit_ready, false);
286
+ const hardFilterConsumer = clone(fixture);
287
+ hardFilterConsumer.preference_assertions[0].consumer = 'hard_filter';
288
+ const hardFilterConsumerReport = scoreMemoryValue(hardFilterConsumer);
289
+ assert.equal(hardFilterConsumerReport.contract_valid, true);
290
+ assert.equal(hardFilterConsumerReport.preference_assertions.hard_filter_violation_count, 1);
291
+ assert.equal(hardFilterConsumerReport.preference_assertions.contract_met, false);
292
+ const missingHardFilterBool = clone(fixture);
293
+ missingHardFilterBool.preference_assertions[0].hard_filter = 'false';
294
+ assertInvalid(missingHardFilterBool, 'hard_filter must be explicit boolean');
295
+ const badConsumer = clone(fixture);
296
+ badConsumer.preference_assertions[0].consumer = 'prompt';
297
+ assertInvalid(badConsumer, 'consumer must be declared enum');
298
+ const earlyP4 = clone(fixture);
299
+ earlyP4.p4.state = 'open';
300
+ const earlyP4Report = scoreMemoryValue(earlyP4);
301
+ assert.equal(earlyP4Report.contract_valid, true);
302
+ assert.equal(earlyP4Report.p4.contract_met, false);
303
+ assert.equal(earlyP4Report.exit_ready, false);
304
+ const badP4Bool = clone(fixture);
305
+ badP4Bool.p4.triggers.real_usage = 'false';
306
+ assertInvalid(badP4Bool, 'P4 triggers must be boolean');
307
+ const badP4State = clone(fixture);
308
+ badP4State.p4.state = 'paused';
309
+ assertInvalid(badP4State, 'P4 state must be declared enum');
310
+ ok('偏好硬过滤、consumer enum、显式 bool 与 P4 状态/触发守卫');
311
+ // ---- 11. CLI E2E: isolated JSON input -> report + exit code; malformed JSON never echoes private text/path ----
312
+ const cliSynthetic = runCli(fixture);
313
+ assert.equal(cliSynthetic.status, 0);
314
+ assert.equal(cliSynthetic.report.contract_valid, true);
315
+ assert.equal(cliSynthetic.report.exit_ready, false);
316
+ const cliExploit = runCli(exploit);
317
+ assert.equal(cliExploit.status, 2);
318
+ assert.equal(cliExploit.report.contract_valid, false);
319
+ assert.equal(cliExploit.report.exit_ready, false);
320
+ const cliExploitText = asText(cliExploit.report);
321
+ assert.doesNotMatch(cliExploitText, /victim@example\.com|attacker_note|raw_transcript|PII leak field/);
322
+ const malformed = runMalformedCli('{"schema":"privacy-sentinel-value", "unterminated": ');
323
+ assert.equal(malformed.status, 2);
324
+ assert.equal(malformed.stdout, '');
325
+ assert.match(malformed.stderr, /MEMORY_VALUE_INPUT_INVALID/);
326
+ assert.doesNotMatch(`${malformed.stdout}\n${malformed.stderr}`, /privacy-sentinel/);
327
+ ok('CLI E2E:隔离 synthetic/恶意/畸形 JSON 输入产生确定 exit 且不回显私有值或路径');
328
+ console.log(`memory-value tests: ${passed} 组断言全绿(严格 schema/source-review digest/raw ratio/PII fail-closed)`);
@@ -1,11 +1,18 @@
1
+ /**
2
+ * #138 指标面板第一切片测试(全离线,隔离 mkdtemp stateRoot;绝不触真实状态目录):
3
+ * 聚合正确性(去重/blocked 率/窗口过滤/百分位)、坏行容忍、空根成型、只读纪律
4
+ * (collect 前后目录条目集合不变)、--out 落盘形态。
5
+ */
1
6
  import { strict as assert } from 'node:assert';
2
7
  import { mkdtempSync, rmSync, mkdirSync, writeFileSync, readdirSync, readFileSync, existsSync } from 'node:fs';
3
8
  import { tmpdir } from 'node:os';
4
9
  import { join } from 'node:path';
5
10
  import { aggregateFacts, aggregateChannels, aggregateIncidents, aggregateLatency, collectMetrics, renderMetricsReport, main } from './build-metrics-report.js';
6
- const NOW = new Date('2026-09-05T12:00:00Z');
11
+ // fixture 时间戳必须相对真实时钟:聚合器(main)按真实 now 取 --days 窗口,
12
+ // 硬编码绝对日期会随日历自然老化出窗(2026-09-11 起 iso(1) 跌出 7 天窗,§6 假红)。
7
13
  const DAY = 86_400_000;
8
- const iso = (daysAgo)=>new Date(NOW.getTime() - daysAgo * DAY).toISOString();
14
+ const NOW = new Date(Date.now() - 1 * DAY);
15
+ const iso = (daysAgo) => new Date(NOW.getTime() - daysAgo * DAY).toISOString();
9
16
  let checkCount = 0;
10
17
  function check(name, fn) {
11
18
  fn();
@@ -15,58 +22,27 @@ function check(name, fn) {
15
22
  async function withRoot(fn) {
16
23
  const root = mkdtempSync(join(tmpdir(), 'gotry-metrics-'));
17
24
  const stateDir = join(root, 'gotry-state');
18
- mkdirSync(stateDir, {
19
- recursive: true
20
- });
25
+ mkdirSync(stateDir, { recursive: true });
21
26
  try {
22
27
  return await fn(root, stateDir);
23
- } finally{
24
- rmSync(root, {
25
- recursive: true,
26
- force: true
27
- });
28
+ }
29
+ finally {
30
+ rmSync(root, { recursive: true, force: true });
28
31
  }
29
32
  }
30
33
  function writeJsonl(dir, name, lines) {
31
- writeFileSync(join(dir, name), lines.map((l)=>typeof l === 'string' ? l : JSON.stringify(l)).join('\n') + '\n', 'utf-8');
34
+ writeFileSync(join(dir, name), lines.map((l) => (typeof l === 'string' ? l : JSON.stringify(l))).join('\n') + '\n', 'utf-8');
32
35
  }
33
- check('事实闸聚合:去重/blocked 率/坏行容忍', ()=>{
36
+ // 1. 事实闸:同 fact_id 取最新 + 坏行跳过 + blocked
37
+ check('事实闸聚合:去重/blocked 率/坏行容忍', () => {
34
38
  const stats = aggregateFacts([
35
- {
36
- schema: 'gotry_bookable_fact.v1',
37
- fact_id: 'f1',
38
- kind: 'flight_like',
39
- verdict: 'hit'
40
- },
39
+ { schema: 'gotry_bookable_fact.v1', fact_id: 'f1', kind: 'flight_like', verdict: 'hit' },
41
40
  'NOT JSON',
42
- {
43
- schema: 'gotry_bookable_fact.v1',
44
- fact_id: 'f1',
45
- kind: 'flight_like',
46
- verdict: 'error'
47
- },
48
- {
49
- schema: 'gotry_bookable_fact.v1',
50
- fact_id: 'f2',
51
- kind: 'flight_like',
52
- verdict: 'hit'
53
- },
54
- {
55
- schema: 'gotry_bookable_fact.v1',
56
- fact_id: 'f3',
57
- kind: 'hotel',
58
- verdict: 'needs-setup'
59
- },
60
- {
61
- schema: 'gotry_bookable_fact.v1',
62
- fact_id: 'f4',
63
- kind: 'hotel',
64
- verdict: 'miss'
65
- },
66
- {
67
- fact_id: 'f5',
68
- kind: 'hotel'
69
- }
41
+ { schema: 'gotry_bookable_fact.v1', fact_id: 'f1', kind: 'flight_like', verdict: 'error' }, // 重查后翻转为 error,后者胜
42
+ { schema: 'gotry_bookable_fact.v1', fact_id: 'f2', kind: 'flight_like', verdict: 'hit' },
43
+ { schema: 'gotry_bookable_fact.v1', fact_id: 'f3', kind: 'hotel', verdict: 'needs-setup' },
44
+ { schema: 'gotry_bookable_fact.v1', fact_id: 'f4', kind: 'hotel', verdict: 'miss' },
45
+ { fact_id: 'f5', kind: 'hotel' }, // 无 verdict → n/a 桶,不计 blocked
70
46
  ]);
71
47
  assert.equal(stats.lines, 7);
72
48
  assert.equal(stats.deduped, 5);
@@ -80,137 +56,57 @@ check('事实闸聚合:去重/blocked 率/坏行容忍', ()=>{
80
56
  assert.equal(hotel.blocked, 1);
81
57
  assert.equal(hotel.verdicts['n/a'], 1);
82
58
  });
83
- check('通道健康聚合:30 天窗/最新事件/计数', ()=>{
59
+ // 2. 通道健康:窗口过滤 + 每通道最新 + 事件计数
60
+ check('通道健康聚合:30 天窗/最新事件/计数', () => {
84
61
  const stats = aggregateChannels([
85
- {
86
- channel: 'flyai',
87
- state: 'down',
88
- reason: 'trial-exhausted',
89
- at: iso(40)
90
- },
91
- {
92
- channel: 'flyai',
93
- state: 'down',
94
- reason: '429',
95
- at: iso(2)
96
- },
97
- {
98
- channel: 'flyai',
99
- state: 'cooldown',
100
- reason: 'retry-after',
101
- at: iso(1)
102
- },
103
- {
104
- channel: 'session:ctrip-hotel',
105
- state: 'down',
106
- reason: 'needs-login',
107
- at: iso(5)
108
- },
109
- 'BROKEN'
110
- ], {
111
- now: NOW
112
- });
62
+ { channel: 'flyai', state: 'down', reason: 'trial-exhausted', at: iso(40) }, // 窗外 → 滤掉
63
+ { channel: 'flyai', state: 'down', reason: '429', at: iso(2) },
64
+ { channel: 'flyai', state: 'cooldown', reason: 'retry-after', at: iso(1) }, // 更新 → 最新
65
+ { channel: 'session:ctrip-hotel', state: 'down', reason: 'needs-login', at: iso(5) },
66
+ 'BROKEN',
67
+ ], { now: NOW });
113
68
  assert.equal(stats.latest.length, 2);
114
- const flyai = stats.latest.find((c)=>c.channel === 'flyai');
69
+ const flyai = stats.latest.find((c) => c.channel === 'flyai');
115
70
  assert.equal(flyai?.state, 'cooldown');
116
71
  assert.equal(flyai?.events, 2);
117
- const ctrip = stats.latest.find((c)=>c.channel === 'session:ctrip-hotel');
72
+ const ctrip = stats.latest.find((c) => c.channel === 'session:ctrip-hotel');
118
73
  assert.equal(ctrip?.events, 1);
119
74
  });
120
- check('事故面聚合:窗口/kind 计数/最近一条/top 来源', ()=>{
75
+ // 3. 事故面:窗口过滤 + byKind + 最近一条 + top 来源
76
+ check('事故面聚合:窗口/kind 计数/最近一条/top 来源', () => {
121
77
  const stats = aggregateIncidents([
122
- {
123
- ts: iso(10),
124
- kind: 'uncaughtException',
125
- message: '太旧不计',
126
- source: 'old-plugin'
127
- },
128
- {
129
- ts: iso(1),
130
- kind: 'plugin_error',
131
- message: 'z3 wasm boom',
132
- source: 'z3'
133
- },
134
- {
135
- ts: iso(0.2),
136
- kind: 'plugin_error',
137
- message: 'second boom',
138
- source: 'z3'
139
- },
140
- {
141
- ts: iso(0.1),
142
- kind: 'tool_execute_error',
143
- message: 'flyai 429',
144
- source: 'flyai'
145
- }
146
- ], {
147
- now: NOW,
148
- windowDays: 7
149
- });
78
+ { ts: iso(10), kind: 'uncaughtException', message: '太旧不计', source: 'old-plugin' },
79
+ { ts: iso(1), kind: 'plugin_error', message: 'z3 wasm boom', source: 'z3' },
80
+ { ts: iso(0.2), kind: 'plugin_error', message: 'second boom', source: 'z3' },
81
+ { ts: iso(0.1), kind: 'tool_execute_error', message: 'flyai 429', source: 'flyai' },
82
+ ], { now: NOW, windowDays: 7 });
150
83
  assert.equal(stats.totalInWindow, 3);
151
84
  assert.equal(stats.byKind.plugin_error.count, 2);
152
85
  assert.equal(stats.byKind.plugin_error.lastMessage, 'second boom');
153
- assert.deepEqual(stats.topSources[0], {
154
- source: 'z3',
155
- count: 2
156
- });
86
+ assert.deepEqual(stats.topSources[0], { source: 'z3', count: 2 });
157
87
  });
158
- check('桥延迟聚合:p50/p95/max/超预算', ()=>{
88
+ // 4. 桥延迟:百分位与超预算计数
89
+ check('桥延迟聚合:p50/p95/max/超预算', () => {
159
90
  const stats = aggregateLatency([
160
- {
161
- ts: iso(1),
162
- kind: 'hotel_search:hbcli',
163
- latencyMs: 100
164
- },
165
- {
166
- ts: iso(1),
167
- kind: 'hotel_search:hbcli',
168
- latencyMs: 200
169
- },
170
- {
171
- ts: iso(0.5),
172
- kind: 'weather:open-meteo',
173
- latencyMs: 300
174
- },
175
- {
176
- ts: iso(0.4),
177
- kind: 'weather:open-meteo',
178
- latencyMs: 400
179
- },
180
- {
181
- ts: iso(0.3),
182
- kind: 'hotel_search:session',
183
- latencyMs: 501
184
- },
185
- {
186
- ts: iso(9),
187
- kind: 'old',
188
- latencyMs: 999_999
189
- },
190
- {
191
- ts: iso(0.2),
192
- kind: 'bad',
193
- latencyMs: Number.NaN
194
- }
195
- ], {
196
- now: NOW,
197
- windowDays: 7
198
- });
91
+ { ts: iso(1), kind: 'hotel_search:hbcli', latencyMs: 100 },
92
+ { ts: iso(1), kind: 'hotel_search:hbcli', latencyMs: 200 },
93
+ { ts: iso(0.5), kind: 'weather:open-meteo', latencyMs: 300 },
94
+ { ts: iso(0.4), kind: 'weather:open-meteo', latencyMs: 400 },
95
+ { ts: iso(0.3), kind: 'hotel_search:session', latencyMs: 501 }, // 超预算
96
+ { ts: iso(9), kind: 'old', latencyMs: 999_999 }, // 窗外
97
+ { ts: iso(0.2), kind: 'bad', latencyMs: Number.NaN }, // 非数 → 跳过
98
+ ], { now: NOW, windowDays: 7 });
199
99
  assert.equal(stats.count, 5);
200
100
  assert.equal(stats.p50, 300);
201
101
  assert.equal(stats.p95, 501);
202
102
  assert.equal(stats.max, 501);
203
103
  assert.equal(stats.overBudget, 1);
204
- assert.deepEqual(stats.topKinds[0], {
205
- kind: 'hotel_search:hbcli',
206
- count: 2
207
- });
104
+ assert.deepEqual(stats.topKinds[0], { kind: 'hotel_search:hbcli', count: 2 });
208
105
  });
209
- await withRoot(async (root)=>{
106
+ // 5. 空根:collect+render 成型、零写入
107
+ await withRoot(async (root) => {
210
108
  const before = readdirSync(root).sort();
211
- const snap = await collectMetrics(root, {
212
- now: NOW
213
- });
109
+ const snap = await collectMetrics(root, { now: NOW });
214
110
  const md = renderMetricsReport(snap);
215
111
  assert.match(md, /事实闸 verdict 分布/);
216
112
  assert.match(md, /通道健康/);
@@ -222,55 +118,25 @@ await withRoot(async (root)=>{
222
118
  checkCount += 1;
223
119
  console.log(` ok ${checkCount} 空 stateRoot:报告成型且零写入`);
224
120
  });
225
- await withRoot(async (root, stateDir)=>{
121
+ // 6. 全量端到端:写各侧车 → 聚合进报告;--out 落盘;gotry-state 目录零改动
122
+ await withRoot(async (root, stateDir) => {
226
123
  writeJsonl(stateDir, 'bookable-facts.jsonl', [
227
- {
228
- schema: 'gotry_bookable_fact.v1',
229
- fact_id: 'a',
230
- kind: 'hotel',
231
- verdict: 'hit'
232
- },
233
- {
234
- schema: 'gotry_bookable_fact.v1',
235
- fact_id: 'b',
236
- kind: 'hotel',
237
- verdict: 'error'
238
- }
124
+ { schema: 'gotry_bookable_fact.v1', fact_id: 'a', kind: 'hotel', verdict: 'hit' },
125
+ { schema: 'gotry_bookable_fact.v1', fact_id: 'b', kind: 'hotel', verdict: 'error' },
239
126
  ]);
240
127
  writeJsonl(stateDir, 'channel-health.jsonl', [
241
- {
242
- channel: 'flyai',
243
- state: 'down',
244
- reason: 'trial-exhausted',
245
- at: iso(1)
246
- }
128
+ { channel: 'flyai', state: 'down', reason: 'trial-exhausted', at: iso(1) },
247
129
  ]);
248
130
  writeJsonl(stateDir, 'incidents.jsonl', [
249
- {
250
- ts: iso(1),
251
- kind: 'plugin_error',
252
- message: 'boom',
253
- source: 'z3'
254
- }
131
+ { ts: iso(1), kind: 'plugin_error', message: 'boom', source: 'z3' },
255
132
  ]);
256
133
  writeJsonl(stateDir, 'bridge-latency.jsonl', [
257
- {
258
- ts: iso(1),
259
- kind: 'hotel_search:hbcli',
260
- latencyMs: 120
261
- }
134
+ { ts: iso(1), kind: 'hotel_search:hbcli', latencyMs: 120 },
262
135
  ]);
263
136
  writeFileSync(join(stateDir, 'doctor-report.md'), '# doctor\n', 'utf-8');
264
137
  const before = readdirSync(stateDir).sort();
265
138
  const outPath = join(root, 'report.md');
266
- await main([
267
- '--state-root',
268
- root,
269
- '--out',
270
- outPath,
271
- '--days',
272
- '7'
273
- ]);
139
+ await main(['--state-root', root, '--out', outPath, '--days', '7'], NOW);
274
140
  const md = readFileSync(outPath, 'utf-8');
275
141
  assert.match(md, /hotel \| 2 \| hit:1 \/ error:1 \| 1\/2\(50%\)/);
276
142
  assert.match(md, /\| flyai \| down \| trial-exhausted \|/);
@@ -283,6 +149,3 @@ await withRoot(async (root, stateDir)=>{
283
149
  console.log(` ok ${checkCount} 端到端:侧车聚合→--out 落盘,stateRoot 只读`);
284
150
  });
285
151
  console.log(`\nMETRICS REPORT TESTS OK(${checkCount} checks)`);
286
-
287
-
288
- //# sourceURL=ts/scripts/metrics-report-tests.ts