teapot-coding-agent 0.37.38 → 0.37.40

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.
@@ -93,6 +93,34 @@ export function applyEol(content, eol) {
93
93
  function staleGuard(pathArg, base, current) {
94
94
  if (typeof base !== "string")
95
95
  return null; // not supplied → no check
96
+ /**
97
+ * A base_content that cannot BE the file is not evidence of a change.
98
+ *
99
+ * MEASURED in tyb2-9-8155867c: four `edit_file` calls, two of which passed `base_content: "x"` —
100
+ * a single byte — and were refused with "changed on disk since you read it … your base_content: 1
101
+ * bytes, on disk now: 52351 bytes". The guard was right that the bytes differ and useless about
102
+ * it: nothing in the file's history can make "x" the content of a 52KB script, so the message sent
103
+ * the model to re-read a file it had never successfully checked against.
104
+ *
105
+ * A guard whose input cannot be the thing it guards is noise. Below a plausible-file floor the
106
+ * value is treated as not supplied — the edit still has to pass the anchor checks, which is where
107
+ * a stale read is actually caught.
108
+ */
109
+ /**
110
+ * A base_content that is a tiny FRACTION of the file cannot be its content.
111
+ *
112
+ * MEASURED in tyb2-9-8155867c: two of four `edit_file` calls passed `base_content: "x"` — one
113
+ * byte — and were refused with "changed on disk since you read it … your base_content: 1 bytes, on
114
+ * disk now: 52351 bytes". The guard was right that the bytes differ and useless about it: nothing
115
+ * can make "x" the content of a 52KB script, so the message sent the model to re-read a file whose
116
+ * state was never in question.
117
+ *
118
+ * The test is "unmistakably not this file", NOT "short": a legitimately small file edited by a
119
+ * model that re-read it must keep the guard, which is what the two #5 tests would have lost.
120
+ */
121
+ const MIN_PLAUSIBLE_BASE = 16;
122
+ if (base.length < MIN_PLAUSIBLE_BASE && current !== null && current.length >= MIN_PLAUSIBLE_BASE * 4)
123
+ return null;
96
124
  if (base === current)
97
125
  return null; // unchanged → proceed
98
126
  const rel = pathArg;
@@ -540,8 +568,13 @@ export function disproportionateSpan(text, anchor) {
540
568
  * the guard unreachable exactly when it matters.
541
569
  */
542
570
  const anchorFirst = anchorLines[0].trimEnd();
543
- const start = text.split("\n").findIndex((l) => l.trimEnd() === anchorFirst);
544
- if (start < 0)
571
+ const allSrcLines = text.split("\n");
572
+ /** every place the anchor's first line appears — the BEST one decides, not the first */
573
+ const starts = [];
574
+ for (let i = 0; i < allSrcLines.length; i++)
575
+ if (allSrcLines[i].trimEnd() === anchorFirst)
576
+ starts.push(i);
577
+ if (!starts.length)
545
578
  return null; // its first line is not in the file at all: not this guard's case
546
579
  /**
547
580
  * How many file lines does the anchor COVER, allowing the same trailing-whitespace tolerance
@@ -551,28 +584,48 @@ export function disproportionateSpan(text, anchor) {
551
584
  * the anchor requires consuming more than twice its own line count, the anchor is too loose:
552
585
  * applying it would replace a region the model did not point at.
553
586
  */
554
- const srcLines = text.split("\n");
555
- const firstLine = start;
556
- let fileAt = firstLine;
557
- let consumed = 0;
558
- for (let a = 0; a < anchorLines.length; a++) {
559
- const want = anchorLines[a].trimEnd();
560
- let found = -1;
561
- for (let f = fileAt; f < srcLines.length; f++) {
562
- if (srcLines[f].trimEnd() === want) {
563
- found = f;
587
+ const srcLines = allSrcLines;
588
+ /**
589
+ * EVERY candidate start is considered, and the guard only fires when ALL of them overshoot.
590
+ *
591
+ * Probing just the FIRST occurrence was wrong whenever the anchor's opening line is not unique —
592
+ * `\tif (pwrseq->skip_power_cycle) {` appears more than once in that file, and the first one led
593
+ * the walk into a distant match, so a legitimate 7-line anchor was refused for "covering 58
594
+ * lines". MEASURED in tyb2-9-8155867c: the model then abandoned `edit_file` for that region.
595
+ *
596
+ * The guard's question is "could this anchor be read as pointing somewhere else?" — if ANY
597
+ * plausible reading fits inside the tolerance, it cannot.
598
+ */
599
+ let best = Number.POSITIVE_INFINITY;
600
+ for (const start of starts) {
601
+ let fileAt = start;
602
+ let consumed = 0;
603
+ let ok = true;
604
+ for (let a = 0; a < anchorLines.length; a++) {
605
+ const want = anchorLines[a].trimEnd();
606
+ let found = -1;
607
+ for (let f = fileAt; f < srcLines.length; f++) {
608
+ if (srcLines[f].trimEnd() === want) {
609
+ found = f;
610
+ break;
611
+ }
612
+ }
613
+ if (found < 0) {
614
+ ok = false;
564
615
  break;
565
616
  }
617
+ consumed = found - start + 1;
618
+ fileAt = found + 1;
566
619
  }
567
- if (found < 0)
568
- return null; // did not resolve under tolerance; not this guard's business
569
- consumed = found - firstLine + 1;
570
- if (consumed > askedLines * 2) {
571
- return (`old_text spans ${askedLines} line(s) but only resolves after covering ${consumed} ` +
572
- `lines of the file — refusing a disproportionate match. An anchor this loose would ` +
573
- `rewrite a region you did not intend. Re-read the file and copy a tighter block.`);
574
- }
575
- fileAt = found + 1;
620
+ if (ok)
621
+ best = Math.min(best, consumed);
622
+ }
623
+ if (!Number.isFinite(best))
624
+ return null; // did not resolve under tolerance; not this guard's business
625
+ if (best > askedLines * 2) {
626
+ return (`old_text spans ${askedLines} line(s) but the nearest reading of it covers ${best} ` +
627
+ `lines of the file — refusing a disproportionate match. An anchor this loose would ` +
628
+ `rewrite a region you did not intend. Re-read the file and copy a tighter block.`);
576
629
  }
577
630
  return null;
578
631
  }
@@ -1085,9 +1138,26 @@ export function resolveAnchor(lines, anchor) {
1085
1138
  };
1086
1139
  if (hits.length === 1)
1087
1140
  return { index: hits[0], how: "unique" };
1141
+ /**
1142
+ * Several lines share this hash — because the hash is DERIVED FROM CONTENT.
1143
+ *
1144
+ * That is the structural weakness of this format compared with the anchor-table implementations
1145
+ * (`pi-hashline-edit-pro` allocates an anchor per line "never derived from content, so
1146
+ * byte-identical lines never share an anchor"). Here two identical lines are one anchor, so the
1147
+ * line number has to break the tie by proximity — a guess, and a guess that can be wrong in a file
1148
+ * with repeated blocks.
1149
+ *
1150
+ * The guess is kept (refusing outright would make editing any file with repeated lines
1151
+ * impossible), but it is now REPORTED instead of being a silent choice: the caller appends it to
1152
+ * the tool result, so a model that landed on the wrong copy can see why.
1153
+ */
1088
1154
  const target = anchor.line - 1;
1089
1155
  const best = hits.reduce((a, b) => (Math.abs(b - target) < Math.abs(a - target) ? b : a));
1090
- return { index: best, how: `ambiguous, resolved by proximity to line ${anchor.line}` };
1156
+ return {
1157
+ index: best,
1158
+ how: `line ${anchor.line} shares its content hash with ${hits.length - 1} other line(s) — ` +
1159
+ `resolved by proximity, VERIFY you edited the copy you meant`,
1160
+ };
1091
1161
  }
1092
1162
  /** Is this whole block a hashline block (every line carries an anchor)? */
1093
1163
  export function isHashlineBlock(block) {
@@ -1372,8 +1442,10 @@ export const TOOLS = [
1372
1442
  description: "Make exactly ONE small, unique replacement in one existing file — the cheapest tool for a single spot change. " +
1373
1443
  "Copy old_text from the file contents (NOT from read_file's `N| ` prefixed display); it must appear exactly once — " +
1374
1444
  "if it matches several places, add surrounding lines or pass replace_all=true. " +
1375
- "Or read with read_file(line_ids:\"hash\") and pass that block VERBATIM — `LINE:HASH| ` anchors are verified against the " +
1376
- "current file, and a block whose anchors no longer match is refused as a stale read instead of editing the wrong lines. " +
1445
+ "Or read with read_file(line_ids:\"hash\") and pass that block VERBATIM as old_text — `LINE:HASH| ` anchors are verified " +
1446
+ "against the current file, and a block whose anchors no longer match is refused as a stale read instead of editing the " +
1447
+ "wrong lines. IMPORTANT with anchors: they belong to the READ, so new_text must be PLAIN content with NO `N:HASH| ` " +
1448
+ "prefixes (they are stripped if sent, but sending them means old_text and new_text look identical). " +
1377
1449
  "Two or more changes (or a rename/delete) → use apply_patch instead. " +
1378
1450
  "Pass base_content (the bytes you read) whenever the file may have changed under you — especially with replace_all, which otherwise rewrites occurrences that appeared after you read.",
1379
1451
  parameters: {
@@ -1397,7 +1469,7 @@ export const TOOLS = [
1397
1469
  if (stale)
1398
1470
  return stale;
1399
1471
  let oldText = str(args.old_text);
1400
- const newText = str(args.new_text);
1472
+ let newText = str(args.new_text);
1401
1473
  // the single most common edit_file failure in the real logs (22
1402
1474
  // occurrences) said only "old_text is required", while the tool's own
1403
1475
  // schema already documents the better form. Name it and say what to do —
@@ -1425,6 +1497,19 @@ export const TOOLS = [
1425
1497
  let hashNote = "";
1426
1498
  let hashNotes = [];
1427
1499
  let hashSpan = null;
1500
+ /**
1501
+ * A hashline-formatted `new_text` is STRIPPED, not pasted.
1502
+ *
1503
+ * MEASURED in tyb2-9-8155867c: `old_text` and `new_text` were IDENTICAL, both carrying anchors
1504
+ * (`155c| …`, `59d3| …`) copied from the read. The model meant "these lines, rewritten" but
1505
+ * pasted the anchor gutter on both sides. The result was refused as "old_text not found",
1506
+ * which names the wrong argument — the anchors had already been resolved away, so the literal
1507
+ * search could not find them, and the reply never mentioned the real mistake.
1508
+ *
1509
+ * The anchors belong to the READ, not to the file. Stripping them from `new_text` is what the
1510
+ * model meant, and writing them into the source would be corruption.
1511
+ */
1512
+ const newTextHasAnchors = isHashlineBlock(newText);
1428
1513
  if (isHashlineBlock(oldText)) {
1429
1514
  const srcLines = text.replace(/\r\n/g, "\n").split("\n");
1430
1515
  const r = resolveHashlineBlock(oldText, srcLines);
@@ -1436,6 +1521,26 @@ export const TOOLS = [
1436
1521
  hashSpan = { start: r.indices[0], end: r.indices[r.indices.length - 1] + 1 };
1437
1522
  }
1438
1523
  }
1524
+ /**
1525
+ * Drop anchors from `new_text` — they are the READ's numbering, not file content.
1526
+ *
1527
+ * A block whose anchors cannot be resolved is still stripped (the content is what was meant);
1528
+ * writing `155c| ` into a source file would be corruption, so the safe direction is to remove
1529
+ * them and let the rest of the edit be judged on its text.
1530
+ */
1531
+ if (newTextHasAnchors) {
1532
+ const stripped = newText
1533
+ .split("\n")
1534
+ .map((l) => {
1535
+ const a = ANCHOR_RE.exec(l);
1536
+ return a ? l.slice(l.indexOf("|") + 1).replace(/^ /, "") : l;
1537
+ })
1538
+ .join("\n");
1539
+ if (stripped !== newText) {
1540
+ newText = stripped;
1541
+ hashNotes = [...hashNotes, "removed the hashline anchors from new_text (they belong to the read, not the file)"];
1542
+ }
1543
+ }
1439
1544
  let count = hashNote ? 0 : text.split(oldText).length - 1;
1440
1545
  /**
1441
1546
  * A MINIMUM ANCHOR LENGTH, from Gemini's `edit.ts`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "teapot-coding-agent",
3
- "version": "0.37.38",
3
+ "version": "0.37.40",
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",