teapot-coding-agent 0.37.111 → 0.37.113

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.
@@ -8,6 +8,7 @@ import { createCatalog } from "./tool-catalog.js";
8
8
  import { resetToolCatalog, executeTool, toolSpecs, currentSkills, safeJoin, isContextOverflow, isRequestNodeLimit, hasRunningBgShells, hasRunningBgShellsFor, killBgShells, runningBgShellCount } from "./tools.js";
9
9
  import { effortChoosable } from "./llm.js";
10
10
  import { turnAnchor } from "./timing.js";
11
+ import { claimsContextLimit, usageLine } from "./context-claim.js";
11
12
  import { mcp } from "./mcp-registry.js";
12
13
  import { flattenToolName } from "./mcp-registry.js";
13
14
  import { metaToolSpecs } from "./meta-tools.js";
@@ -240,6 +241,10 @@ export class Agent {
240
241
  awaitingUser = false;
241
242
  /** when the previous round finished, for the time anchor in harness messages (#194) */
242
243
  lastTurnEndedAt = null;
244
+ /** the last assistant text, so the loop can tell a context-limit stop from a real one (#201) */
245
+ lastAssistantText = "";
246
+ /** how many times this stale round has been answered — bounded so the answer cannot become the loop */
247
+ contextClaimNudges = 0;
243
248
  /**
244
249
  * "21:04 · 1h12m into this session · 3m since the last turn".
245
250
  *
@@ -247,8 +252,37 @@ export class Agent {
247
252
  * clock in there would invalidate the cache on every request. Reported as "AIが時間把握するのが極めて下手"
248
253
  * — a five minute exploit and a device reboot read exactly like an instant in the transcript.
249
254
  */
255
+ /**
256
+ * The answer to a "the context window made me stop" claim — the same words whether it arrived as prose
257
+ * (#201) or inside `finish(goalComplete=false)`'s summary, which is how it actually arrived three times in
258
+ * `mediapad-t5-root-attempt-2-2601e254` (#200 is the same event seen as a stopping habit).
259
+ */
260
+ contextClaimAnswer(where) {
261
+ return (`${this.timeAnchor()}\n[harness] ` +
262
+ (where === "finish"
263
+ ? "That finish() was refused: its summary gives the context window as the blocker, and the window is not yours to run out of."
264
+ : "You stopped because of the context window.") +
265
+ ` The numbers above are measured, not estimated — the harness compacts automatically and has just done so.` +
266
+ `\nContinue the task. If this is a FALSE POSITIVE (you were quoting that phrase in a report rather than ` +
267
+ `stopping for it), say so and carry on, or call finish() with your summary if the work is actually done. ` +
268
+ `If you are genuinely blocked, finish(goalComplete=false) with a blocker that is NOT the context size — and ` +
269
+ `weigh it against the current goal first: a goal that is met is finish(goalComplete=true), not a stop.`);
270
+ }
250
271
  timeAnchor() {
251
- return turnAnchor({ now: Date.now(), startedAt: this.stats.startedAt, lastTurnAt: this.lastTurnEndedAt });
272
+ const clock = turnAnchor({
273
+ now: Date.now(),
274
+ startedAt: this.stats.startedAt,
275
+ lastTurnAt: this.lastTurnEndedAt,
276
+ });
277
+ /**
278
+ * …plus the one fact the model otherwise GUESSES: how much of the window is actually used.
279
+ *
280
+ * deepseek-ai/DeepSeek-V3#1669 measured a model declaring "上下文余量不足" at 44-47% of the window, after a
281
+ * compaction had succeeded — the numbers were never in front of it, so it inferred exhaustion from elapsed
282
+ * time and task size. Telling it the real percentage each turn costs nothing (this line is already
283
+ * per-message) and removes the inference.
284
+ */
285
+ return `${clock} · ${usageLine(this.contextSizeForBudgeting(), this.opts.contextTokenBudget)}`;
252
286
  }
253
287
  activityChars = 0;
254
288
  turnsSinceProgress = 0;
@@ -1405,6 +1439,36 @@ export class Agent {
1405
1439
  this.goal.status !== "active" ||
1406
1440
  !this.goal.text.trim())
1407
1441
  break;
1442
+ /**
1443
+ * The model stopped and blamed the context window (#201).
1444
+ *
1445
+ * "コンテキスト上限のため停止します" with no tool call and no finish() is not a blocker the model is
1446
+ * entitled to declare: compaction is the harness's job and it runs automatically. Accepting the claim
1447
+ * counted the round as idle until `maxConsecutiveIdleRounds` stopped the loop — measured three times in
1448
+ * `mediapad-t5-root-attempt-2-2601e254`. So the loop answers it: compact for real, then say so and
1449
+ * carry on. Bounded, and reset by any round that does actual work, so the answer cannot become its own
1450
+ * spin.
1451
+ */
1452
+ if (this.contextClaimNudges < 2 &&
1453
+ !this.awaitingUser &&
1454
+ claimsContextLimit(this.lastAssistantText)) {
1455
+ this.contextClaimNudges += 1;
1456
+ await this.log.append("system_note", this.currentSession, this.currentBranch, {
1457
+ event: "context-claim-answered",
1458
+ attempt: this.contextClaimNudges,
1459
+ detail: "the agent stopped because of the context window — compaction is the harness's job, so it compacted and told the agent to continue (#201)",
1460
+ });
1461
+ await this.maybeCompact(true);
1462
+ const msg = this.contextClaimAnswer("stop");
1463
+ await this.log.append("prompt", this.currentSession, this.currentBranch, {
1464
+ source: "harness",
1465
+ text: msg,
1466
+ });
1467
+ this.messages.push({ role: "user", content: msg });
1468
+ this.lastAssistantText = "";
1469
+ this.consecutiveIdleRounds = 0;
1470
+ continue;
1471
+ }
1408
1472
  if (++this.consecutiveIdleRounds > this.opts.maxConsecutiveIdleRounds) {
1409
1473
  this.consecutiveIdleRounds = 0;
1410
1474
  await this.log.append("system_note", this.currentSession, this.currentBranch, {
@@ -1517,6 +1581,32 @@ export class Agent {
1517
1581
  }
1518
1582
  metaHandlers = {
1519
1583
  finish: async (call, m) => {
1584
+ /**
1585
+ * A `finish(goalComplete=false)` whose summary blames the context window is not a blocker (#200/#201).
1586
+ *
1587
+ * MEASURED in `mediapad-t5-root-attempt-2-2601e254`: the stopping turns were NOT prose without tool calls —
1588
+ * they were `finish(goalComplete=false)` calls whose summary read "コンテキスト上限のため停止します".
1589
+ * Handling only the prose path would have missed every real occurrence.
1590
+ */
1591
+ const fin = safeParse(call.function.arguments);
1592
+ if (fin.goalComplete !== true &&
1593
+ !this.awaitingUser &&
1594
+ this.contextClaimNudges < 2 &&
1595
+ claimsContextLimit(String(fin.summary ?? ""))) {
1596
+ this.contextClaimNudges += 1;
1597
+ await this.log.append("system_note", this.currentSession, this.currentBranch, {
1598
+ event: "context-claim-answered",
1599
+ attempt: this.contextClaimNudges,
1600
+ via: "finish",
1601
+ detail: "a finish(goalComplete=false) blamed the context window — refused, compacted, and told to continue (#201)",
1602
+ });
1603
+ await this.maybeCompact(true);
1604
+ const msg = this.contextClaimAnswer("finish");
1605
+ await this.log.append("prompt", this.currentSession, this.currentBranch, { source: "harness", text: msg });
1606
+ this.messages.push({ role: "user", content: msg });
1607
+ await this.answerMeta(call, "(finish refused: the context window is the harness's to manage)");
1608
+ return "next";
1609
+ }
1520
1610
  this.pendingSubReport = "";
1521
1611
  await this.answerMeta(call, "(completion submitted for audit)");
1522
1612
  await this.handleFinish(call.function.arguments);
@@ -1924,6 +2014,9 @@ export class Agent {
1924
2014
  toolCalls: m.tool_calls?.map((c) => ({ id: c.id, name: c.function.name })),
1925
2015
  reasoning: res.reasoning,
1926
2016
  });
2017
+ this.lastAssistantText = m.content ?? "";
2018
+ if (m.tool_calls?.length)
2019
+ this.contextClaimNudges = 0;
1927
2020
  this.messages.push(m);
1928
2021
  this.turnsSinceProgress++;
1929
2022
  this.activityChars += m.content?.length ?? 0;
@@ -0,0 +1,76 @@
1
+ /**
2
+ * Does this assistant message claim the CONTEXT WINDOW is why it stopped?
3
+ *
4
+ * Measured in `mediapad-t5-root-attempt-2-2601e254`: three times the model ended a turn with prose —
5
+ * "コンテキスト上限のため一旦区切ります" / "コンテキスト上限のため停止します" — with no tool call and no
6
+ * `finish()`. The loop treats that as an idle round, counts it against `maxConsecutiveIdleRounds`, and stops.
7
+ *
8
+ * The claim is also FALSE in this harness: compaction is the harness's job and it runs automatically, so
9
+ * "out of context" is not a blocker the model is entitled to stop for. Recognising the claim is what lets the
10
+ * loop answer it (compact, then say so) instead of accepting it.
11
+ *
12
+ * Deliberately narrow: it must catch the STOPPING claim, not any mention of the topic. "コンテキスト上限に
13
+ * 注意しながら続けます" is not a reason given for stopping, and matching it would derail a working turn.
14
+ */
15
+ /**
16
+ * Phrasings measured in this project and in the report it links
17
+ * (deepseek-ai/DeepSeek-V3#1669: "上下文余量不足" / "上下文快用满了" after a compaction that had left the window
18
+ * at 44-47% used).
19
+ */
20
+ const STOP_PATTERNS = [
21
+ /コンテキスト(?:の)?上限[^。\n]{0,20}(?:停止|区切|終了|中断|打ち切)/,
22
+ /コンテキスト(?:の)?上限のため/,
23
+ /トークン(?:数)?(?:の)?上限[^。\n]{0,20}(?:停止|区切|終了|中断|打ち切)/,
24
+ // the same claim in the languages the model actually writes in
25
+ /上下文[^。\n]{0,20}(?:余量|余裕)[^。\n]{0,20}(?:不足|不够|快满|用满)/,
26
+ /上下文[^。\n]{0,10}(?:快|即将|要)?(?:用满|耗尽|不足)/,
27
+ /(?:token|上下文)[^。\n]{0,12}(?:预算|额度)[^。\n]{0,12}(?:不足|耗尽)/,
28
+ /(?:context|token)s?[\s-]*(?:window|limit)[^.\n]{0,40}(?:stop|halt|end|paus|abort)/i,
29
+ // deepseek-ai/DeepSeek-V3#1737 / #1744: the web product's own wording, reproduced by the model
30
+ /le?ngth\s*limit[^.\n]{0,20}(?:reached|exceeded|hit)/i,
31
+ /(?:your )?chat limit (?:has )?(?:finished|been reached|reached)/i,
32
+ /(?:limit|full|exhausted|reached)[^.\n]{0,40}start (?:a )?new chat/i,
33
+ /(?:running (?:low on|out of) context|not enough context (?:left|remaining)|context (?:is )?(?:almost )?(?:full|exhausted)|out of context (?:space|budget))/i,
34
+ /(?:stop|halt|end|paus|abort)[a-z]*\s+(?:here|now)?\s*(?:because|due to)[^.\n]{0,40}(?:context|token)/i,
35
+ /stopping (?:here )?(?:because|due to)[^.\n]{0,40}(?:context|token)/i,
36
+ ];
37
+ /**
38
+ * Strip what the agent is REPORTING from what it is SAYING.
39
+ *
40
+ * A turn that summarises an investigation legitimately quotes a log line — "コンテキスト上限のため停止します"
41
+ * inside a fenced block or a `>` quote is evidence, not a reason to stop. Matching it would force a continue on
42
+ * a model that is doing exactly the right thing, which is why the quoted forms are removed before matching.
43
+ */
44
+ function withoutQuotedBlocks(text) {
45
+ const kept = [];
46
+ let fence = false;
47
+ for (const line of text.split(/\r?\n/)) {
48
+ if (/^\s*```/.test(line)) {
49
+ fence = !fence;
50
+ continue;
51
+ }
52
+ if (fence)
53
+ continue;
54
+ if (/^\s*>/.test(line))
55
+ continue;
56
+ kept.push(line);
57
+ }
58
+ return kept.join("\n");
59
+ }
60
+ export function claimsContextLimit(text) {
61
+ const raw = String(text ?? "");
62
+ if (!raw.trim())
63
+ return false;
64
+ const t = withoutQuotedBlocks(raw);
65
+ if (!t.trim())
66
+ return false;
67
+ return STOP_PATTERNS.some((re) => re.test(t));
68
+ }
69
+ /** "68k/200k tokens (34%)" — the ground truth the model does not otherwise have */
70
+ export function usageLine(used, budget) {
71
+ const fmt = (n) => (n >= 1_000 ? `${Math.round(n / 1_000)}k` : String(Math.max(0, Math.round(n))));
72
+ if (!Number.isFinite(budget) || budget <= 0)
73
+ return `context ${fmt(used)} tokens used`;
74
+ const pct = Math.max(0, Math.min(100, Math.round((used / budget) * 100)));
75
+ return `context ${pct}% (${fmt(used)}/${fmt(budget)} tokens)`;
76
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "teapot-coding-agent",
3
- "version": "0.37.111",
3
+ "version": "0.37.113",
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",