pi-dynamic-context-pruning 0.1.15 → 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 +13 -9
  2. package/index.ts +50 -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
@@ -109,7 +111,8 @@ Before any strategy is allowed to propose a prune, several protections apply:
109
111
 
110
112
  - **Protected tool names** (case-insensitive, never pruned): `todo`, `task`,
111
113
  `subagent`, `agent`, `skill`, `oracle`, `librarian`, `contrarian`,
112
- `code_reviewer`, `gnosis`, `triage_comments`.
114
+ `code_reviewer`, `gnosis`, `triage_comments`, `developer`, `diff_summarizer`,
115
+ `repo_scout`, `test_runner`, `web_scout`.
113
116
  - **Protected path globs** (never pruned if any string argument matches):
114
117
  `**/.env`, `**/.env.*`, `**/*.pem`, `**/*.key`, `**/id_rsa*`,
115
118
  `**/id_ed25519*`, `**/secrets/**`, `**/*.p12`, `**/*.pfx`.
@@ -118,6 +121,7 @@ Before any strategy is allowed to propose a prune, several protections apply:
118
121
  what they contain.
119
122
 
120
123
  All three lists/values are configurable (see [Configuration](#configuration)).
124
+ A user-set `protections.toolNames` replaces the default list.
121
125
 
122
126
  ## Savings accounting and the net-benefit gate
123
127
 
@@ -219,7 +223,7 @@ never required to exist. Full shape, with defaults:
219
223
  {
220
224
  "enabled": true,
221
225
  "protections": {
222
- "toolNames": ["todo", "task", "subagent", "agent", "skill", "oracle", "librarian", "contrarian", "code_reviewer", "gnosis", "triage_comments"],
226
+ "toolNames": ["todo", "task", "subagent", "agent", "skill", "oracle", "librarian", "contrarian", "code_reviewer", "gnosis", "triage_comments", "developer", "diff_summarizer", "repo_scout", "test_runner", "web_scout"],
223
227
  "pathGlobs": ["**/.env", "**/.env.*", "**/*.pem", "**/*.key", "**/id_rsa*", "**/id_ed25519*", "**/secrets/**", "**/*.p12", "**/*.pfx"],
224
228
  "recentTurns": 4
225
229
  },
package/index.ts CHANGED
@@ -177,6 +177,11 @@ const DEFAULT_PROTECTED_TOOL_NAMES = [
177
177
  "code_reviewer",
178
178
  "gnosis",
179
179
  "triage_comments",
180
+ "developer",
181
+ "diff_summarizer",
182
+ "repo_scout",
183
+ "test_runner",
184
+ "web_scout",
180
185
  ];
181
186
 
182
187
  /** File globs never pruned by default: secrets/credential-shaped paths. */
@@ -298,21 +303,19 @@ function asStringArray(value: unknown, fallback: string[]): string[] {
298
303
  }
299
304
 
300
305
  function asNonNegativeInt(value: unknown, fallback: number): number {
301
- const parsed = typeof value === "number" ? value : Number(value);
302
- if (!Number.isFinite(parsed) || parsed < 0) return fallback;
303
- return Math.floor(parsed);
306
+ if (typeof value !== "number" || !Number.isFinite(value) || value < 0) return fallback;
307
+ return Math.floor(value);
304
308
  }
305
309
 
306
310
  function asNonNegativeNumber(value: unknown, fallback: number): number {
307
- const parsed = typeof value === "number" ? value : Number(value);
308
- if (!Number.isFinite(parsed) || parsed < 0) return fallback;
309
- return parsed;
311
+ if (typeof value !== "number" || !Number.isFinite(value) || value < 0) return fallback;
312
+ return value;
310
313
  }
311
314
 
312
315
  function asRatio(value: unknown, fallback: number): number {
313
- const parsed = typeof value === "number" ? value : Number(value);
314
- if (!Number.isFinite(parsed) || parsed < 0 || parsed > 1) return fallback;
315
- return parsed;
316
+ if (typeof value !== "number" || !Number.isFinite(value) || value < 0 || value > 1)
317
+ return fallback;
318
+ return value;
316
319
  }
317
320
 
318
321
  function asGateMode(value: unknown, fallback: GateMode): GateMode {
@@ -1634,17 +1637,8 @@ export function computeFileReadRange(args: Record<string, unknown> | undefined):
1634
1637
  }
1635
1638
 
1636
1639
  /**
1637
- * Conservative range-coverage check: does `later` fully cover `earlier`?
1638
- * Implements rules 1+2 above via one unified representation (a full-file
1639
- * read is just the range [1, EOF)):
1640
- * - `later` must start at or before `earlier` (later.start <= earlier.start).
1641
- * - If `later` has no end (reads to EOF), it covers any bounded-or-unbounded
1642
- * earlier range that starts at/after it (the earlier read already proved
1643
- * the file has at least `earlier.end` lines, if bounded).
1644
- * - If `later` IS bounded but `earlier` is unbounded (reads to EOF),
1645
- * coverage can't be reliably determined (later might stop short of the
1646
- * file's actual end) — conservatively return false (skip).
1647
- * - 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.
1648
1642
  */
1649
1643
  export function readRangeCovers(later: FileReadRange, earlier: FileReadRange): boolean {
1650
1644
  if (later.start > earlier.start) return false;
@@ -1653,6 +1647,37 @@ export function readRangeCovers(later: FileReadRange, earlier: FileReadRange): b
1653
1647
  return later.end >= earlier.end;
1654
1648
  }
1655
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
+
1656
1681
  interface FileOpOccurrence {
1657
1682
  toolCallId: string;
1658
1683
  kind: FileOpKind;
@@ -1680,7 +1705,10 @@ function collectFileOpOccurrences(
1680
1705
  toolCallId: occurrence.toolCallId,
1681
1706
  kind: kind as FileOpKind,
1682
1707
  normalizedPath,
1683
- range: kind === "read" ? computeFileReadRange(occurrence.arguments) : undefined,
1708
+ range:
1709
+ kind === "read"
1710
+ ? computeDeliveredFileReadRange(occurrence.arguments, resultMessage)
1711
+ : undefined,
1684
1712
  isError: occurrence.isError,
1685
1713
  resultTextLength: contentTextLength(resultMessage.content),
1686
1714
  assistantIndex: occurrence.assistantIndex,
@@ -1817,7 +1845,7 @@ export const supersededFileOpsStrategy: PruneStrategy = {
1817
1845
  // Fold this occurrence into the tracking state for the next (earlier) iteration.
1818
1846
  if (occurrence.kind !== "read") {
1819
1847
  if (!occurrence.isError) nearestLaterSuccessfulWrite = occurrence;
1820
- } else if (occurrence.range !== undefined) {
1848
+ } else if (!occurrence.isError && occurrence.range !== undefined) {
1821
1849
  const occurrenceRange = occurrence.range;
1822
1850
  for (let j = nearestCoveringReads.length - 1; j >= 0; j--) {
1823
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.15",
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": [