teapot-coding-agent 0.37.151 → 0.37.154

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.
@@ -9,6 +9,7 @@ import { resetToolCatalog, executeTool, toolSpecs, currentSkills, safeJoin, isCo
9
9
  import { effortChoosable } from "./llm.js";
10
10
  import { turnAnchor } from "./timing.js";
11
11
  import { claimsContextLimit, claimsOwnLimit, usageLine } from "./context-claim.js";
12
+ import { compactionNotice } from "./compaction-notice.js";
12
13
  import { isDegenerateRepeat } from "./loop-guard.js";
13
14
  import { namesBlocker } from "./blocker.js";
14
15
  import { mcp } from "./mcp-registry.js";
@@ -3003,6 +3004,8 @@ export class Agent {
3003
3004
  const oldChars = old.reduce((n, m) => n + (m.content?.length ?? 0), 0);
3004
3005
  const oldShareOfBefore = totalChars > 0 ? Math.round((before * oldChars) / totalChars) : 0;
3005
3006
  let summary = "";
3007
+ /** the usage line the notice carries, recorded with the compaction so a restore reproduces the same bytes */
3008
+ let noticeLine = "";
3006
3009
  let mode = "summarize";
3007
3010
  let summarizedCount = oldCount;
3008
3011
  let droppedCount = 0;
@@ -3136,13 +3139,18 @@ export class Agent {
3136
3139
  ];
3137
3140
  }
3138
3141
  else {
3142
+ noticeLine = usageLine(this.estimateTokens(this.messages.slice(cut)), this.opts.contextTokenBudget);
3143
+ /**
3144
+ * The usage line is computed ONCE, from the kept tail, and then WRITTEN INTO the compaction event.
3145
+ *
3146
+ * It cannot be recomputed at restore time: the restored notice must be byte-identical to the one the model saw,
3147
+ * or the front of every request changes after a restart and the provider's prefix cache misses for the whole
3148
+ * session. The line is therefore a recorded fact, and `compactionNotice()` is the single builder both paths use.
3149
+ */
3139
3150
  this.messages = [
3140
3151
  {
3141
3152
  role: "user",
3142
- content: `[harness] Context was compacted: ${oldCount} earlier messages were summarized. ` +
3143
- "AGENTS.md is embedded in your system prompt; memory.md, todo.md and decisions.md are NOT — "
3144
- + "call read_memory / get_todo / get_decisions if you need them.\n\n" +
3145
- `## Summary of earlier conversation\n${summary}`,
3153
+ content: compactionNotice({ summarized: oldCount, summary, contextLine: noticeLine }),
3146
3154
  },
3147
3155
  ...this.messages.slice(cut),
3148
3156
  ];
@@ -3157,10 +3165,6 @@ export class Agent {
3157
3165
  * computed from the messages as they are NOW (`after`), which is the only value that answers "how much do I
3158
3166
  * still have?", and it is appended rather than prepended so the checkpoint text stays first.
3159
3167
  */
3160
- const notice = this.messages[0];
3161
- if (notice?.role === "user" && /^\[harness\] (Context was compacted|[\d,]+ earlier messages were DROPPED)/.test(String(notice.content))) {
3162
- notice.content = `${String(notice.content)}\n\n${usageLine(after, this.opts.contextTokenBudget)} — the window is roomy again, not nearly exhausted.`;
3163
- }
3164
3168
  this.stats.compactions++;
3165
3169
  if (mode === "summarize")
3166
3170
  this.consecutiveCompactFailures = 0;
@@ -3252,6 +3256,15 @@ export class Agent {
3252
3256
  dropped: droppedCount,
3253
3257
  mode,
3254
3258
  ...(summary ? { summary: String(summary).slice(0, 20_000) } : {}),
3259
+ /**
3260
+ * The usage line the notice carries, recorded HERE because this is the event `restoreFromLog` replays.
3261
+ *
3262
+ * The notice sits at the front of every request, so the restored text must be byte-identical to the live one or
3263
+ * the prefix cache misses for the whole session. Recomputing the line at restore would be a second owner of a
3264
+ * fact about a moment in time; recording it here makes the notice reproducible, and an older log without it gets
3265
+ * exactly the notice it always had.
3266
+ */
3267
+ contextLine: noticeLine,
3255
3268
  });
3256
3269
  if (summary)
3257
3270
  this.lastCompactSummary = summary;
@@ -3645,17 +3658,29 @@ async function rebuildMessagesFrom(list, imagesDir) {
3645
3658
  }
3646
3659
  else {
3647
3660
  const sum = typeof d.summary === "string" ? d.summary.trim() : "";
3648
- const summarized = Math.min(msgs.length, Number(d.summarized ?? 0) || msgs.length);
3661
+ /**
3662
+ * The COUNT in the notice is the recorded one; only the SPLICE is clamped.
3663
+ *
3664
+ * It used to be `Math.min(msgs.length, recorded)`, which is right for the splice and wrong for the text: while
3665
+ * replaying, `msgs.length` is the number of messages restored SO FAR, so the rebuilt notice said "0 earlier
3666
+ * messages were summarized" where the model had been told 59. That is a different byte at the FRONT of every
3667
+ * request after a restart — the prefix-cache miss the operator warned about, and it was already there before the
3668
+ * usage line was added.
3669
+ */
3670
+ const recorded = Number(d.summarized ?? 0) || msgs.length;
3671
+ const summarized = Math.min(msgs.length, recorded);
3649
3672
  if (!sum) {
3650
3673
  console.warn(`[teapot] compaction at seq ${e.seq} has no summary text — keeping the full history rather than discarding it`);
3651
3674
  }
3652
3675
  else {
3653
3676
  msgs.splice(0, summarized, {
3654
3677
  role: "user",
3655
- content: `[harness] Context was compacted: ${summarized} earlier messages were summarized. ` +
3656
- "AGENTS.md is embedded in your system prompt; memory.md, todo.md and decisions.md are NOT — "
3657
- + "call read_memory / get_todo / get_decisions if you need them.\n\n" +
3658
- `## Summary of earlier conversation\n${sum}`,
3678
+ // the SAME builder as the compaction itself, with the line as it was recorded then
3679
+ content: compactionNotice({
3680
+ summarized: recorded,
3681
+ summary: sum,
3682
+ contextLine: typeof d.contextLine === "string" ? d.contextLine : undefined,
3683
+ }),
3659
3684
  });
3660
3685
  }
3661
3686
  }
@@ -0,0 +1,8 @@
1
+ export function compactionNotice(input) {
2
+ const base = `[harness] Context was compacted: ${input.summarized} earlier messages were summarized. ` +
3
+ "AGENTS.md is embedded in your system prompt; memory.md, todo.md and decisions.md are NOT — " +
4
+ "call read_memory / get_todo / get_decisions if you need them.\n\n" +
5
+ `## Summary of earlier conversation\n${input.summary}`;
6
+ const line = (input.contextLine ?? "").trim();
7
+ return line ? `${base}\n\n${line} — the window is roomy again, not nearly exhausted.` : base;
8
+ }
@@ -38,6 +38,14 @@ const STOP_PATTERNS = [
38
38
  /(?:running (?:low on|out of) context|not enough context (?:left|remaining)|context (?:is )?(?:almost )?(?:full|exhausted)|out of context (?:space|budget))/i,
39
39
  /(?:stop|halt|end|paus|abort)[a-z]*\s+(?:here|now)?\s*(?:because|due to)[^.\n]{0,40}(?:context|token)/i,
40
40
  /stopping (?:here )?(?:because|due to)[^.\n]{0,40}(?:context|token)/i,
41
+ /**
42
+ * Japanese phrasings, MEASURED on mediapad-t5-root-attempt-2: right after a compaction that left 26,610 of ~750,000
43
+ * tokens, the model wrote "ここで一旦区切ります(残コンテキストが僅少)" and stopped — the vocabulary above knew the
44
+ * Chinese and English forms and missed this one, so the guard that answers with the real usage never fired.
45
+ */
46
+ /残(?:り)?(?:の)?(?:コンテキスト|トークン)[^。\n]{0,12}(?:僅少|わずか|少な|尽き|残っていない|無くな)/,
47
+ /コンテキスト(?:の)?(?:残量|残り)[^。\n]{0,12}(?:僅少|わずか|少な|尽き|残っていない|無くな)/,
48
+ /(?:コンテキスト|トークン)(?:を)?(?:節約|温存)[^。\n]{0,20}(?:区切|停止|終了|打ち切)/,
41
49
  ];
42
50
  /**
43
51
  * Strip what the agent is REPORTING from what it is SAYING.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "teapot-coding-agent",
3
- "version": "0.37.151",
3
+ "version": "0.37.154",
4
4
  "description": "A lightweight, always-on multi-agent harness for AI coding agents",
5
5
  "type": "module",
6
6
  "license": "AGPL-3.0-or-later",