@vellumai/assistant 0.11.0-dev.202607302128.ff52e0f → 0.11.0-dev.202607302226.af83155

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vellumai/assistant",
3
- "version": "0.11.0-dev.202607302128.ff52e0f",
3
+ "version": "0.11.0-dev.202607302226.af83155",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "exports": {
@@ -8,6 +8,9 @@
8
8
  * `messages`; any other stop reason → stop; non-main-agent call sites →
9
9
  * stop; a provider rejection → stop; an entirely-stripped (empty) truncated
10
10
  * turn → stop.
11
+ * - Which continuation nudge the hook picks: a truncated turn that emitted
12
+ * only thinking gets the thinking-only nudge; one that emitted partial
13
+ * output keeps the generic continuation.
11
14
  * - The per-run budget is split across the two hooks: `post-model-call`
12
15
  * consumes one unit per continue (up to `MAX_TOKENS_AUTO_CONTINUES`) and
13
16
  * the `stop` hook clears the counter on the definitive terminal so the next
@@ -33,6 +36,7 @@ import {
33
36
  } from "../plugins/defaults/max-tokens-continue/continue-state-store.js";
34
37
  import postModelCall, {
35
38
  MAX_TOKENS_CONTINUE_NUDGE_TEXT,
39
+ MAX_TOKENS_THINKING_ONLY_NUDGE_TEXT,
36
40
  } from "../plugins/defaults/max-tokens-continue/hooks/post-model-call.js";
37
41
  import stop from "../plugins/defaults/max-tokens-continue/hooks/stop.js";
38
42
  import type { ContentBlock } from "../providers/types.js";
@@ -51,6 +55,20 @@ const truncatedText: ContentBlock = {
51
55
  text: "Here is the start of a very long answer that got cut o",
52
56
  };
53
57
 
58
+ const thinkingBlock: ContentBlock = {
59
+ type: "thinking",
60
+ thinking: "Writing SVG code... Writing component markup...",
61
+ signature: "sig-abc",
62
+ };
63
+
64
+ /** The nudge text the hook appended after the truncated turn. */
65
+ function appendedNudgeText(ctx: PostModelCallContext): unknown {
66
+ const nudge = ctx.messages[ctx.messages.length - 1];
67
+ return nudge?.content[0] && "text" in nudge.content[0]
68
+ ? nudge.content[0].text
69
+ : undefined;
70
+ }
71
+
54
72
  function makeCtx(
55
73
  overrides: Partial<PostModelCallContext> = {},
56
74
  ): PostModelCallContext {
@@ -89,6 +107,24 @@ describe("max-tokens-continue NUDGE_TEXT — internal-notice suppression", () =>
89
107
  "Continue exactly where you stopped",
90
108
  );
91
109
  });
110
+
111
+ test("the thinking-only nudge names the failure and demands output now", () => {
112
+ expect(MAX_TOKENS_THINKING_ONLY_NUDGE_TEXT).toContain(
113
+ INTERNAL_NUDGE_OUTPUT_SUPPRESSION,
114
+ );
115
+ expect(
116
+ MAX_TOKENS_THINKING_ONLY_NUDGE_TEXT.startsWith("<system_notice>"),
117
+ ).toBe(true);
118
+ expect(
119
+ MAX_TOKENS_THINKING_ONLY_NUDGE_TEXT.endsWith("</system_notice>"),
120
+ ).toBe(true);
121
+ expect(MAX_TOKENS_THINKING_ONLY_NUDGE_TEXT).toContain(
122
+ "spent its entire output budget inside thinking",
123
+ );
124
+ expect(MAX_TOKENS_THINKING_ONLY_NUDGE_TEXT).toContain(
125
+ "Do not resume planning",
126
+ );
127
+ });
92
128
  });
93
129
 
94
130
  describe("max-tokens-continue post-model-call hook", () => {
@@ -117,6 +153,90 @@ describe("max-tokens-continue post-model-call hook", () => {
117
153
  });
118
154
  });
119
155
 
156
+ test("thinking-only truncated turn → continue with the thinking-only nudge", async () => {
157
+ /**
158
+ * Tests that a turn which burned its whole budget inside thinking is told
159
+ * so, rather than being invited to resume drafting in thought.
160
+ */
161
+ // GIVEN a truncated turn carrying only a thinking block.
162
+ const ctx = makeCtx({ content: [thinkingBlock] });
163
+
164
+ // WHEN the hook runs.
165
+ await postModelCall(ctx);
166
+
167
+ // THEN it continues with the thinking-only nudge.
168
+ expect(ctx.decision).toBe("continue");
169
+ expect(appendedNudgeText(ctx)).toBe(MAX_TOKENS_THINKING_ONLY_NUDGE_TEXT);
170
+ });
171
+
172
+ test("thinking plus whitespace-only text → thinking-only nudge", async () => {
173
+ /**
174
+ * Tests that a blank text block does not count as emitted output.
175
+ */
176
+ // GIVEN a truncated turn whose only text block is whitespace.
177
+ const ctx = makeCtx({
178
+ content: [thinkingBlock, { type: "text", text: " \n" }],
179
+ });
180
+
181
+ // WHEN the hook runs.
182
+ await postModelCall(ctx);
183
+
184
+ // THEN it still uses the thinking-only nudge.
185
+ expect(ctx.decision).toBe("continue");
186
+ expect(appendedNudgeText(ctx)).toBe(MAX_TOKENS_THINKING_ONLY_NUDGE_TEXT);
187
+ });
188
+
189
+ test("thinking plus partial text → generic continuation nudge", async () => {
190
+ /**
191
+ * Tests that a turn which did emit output keeps the resume-where-you-left
192
+ * instruction.
193
+ */
194
+ // GIVEN a truncated turn that emitted thinking and partial text.
195
+ const ctx = makeCtx({ content: [thinkingBlock, truncatedText] });
196
+
197
+ // WHEN the hook runs.
198
+ await postModelCall(ctx);
199
+
200
+ // THEN it continues with the generic nudge.
201
+ expect(ctx.decision).toBe("continue");
202
+ expect(appendedNudgeText(ctx)).toBe(MAX_TOKENS_CONTINUE_NUDGE_TEXT);
203
+ });
204
+
205
+ test("thinking plus a tool call → generic continuation nudge", async () => {
206
+ /**
207
+ * Tests that a tool call counts as emitted output.
208
+ */
209
+ // GIVEN a truncated turn carrying thinking and a tool call.
210
+ const ctx = makeCtx({
211
+ content: [
212
+ thinkingBlock,
213
+ { type: "tool_use", id: "tool-1", name: "write_file", input: {} },
214
+ ],
215
+ });
216
+
217
+ // WHEN the hook runs.
218
+ await postModelCall(ctx);
219
+
220
+ // THEN it continues with the generic nudge.
221
+ expect(ctx.decision).toBe("continue");
222
+ expect(appendedNudgeText(ctx)).toBe(MAX_TOKENS_CONTINUE_NUDGE_TEXT);
223
+ });
224
+
225
+ test("thinking-only turn on a non-max-tokens stop → untouched", async () => {
226
+ /**
227
+ * Tests that the nudge selection never fires outside a max_tokens stop.
228
+ */
229
+ // GIVEN a thinking-only turn that ended normally.
230
+ const ctx = makeCtx({ content: [thinkingBlock], stopReason: "end_turn" });
231
+
232
+ // WHEN the hook runs.
233
+ await postModelCall(ctx);
234
+
235
+ // THEN nothing is appended.
236
+ expect(ctx.decision).toBe("stop");
237
+ expect(ctx.messages).toHaveLength(1);
238
+ });
239
+
120
240
  test("non-max-tokens stop reason → stop", async () => {
121
241
  /**
122
242
  * Tests that ordinary stop reasons are left alone.
@@ -834,8 +834,8 @@ describe("Anthropic models on OpenRouter", () => {
834
834
  });
835
835
 
836
836
  expect(result.pricingStatus).toBe("priced");
837
- // 0.05M x $1 direct + 0.05M x $1.25 write + 0.05M x $0.1 read
838
- expect(result.estimatedCostUsd).toBeCloseTo(0.05 + 0.0625 + 0.005, 10);
837
+ // 0.05M x $0.1 direct + 0.05M x $0.125 write + 0.05M x $0.01 read
838
+ expect(result.estimatedCostUsd).toBeCloseTo(0.005 + 0.00625 + 0.0005, 10);
839
839
  });
840
840
 
841
841
  test("returns unpriced for unknown non-Anthropic OpenRouter model", () => {
@@ -28,9 +28,16 @@
28
28
  * empty remainder leaves nothing to resume from, so the hook lets the turn
29
29
  * end rather than pushing an empty assistant message the provider would
30
30
  * reject.
31
+ *
32
+ * The nudge is chosen from the shape of the truncated reply. A reply that
33
+ * emitted partial output resumes it; a reply that spent the whole budget
34
+ * inside thinking and emitted nothing gets told so explicitly, because a
35
+ * generic "continue where you stopped" lets the model resume drafting in
36
+ * thought and stall the same way on every resume.
31
37
  */
32
38
 
33
39
  import {
40
+ type ContentBlock,
34
41
  type HookFunction,
35
42
  INTERNAL_NUDGE_OUTPUT_SUPPRESSION,
36
43
  isMaxTokensStopReason,
@@ -51,6 +58,35 @@ export const MAX_TOKENS_CONTINUE_NUDGE_TEXT =
51
58
  INTERNAL_NUDGE_OUTPUT_SUPPRESSION +
52
59
  "</system_notice>";
53
60
 
61
+ /**
62
+ * Continuation nudge for a reply that reached the output limit having emitted
63
+ * only thinking. Shown to the LLM, not the user.
64
+ */
65
+ export const MAX_TOKENS_THINKING_ONLY_NUDGE_TEXT =
66
+ "<system_notice>Your previous response spent its entire output budget inside thinking and emitted nothing: no tool call, no text. Do not resume planning and do not restate the plan. Produce the tool call or the reply text now, composing any remaining content directly in the output instead of in thought." +
67
+ INTERNAL_NUDGE_OUTPUT_SUPPRESSION +
68
+ "</system_notice>";
69
+
70
+ /**
71
+ * Whether the truncated reply is thinking and nothing else: at least one
72
+ * thinking block, and no block carrying output (a tool call, non-empty text,
73
+ * or any other emitted content).
74
+ */
75
+ function isThinkingOnlyStall(content: ContentBlock[]): boolean {
76
+ let sawThinking = false;
77
+ for (const block of content) {
78
+ if (block.type === "thinking" || block.type === "redacted_thinking") {
79
+ sawThinking = true;
80
+ continue;
81
+ }
82
+ if (block.type === "text" && block.text.trim().length === 0) {
83
+ continue;
84
+ }
85
+ return false;
86
+ }
87
+ return sawThinking;
88
+ }
89
+
54
90
  const postModelCall: HookFunction<PostModelCallContext> = async (ctx) => {
55
91
  if (ctx.error) {
56
92
  return;
@@ -75,6 +111,7 @@ const postModelCall: HookFunction<PostModelCallContext> = async (ctx) => {
75
111
  return;
76
112
  }
77
113
 
114
+ const thinkingOnly = isThinkingOnlyStall(ctx.content);
78
115
  consumeMaxTokensContinueBudget(ctx.conversationId);
79
116
  ctx.messages.push({
80
117
  role: "assistant",
@@ -82,11 +119,22 @@ const postModelCall: HookFunction<PostModelCallContext> = async (ctx) => {
82
119
  });
83
120
  ctx.messages.push({
84
121
  role: "user",
85
- content: [{ type: "text", text: MAX_TOKENS_CONTINUE_NUDGE_TEXT }],
122
+ content: [
123
+ {
124
+ type: "text",
125
+ text: thinkingOnly
126
+ ? MAX_TOKENS_THINKING_ONLY_NUDGE_TEXT
127
+ : MAX_TOKENS_CONTINUE_NUDGE_TEXT,
128
+ },
129
+ ],
86
130
  });
87
131
  ctx.decision = "continue";
88
132
  ctx.logger.warn(
89
- { plugin: "max-tokens-continue", conversationId: ctx.conversationId },
133
+ {
134
+ plugin: "max-tokens-continue",
135
+ conversationId: ctx.conversationId,
136
+ thinkingOnly,
137
+ },
90
138
  "Turn truncated at the output token limit — auto-continuing",
91
139
  );
92
140
  };
@@ -416,17 +416,17 @@ const RAW_PROVIDER_CATALOG: ProviderCatalogEntry[] = [
416
416
  supportsToolUse: true,
417
417
  supportsPromptCacheBreakpoints: true,
418
418
  pricing: {
419
- inputPer1mTokens: 2.5,
420
- outputPer1mTokens: 15.0,
421
- cacheWritePer1mTokens: 3.125,
422
- cacheReadPer1mTokens: 0.25,
419
+ inputPer1mTokens: 2.0,
420
+ outputPer1mTokens: 12.0,
421
+ cacheWritePer1mTokens: 2.5,
422
+ cacheReadPer1mTokens: 0.2,
423
423
  tiers: [
424
424
  {
425
425
  inputTokenThreshold: OPENAI_LONG_CONTEXT_PRICING_THRESHOLD_TOKENS,
426
- inputPer1mTokens: 5,
427
- outputPer1mTokens: 22.5,
428
- cacheWritePer1mTokens: 6.25,
429
- cacheReadPer1mTokens: 0.5,
426
+ inputPer1mTokens: 4,
427
+ outputPer1mTokens: 18,
428
+ cacheWritePer1mTokens: 5,
429
+ cacheReadPer1mTokens: 0.4,
430
430
  },
431
431
  ],
432
432
  },
@@ -444,17 +444,17 @@ const RAW_PROVIDER_CATALOG: ProviderCatalogEntry[] = [
444
444
  supportsToolUse: true,
445
445
  supportsPromptCacheBreakpoints: true,
446
446
  pricing: {
447
- inputPer1mTokens: 1.0,
448
- outputPer1mTokens: 6.0,
449
- cacheWritePer1mTokens: 1.25,
450
- cacheReadPer1mTokens: 0.1,
447
+ inputPer1mTokens: 0.2,
448
+ outputPer1mTokens: 1.2,
449
+ cacheWritePer1mTokens: 0.25,
450
+ cacheReadPer1mTokens: 0.02,
451
451
  tiers: [
452
452
  {
453
453
  inputTokenThreshold: OPENAI_LONG_CONTEXT_PRICING_THRESHOLD_TOKENS,
454
- inputPer1mTokens: 2,
455
- outputPer1mTokens: 9,
456
- cacheWritePer1mTokens: 2.5,
457
- cacheReadPer1mTokens: 0.2,
454
+ inputPer1mTokens: 0.4,
455
+ outputPer1mTokens: 1.8,
456
+ cacheWritePer1mTokens: 0.5,
457
+ cacheReadPer1mTokens: 0.04,
458
458
  },
459
459
  ],
460
460
  },
@@ -1206,6 +1206,10 @@ const RAW_PROVIDER_CATALOG: ProviderCatalogEntry[] = [
1206
1206
  // `cacheCreationInputTokens`). Long-context (>272K input) is 2x input
1207
1207
  // / 1.5x output / 2x cache-read+write for the whole request, per
1208
1208
  // OpenAI's model cards.
1209
+ //
1210
+ // Rates are OpenRouter's own card (https://openrouter.ai/api/v1/models),
1211
+ // which discounts Terra and Luna below OpenAI's direct list price; Sol
1212
+ // matches direct pricing.
1209
1213
  {
1210
1214
  id: "openai/gpt-5.6-sol",
1211
1215
  displayName: "GPT-5.6 Sol",
@@ -1275,17 +1279,17 @@ const RAW_PROVIDER_CATALOG: ProviderCatalogEntry[] = [
1275
1279
  supportsToolUse: true,
1276
1280
  supportsPromptCacheBreakpoints: true,
1277
1281
  pricing: {
1278
- inputPer1mTokens: 2.5,
1279
- outputPer1mTokens: 15.0,
1280
- cacheWritePer1mTokens: 3.125,
1281
- cacheReadPer1mTokens: 0.25,
1282
+ inputPer1mTokens: 1.0,
1283
+ outputPer1mTokens: 6.0,
1284
+ cacheWritePer1mTokens: 1.25,
1285
+ cacheReadPer1mTokens: 0.1,
1282
1286
  tiers: [
1283
1287
  {
1284
1288
  inputTokenThreshold: OPENAI_LONG_CONTEXT_PRICING_THRESHOLD_TOKENS,
1285
- inputPer1mTokens: 5,
1286
- outputPer1mTokens: 22.5,
1287
- cacheWritePer1mTokens: 6.25,
1288
- cacheReadPer1mTokens: 0.5,
1289
+ inputPer1mTokens: 2,
1290
+ outputPer1mTokens: 9,
1291
+ cacheWritePer1mTokens: 2.5,
1292
+ cacheReadPer1mTokens: 0.2,
1289
1293
  },
1290
1294
  ],
1291
1295
  },
@@ -1303,17 +1307,17 @@ const RAW_PROVIDER_CATALOG: ProviderCatalogEntry[] = [
1303
1307
  supportsToolUse: true,
1304
1308
  supportsPromptCacheBreakpoints: true,
1305
1309
  pricing: {
1306
- inputPer1mTokens: 2.5,
1307
- outputPer1mTokens: 15.0,
1308
- cacheWritePer1mTokens: 3.125,
1309
- cacheReadPer1mTokens: 0.25,
1310
+ inputPer1mTokens: 1.0,
1311
+ outputPer1mTokens: 6.0,
1312
+ cacheWritePer1mTokens: 1.25,
1313
+ cacheReadPer1mTokens: 0.1,
1310
1314
  tiers: [
1311
1315
  {
1312
1316
  inputTokenThreshold: OPENAI_LONG_CONTEXT_PRICING_THRESHOLD_TOKENS,
1313
- inputPer1mTokens: 5,
1314
- outputPer1mTokens: 22.5,
1315
- cacheWritePer1mTokens: 6.25,
1316
- cacheReadPer1mTokens: 0.5,
1317
+ inputPer1mTokens: 2,
1318
+ outputPer1mTokens: 9,
1319
+ cacheWritePer1mTokens: 2.5,
1320
+ cacheReadPer1mTokens: 0.2,
1317
1321
  },
1318
1322
  ],
1319
1323
  },
@@ -1331,17 +1335,17 @@ const RAW_PROVIDER_CATALOG: ProviderCatalogEntry[] = [
1331
1335
  supportsToolUse: true,
1332
1336
  supportsPromptCacheBreakpoints: true,
1333
1337
  pricing: {
1334
- inputPer1mTokens: 1.0,
1335
- outputPer1mTokens: 6.0,
1336
- cacheWritePer1mTokens: 1.25,
1337
- cacheReadPer1mTokens: 0.1,
1338
+ inputPer1mTokens: 0.1,
1339
+ outputPer1mTokens: 0.6,
1340
+ cacheWritePer1mTokens: 0.125,
1341
+ cacheReadPer1mTokens: 0.01,
1338
1342
  tiers: [
1339
1343
  {
1340
1344
  inputTokenThreshold: OPENAI_LONG_CONTEXT_PRICING_THRESHOLD_TOKENS,
1341
- inputPer1mTokens: 2,
1342
- outputPer1mTokens: 9,
1343
- cacheWritePer1mTokens: 2.5,
1344
- cacheReadPer1mTokens: 0.2,
1345
+ inputPer1mTokens: 0.2,
1346
+ outputPer1mTokens: 0.9,
1347
+ cacheWritePer1mTokens: 0.25,
1348
+ cacheReadPer1mTokens: 0.02,
1345
1349
  },
1346
1350
  ],
1347
1351
  },
@@ -1359,17 +1363,17 @@ const RAW_PROVIDER_CATALOG: ProviderCatalogEntry[] = [
1359
1363
  supportsToolUse: true,
1360
1364
  supportsPromptCacheBreakpoints: true,
1361
1365
  pricing: {
1362
- inputPer1mTokens: 1.0,
1363
- outputPer1mTokens: 6.0,
1364
- cacheWritePer1mTokens: 1.25,
1365
- cacheReadPer1mTokens: 0.1,
1366
+ inputPer1mTokens: 0.1,
1367
+ outputPer1mTokens: 0.6,
1368
+ cacheWritePer1mTokens: 0.125,
1369
+ cacheReadPer1mTokens: 0.01,
1366
1370
  tiers: [
1367
1371
  {
1368
1372
  inputTokenThreshold: OPENAI_LONG_CONTEXT_PRICING_THRESHOLD_TOKENS,
1369
- inputPer1mTokens: 2,
1370
- outputPer1mTokens: 9,
1371
- cacheWritePer1mTokens: 2.5,
1372
- cacheReadPer1mTokens: 0.2,
1373
+ inputPer1mTokens: 0.2,
1374
+ outputPer1mTokens: 0.9,
1375
+ cacheWritePer1mTokens: 0.25,
1376
+ cacheReadPer1mTokens: 0.02,
1373
1377
  },
1374
1378
  ],
1375
1379
  },
@@ -126,22 +126,27 @@ describe("capPayloadForStorage", () => {
126
126
 
127
127
  test("the ceiling holds for a payload with no long string to trim", () => {
128
128
  // Numbers are individually tiny, so a wide, deep tree of them is bounded by
129
- // neither the text cap nor the field caps.
129
+ // neither the text cap nor the field caps. Branching stays narrow: the tree
130
+ // only has to clear the row ceiling, which a 4-way tree at this depth does
131
+ // several times over.
130
132
  const build = (depth: number): unknown => {
131
133
  if (depth === 0) {
132
134
  return Array.from({ length: 40 }, (_, i) => i * 1_234_567);
133
135
  }
134
136
  const out: Record<string, unknown> = {};
135
- for (let i = 0; i < 12; i++) {
137
+ for (let i = 0; i < 4; i++) {
136
138
  out[`n${i}`] = build(depth - 1);
137
139
  }
138
140
  return out;
139
141
  };
140
142
 
141
- const stored = JSON.stringify(
142
- capPayloadForStorage(build(5) as Record<string, unknown>),
143
+ const tree = build(5) as Record<string, unknown>;
144
+ expect(JSON.stringify(tree).length).toBeGreaterThan(
145
+ WATCHER_PAYLOAD_ROW_MAX_CHARS,
143
146
  );
144
147
 
148
+ const stored = JSON.stringify(capPayloadForStorage(tree));
149
+
145
150
  expect(stored.length).toBeLessThanOrEqual(WATCHER_PAYLOAD_ROW_MAX_CHARS);
146
151
  });
147
152
 
@@ -193,12 +193,16 @@ const KEY_DISAMBIGUATOR = "~";
193
193
  * what the model actually reads, and an escape costs what it costs.
194
194
  */
195
195
  function prefixWithinLimit(text: string, limit: number): string {
196
- if (JSON.stringify(text).length <= limit) {
196
+ // Both bounds here lean on escaping never shrinking a string: its serialized
197
+ // form is at least the text plus two quotes. So text already past the limit
198
+ // needs no measuring, and no prefix longer than the limit can fit either.
199
+ // Searching the whole text instead costs a stringify of it on every step.
200
+ if (text.length + 2 <= limit && JSON.stringify(text).length <= limit) {
197
201
  return text;
198
202
  }
199
203
 
200
204
  let low = 0;
201
- let high = text.length;
205
+ let high = Math.min(text.length, limit);
202
206
  while (low < high) {
203
207
  const mid = Math.ceil((low + high) / 2);
204
208
  if (JSON.stringify(truncate(text, mid)).length <= limit) {
@@ -275,7 +279,7 @@ function shareBudget(costs: readonly number[], budget: number): number[] {
275
279
  */
276
280
  function renderEntry(
277
281
  key: string,
278
- raw: unknown,
282
+ text: string,
279
283
  serialized: string,
280
284
  allowance: number,
281
285
  taken: Set<string>,
@@ -299,7 +303,7 @@ function renderEntry(
299
303
  const value =
300
304
  serialized.length <= valueLimit
301
305
  ? serialized
302
- : serializeWithinLimit(stringify(raw), valueLimit);
306
+ : serializeWithinLimit(text, valueLimit);
303
307
  return `${uniqueKeyJson}:${value}`;
304
308
  }
305
309
 
@@ -360,7 +364,7 @@ export function capPayloadForRender(
360
364
  // escape walk below and fail the same pending row on every tick.
361
365
  const bounded = capRecord(parsed, 0);
362
366
 
363
- const entries: Array<{ key: string; serialized: string; raw: unknown }> = [];
367
+ const entries: Array<{ key: string; serialized: string; text: string }> = [];
364
368
  for (const [rawKey, value] of Object.entries(bounded)) {
365
369
  // Keys are provider-authored in practice, but this payload was parsed back
366
370
  // out of the database, so treat them with the same suspicion as values.
@@ -368,10 +372,14 @@ export function capPayloadForRender(
368
372
  escapeContentBoundaries(rawKey),
369
373
  WATCHER_PAYLOAD_KEY_MAX_CHARS,
370
374
  );
375
+ // Escape once: the serialized form is what is measured, and the escaped
376
+ // text is what a truncated value is cut from.
377
+ const escaped = escapeStrings(value);
378
+ const serialized = JSON.stringify(escaped);
371
379
  entries.push({
372
380
  key,
373
- serialized: JSON.stringify(escapeStrings(value)),
374
- raw: value,
381
+ serialized,
382
+ text: typeof escaped === "string" ? escaped : serialized,
375
383
  });
376
384
  }
377
385
 
@@ -399,7 +407,7 @@ export function capPayloadForRender(
399
407
  const parts = kept.map((entry, index) =>
400
408
  renderEntry(
401
409
  entry.key,
402
- entry.raw,
410
+ entry.text,
403
411
  entry.serialized,
404
412
  allowances[index] ?? 0,
405
413
  taken,
@@ -416,12 +424,6 @@ export function capPayloadForRender(
416
424
  return truncate(`{${parts.join(",")}}`, budget);
417
425
  }
418
426
 
419
- /** Serialize a value to the text used when it has to be truncated. */
420
- function stringify(value: unknown): string {
421
- const escaped = escapeStrings(value);
422
- return typeof escaped === "string" ? escaped : JSON.stringify(escaped);
423
- }
424
-
425
427
  /** Escape fence-boundary sequences in every string within a value. */
426
428
  function escapeStrings(value: unknown): unknown {
427
429
  if (typeof value === "string") {