pi-dynamic-context-pruning 0.1.16 → 0.1.17

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 (3) hide show
  1. package/README.md +9 -7
  2. package/index.ts +45 -22
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -82,13 +82,15 @@ When the same file path is read and/or written more than once, older
82
82
  strategy is deliberately conservative — it only proposes a prune when it can
83
83
  prove the older output is stale, never on a guess:
84
84
 
85
- 1. A **full-file read** (no `offset`/`limit`) is superseded by a **later**
86
- full-file read of the same normalized path.
87
- 2. A **partial read** (has `offset`/`limit`) is *not* superseded by a later
88
- partial read unless the later read's range fully covers the earlier one.
89
- If range coverage can't be reliably determined from the arguments (e.g. the
90
- earlier read has no `limit` — reads to EOF — but the later read is
91
- bounded), the strategy skips it rather than guessing.
85
+ 1. A read is superseded by a **later, successful** read of the same normalized
86
+ path only when the later delivered range fully covers the earlier one.
87
+ An errored read never supersedes anything; a bounded range cannot prove
88
+ coverage of a read to EOF.
89
+ 2. With no truncation metadata or `truncated: false`, the delivered range keeps
90
+ the argument range (`offset`/`limit`, or the whole file if omitted).
91
+ With `truncated: true` and a positive integer `outputLines`, its end is
92
+ limited to the delivered lines. Any other truncation metadata cannot
93
+ establish coverage, so the strategy skips it.
92
94
  3. **Any** read of a path (full or partial) is superseded once a **later,
93
95
  successful** (non-error) write/edit to that path occurs — the old read is
94
96
  now known-stale regardless of range comparisons. An errored later
package/index.ts CHANGED
@@ -303,21 +303,19 @@ function asStringArray(value: unknown, fallback: string[]): string[] {
303
303
  }
304
304
 
305
305
  function asNonNegativeInt(value: unknown, fallback: number): number {
306
- const parsed = typeof value === "number" ? value : Number(value);
307
- if (!Number.isFinite(parsed) || parsed < 0) return fallback;
308
- return Math.floor(parsed);
306
+ if (typeof value !== "number" || !Number.isFinite(value) || value < 0) return fallback;
307
+ return Math.floor(value);
309
308
  }
310
309
 
311
310
  function asNonNegativeNumber(value: unknown, fallback: number): number {
312
- const parsed = typeof value === "number" ? value : Number(value);
313
- if (!Number.isFinite(parsed) || parsed < 0) return fallback;
314
- return parsed;
311
+ if (typeof value !== "number" || !Number.isFinite(value) || value < 0) return fallback;
312
+ return value;
315
313
  }
316
314
 
317
315
  function asRatio(value: unknown, fallback: number): number {
318
- const parsed = typeof value === "number" ? value : Number(value);
319
- if (!Number.isFinite(parsed) || parsed < 0 || parsed > 1) return fallback;
320
- return parsed;
316
+ if (typeof value !== "number" || !Number.isFinite(value) || value < 0 || value > 1)
317
+ return fallback;
318
+ return value;
321
319
  }
322
320
 
323
321
  function asGateMode(value: unknown, fallback: GateMode): GateMode {
@@ -1639,17 +1637,8 @@ export function computeFileReadRange(args: Record<string, unknown> | undefined):
1639
1637
  }
1640
1638
 
1641
1639
  /**
1642
- * Conservative range-coverage check: does `later` fully cover `earlier`?
1643
- * Implements rules 1+2 above via one unified representation (a full-file
1644
- * read is just the range [1, EOF)):
1645
- * - `later` must start at or before `earlier` (later.start <= earlier.start).
1646
- * - If `later` has no end (reads to EOF), it covers any bounded-or-unbounded
1647
- * earlier range that starts at/after it (the earlier read already proved
1648
- * the file has at least `earlier.end` lines, if bounded).
1649
- * - If `later` IS bounded but `earlier` is unbounded (reads to EOF),
1650
- * coverage can't be reliably determined (later might stop short of the
1651
- * file's actual end) — conservatively return false (skip).
1652
- * - Otherwise (both bounded), `later` covers `earlier` iff later.end >= earlier.end.
1640
+ * Implements README rules 1-2: does `later` fully cover `earlier`?
1641
+ * An undefined end means EOF; a bounded range cannot prove coverage to EOF.
1653
1642
  */
1654
1643
  export function readRangeCovers(later: FileReadRange, earlier: FileReadRange): boolean {
1655
1644
  if (later.start > earlier.start) return false;
@@ -1658,6 +1647,37 @@ export function readRangeCovers(later: FileReadRange, earlier: FileReadRange): b
1658
1647
  return later.end >= earlier.end;
1659
1648
  }
1660
1649
 
1650
+ /**
1651
+ * Derive the range delivered by a read result. Successful untruncated results
1652
+ * retain the request-derived range; a truncated result is limited to the
1653
+ * positive line count reported by the SDK. Unknown truncation metadata is
1654
+ * treated as unprovable so it cannot establish read coverage.
1655
+ */
1656
+ function computeDeliveredFileReadRange(
1657
+ args: Record<string, unknown> | undefined,
1658
+ resultMessage: MinimalToolResultMessage,
1659
+ ): FileReadRange | undefined {
1660
+ const requestedRange = computeFileReadRange(args);
1661
+ const details = resultMessage.details;
1662
+ if (!isRecord(details) || !("truncation" in details)) return requestedRange;
1663
+
1664
+ const truncation = details.truncation;
1665
+ if (!isRecord(truncation)) return undefined;
1666
+ if (truncation.truncated === false) return requestedRange;
1667
+ if (truncation.truncated !== true) return undefined;
1668
+
1669
+ const outputLines = truncation.outputLines;
1670
+ if (typeof outputLines !== "number" || !Number.isSafeInteger(outputLines) || outputLines <= 0)
1671
+ return undefined;
1672
+ const deliveredEnd = requestedRange.start + outputLines - 1;
1673
+ if (!Number.isSafeInteger(deliveredEnd)) return undefined;
1674
+ return {
1675
+ start: requestedRange.start,
1676
+ end:
1677
+ requestedRange.end === undefined ? deliveredEnd : Math.min(requestedRange.end, deliveredEnd),
1678
+ };
1679
+ }
1680
+
1661
1681
  interface FileOpOccurrence {
1662
1682
  toolCallId: string;
1663
1683
  kind: FileOpKind;
@@ -1685,7 +1705,10 @@ function collectFileOpOccurrences(
1685
1705
  toolCallId: occurrence.toolCallId,
1686
1706
  kind: kind as FileOpKind,
1687
1707
  normalizedPath,
1688
- range: kind === "read" ? computeFileReadRange(occurrence.arguments) : undefined,
1708
+ range:
1709
+ kind === "read"
1710
+ ? computeDeliveredFileReadRange(occurrence.arguments, resultMessage)
1711
+ : undefined,
1689
1712
  isError: occurrence.isError,
1690
1713
  resultTextLength: contentTextLength(resultMessage.content),
1691
1714
  assistantIndex: occurrence.assistantIndex,
@@ -1822,7 +1845,7 @@ export const supersededFileOpsStrategy: PruneStrategy = {
1822
1845
  // Fold this occurrence into the tracking state for the next (earlier) iteration.
1823
1846
  if (occurrence.kind !== "read") {
1824
1847
  if (!occurrence.isError) nearestLaterSuccessfulWrite = occurrence;
1825
- } else if (occurrence.range !== undefined) {
1848
+ } else if (!occurrence.isError && occurrence.range !== undefined) {
1826
1849
  const occurrenceRange = occurrence.range;
1827
1850
  for (let j = nearestCoveringReads.length - 1; j >= 0; j--) {
1828
1851
  const existing = nearestCoveringReads[j];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-dynamic-context-pruning",
3
- "version": "0.1.16",
3
+ "version": "0.1.17",
4
4
  "type": "module",
5
5
  "description": "A pi extension that dynamically prunes stale context to keep sessions lean.",
6
6
  "keywords": [