@fgv/ts-extras 5.1.0-54 → 5.1.0-56

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 (166) hide show
  1. package/CAPABILITIES.md +95 -0
  2. package/dist/packlets/ai-assist/cacheRequest.js +118 -0
  3. package/dist/packlets/ai-assist/cacheRequest.js.map +1 -0
  4. package/dist/packlets/ai-assist/chatRequestBuilders.js +70 -5
  5. package/dist/packlets/ai-assist/chatRequestBuilders.js.map +1 -1
  6. package/dist/packlets/ai-assist/completionClient.js +122 -45
  7. package/dist/packlets/ai-assist/completionClient.js.map +1 -1
  8. package/dist/packlets/ai-assist/imageGenerationClient.js +1 -1
  9. package/dist/packlets/ai-assist/imageGenerationClient.js.map +1 -1
  10. package/dist/packlets/ai-assist/index.js +3 -1
  11. package/dist/packlets/ai-assist/index.js.map +1 -1
  12. package/dist/packlets/ai-assist/model.js.map +1 -1
  13. package/dist/packlets/ai-assist/promptCacheAnthropicBreakpointHarness.js +200 -0
  14. package/dist/packlets/ai-assist/promptCacheAnthropicBreakpointHarness.js.map +1 -0
  15. package/dist/packlets/ai-assist/promptCacheObservabilityHarness.js +137 -0
  16. package/dist/packlets/ai-assist/promptCacheObservabilityHarness.js.map +1 -0
  17. package/dist/packlets/ai-assist/promptCacheRoutingAbHarness.js +156 -0
  18. package/dist/packlets/ai-assist/promptCacheRoutingAbHarness.js.map +1 -0
  19. package/dist/packlets/ai-assist/promptCacheUsageMath.js +124 -0
  20. package/dist/packlets/ai-assist/promptCacheUsageMath.js.map +1 -0
  21. package/dist/packlets/ai-assist/registry.js +9 -11
  22. package/dist/packlets/ai-assist/registry.js.map +1 -1
  23. package/dist/packlets/ai-assist/streamUsageCapability.js +124 -0
  24. package/dist/packlets/ai-assist/streamUsageCapability.js.map +1 -0
  25. package/dist/packlets/ai-assist/streamingAdapters/anthropic.js +24 -5
  26. package/dist/packlets/ai-assist/streamingAdapters/anthropic.js.map +1 -1
  27. package/dist/packlets/ai-assist/streamingAdapters/clientToolContinuationBuilder.js +64 -33
  28. package/dist/packlets/ai-assist/streamingAdapters/clientToolContinuationBuilder.js.map +1 -1
  29. package/dist/packlets/ai-assist/streamingAdapters/common.js +25 -1
  30. package/dist/packlets/ai-assist/streamingAdapters/common.js.map +1 -1
  31. package/dist/packlets/ai-assist/streamingAdapters/gemini.js +12 -5
  32. package/dist/packlets/ai-assist/streamingAdapters/gemini.js.map +1 -1
  33. package/dist/packlets/ai-assist/streamingAdapters/openaiChat.js +34 -8
  34. package/dist/packlets/ai-assist/streamingAdapters/openaiChat.js.map +1 -1
  35. package/dist/packlets/ai-assist/streamingAdapters/openaiResponses.js +22 -14
  36. package/dist/packlets/ai-assist/streamingAdapters/openaiResponses.js.map +1 -1
  37. package/dist/packlets/ai-assist/streamingClient.js +3 -2
  38. package/dist/packlets/ai-assist/streamingClient.js.map +1 -1
  39. package/dist/packlets/ai-assist/structuredOutput.js +84 -7
  40. package/dist/packlets/ai-assist/structuredOutput.js.map +1 -1
  41. package/dist/packlets/ai-assist/structuredOutputTypes.js.map +1 -1
  42. package/dist/packlets/ai-assist/thinkingOptionsResolver.js +18 -3
  43. package/dist/packlets/ai-assist/thinkingOptionsResolver.js.map +1 -1
  44. package/dist/packlets/ai-assist/toolFormats.js +80 -0
  45. package/dist/packlets/ai-assist/toolFormats.js.map +1 -1
  46. package/dist/packlets/ai-assist/toolTypes.js +21 -0
  47. package/dist/packlets/ai-assist/toolTypes.js.map +1 -0
  48. package/dist/packlets/ai-assist/usageNormalization.js +178 -0
  49. package/dist/packlets/ai-assist/usageNormalization.js.map +1 -0
  50. package/dist/packlets/ai-assist/usageTypes.js +21 -0
  51. package/dist/packlets/ai-assist/usageTypes.js.map +1 -0
  52. package/dist/packlets/mustache/index.js.map +1 -1
  53. package/dist/packlets/mustache/interfaces.js.map +1 -1
  54. package/dist/packlets/mustache/mustacheTemplate.js +91 -0
  55. package/dist/packlets/mustache/mustacheTemplate.js.map +1 -1
  56. package/dist/ts-extras.d.ts +577 -30
  57. package/lib/packlets/ai-assist/cacheRequest.d.ts +100 -0
  58. package/lib/packlets/ai-assist/cacheRequest.d.ts.map +1 -0
  59. package/lib/packlets/ai-assist/cacheRequest.js +123 -0
  60. package/lib/packlets/ai-assist/cacheRequest.js.map +1 -0
  61. package/lib/packlets/ai-assist/chatRequestBuilders.d.ts +34 -4
  62. package/lib/packlets/ai-assist/chatRequestBuilders.d.ts.map +1 -1
  63. package/lib/packlets/ai-assist/chatRequestBuilders.js +73 -5
  64. package/lib/packlets/ai-assist/chatRequestBuilders.js.map +1 -1
  65. package/lib/packlets/ai-assist/completionClient.d.ts +6 -0
  66. package/lib/packlets/ai-assist/completionClient.d.ts.map +1 -1
  67. package/lib/packlets/ai-assist/completionClient.js +121 -44
  68. package/lib/packlets/ai-assist/completionClient.js.map +1 -1
  69. package/lib/packlets/ai-assist/imageGenerationClient.d.ts.map +1 -1
  70. package/lib/packlets/ai-assist/imageGenerationClient.js +1 -1
  71. package/lib/packlets/ai-assist/imageGenerationClient.js.map +1 -1
  72. package/lib/packlets/ai-assist/index.d.ts +7 -2
  73. package/lib/packlets/ai-assist/index.d.ts.map +1 -1
  74. package/lib/packlets/ai-assist/index.js +11 -1
  75. package/lib/packlets/ai-assist/index.js.map +1 -1
  76. package/lib/packlets/ai-assist/model.d.ts +59 -288
  77. package/lib/packlets/ai-assist/model.d.ts.map +1 -1
  78. package/lib/packlets/ai-assist/model.js.map +1 -1
  79. package/lib/packlets/ai-assist/promptCacheAnthropicBreakpointHarness.d.ts +124 -0
  80. package/lib/packlets/ai-assist/promptCacheAnthropicBreakpointHarness.d.ts.map +1 -0
  81. package/lib/packlets/ai-assist/promptCacheAnthropicBreakpointHarness.js +208 -0
  82. package/lib/packlets/ai-assist/promptCacheAnthropicBreakpointHarness.js.map +1 -0
  83. package/lib/packlets/ai-assist/promptCacheObservabilityHarness.d.ts +77 -0
  84. package/lib/packlets/ai-assist/promptCacheObservabilityHarness.d.ts.map +1 -0
  85. package/lib/packlets/ai-assist/promptCacheObservabilityHarness.js +143 -0
  86. package/lib/packlets/ai-assist/promptCacheObservabilityHarness.js.map +1 -0
  87. package/lib/packlets/ai-assist/promptCacheRoutingAbHarness.d.ts +84 -0
  88. package/lib/packlets/ai-assist/promptCacheRoutingAbHarness.d.ts.map +1 -0
  89. package/lib/packlets/ai-assist/promptCacheRoutingAbHarness.js +163 -0
  90. package/lib/packlets/ai-assist/promptCacheRoutingAbHarness.js.map +1 -0
  91. package/lib/packlets/ai-assist/promptCacheUsageMath.d.ts +105 -0
  92. package/lib/packlets/ai-assist/promptCacheUsageMath.d.ts.map +1 -0
  93. package/lib/packlets/ai-assist/promptCacheUsageMath.js +130 -0
  94. package/lib/packlets/ai-assist/promptCacheUsageMath.js.map +1 -0
  95. package/lib/packlets/ai-assist/registry.d.ts.map +1 -1
  96. package/lib/packlets/ai-assist/registry.js +9 -11
  97. package/lib/packlets/ai-assist/registry.js.map +1 -1
  98. package/lib/packlets/ai-assist/streamUsageCapability.d.ts +119 -0
  99. package/lib/packlets/ai-assist/streamUsageCapability.d.ts.map +1 -0
  100. package/lib/packlets/ai-assist/streamUsageCapability.js +130 -0
  101. package/lib/packlets/ai-assist/streamUsageCapability.js.map +1 -0
  102. package/lib/packlets/ai-assist/streamingAdapters/anthropic.d.ts.map +1 -1
  103. package/lib/packlets/ai-assist/streamingAdapters/anthropic.js +23 -4
  104. package/lib/packlets/ai-assist/streamingAdapters/anthropic.js.map +1 -1
  105. package/lib/packlets/ai-assist/streamingAdapters/clientToolContinuationBuilder.d.ts +21 -12
  106. package/lib/packlets/ai-assist/streamingAdapters/clientToolContinuationBuilder.d.ts.map +1 -1
  107. package/lib/packlets/ai-assist/streamingAdapters/clientToolContinuationBuilder.js +64 -34
  108. package/lib/packlets/ai-assist/streamingAdapters/clientToolContinuationBuilder.js.map +1 -1
  109. package/lib/packlets/ai-assist/streamingAdapters/common.d.ts +21 -0
  110. package/lib/packlets/ai-assist/streamingAdapters/common.d.ts.map +1 -1
  111. package/lib/packlets/ai-assist/streamingAdapters/common.js +25 -1
  112. package/lib/packlets/ai-assist/streamingAdapters/common.js.map +1 -1
  113. package/lib/packlets/ai-assist/streamingAdapters/gemini.d.ts.map +1 -1
  114. package/lib/packlets/ai-assist/streamingAdapters/gemini.js +12 -5
  115. package/lib/packlets/ai-assist/streamingAdapters/gemini.js.map +1 -1
  116. package/lib/packlets/ai-assist/streamingAdapters/openaiChat.d.ts +1 -1
  117. package/lib/packlets/ai-assist/streamingAdapters/openaiChat.d.ts.map +1 -1
  118. package/lib/packlets/ai-assist/streamingAdapters/openaiChat.js +33 -7
  119. package/lib/packlets/ai-assist/streamingAdapters/openaiChat.js.map +1 -1
  120. package/lib/packlets/ai-assist/streamingAdapters/openaiResponses.d.ts +1 -1
  121. package/lib/packlets/ai-assist/streamingAdapters/openaiResponses.d.ts.map +1 -1
  122. package/lib/packlets/ai-assist/streamingAdapters/openaiResponses.js +21 -13
  123. package/lib/packlets/ai-assist/streamingAdapters/openaiResponses.js.map +1 -1
  124. package/lib/packlets/ai-assist/streamingClient.d.ts.map +1 -1
  125. package/lib/packlets/ai-assist/streamingClient.js +3 -2
  126. package/lib/packlets/ai-assist/streamingClient.js.map +1 -1
  127. package/lib/packlets/ai-assist/structuredOutput.d.ts +32 -0
  128. package/lib/packlets/ai-assist/structuredOutput.d.ts.map +1 -1
  129. package/lib/packlets/ai-assist/structuredOutput.js +85 -7
  130. package/lib/packlets/ai-assist/structuredOutput.js.map +1 -1
  131. package/lib/packlets/ai-assist/structuredOutputTypes.d.ts +30 -0
  132. package/lib/packlets/ai-assist/structuredOutputTypes.d.ts.map +1 -1
  133. package/lib/packlets/ai-assist/structuredOutputTypes.js.map +1 -1
  134. package/lib/packlets/ai-assist/thinkingOptionsResolver.d.ts.map +1 -1
  135. package/lib/packlets/ai-assist/thinkingOptionsResolver.js +18 -3
  136. package/lib/packlets/ai-assist/thinkingOptionsResolver.js.map +1 -1
  137. package/lib/packlets/ai-assist/toolFormats.d.ts +55 -1
  138. package/lib/packlets/ai-assist/toolFormats.d.ts.map +1 -1
  139. package/lib/packlets/ai-assist/toolFormats.js +82 -0
  140. package/lib/packlets/ai-assist/toolFormats.js.map +1 -1
  141. package/lib/packlets/ai-assist/toolTypes.d.ts +328 -0
  142. package/lib/packlets/ai-assist/toolTypes.d.ts.map +1 -0
  143. package/lib/packlets/ai-assist/toolTypes.js +22 -0
  144. package/lib/packlets/ai-assist/toolTypes.js.map +1 -0
  145. package/lib/packlets/ai-assist/usageNormalization.d.ts +58 -0
  146. package/lib/packlets/ai-assist/usageNormalization.d.ts.map +1 -0
  147. package/lib/packlets/ai-assist/usageNormalization.js +184 -0
  148. package/lib/packlets/ai-assist/usageNormalization.js.map +1 -0
  149. package/lib/packlets/ai-assist/usageTypes.d.ts +84 -0
  150. package/lib/packlets/ai-assist/usageTypes.d.ts.map +1 -0
  151. package/lib/packlets/ai-assist/usageTypes.js +22 -0
  152. package/lib/packlets/ai-assist/usageTypes.js.map +1 -0
  153. package/lib/packlets/mustache/index.d.ts +1 -1
  154. package/lib/packlets/mustache/index.d.ts.map +1 -1
  155. package/lib/packlets/mustache/index.js.map +1 -1
  156. package/lib/packlets/mustache/interfaces.d.ts +30 -0
  157. package/lib/packlets/mustache/interfaces.d.ts.map +1 -1
  158. package/lib/packlets/mustache/interfaces.js.map +1 -1
  159. package/lib/packlets/mustache/mustacheTemplate.d.ts +56 -1
  160. package/lib/packlets/mustache/mustacheTemplate.d.ts.map +1 -1
  161. package/lib/packlets/mustache/mustacheTemplate.js +91 -0
  162. package/lib/packlets/mustache/mustacheTemplate.js.map +1 -1
  163. package/package.json +16 -16
  164. package/perf/promptCacheAnthropicBreakpoint.js +135 -0
  165. package/perf/promptCacheObservability.js +107 -0
  166. package/perf/promptCacheRoutingAb.js +105 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fgv/ts-extras",
3
- "version": "5.1.0-54",
3
+ "version": "5.1.0-56",
4
4
  "description": "Assorted Typescript Utilities",
5
5
  "main": "lib/index.js",
6
6
  "types": "dist/ts-extras.d.ts",
@@ -69,10 +69,10 @@
69
69
  },
70
70
  "homepage": "https://github.com/ErikFortune/fgv/tree/main/libraries/ts-extras#ts-utils",
71
71
  "devDependencies": {
72
- "@jest/expect-utils": "^29.7.0",
72
+ "@jest/expect-utils": "^30.5.2",
73
73
  "@microsoft/api-documenter": "^7.28.2",
74
74
  "@microsoft/api-extractor": "^7.55.2",
75
- "@types/jest": "^29.5.14",
75
+ "@types/jest": "^30.0.0",
76
76
  "@types/mustache": "^4.2.5",
77
77
  "@types/node": "^20.14.9",
78
78
  "@types/papaparse": "^5.3.14",
@@ -82,28 +82,28 @@
82
82
  "eslint-plugin-import": "^2.32.0",
83
83
  "eslint-plugin-node": "^11.1.0",
84
84
  "eslint-plugin-promise": "^7.2.1",
85
- "jest": "^29.7.0",
85
+ "jest": "^30.5.2",
86
86
  "jest-extended": "^4.0.2",
87
- "jest-matcher-utils": "^29.7.0",
87
+ "jest-matcher-utils": "^30.5.2",
88
88
  "rimraf": "^6.1.2",
89
- "ts-jest": "^29.4.6",
89
+ "ts-jest": "^29.4.12",
90
90
  "ts-node": "^10.9.2",
91
91
  "typescript": "5.9.3",
92
92
  "eslint-plugin-n": "^17.23.1",
93
- "jest-snapshot": "~29.7.0",
94
- "@rushstack/heft": "1.2.7",
95
- "@rushstack/heft-node-rig": "2.11.27",
93
+ "jest-snapshot": "~30.5.2",
94
+ "@rushstack/heft": "1.3.0",
95
+ "@rushstack/heft-node-rig": "2.11.50",
96
96
  "@rushstack/eslint-config": "4.6.4",
97
97
  "@types/heft-jest": "1.0.6",
98
- "@rushstack/heft-jest-plugin": "1.2.6",
98
+ "@rushstack/heft-jest-plugin": "2.0.17",
99
99
  "eslint-plugin-tsdoc": "~0.5.2",
100
100
  "@types/js-yaml": "~4.0.9",
101
101
  "typedoc": "~0.28.16",
102
102
  "typedoc-plugin-markdown": "~4.9.0",
103
- "@fgv/typedoc-compact-theme": "5.1.0-54",
104
- "@fgv/heft-dual-rig": "5.1.0-54",
105
- "@fgv/ts-utils-jest": "5.1.0-54",
106
- "@fgv/ts-utils": "5.1.0-54"
103
+ "@fgv/heft-dual-rig": "5.1.0-56",
104
+ "@fgv/typedoc-compact-theme": "5.1.0-56",
105
+ "@fgv/ts-utils": "5.1.0-56",
106
+ "@fgv/ts-utils-jest": "5.1.0-56"
107
107
  },
108
108
  "dependencies": {
109
109
  "@types/luxon": "^3.7.1",
@@ -112,10 +112,10 @@
112
112
  "papaparse": "^5.4.1",
113
113
  "fflate": "~0.8.2",
114
114
  "js-yaml": "~4.1.1",
115
- "@fgv/ts-json-base": "5.1.0-54"
115
+ "@fgv/ts-json-base": "5.1.0-56"
116
116
  },
117
117
  "peerDependencies": {
118
- "@fgv/ts-utils": "5.1.0-54"
118
+ "@fgv/ts-utils": "5.1.0-56"
119
119
  },
120
120
  "repository": {
121
121
  "type": "git",
@@ -0,0 +1,135 @@
1
+ /*
2
+ * Copyright (c) 2026 Erik Fortune
3
+ * SPDX-License-Identifier: MIT
4
+ *
5
+ * A/B for whether C3's Anthropic prompt-cache breakpoint emission (IAiCacheRequest.systemBreakpoints
6
+ * -> `cache_control`) actually produces a cache hit against a live provider.
7
+ *
8
+ * Every prompt-cache measurement shipped by this stream so far (promptCacheObservability.js,
9
+ * promptCacheRoutingAb.js) ran against xAI, whose caching is automatic — no directive is sent, so
10
+ * neither harness exercises C3's emit path at all. Anthropic is the sharp case: its cache is
11
+ * EXPLICIT OPT-IN PER CONTENT BLOCK. There is no automatic path, so a breakpoint is the only thing
12
+ * that can produce a hit. If C3's Anthropic emission is wrong, nothing else in the repo would show
13
+ * it — this is the first live exercise of that code path.
14
+ *
15
+ * ANTHROPIC_API_KEY=... node perf/promptCacheAnthropicBreakpoint.js
16
+ * SALT=run2 ANTHROPIC_API_KEY=... node perf/promptCacheAnthropicBreakpoint.js # see "traps" below
17
+ *
18
+ * Requires a built lib/ (`rushx build` first). Deliberately NOT a jest test: live traffic and a
19
+ * bill do not belong behind CI's green (TESTING_GUIDELINES.md § Measurement Harnesses).
20
+ *
21
+ * The prefix builder, the ratio arithmetic, and the CONFIRMS/MISSES/AMBIGUOUS/inconclusive verdict
22
+ * below all live in `src/packlets/ai-assist/promptCacheAnthropicBreakpointHarness.ts`, reusing
23
+ * `promptCacheUsageMath.ts`'s shared comparison rather than re-deriving it, and unit-tested against
24
+ * fixture usage blocks in `src/test/unit/ai-assist/promptCacheAnthropicBreakpointHarness.test.ts` —
25
+ * this file is a thin wrapper that supplies the live network transport and prints the result.
26
+ *
27
+ * ---------------------------------------------------------------------------
28
+ * TARGET MODEL AND FIXTURE SIZING
29
+ *
30
+ * Target: `claude-sonnet-5` — this provider's default `'base'` tier (registry.ts), the same tier
31
+ * this stream's other harnesses default to. design.md §7's verified-minimum table gives Sonnet-class
32
+ * models (including claude-sonnet-5) a 1,024-token minimum cacheable prefix. Anthropic's minimum is
33
+ * non-monotonic across the model family (Haiku-class models need 4,096), so this harness's fixture
34
+ * is sized well above BOTH floors rather than just the one this model needs: the 400-line generator
35
+ * this file shares with promptCacheObservability.js / promptCacheRoutingAb.js measured ~8,700 tokens
36
+ * for a comparable prefix earlier in this stream. A fixture below the minimum would produce a real
37
+ * miss that means nothing about C3 — this one is not close to that line.
38
+ *
39
+ * ---------------------------------------------------------------------------
40
+ * WHAT IT COMPARES
41
+ *
42
+ * Both arms send the same large system prefix twice (cold, then warm) and vary only the final user
43
+ * turn — what a real caller does. The arms differ in exactly one thing: whether `cache.systemBreakpoints`
44
+ * is supplied, marking almost the entire prefix as a single cacheable block.
45
+ *
46
+ * - arm 1 (WITHOUT systemBreakpoints) — the negative control. Anthropic caching is opt-in per
47
+ * block; with no `cache_control` sent, nothing should be written or read, on either call.
48
+ * - arm 2 (WITH systemBreakpoints) — the cold call should show a cache WRITE
49
+ * (`cacheWriteTokens` > 0, `cachedInputTokens` ~ 0); the warm call should show a cache READ
50
+ * (`cachedInputTokens` clearing roughly the size of the cached prefix).
51
+ *
52
+ * `usage.reports` should be `'reads-and-writes'` on every call here — unlike xAI, which this
53
+ * stream's other harnesses measured as `'reads'`-only on both of its routes. Anthropic's normalizer
54
+ * (`normalizeAnthropicUsage`) reports both fields unconditionally.
55
+ *
56
+ * ---------------------------------------------------------------------------
57
+ * THE PREDICTION, WRITTEN DOWN BEFORE THE FIRST RUN
58
+ * (TESTING_GUIDELINES.md § "State the prediction before running it")
59
+ *
60
+ * - arm 1 cold and warm — `cachedInputTokens` and `cacheWriteTokens` both absent or 0 on both
61
+ * calls. No directive was sent, so nothing should be written or read.
62
+ * - arm 2 cold — `cacheWriteTokens` > 0 (writing the ~8,700-token prefix into the
63
+ * cache), `cachedInputTokens` near 0 (nothing to read yet on a first-ever write).
64
+ * - arm 2 warm — `cachedInputTokens` clears roughly the prefix size — high, not a small
65
+ * fixed floor the way xAI's automatic cache showed a ~192-token floor even on a cold call in
66
+ * this stream's earlier runs. Anthropic's cache is explicit, so there is no equivalent
67
+ * scaffolding-cost floor predicted here.
68
+ *
69
+ * A miss means C3's Anthropic emission is wrong, or this design's model of Anthropic caching is
70
+ * wrong. The response is to revise the design or the code — NOT to lower a threshold or re-run
71
+ * until a favourable number appears.
72
+ *
73
+ * ---------------------------------------------------------------------------
74
+ * TRAPS THAT MAKE A SINGLE RUN MISLEADING (carried over from promptCacheRoutingAb.js, and still
75
+ * live even though Anthropic's cache mechanism differs from xAI's per-server routing)
76
+ *
77
+ * 1. Re-running within the cache TTL taints the cold reading, because the "cold" call would hit a
78
+ * prefix a previous run already wrote. Pass a fresh SALT (or wait out the TTL, ~5 minutes on
79
+ * Anthropic's default ephemeral cache) — each run's prefix is salted so re-runs cannot warm a
80
+ * prior run's cache, and the two arms are independently salted so they cannot warm each other.
81
+ *
82
+ * 2. A failed call must not print a ratio. If either arm's cold or warm call fails outright, the
83
+ * verdict reports 'inconclusive' rather than fabricating a 0% or 100% from missing data — see
84
+ * `runPromptCacheAnthropicBreakpointHarness`'s propagated failures and
85
+ * `compareCacheHitRatios`'s `undefined` handling in `promptCacheUsageMath.ts`.
86
+ * ---------------------------------------------------------------------------
87
+ */
88
+
89
+ /* eslint-disable no-console */
90
+
91
+ const { AiAssist } = require('../lib/index');
92
+ const {
93
+ describeAnthropicBreakpointUsage,
94
+ formatAnthropicBreakpointVerdict,
95
+ runPromptCacheAnthropicBreakpointHarness
96
+ } = require('../lib/packlets/ai-assist/promptCacheAnthropicBreakpointHarness');
97
+
98
+ const API_KEY = process.env.ANTHROPIC_API_KEY;
99
+ if (!API_KEY) {
100
+ console.error('ANTHROPIC_API_KEY is required. See the file header for usage.');
101
+ process.exit(1);
102
+ }
103
+ const SALT = process.env.SALT ?? 'run1';
104
+
105
+ async function main() {
106
+ console.log(`=== Anthropic prompt-cache breakpoint A/B (salt=${SALT}) ===`);
107
+ console.log('Both arms vary only the final user turn. They differ only in cache.systemBreakpoints.');
108
+
109
+ const descriptor = AiAssist.getProviderDescriptor('anthropic').orThrow();
110
+
111
+ const result = await runPromptCacheAnthropicBreakpointHarness(
112
+ { callCompletion: AiAssist.callProviderCompletion },
113
+ { descriptor, apiKey: API_KEY, salt: SALT }
114
+ );
115
+ if (result.isFailure()) {
116
+ console.error(result.message);
117
+ process.exit(1);
118
+ }
119
+
120
+ const { value } = result;
121
+ console.log(`\n--- ${value.withoutBreakpoint.label} ---`);
122
+ console.log(describeAnthropicBreakpointUsage(' cold', value.withoutBreakpoint.coldUsage));
123
+ console.log(describeAnthropicBreakpointUsage(' warm', value.withoutBreakpoint.warmUsage));
124
+ console.log(`\n--- ${value.withBreakpoint.label} ---`);
125
+ console.log(describeAnthropicBreakpointUsage(' cold', value.withBreakpoint.coldUsage));
126
+ console.log(describeAnthropicBreakpointUsage(' warm', value.withBreakpoint.warmUsage));
127
+
128
+ console.log('\n--- verdict ---');
129
+ console.log(formatAnthropicBreakpointVerdict(value));
130
+ }
131
+
132
+ main().catch((e) => {
133
+ console.error(e);
134
+ process.exit(1);
135
+ });
@@ -0,0 +1,107 @@
1
+ /*
2
+ * Copyright (c) 2026 Erik Fortune
3
+ * SPDX-License-Identifier: MIT
4
+ *
5
+ * Standing assertion for C1 of `ai-assist-prompt-caching` (design.md §8).
6
+ *
7
+ * Deliberately NOT a jest test. It is a live-traffic measurement, not a behavior —
8
+ * putting it in the suite would put CI's runtime and its green/red behind a network
9
+ * call and a bill (see TESTING_GUIDELINES.md § "Measurement Harnesses"). It lives
10
+ * here so it can be run on demand and its output pasted into the stream's
11
+ * `result.md`.
12
+ *
13
+ * XAI_API_KEY=... node perf/promptCacheObservability.js
14
+ *
15
+ * Requires a built `lib/` (`rushx build` first).
16
+ *
17
+ * The ratio arithmetic, the cold/warm comparison, and the prediction verdict below all live in
18
+ * `src/packlets/ai-assist/promptCacheObservabilityHarness.ts`, unit-tested against fixture usage
19
+ * blocks in `src/test/unit/ai-assist/promptCacheObservabilityHarness.test.ts` — this file is a
20
+ * thin wrapper that supplies the live network transport and prints the result.
21
+ *
22
+ * ---------------------------------------------------------------------------
23
+ * WHY xAI, NOT ANTHROPIC (corrected 2026-09-19 — the previous version of this note argued from a
24
+ * premise C3 has since retired; see below for what changed):
25
+ *
26
+ * xAI (and Gemini, and OpenAI's default mode) cache automatically with no request-side opt-in —
27
+ * confirmed live against xAI in this stream's design phase (design.md §12, OQ-1, the
28
+ * `xai-cache-probe` testbed scenario). A repeat request against xAI is therefore a valid test of
29
+ * whether C1's *reading* of the usage block is correct, on a provider whose cache needs no
30
+ * cooperation from the request this harness sends.
31
+ *
32
+ * Anthropic's prompt cache, by contrast, is opt-in per content block via `cache_control`, and
33
+ * this harness sends no cache directive of any kind — it calls `AiAssist.callProviderCompletion`
34
+ * with a plain `system` string and no `cache` field. That is no longer because "C1 sends no cache
35
+ * directive" (the reason this note originally gave): **C3 shipped and does emit `cache_control`
36
+ * when a caller supplies `IAiCacheRequest.systemBreakpoints`** — this harness simply doesn't pass
37
+ * one. So an Anthropic leg is possible now, in a way it structurally was not when this note was
38
+ * first written; it would need `IAiCacheRequest.systemBreakpoints` set on the request. It has
39
+ * never been run and is out of scope for this change — see design.md §8's addendum for what is
40
+ * and is not claimed here.
41
+ * ---------------------------------------------------------------------------
42
+ *
43
+ * THE PREDICTION, WRITTEN DOWN BEFORE THE FIRST RUN (per TESTING_GUIDELINES.md
44
+ * § "State the prediction before running it"), calibrated to the live numbers
45
+ * design.md §12 already recorded for this exact provider:
46
+ *
47
+ * Two back-to-back requests share one large, byte-identical system-prompt
48
+ * prefix and differ only in their final user turn.
49
+ *
50
+ * - `usage.reports` should be `'reads'` on both calls — xAI never reports
51
+ * cache writes on either of its routes (design.md §12, OQ-1).
52
+ * - The FIRST (cold) call may still show a small nonzero
53
+ * `usage.cachedInputTokens` — design.md §12 recorded a **128-token floor**
54
+ * on a genuinely cold call against a prefix xAI had never seen, most
55
+ * plausibly fixed chat-template scaffolding. A nonzero cold figure is
56
+ * therefore NOT itself evidence of a bug; judge by the ratio, not the raw
57
+ * count (design.md §9's explicit warning).
58
+ * - The SECOND (warm) call should show `usage.cachedInputTokens` at
59
+ * roughly **99%** of `usage.uncachedInputTokens + usage.cachedInputTokens`
60
+ * for that call — design.md §12 measured 99.5% (Chat Completions) and
61
+ * 99.2% (Responses) on the same prefix-caching mechanism this harness
62
+ * exercises.
63
+ *
64
+ * A miss (cold and warm ratios roughly equal, or `reports` not `'reads'`)
65
+ * means the C1 normalization is not doing what the design claims — the
66
+ * response is to revise the design or the code, not to lower a threshold
67
+ * until this harness goes green.
68
+ * ---------------------------------------------------------------------------
69
+ */
70
+
71
+ /* eslint-disable no-console */
72
+
73
+ const { AiAssist } = require('../lib/index');
74
+ const {
75
+ describeObservabilityUsage,
76
+ runPromptCacheObservabilityHarness
77
+ } = require('../lib/packlets/ai-assist/promptCacheObservabilityHarness');
78
+
79
+ const API_KEY = process.env.XAI_API_KEY;
80
+ if (!API_KEY) {
81
+ console.error('XAI_API_KEY is required. See the file header for usage.');
82
+ process.exit(1);
83
+ }
84
+
85
+ async function main() {
86
+ const descriptor = AiAssist.getProviderDescriptor('xai-grok').orThrow();
87
+
88
+ const result = await runPromptCacheObservabilityHarness(
89
+ { callCompletion: AiAssist.callProviderCompletion },
90
+ descriptor,
91
+ API_KEY
92
+ );
93
+ if (result.isFailure()) {
94
+ console.error(result.message);
95
+ process.exit(1);
96
+ }
97
+
98
+ const { value } = result;
99
+ console.log(describeObservabilityUsage('cold call ', value.cold));
100
+ console.log(describeObservabilityUsage('warm call ', value.warm));
101
+ console.log(`\nverdict: ${value.verdict.toUpperCase()} — ${value.detail}`);
102
+ }
103
+
104
+ main().catch((e) => {
105
+ console.error(e);
106
+ process.exit(1);
107
+ });
@@ -0,0 +1,105 @@
1
+ /*
2
+ * Copyright (c) 2026 Erik Fortune
3
+ * SPDX-License-Identifier: MIT
4
+ *
5
+ * A/B for the xAI prompt-cache ROUTING key (IAiCacheRequest.cacheKey).
6
+ *
7
+ * Sibling to promptCacheObservability.js, which measures whether C1 *reads* the usage block
8
+ * correctly. This one measures whether the routing key actually *buys* anything — the claim
9
+ * this branch ships and the one thing its request-shape tests cannot check.
10
+ *
11
+ * XAI_API_KEY=... node perf/promptCacheRoutingAb.js
12
+ * SALT=run2 XAI_API_KEY=... node perf/promptCacheRoutingAb.js # see "second trap" below
13
+ *
14
+ * Requires a built lib/ (`rushx build` first). Deliberately NOT a jest test: live traffic and a
15
+ * bill do not belong behind CI's green (TESTING_GUIDELINES.md § Measurement Harnesses).
16
+ *
17
+ * The prefix builder, the ratio arithmetic, and the CONFIRMS/MISSES/AMBIGUOUS verdict below all
18
+ * live in `src/packlets/ai-assist/promptCacheRoutingAbHarness.ts`, unit-tested against fixture
19
+ * usage blocks in `src/test/unit/ai-assist/promptCacheRoutingAbHarness.test.ts` — this file is a
20
+ * thin wrapper that supplies the live network transport and prints the result.
21
+ *
22
+ * ---------------------------------------------------------------------------
23
+ * WHAT IT COMPARES
24
+ *
25
+ * Both arms send the same large system prefix twice and vary only the final user turn — which
26
+ * is what a real caller does, and what the plain observability harness showed returns ~2%
27
+ * cached. The arms differ in exactly one thing: whether a stable `cache.cacheKey` is supplied.
28
+ *
29
+ * xAI matches byte-for-byte from the start of the messages array, but its cache is PER-SERVER
30
+ * and evictable, so two requests sharing a prefix can still miss when routed to different
31
+ * boxes. The key (x-grok-conv-id on Chat Completions) pins them together.
32
+ *
33
+ * ---------------------------------------------------------------------------
34
+ * THE PREDICTION, WRITTEN DOWN BEFORE THE FIRST RUN
35
+ * (TESTING_GUIDELINES.md § "State the prediction before running it")
36
+ *
37
+ * - arm 1 (no key) — warm stays near the ~2% floor, matching the observability harness's
38
+ * measured 192-of-8876 on this exact shape.
39
+ * - arm 2 (key) — warm jumps toward ~99%, matching what the OQ-1 probe gets from
40
+ * byte-identical requests.
41
+ *
42
+ * A miss means the routing key does not do what this branch claims. The response is to revise
43
+ * the change or the claim — NOT to re-run until a favourable number appears.
44
+ *
45
+ * ---------------------------------------------------------------------------
46
+ * TWO TRAPS THAT MAKE A SINGLE RUN MISLEADING
47
+ *
48
+ * 1. A hit in arm 1 is NOT a refutation. Without a routing key you can still land on the same
49
+ * server by luck — that non-determinism is the whole reason the key exists. The meaningful
50
+ * negative result is arm 2 FAILING to jump, not arm 1 happening to succeed.
51
+ *
52
+ * 2. Re-running within the cache TTL taints the cold reading, because the "cold" call hits a
53
+ * prefix the previous run already warmed. Pass a fresh SALT (or wait out the TTL) — each arm
54
+ * already gets its own salted prefix so the arms cannot warm each other, which is the
55
+ * mistake the OQ-1 probe had to fix in its own first version.
56
+ * ---------------------------------------------------------------------------
57
+ */
58
+
59
+ /* eslint-disable no-console */
60
+
61
+ const { AiAssist } = require('../lib/index');
62
+ const {
63
+ describeRoutingAbUsage,
64
+ formatRoutingAbVerdict,
65
+ runPromptCacheRoutingAbHarness
66
+ } = require('../lib/packlets/ai-assist/promptCacheRoutingAbHarness');
67
+
68
+ const API_KEY = process.env.XAI_API_KEY;
69
+ if (!API_KEY) {
70
+ console.error('XAI_API_KEY is required. See the file header for usage.');
71
+ process.exit(1);
72
+ }
73
+ const SALT = process.env.SALT ?? 'run1';
74
+
75
+ async function main() {
76
+ console.log(`=== xAI prompt-cache routing A/B (salt=${SALT}) ===`);
77
+ console.log('Both arms vary only the final user turn. They differ only in the routing key.');
78
+
79
+ const descriptor = AiAssist.getProviderDescriptor('xai-grok').orThrow();
80
+
81
+ const result = await runPromptCacheRoutingAbHarness(
82
+ { callCompletion: AiAssist.callProviderCompletion },
83
+ { descriptor, apiKey: API_KEY, salt: SALT }
84
+ );
85
+ if (result.isFailure()) {
86
+ console.error(result.message);
87
+ process.exit(1);
88
+ }
89
+
90
+ const { value } = result;
91
+ console.log(`\n--- ${value.withoutKey.label} ---`);
92
+ console.log(` cold: ${describeRoutingAbUsage(value.withoutKey.coldUsage)}`);
93
+ console.log(` warm: ${describeRoutingAbUsage(value.withoutKey.warmUsage)}`);
94
+ console.log(`\n--- ${value.withKey.label} ---`);
95
+ console.log(` cold: ${describeRoutingAbUsage(value.withKey.coldUsage)}`);
96
+ console.log(` warm: ${describeRoutingAbUsage(value.withKey.warmUsage)}`);
97
+
98
+ console.log('\n--- verdict ---');
99
+ console.log(formatRoutingAbVerdict(value));
100
+ }
101
+
102
+ main().catch((e) => {
103
+ console.error(e);
104
+ process.exit(1);
105
+ });