akm-cli 0.6.1 → 0.7.0-rc1

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 (319) hide show
  1. package/CHANGELOG.md +66 -0
  2. package/dist/{cli.js → src/cli.js} +620 -26
  3. package/dist/{commands → src/commands}/config-cli.js +5 -4
  4. package/dist/src/commands/distill.js +283 -0
  5. package/dist/src/commands/events.js +108 -0
  6. package/dist/src/commands/history.js +120 -0
  7. package/dist/{commands → src/commands}/installed-stashes.js +1 -1
  8. package/dist/src/commands/proposal.js +119 -0
  9. package/dist/src/commands/propose.js +171 -0
  10. package/dist/src/commands/reflect.js +193 -0
  11. package/dist/{commands → src/commands}/registry-search.js +2 -1
  12. package/dist/{commands → src/commands}/remember.js +12 -0
  13. package/dist/{commands → src/commands}/search.js +74 -1
  14. package/dist/{commands → src/commands}/self-update.js +4 -3
  15. package/dist/{commands → src/commands}/show.js +44 -0
  16. package/dist/{core → src/core}/asset-ref.js +5 -5
  17. package/dist/{core → src/core}/asset-spec.js +12 -0
  18. package/dist/{core → src/core}/common.js +1 -1
  19. package/dist/{core → src/core}/config.js +175 -121
  20. package/dist/{core → src/core}/errors.js +4 -0
  21. package/dist/src/core/events.js +239 -0
  22. package/dist/src/core/lesson-lint.js +86 -0
  23. package/dist/src/core/proposals.js +406 -0
  24. package/dist/src/core/warn.js +72 -0
  25. package/dist/{core → src/core}/write-source.js +80 -5
  26. package/dist/{indexer → src/indexer}/db-search.js +113 -24
  27. package/dist/{indexer → src/indexer}/db.js +76 -23
  28. package/dist/{indexer → src/indexer}/file-context.js +0 -3
  29. package/dist/src/indexer/graph-boost.js +179 -0
  30. package/dist/src/indexer/graph-extraction.js +212 -0
  31. package/dist/{indexer → src/indexer}/indexer.js +73 -6
  32. package/dist/src/indexer/memory-inference.js +263 -0
  33. package/dist/{indexer → src/indexer}/metadata.js +111 -3
  34. package/dist/src/integrations/agent/config.js +292 -0
  35. package/dist/src/integrations/agent/detect.js +94 -0
  36. package/dist/src/integrations/agent/index.js +17 -0
  37. package/dist/src/integrations/agent/profiles.js +65 -0
  38. package/dist/src/integrations/agent/prompts.js +167 -0
  39. package/dist/src/integrations/agent/spawn.js +221 -0
  40. package/dist/{integrations → src/integrations}/lockfile.js +0 -26
  41. package/dist/{llm → src/llm}/client.js +33 -2
  42. package/dist/src/llm/feature-gate.js +108 -0
  43. package/dist/src/llm/graph-extract.js +107 -0
  44. package/dist/src/llm/index-passes.js +35 -0
  45. package/dist/src/llm/memory-infer.js +86 -0
  46. package/dist/{output → src/output}/renderers.js +60 -1
  47. package/dist/src/output/shapes.js +516 -0
  48. package/dist/{output → src/output}/text.js +447 -4
  49. package/dist/{registry → src/registry}/build-index.js +14 -4
  50. package/dist/{registry → src/registry}/factory.js +0 -8
  51. package/dist/{registry → src/registry}/providers/static-index.js +3 -2
  52. package/dist/{registry → src/registry}/resolve.js +68 -2
  53. package/dist/{setup → src/setup}/setup.js +43 -5
  54. package/dist/{sources → src/sources}/providers/git.js +7 -15
  55. package/dist/tests/add-website-source.test.js +119 -0
  56. package/dist/tests/agent/agent-config-loader.test.js +70 -0
  57. package/dist/tests/agent/agent-config.test.js +221 -0
  58. package/dist/tests/agent/agent-detect.test.js +100 -0
  59. package/dist/tests/agent/agent-spawn.test.js +234 -0
  60. package/dist/tests/agent-output.test.js +186 -0
  61. package/dist/tests/architecture/agent-no-llm-sdk-guard.test.js +103 -0
  62. package/dist/tests/architecture/agent-spawn-seam.test.js +193 -0
  63. package/dist/tests/architecture/llm-stateless-seam.test.js +112 -0
  64. package/dist/tests/asset-ref.test.js +192 -0
  65. package/dist/tests/asset-registry.test.js +103 -0
  66. package/dist/tests/asset-spec.test.js +241 -0
  67. package/dist/tests/bench/attribution.test.js +995 -0
  68. package/dist/tests/bench/cleanup-sigint.test.js +83 -0
  69. package/dist/tests/bench/cleanup.js +203 -0
  70. package/dist/tests/bench/cleanup.test.js +166 -0
  71. package/dist/tests/bench/cli.js +683 -0
  72. package/dist/tests/bench/cli.test.js +177 -0
  73. package/dist/tests/bench/compare.test.js +556 -0
  74. package/dist/tests/bench/corpus.js +314 -0
  75. package/dist/tests/bench/corpus.test.js +258 -0
  76. package/dist/tests/bench/driver.js +346 -0
  77. package/dist/tests/bench/driver.test.js +443 -0
  78. package/dist/tests/bench/evolve-metrics.js +179 -0
  79. package/dist/tests/bench/evolve-metrics.test.js +187 -0
  80. package/dist/tests/bench/evolve.js +580 -0
  81. package/dist/tests/bench/evolve.test.js +616 -0
  82. package/dist/tests/bench/failure-modes.test.js +300 -0
  83. package/dist/tests/bench/feedback-integrity.test.js +456 -0
  84. package/dist/tests/bench/leakage.test.js +125 -0
  85. package/dist/tests/bench/learning-curve.test.js +133 -0
  86. package/dist/tests/bench/metrics.js +2319 -0
  87. package/dist/tests/bench/metrics.test.js +1144 -0
  88. package/dist/tests/bench/no-os-tmpdir-invariant.test.js +43 -0
  89. package/dist/tests/bench/report.js +1821 -0
  90. package/dist/tests/bench/report.test.js +989 -0
  91. package/dist/tests/bench/runner.js +536 -0
  92. package/dist/tests/bench/runner.test.js +958 -0
  93. package/dist/tests/bench/search-bridge.test.js +331 -0
  94. package/dist/tests/bench/tmp.js +41 -0
  95. package/dist/tests/bench/trajectory.js +116 -0
  96. package/dist/tests/bench/trajectory.test.js +127 -0
  97. package/dist/tests/bench/verifier.js +109 -0
  98. package/dist/tests/bench/verifier.test.js +118 -0
  99. package/dist/tests/bench/workflow-evaluator.js +557 -0
  100. package/dist/tests/bench/workflow-evaluator.test.js +421 -0
  101. package/dist/tests/bench/workflow-spec.js +358 -0
  102. package/dist/tests/bench/workflow-spec.test.js +363 -0
  103. package/dist/tests/bench/workflow-trace.js +438 -0
  104. package/dist/tests/bench/workflow-trace.test.js +254 -0
  105. package/dist/tests/benchmark-search-quality.js +536 -0
  106. package/dist/tests/benchmark-suite.js +1441 -0
  107. package/dist/tests/capture-cli.test.js +112 -0
  108. package/dist/tests/cli-errors.test.js +203 -0
  109. package/dist/tests/commands/events.test.js +370 -0
  110. package/dist/tests/commands/history.test.js +223 -0
  111. package/dist/tests/commands/import.test.js +103 -0
  112. package/dist/tests/commands/proposal-cli.test.js +209 -0
  113. package/dist/tests/commands/reflect-propose-cli.test.js +333 -0
  114. package/dist/tests/commands/remember.test.js +97 -0
  115. package/dist/tests/commands/scope-flags.test.js +300 -0
  116. package/dist/tests/commands/search.test.js +537 -0
  117. package/dist/tests/commands/show-indexer-parity.test.js +117 -0
  118. package/dist/tests/commands/show.test.js +294 -0
  119. package/dist/tests/common.test.js +266 -0
  120. package/dist/tests/completions.test.js +142 -0
  121. package/dist/tests/config-cli.test.js +193 -0
  122. package/dist/tests/config-llm-features.test.js +139 -0
  123. package/dist/tests/config.test.js +544 -0
  124. package/dist/tests/contracts/migration-baseline.test.js +43 -0
  125. package/dist/tests/contracts/reflect-propose-envelope.test.js +139 -0
  126. package/dist/tests/contracts/spec-helpers.js +46 -0
  127. package/dist/tests/contracts/v1-spec-section-11-proposal-queue.test.js +228 -0
  128. package/dist/tests/contracts/v1-spec-section-12-agent-config.test.js +56 -0
  129. package/dist/tests/contracts/v1-spec-section-13-lesson-type.test.js +34 -0
  130. package/dist/tests/contracts/v1-spec-section-14-llm-features.test.js +94 -0
  131. package/dist/tests/contracts/v1-spec-section-4-1-asset-types.test.js +39 -0
  132. package/dist/tests/contracts/v1-spec-section-4-2-quality-rules.test.js +44 -0
  133. package/dist/tests/contracts/v1-spec-section-5-configuration.test.js +47 -0
  134. package/dist/tests/contracts/v1-spec-section-6-orchestration.test.js +40 -0
  135. package/dist/tests/contracts/v1-spec-section-7-module-layout.test.js +58 -0
  136. package/dist/tests/contracts/v1-spec-section-8-extension-points.test.js +34 -0
  137. package/dist/tests/contracts/v1-spec-section-9-4-cli-surface.test.js +75 -0
  138. package/dist/tests/contracts/v1-spec-section-9-7-llm-agent-boundary.test.js +36 -0
  139. package/dist/tests/core/write-source.test.js +366 -0
  140. package/dist/tests/curate-command.test.js +87 -0
  141. package/dist/tests/db-scoring.test.js +201 -0
  142. package/dist/tests/db.test.js +654 -0
  143. package/dist/tests/distill-cli-flag.test.js +208 -0
  144. package/dist/tests/distill.test.js +515 -0
  145. package/dist/tests/docker-install.test.js +120 -0
  146. package/dist/tests/e2e.test.js +1398 -0
  147. package/dist/tests/embedder.test.js +340 -0
  148. package/dist/tests/embedding-model-config.test.js +379 -0
  149. package/dist/tests/feedback-command.test.js +172 -0
  150. package/dist/tests/file-context.test.js +552 -0
  151. package/dist/tests/fixtures/scripts/git/summarize-diff.js +9 -0
  152. package/dist/tests/fixtures/scripts/lint/eslint-check.js +7 -0
  153. package/dist/tests/fixtures/stashes/load.js +166 -0
  154. package/dist/tests/fixtures/stashes/load.test.js +88 -0
  155. package/dist/tests/fixtures/stashes/ranking-baseline/scripts/mem0-search.js +12 -0
  156. package/dist/tests/frontmatter.test.js +190 -0
  157. package/dist/tests/fts-field-weighting.test.js +254 -0
  158. package/dist/tests/fuzzy-search.test.js +230 -0
  159. package/dist/tests/git-provider-clone.test.js +45 -0
  160. package/dist/tests/github.test.js +161 -0
  161. package/dist/tests/graph-boost-ranking.test.js +305 -0
  162. package/dist/tests/graph-extraction.test.js +282 -0
  163. package/dist/tests/helpers/usage-events.js +8 -0
  164. package/dist/tests/index-pass-llm.test.js +161 -0
  165. package/dist/tests/indexer.test.js +559 -0
  166. package/dist/tests/info-command.test.js +166 -0
  167. package/dist/tests/init.test.js +69 -0
  168. package/dist/tests/install-script.test.js +246 -0
  169. package/dist/tests/integration/agent-real-profile.test.js +94 -0
  170. package/dist/tests/issue-36-repro.test.js +304 -0
  171. package/dist/tests/issues-191-194.test.js +160 -0
  172. package/dist/tests/lesson-lint.test.js +111 -0
  173. package/dist/tests/llm-client.test.js +115 -0
  174. package/dist/tests/llm-feature-gate.test.js +151 -0
  175. package/dist/tests/llm.test.js +139 -0
  176. package/dist/tests/lockfile.test.js +216 -0
  177. package/dist/tests/manifest.test.js +205 -0
  178. package/dist/tests/markdown.test.js +126 -0
  179. package/dist/tests/matchers-unit.test.js +189 -0
  180. package/dist/tests/memory-inference.test.js +299 -0
  181. package/dist/tests/merge-scoring.test.js +136 -0
  182. package/dist/tests/metadata.test.js +313 -0
  183. package/dist/tests/migration-help.test.js +89 -0
  184. package/dist/tests/origin-resolve.test.js +124 -0
  185. package/dist/tests/output-baseline.test.js +217 -0
  186. package/dist/tests/output-shapes-unit.test.js +476 -0
  187. package/dist/tests/parallel-search.test.js +272 -0
  188. package/dist/tests/parameter-metadata.test.js +365 -0
  189. package/dist/tests/paths.test.js +177 -0
  190. package/dist/tests/progressive-disclosure.test.js +280 -0
  191. package/dist/tests/proposals.test.js +279 -0
  192. package/dist/tests/proposed-quality.test.js +271 -0
  193. package/dist/tests/provider-registry.test.js +32 -0
  194. package/dist/tests/ranking-regression.test.js +548 -0
  195. package/dist/tests/reflect-propose.test.js +455 -0
  196. package/dist/tests/registry-build-index.test.js +378 -0
  197. package/dist/tests/registry-cli.test.js +290 -0
  198. package/dist/tests/registry-index-v2.test.js +430 -0
  199. package/dist/tests/registry-install.test.js +728 -0
  200. package/dist/tests/registry-providers/parity.test.js +189 -0
  201. package/dist/tests/registry-providers/skills-sh.test.js +309 -0
  202. package/dist/tests/registry-providers/static-index.test.js +204 -0
  203. package/dist/tests/registry-resolve.test.js +126 -0
  204. package/dist/tests/registry-search.test.js +723 -0
  205. package/dist/tests/remember-frontmatter.test.js +380 -0
  206. package/dist/tests/remember-unit.test.js +123 -0
  207. package/dist/tests/ripgrep-install.test.js +251 -0
  208. package/dist/tests/ripgrep-resolve.test.js +108 -0
  209. package/dist/tests/ripgrep.test.js +163 -0
  210. package/dist/tests/save-command.test.js +94 -0
  211. package/dist/tests/save-trust-qa-fixes.test.js +270 -0
  212. package/dist/tests/scoring-pipeline.test.js +648 -0
  213. package/dist/tests/search-include-proposed-cli.test.js +118 -0
  214. package/dist/tests/self-update.test.js +442 -0
  215. package/dist/tests/semantic-search-e2e.test.js +512 -0
  216. package/dist/tests/semantic-status.test.js +471 -0
  217. package/dist/tests/setup-run.integration.js +877 -0
  218. package/dist/tests/setup-wizard.test.js +198 -0
  219. package/dist/tests/setup.test.js +131 -0
  220. package/dist/tests/source-add.test.js +11 -0
  221. package/dist/tests/source-clone.test.js +254 -0
  222. package/dist/tests/source-manage.test.js +366 -0
  223. package/dist/tests/source-providers/filesystem.test.js +82 -0
  224. package/dist/tests/source-providers/git.test.js +252 -0
  225. package/dist/tests/source-providers/website.test.js +128 -0
  226. package/dist/tests/source-qa-fixes.test.js +268 -0
  227. package/dist/tests/source-registry.test.js +350 -0
  228. package/dist/tests/source-resolve.test.js +100 -0
  229. package/dist/tests/source-source.test.js +221 -0
  230. package/dist/tests/source.test.js +533 -0
  231. package/dist/tests/tar-utils-scan.test.js +73 -0
  232. package/dist/tests/toggle-components.test.js +73 -0
  233. package/dist/tests/usage-telemetry.test.js +265 -0
  234. package/dist/tests/utility-scoring.test.js +558 -0
  235. package/dist/tests/vault-load-error.test.js +78 -0
  236. package/dist/tests/vault-qa-fixes.test.js +194 -0
  237. package/dist/tests/vault.test.js +429 -0
  238. package/dist/tests/vector-search.test.js +608 -0
  239. package/dist/tests/walker.test.js +252 -0
  240. package/dist/tests/wave2-cluster-bc.test.js +228 -0
  241. package/dist/tests/wave2-cluster-d.test.js +180 -0
  242. package/dist/tests/wave2-cluster-e.test.js +179 -0
  243. package/dist/tests/wiki-qa-fixes.test.js +270 -0
  244. package/dist/tests/wiki.test.js +529 -0
  245. package/dist/tests/workflow-cli.test.js +271 -0
  246. package/dist/tests/workflow-markdown.test.js +171 -0
  247. package/dist/tests/workflow-path-escape.test.js +132 -0
  248. package/dist/tests/workflow-qa-fixes.test.js +377 -0
  249. package/dist/tests/workflows/indexer-rejection.test.js +213 -0
  250. package/docs/README.md +8 -0
  251. package/docs/migration/release-notes/0.7.0.md +244 -0
  252. package/package.json +2 -2
  253. package/dist/core/warn.js +0 -27
  254. package/dist/output/shapes.js +0 -212
  255. /package/dist/{commands → src/commands}/completions.js +0 -0
  256. /package/dist/{commands → src/commands}/curate.js +0 -0
  257. /package/dist/{commands → src/commands}/info.js +0 -0
  258. /package/dist/{commands → src/commands}/init.js +0 -0
  259. /package/dist/{commands → src/commands}/install-audit.js +0 -0
  260. /package/dist/{commands → src/commands}/migration-help.js +0 -0
  261. /package/dist/{commands → src/commands}/source-add.js +0 -0
  262. /package/dist/{commands → src/commands}/source-clone.js +0 -0
  263. /package/dist/{commands → src/commands}/source-manage.js +0 -0
  264. /package/dist/{commands → src/commands}/vault.js +0 -0
  265. /package/dist/{core → src/core}/asset-registry.js +0 -0
  266. /package/dist/{core → src/core}/frontmatter.js +0 -0
  267. /package/dist/{core → src/core}/markdown.js +0 -0
  268. /package/dist/{core → src/core}/paths.js +0 -0
  269. /package/dist/{indexer → src/indexer}/manifest.js +0 -0
  270. /package/dist/{indexer → src/indexer}/matchers.js +0 -0
  271. /package/dist/{indexer → src/indexer}/search-fields.js +0 -0
  272. /package/dist/{indexer → src/indexer}/search-source.js +0 -0
  273. /package/dist/{indexer → src/indexer}/semantic-status.js +0 -0
  274. /package/dist/{indexer → src/indexer}/usage-events.js +0 -0
  275. /package/dist/{indexer → src/indexer}/walker.js +0 -0
  276. /package/dist/{integrations → src/integrations}/github.js +0 -0
  277. /package/dist/{llm → src/llm}/embedder.js +0 -0
  278. /package/dist/{llm → src/llm}/embedders/cache.js +0 -0
  279. /package/dist/{llm → src/llm}/embedders/local.js +0 -0
  280. /package/dist/{llm → src/llm}/embedders/remote.js +0 -0
  281. /package/dist/{llm → src/llm}/embedders/types.js +0 -0
  282. /package/dist/{llm → src/llm}/metadata-enhance.js +0 -0
  283. /package/dist/{output → src/output}/cli-hints.js +0 -0
  284. /package/dist/{output → src/output}/context.js +0 -0
  285. /package/dist/{registry → src/registry}/create-provider-registry.js +0 -0
  286. /package/dist/{registry → src/registry}/origin-resolve.js +0 -0
  287. /package/dist/{registry → src/registry}/providers/index.js +0 -0
  288. /package/dist/{registry → src/registry}/providers/skills-sh.js +0 -0
  289. /package/dist/{registry → src/registry}/providers/types.js +0 -0
  290. /package/dist/{registry → src/registry}/types.js +0 -0
  291. /package/dist/{setup → src/setup}/detect.js +0 -0
  292. /package/dist/{setup → src/setup}/ripgrep-install.js +0 -0
  293. /package/dist/{setup → src/setup}/ripgrep-resolve.js +0 -0
  294. /package/dist/{setup → src/setup}/steps.js +0 -0
  295. /package/dist/{sources → src/sources}/include.js +0 -0
  296. /package/dist/{sources → src/sources}/provider-factory.js +0 -0
  297. /package/dist/{sources → src/sources}/provider.js +0 -0
  298. /package/dist/{sources → src/sources}/providers/filesystem.js +0 -0
  299. /package/dist/{sources → src/sources}/providers/index.js +0 -0
  300. /package/dist/{sources → src/sources}/providers/install-types.js +0 -0
  301. /package/dist/{sources → src/sources}/providers/npm.js +0 -0
  302. /package/dist/{sources → src/sources}/providers/provider-utils.js +0 -0
  303. /package/dist/{sources → src/sources}/providers/sync-from-ref.js +0 -0
  304. /package/dist/{sources → src/sources}/providers/tar-utils.js +0 -0
  305. /package/dist/{sources → src/sources}/providers/website.js +0 -0
  306. /package/dist/{sources → src/sources}/resolve.js +0 -0
  307. /package/dist/{sources → src/sources}/types.js +0 -0
  308. /package/dist/{templates → src/templates}/wiki-templates.js +0 -0
  309. /package/dist/{version.js → src/version.js} +0 -0
  310. /package/dist/{wiki → src/wiki}/wiki.js +0 -0
  311. /package/dist/{workflows → src/workflows}/authoring.js +0 -0
  312. /package/dist/{workflows → src/workflows}/cli.js +0 -0
  313. /package/dist/{workflows → src/workflows}/db.js +0 -0
  314. /package/dist/{workflows → src/workflows}/document-cache.js +0 -0
  315. /package/dist/{workflows → src/workflows}/parser.js +0 -0
  316. /package/dist/{workflows → src/workflows}/renderer.js +0 -0
  317. /package/dist/{workflows → src/workflows}/runs.js +0 -0
  318. /package/dist/{workflows → src/workflows}/schema.js +0 -0
  319. /package/dist/{workflows → src/workflows}/validator.js +0 -0
@@ -0,0 +1,438 @@
1
+ /**
2
+ * akm-bench workflow trace normalization (issue #254).
3
+ *
4
+ * Produces a single, normalized stream of `WorkflowTraceEvent`s from a finished
5
+ * `RunResult` so downstream consumers (#256 evaluator, workflow-compliance
6
+ * reports) have ONE place to ask "what did this agent actually do?" instead of
7
+ * scraping `events.jsonl`, agent stdout, workspace diffs, and verifier output
8
+ * independently.
9
+ *
10
+ * Inputs:
11
+ * • `RunResult.events` — structured AKM events (preferred when present).
12
+ * • Captured agent stdout / tool-call text (`agentStdout`).
13
+ * • Verifier output already lives on the run (`verifierStdout`).
14
+ * • Optional `workspaceWrites` — paths the harness observed touched. The
15
+ * normalizer will pick the first one as `first_workspace_write`.
16
+ * • Optional `harness` lifecycle markers (started/finished timestamps).
17
+ *
18
+ * Outputs a `WorkflowTraceResult` with a stable `schemaVersion: 1` field on the
19
+ * envelope so #256 (workflow evaluator, Wave 3) can pin against this contract.
20
+ *
21
+ * Design rules:
22
+ * • Pure: never mutates the input `RunResult` and never touches disk.
23
+ * • Bounded: caps total event count and per-event byte size. A pathological
24
+ * 1MiB stdout line CANNOT become a 1MiB event — see
25
+ * `MAX_EVENT_COUNT` / `MAX_EVENT_BYTES`.
26
+ * • Stable order: events sort by `(orderHint asc, sourceRank asc, originalIndex asc)`
27
+ * so the same RunResult always normalizes to the same trace.
28
+ * • Source attribution: every event carries a `source` so callers can tell
29
+ * where evidence came from when reconstructing the run.
30
+ *
31
+ * NOT in scope: live tailing, persistence, evaluation. Those belong to #256.
32
+ */
33
+ /* ─── Caps (documented contract) ──────────────────────────────────────────── */
34
+ /** Hard cap on total events per trace. Prevents a noisy run from OOM-ing the harness. */
35
+ export const MAX_EVENT_COUNT = 4096;
36
+ /**
37
+ * Per-string-field byte cap. Applied to `query`, `assetRef`, `filePath`,
38
+ * `command`, and each `args[]` / `resultRefs[]` element. A pathological 1MiB
39
+ * stdout line cannot become a 1MiB event because every string we copy off it
40
+ * passes through `clamp(...)`.
41
+ */
42
+ export const MAX_EVENT_BYTES = 4096;
43
+ /**
44
+ * Cap on bytes scanned from `agentStdout`. A runaway agent could emit GBs.
45
+ * 16 MiB matches `EVENTS_READ_CAP_BYTES` / `VERIFIER_STDOUT_SCAN_CAP` so the
46
+ * harness has a single mental model of "how much output we look at".
47
+ */
48
+ export const MAX_STDOUT_SCAN_BYTES = 16 * 1024 * 1024;
49
+ const SOURCE_RANK = {
50
+ akm_events: 0,
51
+ harness: 1,
52
+ filesystem_diff: 2,
53
+ verifier: 3,
54
+ agent_stdout: 4,
55
+ };
56
+ /**
57
+ * Normalize a `RunResult` (plus optional sidecar inputs) into a stable
58
+ * workflow trace. Pure function; never throws on malformed input — bad lines
59
+ * are skipped and a warning is appended to `options.warnings` if provided.
60
+ */
61
+ export function normalizeRunToTrace(run, options = {}) {
62
+ const seeds = [];
63
+ let originalIndex = 0;
64
+ // 1) Harness lifecycle (if supplied).
65
+ if (options.harness?.agentStartedTs) {
66
+ seeds.push({
67
+ orderHint: options.harness.agentStartedTs,
68
+ sourceRank: SOURCE_RANK.harness,
69
+ originalIndex: originalIndex++,
70
+ partial: makePartial(run, options, {
71
+ type: "agent_started",
72
+ ts: options.harness.agentStartedTs,
73
+ source: "harness",
74
+ }),
75
+ });
76
+ }
77
+ // 2) AKM events.jsonl — preferred evidence for akm_search/show/feedback/etc.
78
+ for (const ev of run.events ?? []) {
79
+ const seed = fromAkmEvent(ev, run, options, originalIndex);
80
+ if (seed) {
81
+ seeds.push(seed);
82
+ originalIndex += 1;
83
+ }
84
+ }
85
+ // 3) Agent stdout — scan for `akm <verb>` invocations not already covered.
86
+ const stdoutSeeds = fromAgentStdout(options.agentStdout, run, options, originalIndex);
87
+ for (const seed of stdoutSeeds) {
88
+ seeds.push(seed);
89
+ originalIndex += 1;
90
+ }
91
+ // 4) Workspace writes — first becomes `first_workspace_write`.
92
+ if (options.workspaceWrites && options.workspaceWrites.length > 0) {
93
+ let isFirst = true;
94
+ for (const filePath of options.workspaceWrites) {
95
+ if (typeof filePath !== "string" || filePath.length === 0)
96
+ continue;
97
+ seeds.push({
98
+ // Workspace writes have no native timestamp; place them after akm_events
99
+ // by giving them a high lexical hint anchored to the run finish marker
100
+ // when present, else a sentinel that sorts after ISO timestamps.
101
+ orderHint: options.harness?.agentFinishedTs ?? "~workspace",
102
+ sourceRank: SOURCE_RANK.filesystem_diff,
103
+ originalIndex: originalIndex++,
104
+ partial: (() => {
105
+ const clampedFilePath = clamp(filePath);
106
+ const partial = makePartial(run, options, {
107
+ type: isFirst ? "first_workspace_write" : "workspace_write",
108
+ source: "filesystem_diff",
109
+ filePath: clampedFilePath.value,
110
+ });
111
+ if (clampedFilePath.truncated)
112
+ partial.bytesTruncated = true;
113
+ return partial;
114
+ })(),
115
+ });
116
+ isFirst = false;
117
+ }
118
+ }
119
+ // 5) Verifier — derive a single `verifier_run` event from the run envelope.
120
+ if (run.verifierExitCode !== undefined && run.verifierExitCode !== null) {
121
+ seeds.push({
122
+ orderHint: options.harness?.agentFinishedTs ?? "~verifier",
123
+ sourceRank: SOURCE_RANK.verifier,
124
+ originalIndex: originalIndex++,
125
+ partial: makePartial(run, options, {
126
+ type: "verifier_run",
127
+ source: "verifier",
128
+ exitCode: run.verifierExitCode,
129
+ }),
130
+ });
131
+ }
132
+ // 6) Harness finish (if supplied).
133
+ if (options.harness?.agentFinishedTs) {
134
+ seeds.push({
135
+ orderHint: `${options.harness.agentFinishedTs}~`,
136
+ sourceRank: SOURCE_RANK.harness,
137
+ originalIndex: originalIndex++,
138
+ partial: makePartial(run, options, {
139
+ type: "agent_finished",
140
+ ts: options.harness.agentFinishedTs,
141
+ source: "harness",
142
+ exitCode: run.verifierExitCode,
143
+ }),
144
+ });
145
+ }
146
+ // Stable sort: orderHint, then sourceRank, then originalIndex.
147
+ seeds.sort((a, b) => {
148
+ if (a.orderHint < b.orderHint)
149
+ return -1;
150
+ if (a.orderHint > b.orderHint)
151
+ return 1;
152
+ if (a.sourceRank !== b.sourceRank)
153
+ return a.sourceRank - b.sourceRank;
154
+ return a.originalIndex - b.originalIndex;
155
+ });
156
+ // Cap event count.
157
+ let truncated = false;
158
+ let kept = seeds;
159
+ if (seeds.length > MAX_EVENT_COUNT) {
160
+ kept = seeds.slice(0, MAX_EVENT_COUNT);
161
+ truncated = true;
162
+ if (options.warnings) {
163
+ options.warnings.push(`workflow trace truncated: ${seeds.length} events exceed ${MAX_EVENT_COUNT}-event cap; remainder dropped.`);
164
+ }
165
+ }
166
+ const events = kept.map((seed, idx) => ({ id: idx, ...seed.partial }));
167
+ return {
168
+ schemaVersion: 1,
169
+ taskId: run.taskId,
170
+ arm: run.arm,
171
+ seed: run.seed,
172
+ events,
173
+ truncated,
174
+ };
175
+ }
176
+ /* ─── Source: AKM events.jsonl envelopes ──────────────────────────────────── */
177
+ const AKM_EVENT_TYPE_MAP = {
178
+ search: "akm_search",
179
+ show: "akm_show",
180
+ feedback: "akm_feedback",
181
+ reflect_invoked: "akm_reflect",
182
+ distill_invoked: "akm_distill",
183
+ propose_invoked: "akm_propose",
184
+ promoted: "akm_proposal_accept",
185
+ };
186
+ function fromAkmEvent(ev, run, options, originalIndex) {
187
+ if (!ev || typeof ev !== "object")
188
+ return null;
189
+ const envelope = ev;
190
+ const eventType = typeof envelope.eventType === "string" ? envelope.eventType : undefined;
191
+ if (!eventType)
192
+ return null;
193
+ const traceType = AKM_EVENT_TYPE_MAP[eventType];
194
+ if (!traceType)
195
+ return null; // ignore non-workflow events (add/remove/update/etc.)
196
+ const ts = typeof envelope.ts === "string" && envelope.ts.length > 0 ? envelope.ts : undefined;
197
+ const ref = typeof envelope.ref === "string" ? envelope.ref : undefined;
198
+ const meta = (envelope.metadata ?? undefined);
199
+ const partial = makePartial(run, options, {
200
+ type: traceType,
201
+ source: "akm_events",
202
+ ts,
203
+ });
204
+ let bytesTruncated = false;
205
+ if (ref) {
206
+ const clampedRef = clamp(ref);
207
+ partial.assetRef = clampedRef.value;
208
+ bytesTruncated ||= clampedRef.truncated;
209
+ }
210
+ // Pull useful structured fields off metadata when present. We deliberately
211
+ // probe a small whitelist so a malicious agent can't smuggle arbitrary
212
+ // payloads into the trace.
213
+ if (meta && typeof meta === "object") {
214
+ const q = meta.query;
215
+ if (typeof q === "string") {
216
+ const clampedQuery = clamp(q);
217
+ partial.query = clampedQuery.value;
218
+ bytesTruncated ||= clampedQuery.truncated;
219
+ }
220
+ const fp = meta.path ?? meta.filePath;
221
+ if (typeof fp === "string") {
222
+ const clampedFilePath = clamp(fp);
223
+ partial.filePath = clampedFilePath.value;
224
+ bytesTruncated ||= clampedFilePath.truncated;
225
+ }
226
+ const ec = meta.exitCode;
227
+ if (typeof ec === "number" && Number.isFinite(ec))
228
+ partial.exitCode = ec;
229
+ const refs = meta.resultRefs;
230
+ if (Array.isArray(refs)) {
231
+ partial.resultRefs = refs
232
+ .filter((r) => typeof r === "string")
233
+ .map((r) => {
234
+ const clampedRef = clamp(r);
235
+ bytesTruncated ||= clampedRef.truncated;
236
+ return clampedRef.value;
237
+ });
238
+ }
239
+ }
240
+ if (bytesTruncated)
241
+ partial.bytesTruncated = true;
242
+ return {
243
+ orderHint: ts ?? "￿akm-events", // events without ts sort after timestamped ones
244
+ sourceRank: SOURCE_RANK.akm_events,
245
+ originalIndex,
246
+ partial,
247
+ };
248
+ }
249
+ function fromAgentStdout(stdoutRaw, run, options, startIndex) {
250
+ if (!stdoutRaw)
251
+ return [];
252
+ const stdout = stdoutRaw.length > MAX_STDOUT_SCAN_BYTES ? stdoutRaw.slice(0, MAX_STDOUT_SCAN_BYTES) : stdoutRaw;
253
+ if (stdout.length < stdoutRaw.length && options.warnings) {
254
+ options.warnings.push(`workflow trace stdout truncated: ${stdoutRaw.length} chars exceeds ${MAX_STDOUT_SCAN_BYTES}-char cap; trace computed from the prefix.`);
255
+ }
256
+ const out = [];
257
+ let idx = startIndex;
258
+ // Match invocations like `akm search "query"`, `akm show skill:foo`, `akm feedback +1 skill:foo`.
259
+ // We scan line-by-line so position in the stdout becomes a stable order hint.
260
+ const lines = stdout.split("\n");
261
+ for (let lineNo = 0; lineNo < lines.length; lineNo += 1) {
262
+ const line = lines[lineNo];
263
+ const match = parseAkmCli(line);
264
+ if (!match)
265
+ continue;
266
+ const clampedCommand = clamp(match.command);
267
+ let bytesTruncated = clampedCommand.truncated;
268
+ const partial = makePartial(run, options, {
269
+ type: match.type,
270
+ source: "agent_stdout",
271
+ command: clampedCommand.value,
272
+ });
273
+ if (match.assetRef) {
274
+ const clampedRef = clamp(match.assetRef);
275
+ partial.assetRef = clampedRef.value;
276
+ bytesTruncated ||= clampedRef.truncated;
277
+ }
278
+ if (match.query) {
279
+ const clampedQuery = clamp(match.query);
280
+ partial.query = clampedQuery.value;
281
+ bytesTruncated ||= clampedQuery.truncated;
282
+ }
283
+ if (match.args) {
284
+ partial.args = match.args.map((arg) => {
285
+ const clampedArg = clamp(arg);
286
+ bytesTruncated ||= clampedArg.truncated;
287
+ return clampedArg.value;
288
+ });
289
+ }
290
+ if (bytesTruncated)
291
+ partial.bytesTruncated = true;
292
+ out.push({
293
+ // Stdout has no native timestamp; lexically place after timestamped events
294
+ // by using a leading `~` sentinel (sorts after all printable ASCII timestamps),
295
+ // then stable-order by line number padded to 8 digits.
296
+ orderHint: `~stdout-${String(lineNo).padStart(8, "0")}`,
297
+ sourceRank: SOURCE_RANK.agent_stdout,
298
+ originalIndex: idx,
299
+ partial,
300
+ });
301
+ idx += 1;
302
+ }
303
+ return out;
304
+ }
305
+ /**
306
+ * Recognise an `akm <verb> ...` invocation in a single line of agent output.
307
+ * Returns null when the line does not look like an akm CLI call.
308
+ *
309
+ * Supported shapes:
310
+ * • Bare CLI: `akm search "deploy docker"`
311
+ * • Tool-call: `tool: akm show skill:foo`
312
+ * • JSON-ish: `{"command":"akm","args":["search","deploy"]}`
313
+ */
314
+ function parseAkmCli(line) {
315
+ if (!line || line.length === 0)
316
+ return null;
317
+ // Quick reject — every form contains the literal `akm` token.
318
+ if (line.indexOf("akm") === -1)
319
+ return null;
320
+ // JSON tool-call form first (most specific).
321
+ const jsonMatch = line.match(/"command"\s*:\s*"akm"\s*,\s*"args"\s*:\s*\[([^\]]*)\]/);
322
+ if (jsonMatch) {
323
+ const argList = jsonMatch[1]
324
+ .split(",")
325
+ .map((a) => a.trim().replace(/^"|"$/g, ""))
326
+ .filter(Boolean);
327
+ return classifyArgs("akm", argList);
328
+ }
329
+ // Bare CLI form: capture everything after `akm `.
330
+ const bareMatch = line.match(/(?:^|[\s>`'"])akm\s+(\w[\w-]*)(?:\s+([^\n]*))?/);
331
+ if (bareMatch) {
332
+ const verb = bareMatch[1];
333
+ const rest = (bareMatch[2] ?? "").trim();
334
+ const args = tokenizeArgs(rest);
335
+ return classifyArgs("akm", [verb, ...args]);
336
+ }
337
+ return null;
338
+ }
339
+ function classifyArgs(command, argv) {
340
+ if (argv.length === 0)
341
+ return null;
342
+ const verb = argv[0];
343
+ const rest = argv.slice(1);
344
+ switch (verb) {
345
+ case "search":
346
+ return { type: "akm_search", command, args: argv, query: rest.join(" ") || undefined };
347
+ case "show":
348
+ return { type: "akm_show", command, args: argv, assetRef: rest[0] };
349
+ case "feedback":
350
+ return { type: "akm_feedback", command, args: argv, assetRef: rest.find((a) => a.includes(":")) };
351
+ case "reflect":
352
+ return { type: "akm_reflect", command, args: argv };
353
+ case "distill":
354
+ return { type: "akm_distill", command, args: argv };
355
+ case "propose":
356
+ return { type: "akm_propose", command, args: argv };
357
+ default:
358
+ return null;
359
+ }
360
+ }
361
+ /**
362
+ * Cheap shell-like tokenizer. Handles double-quoted and single-quoted strings;
363
+ * everything else splits on whitespace. Not a full POSIX parser — we only need
364
+ * "good enough" for opencode's tool-call logging.
365
+ */
366
+ function tokenizeArgs(rest) {
367
+ const out = [];
368
+ let i = 0;
369
+ while (i < rest.length) {
370
+ while (i < rest.length && rest[i] === " ")
371
+ i += 1;
372
+ if (i >= rest.length)
373
+ break;
374
+ const ch = rest[i];
375
+ if (ch === '"' || ch === "'") {
376
+ const quote = ch;
377
+ i += 1;
378
+ const start = i;
379
+ while (i < rest.length && rest[i] !== quote)
380
+ i += 1;
381
+ out.push(rest.slice(start, i));
382
+ if (i < rest.length)
383
+ i += 1;
384
+ }
385
+ else {
386
+ const start = i;
387
+ while (i < rest.length && rest[i] !== " ")
388
+ i += 1;
389
+ out.push(rest.slice(start, i));
390
+ }
391
+ }
392
+ return out;
393
+ }
394
+ /* ─── Helpers ─────────────────────────────────────────────────────────────── */
395
+ function makePartial(run, options, fields) {
396
+ const base = {
397
+ taskId: run.taskId,
398
+ arm: run.arm,
399
+ seed: run.seed,
400
+ type: fields.type,
401
+ source: fields.source,
402
+ };
403
+ if (options.runId)
404
+ base.runId = options.runId;
405
+ if (fields.ts !== undefined)
406
+ base.ts = fields.ts;
407
+ if (fields.command !== undefined)
408
+ base.command = fields.command;
409
+ if (fields.args !== undefined)
410
+ base.args = fields.args;
411
+ if (fields.assetRef !== undefined)
412
+ base.assetRef = fields.assetRef;
413
+ if (fields.query !== undefined)
414
+ base.query = fields.query;
415
+ if (fields.resultRefs !== undefined)
416
+ base.resultRefs = fields.resultRefs;
417
+ if (fields.filePath !== undefined)
418
+ base.filePath = fields.filePath;
419
+ if (fields.exitCode !== undefined)
420
+ base.exitCode = fields.exitCode;
421
+ return base;
422
+ }
423
+ /** Clamp a single string to MAX_EVENT_BYTES UTF-8 bytes without splitting code points. */
424
+ function clamp(value) {
425
+ if (Buffer.byteLength(value, "utf8") <= MAX_EVENT_BYTES) {
426
+ return { value, truncated: false };
427
+ }
428
+ let out = "";
429
+ let bytes = 0;
430
+ for (const ch of value) {
431
+ const nextBytes = Buffer.byteLength(ch, "utf8");
432
+ if (bytes + nextBytes > MAX_EVENT_BYTES)
433
+ break;
434
+ out += ch;
435
+ bytes += nextBytes;
436
+ }
437
+ return { value: out, truncated: true };
438
+ }
@@ -0,0 +1,254 @@
1
+ /**
2
+ * Tests for tests/bench/workflow-trace.ts (issue #254).
3
+ */
4
+ import { describe, expect, test } from "bun:test";
5
+ import { MAX_EVENT_BYTES, MAX_EVENT_COUNT, MAX_STDOUT_SCAN_BYTES, normalizeRunToTrace, } from "./workflow-trace";
6
+ function makeRun(overrides = {}) {
7
+ return {
8
+ schemaVersion: 1,
9
+ taskId: "deploy-docker",
10
+ arm: "akm",
11
+ seed: 42,
12
+ model: "anthropic/claude-opus-4-7",
13
+ outcome: "pass",
14
+ tokens: { input: 0, output: 0 },
15
+ tokenMeasurement: "parsed",
16
+ wallclockMs: 1234,
17
+ trajectory: { correctAssetLoaded: null, feedbackRecorded: null },
18
+ events: [],
19
+ verifierStdout: "",
20
+ verifierExitCode: 0,
21
+ assetsLoaded: [],
22
+ ...overrides,
23
+ };
24
+ }
25
+ function ev(eventType, ts, extra = {}) {
26
+ return {
27
+ schemaVersion: 1,
28
+ id: 0,
29
+ ts,
30
+ eventType,
31
+ ...extra,
32
+ };
33
+ }
34
+ describe("normalizeRunToTrace — AKM event input", () => {
35
+ test("maps search/show/feedback events to typed trace events with stable order", () => {
36
+ const run = makeRun({
37
+ events: [
38
+ ev("show", "2026-04-27T10:00:01.000Z", { ref: "skill:deploy" }),
39
+ ev("search", "2026-04-27T10:00:00.000Z", { metadata: { query: "deploy docker" } }),
40
+ ev("feedback", "2026-04-27T10:00:02.000Z", { ref: "skill:deploy", metadata: { vote: 1 } }),
41
+ ],
42
+ });
43
+ const trace = normalizeRunToTrace(run);
44
+ expect(trace.schemaVersion).toBe(1);
45
+ expect(trace.taskId).toBe("deploy-docker");
46
+ expect(trace.arm).toBe("akm");
47
+ expect(trace.seed).toBe(42);
48
+ expect(trace.truncated).toBe(false);
49
+ // `verifier_run` is auto-derived from RunResult.verifierExitCode, so we
50
+ // expect 4 events: search, show, feedback, verifier.
51
+ const types = trace.events.map((e) => e.type);
52
+ expect(types).toEqual(["akm_search", "akm_show", "akm_feedback", "verifier_run"]);
53
+ expect(trace.events[0].source).toBe("akm_events");
54
+ expect(trace.events[0].query).toBe("deploy docker");
55
+ expect(trace.events[1].assetRef).toBe("skill:deploy");
56
+ expect(trace.events[2].assetRef).toBe("skill:deploy");
57
+ expect(trace.events[3].source).toBe("verifier");
58
+ expect(trace.events[3].exitCode).toBe(0);
59
+ // Ids are 0..n-1, monotonic.
60
+ for (let i = 0; i < trace.events.length; i += 1) {
61
+ expect(trace.events[i].id).toBe(i);
62
+ }
63
+ });
64
+ test("ignores unrelated AKM event types (add/remove/update)", () => {
65
+ const run = makeRun({
66
+ events: [
67
+ ev("add", "2026-04-27T10:00:00.000Z", { ref: "skill:foo" }),
68
+ ev("remove", "2026-04-27T10:00:01.000Z", { ref: "skill:bar" }),
69
+ ev("search", "2026-04-27T10:00:02.000Z", { metadata: { query: "x" } }),
70
+ ],
71
+ });
72
+ const trace = normalizeRunToTrace(run);
73
+ const types = trace.events.map((e) => e.type);
74
+ expect(types).toEqual(["akm_search", "verifier_run"]);
75
+ });
76
+ test("records verifier_run with exitCode from the run envelope", () => {
77
+ const run = makeRun({ verifierExitCode: 1, outcome: "fail" });
78
+ const trace = normalizeRunToTrace(run);
79
+ const verifier = trace.events.find((e) => e.type === "verifier_run");
80
+ expect(verifier).toBeDefined();
81
+ expect(verifier?.exitCode).toBe(1);
82
+ expect(verifier?.source).toBe("verifier");
83
+ });
84
+ });
85
+ describe("normalizeRunToTrace — stdout / tool-call input", () => {
86
+ test("detects akm CLI invocations from agent stdout", () => {
87
+ const stdout = [
88
+ 'tool: akm search "deploy docker"',
89
+ "tool: akm show skill:deploy",
90
+ "tool: akm feedback +1 skill:deploy",
91
+ ].join("\n");
92
+ const run = makeRun();
93
+ const trace = normalizeRunToTrace(run, { agentStdout: stdout });
94
+ const cliEvents = trace.events.filter((e) => e.source === "agent_stdout");
95
+ expect(cliEvents.map((e) => e.type)).toEqual(["akm_search", "akm_show", "akm_feedback"]);
96
+ expect(cliEvents[0].query).toBe("deploy docker");
97
+ expect(cliEvents[1].assetRef).toBe("skill:deploy");
98
+ expect(cliEvents[2].assetRef).toBe("skill:deploy");
99
+ });
100
+ test("detects JSON tool-call shape", () => {
101
+ const stdout = '{"command":"akm","args":["show","skill:foo"]}';
102
+ const run = makeRun();
103
+ const trace = normalizeRunToTrace(run, { agentStdout: stdout });
104
+ const showEv = trace.events.find((e) => e.type === "akm_show");
105
+ expect(showEv).toBeDefined();
106
+ expect(showEv?.assetRef).toBe("skill:foo");
107
+ expect(showEv?.source).toBe("agent_stdout");
108
+ });
109
+ test("AKM events sort before stdout-derived events when both have the same verb", () => {
110
+ const run = makeRun({
111
+ events: [ev("search", "2026-04-27T10:00:00.000Z", { metadata: { query: "structured" } })],
112
+ });
113
+ const trace = normalizeRunToTrace(run, { agentStdout: 'akm search "stdout"' });
114
+ const searches = trace.events.filter((e) => e.type === "akm_search");
115
+ expect(searches.length).toBe(2);
116
+ expect(searches[0].source).toBe("akm_events");
117
+ expect(searches[1].source).toBe("agent_stdout");
118
+ });
119
+ });
120
+ describe("normalizeRunToTrace — workspace-write detection", () => {
121
+ test("first workspace path becomes first_workspace_write, rest become workspace_write", () => {
122
+ const run = makeRun();
123
+ const trace = normalizeRunToTrace(run, {
124
+ workspaceWrites: ["src/a.ts", "src/b.ts", "src/c.ts"],
125
+ });
126
+ const writes = trace.events.filter((e) => e.type === "first_workspace_write" || e.type === "workspace_write");
127
+ expect(writes.length).toBe(3);
128
+ expect(writes[0].type).toBe("first_workspace_write");
129
+ expect(writes[0].filePath).toBe("src/a.ts");
130
+ expect(writes[1].type).toBe("workspace_write");
131
+ expect(writes[1].filePath).toBe("src/b.ts");
132
+ expect(writes[2].type).toBe("workspace_write");
133
+ expect(writes[2].filePath).toBe("src/c.ts");
134
+ for (const w of writes) {
135
+ expect(w.source).toBe("filesystem_diff");
136
+ }
137
+ });
138
+ test("no workspace writes => no workspace events", () => {
139
+ const run = makeRun();
140
+ const trace = normalizeRunToTrace(run, { workspaceWrites: [] });
141
+ const writes = trace.events.filter((e) => e.type === "first_workspace_write" || e.type === "workspace_write");
142
+ expect(writes.length).toBe(0);
143
+ });
144
+ test("harness lifecycle markers emit agent_started and agent_finished", () => {
145
+ const run = makeRun();
146
+ const trace = normalizeRunToTrace(run, {
147
+ harness: {
148
+ agentStartedTs: "2026-04-27T09:59:59.000Z",
149
+ agentFinishedTs: "2026-04-27T10:00:30.000Z",
150
+ },
151
+ });
152
+ const types = trace.events.map((e) => e.type);
153
+ expect(types[0]).toBe("agent_started");
154
+ expect(types[types.length - 1]).toBe("agent_finished");
155
+ });
156
+ });
157
+ describe("normalizeRunToTrace — malformed/noisy input", () => {
158
+ test("ignores malformed events instead of throwing", () => {
159
+ const malformed = [
160
+ null,
161
+ undefined,
162
+ 42,
163
+ "string-not-an-event",
164
+ { eventType: 123 }, // eventType not a string
165
+ { eventType: "search" }, // no ts — still valid, mapped
166
+ ];
167
+ const run = makeRun({
168
+ // Cast through unknown — we explicitly want to test garbage input.
169
+ events: malformed,
170
+ });
171
+ const trace = normalizeRunToTrace(run);
172
+ // The only valid event is the trailing { eventType: "search" } (it has no
173
+ // ts so it sorts after the synthesised verifier_run sentinel), plus the
174
+ // verifier_run derived from RunResult.
175
+ const types = trace.events.map((e) => e.type).sort();
176
+ expect(types).toEqual(["akm_search", "verifier_run"].sort());
177
+ expect(trace.events.length).toBe(2);
178
+ });
179
+ test("clamps oversized fields to MAX_EVENT_BYTES UTF-8 bytes and flags bytesTruncated", () => {
180
+ const giantQuery = "😀".repeat(MAX_EVENT_BYTES);
181
+ const run = makeRun({
182
+ events: [ev("search", "2026-04-27T10:00:00.000Z", { metadata: { query: giantQuery } })],
183
+ });
184
+ const trace = normalizeRunToTrace(run);
185
+ const search = trace.events.find((e) => e.type === "akm_search");
186
+ expect(Buffer.byteLength(search.query ?? "", "utf8")).toBeLessThanOrEqual(MAX_EVENT_BYTES);
187
+ expect(Buffer.byteLength(search.query ?? "", "utf8")).toBeLessThan(Buffer.byteLength(giantQuery, "utf8"));
188
+ expect(search.bytesTruncated).toBe(true);
189
+ });
190
+ test("does not flag bytesTruncated when a field already fits the byte cap exactly", () => {
191
+ const exactQuery = "x".repeat(MAX_EVENT_BYTES);
192
+ const run = makeRun({
193
+ events: [ev("search", "2026-04-27T10:00:00.000Z", { metadata: { query: exactQuery } })],
194
+ });
195
+ const trace = normalizeRunToTrace(run);
196
+ const search = trace.events.find((e) => e.type === "akm_search");
197
+ expect(search.query).toBe(exactQuery);
198
+ expect(search.bytesTruncated).toBeUndefined();
199
+ });
200
+ test("caps total event count at MAX_EVENT_COUNT and surfaces a warning", () => {
201
+ const events = [];
202
+ for (let i = 0; i < MAX_EVENT_COUNT + 50; i += 1) {
203
+ events.push(ev("search",
204
+ // Pad timestamps so they sort lexicographically.
205
+ `2026-04-27T${String(Math.floor(i / 60) % 24).padStart(2, "0")}:${String(i % 60).padStart(2, "0")}:00.${String(i).padStart(6, "0").slice(-3)}Z`, { metadata: { query: `q-${i}` } }));
206
+ }
207
+ const run = makeRun({ events });
208
+ const warnings = [];
209
+ const trace = normalizeRunToTrace(run, { warnings });
210
+ expect(trace.events.length).toBe(MAX_EVENT_COUNT);
211
+ expect(trace.truncated).toBe(true);
212
+ expect(warnings.some((w) => w.includes("workflow trace truncated"))).toBe(true);
213
+ });
214
+ test("clamps oversized stdout and surfaces a warning instead of OOM", () => {
215
+ // A pathological 1MiB single line plus a trailing real CLI invocation that
216
+ // would be cut off if the cap weren't applied.
217
+ const giantLine = "akm show ".concat("y".repeat(MAX_EVENT_BYTES * 4));
218
+ const run = makeRun();
219
+ const warnings = [];
220
+ const stdout = giantLine; // single line, larger than MAX_EVENT_BYTES but below MAX_STDOUT_SCAN_BYTES
221
+ const trace = normalizeRunToTrace(run, { agentStdout: stdout, warnings });
222
+ const showEv = trace.events.find((e) => e.type === "akm_show");
223
+ expect(showEv).toBeDefined();
224
+ // assetRef must be clamped — it cannot be 4 * MAX_EVENT_BYTES.
225
+ expect(showEv?.assetRef?.length ?? 0).toBeLessThanOrEqual(MAX_EVENT_BYTES);
226
+ expect(showEv?.bytesTruncated).toBe(true);
227
+ });
228
+ test("stdout larger than MAX_STDOUT_SCAN_BYTES is truncated with a warning", () => {
229
+ // Build a stdout > scan cap. We want this to actually exceed the cap so the
230
+ // truncation branch fires, but we keep it minimally larger to keep the test fast.
231
+ const overshoot = 1024;
232
+ const huge = "z".repeat(MAX_STDOUT_SCAN_BYTES + overshoot);
233
+ const run = makeRun();
234
+ const warnings = [];
235
+ normalizeRunToTrace(run, { agentStdout: huge, warnings });
236
+ expect(warnings.some((w) => w.includes("workflow trace stdout truncated"))).toBe(true);
237
+ });
238
+ test("no events, no stdout, no writes => only verifier_run is produced", () => {
239
+ const trace = normalizeRunToTrace(makeRun());
240
+ expect(trace.events.length).toBe(1);
241
+ expect(trace.events[0].type).toBe("verifier_run");
242
+ });
243
+ test("identical inputs produce identical traces (deterministic)", () => {
244
+ const run = makeRun({
245
+ events: [
246
+ ev("show", "2026-04-27T10:00:01.000Z", { ref: "skill:a" }),
247
+ ev("search", "2026-04-27T10:00:00.000Z", { metadata: { query: "q" } }),
248
+ ],
249
+ });
250
+ const a = normalizeRunToTrace(run, { agentStdout: "akm show skill:b" });
251
+ const b = normalizeRunToTrace(run, { agentStdout: "akm show skill:b" });
252
+ expect(JSON.stringify(a)).toBe(JSON.stringify(b));
253
+ });
254
+ });