pi-rtk-optimizer 0.5.4 → 0.5.5

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.
package/CHANGELOG.md CHANGED
@@ -7,15 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
- ## [0.5.4] - 2026-04-22
11
-
12
- ### Fixed
13
- - Respected `PI_CODING_AGENT_DIR` when resolving the global extension config directory and preserved exact skill reads under the configured global Pi skills path.
10
+ ## [0.5.5] - 2026-04-24
14
11
 
15
12
  ### Changed
16
- - Updated README installation/configuration guidance and the settings modal description to document configurable global Pi paths.
17
- - Removed session-switch lifecycle reinitialization from the extension runtime.
18
- - Updated `@mariozechner/pi-coding-agent` and `@mariozechner/pi-tui` peer dependencies to ^0.68.1.
13
+ - Config path resolution now uses Pi's `getAgentDir()` API so `PI_CODING_AGENT_DIR` is respected for extension config paths (thanks to @tynanbe for PR #3).
14
+ - Global skill-read preservation paths now resolve through Pi's agent directory so `PI_CODING_AGENT_DIR` is respected (thanks to @tynanbe for PR #3).
15
+ - Source-filter troubleshooting note injection now only runs when output compaction, source filtering, and read truncation safeguards are active (thanks to @philipbjorge for PR #4).
16
+ - Updated `@mariozechner/pi-coding-agent` and `@mariozechner/pi-tui` peer dependencies to ^0.70.0.
17
+ - Clarified README and settings modal copy for global extension/config paths, skill directory paths, source-filter note behavior, architecture, and event hooks.
18
+
19
+ ### Removed
20
+ - Removed the unused local asset directory.
21
+ - Removed the `session_switch` event refresh handler.
19
22
 
20
23
  ## [0.5.3] - 2026-04-01
21
24
 
package/README.md CHANGED
@@ -60,8 +60,9 @@ Multi-stage pipeline to reduce token consumption:
60
60
  Place this folder in one of the following locations:
61
61
 
62
62
  ```text
63
- ~/.pi/agent/extensions/pi-rtk-optimizer # Global default (when PI_CODING_AGENT_DIR is unset)
64
- .pi/extensions/pi-rtk-optimizer # Project-specific
63
+ ~/.pi/agent/extensions/pi-rtk-optimizer # Global default (when PI_CODING_AGENT_DIR is unset)
64
+ $PI_CODING_AGENT_DIR/extensions/pi-rtk-optimizer # Global when PI_CODING_AGENT_DIR is set
65
+ .pi/extensions/pi-rtk-optimizer # Project-specific
65
66
  ```
66
67
 
67
68
  Pi auto-discovers extensions in these paths on startup.
@@ -146,7 +147,7 @@ A starter template is included at `config/config.example.json`.
146
147
  | `outputCompaction.enabled` | boolean | `true` | Enable output compaction pipeline |
147
148
  | `outputCompaction.stripAnsi` | boolean | `true` | Remove ANSI escape codes |
148
149
  | `outputCompaction.sourceCodeFilteringEnabled` | boolean | `true` | Enable source code filtering for `read` output |
149
- | `outputCompaction.preserveExactSkillReads` | boolean | `false` | Keep reads under Pi skill directories exact, bypassing read compaction |
150
+ | `outputCompaction.preserveExactSkillReads` | boolean | `false` | Keep reads under configured Pi/global/project skill directories exact, bypassing read compaction |
150
151
  | `outputCompaction.sourceCodeFiltering` | string | `"minimal"` | Filter level: `"none"`, `"minimal"`, `"aggressive"` |
151
152
  | `outputCompaction.aggregateTestOutput` | boolean | `true` | Summarize test runner output |
152
153
  | `outputCompaction.filterBuildOutput` | boolean | `true` | Filter build/compile output |
@@ -155,6 +156,8 @@ A starter template is included at `config/config.example.json`.
155
156
  | `outputCompaction.groupSearchOutput` | boolean | `true` | Group search results by file |
156
157
  | `outputCompaction.trackSavings` | boolean | `true` | Track compaction metrics |
157
158
 
159
+ Skill-read preservation covers the global Pi skills directory (`~/.pi/agent/skills` by default, or `$PI_CODING_AGENT_DIR/skills` when set), `~/.agents/skills`, project `.pi/skills`, and ancestor `.agents/skills` directories.
160
+
158
161
  #### Truncation Settings
159
162
 
160
163
  | Option | Type | Default | Range | Description |
@@ -172,7 +175,7 @@ A starter template is included at `config/config.example.json`.
172
175
  | `minimal` | Removes non-doc comments, collapses blank lines |
173
176
  | `aggressive` | Also removes imports, keeps only signatures and key logic |
174
177
 
175
- > **Note:** If file edits fail because "old text does not match," disable source filtering via `/rtk`, re-read the file, apply the edit, then re-enable filtering.
178
+ > **Note:** When source filtering and read truncation safeguards are active, Pi injects a troubleshooting note for repeated file-edit mismatches. If edits fail because "old text does not match," disable source filtering via `/rtk`, re-read the file, apply the edit, then re-enable filtering.
176
179
 
177
180
  ### Example Configuration
178
181
 
@@ -225,15 +228,19 @@ src/
225
228
  ├── index.ts # Extension bootstrap and event wiring
226
229
  ├── config-store.ts # Config load/save with normalization
227
230
  ├── config-modal.ts # TUI settings modal and /rtk handler
228
- ├── command-rewriter.ts # Command tokenization and rewrite logic
229
- ├── rewrite-bypass.ts # Rewrite safety bypass rules for interactive/structured commands
230
- ├── rewrite-rules.ts # Rewrite rule catalog
231
- ├── runtime-guard.ts # Runtime availability guard helpers for rewrite mode
232
- ├── output-compactor.ts # Tool result compaction pipeline
233
- ├── output-metrics.ts # Savings tracking and reporting
234
- ├── command-completions.ts # /rtk subcommand completions
231
+ ├── command-rewriter.ts # Command tokenization and rewrite logic
232
+ ├── rewrite-bypass.ts # Rewrite safety bypass rules for interactive/structured commands
233
+ ├── rewrite-rules.ts # Rewrite rule catalog
234
+ ├── rewrite-pipeline-safety.ts # Shell-safety fixups for rewritten commands
235
+ ├── rtk-command-environment.ts # RTK_DB_PATH scoping for rewritten commands
236
+ ├── shell-env-prefix.ts # Environment assignment parsing helpers
237
+ ├── runtime-guard.ts # Runtime availability guard helpers for rewrite mode
238
+ ├── output-compactor.ts # Tool result compaction pipeline
239
+ ├── output-metrics.ts # Savings tracking and reporting
240
+ ├── tool-execution-sanitizer.ts # Streaming bash execution output sanitizer
241
+ ├── command-completions.ts # /rtk subcommand completions
235
242
  ├── windows-command-helpers.ts # Windows bash compatibility
236
- └── techniques/ # Compaction technique implementations
243
+ └── techniques/ # Compaction technique implementations
237
244
  ├── ansi.ts # ANSI code stripping
238
245
  ├── build.ts # Build output filtering
239
246
  ├── test-output.ts # Test output aggregation
@@ -248,9 +255,12 @@ src/
248
255
 
249
256
  The extension hooks into Pi's event system:
250
257
 
251
- - **`beforeToolCall`** — Rewrites bash commands to rtk equivalents
252
- - **`afterToolResult`** — Compacts tool output before context consumption
253
- - **`command`** Handles `/rtk` command and subcommands
258
+ - **`tool_call`** — Rewrites bash commands to rtk equivalents or emits suggestions
259
+ - **`tool_result`** — Compacts completed tool output before context consumption
260
+ - **`tool_execution_start` / `tool_execution_update` / `tool_execution_end`** Tracks and sanitizes streamed bash output
261
+ - **`before_agent_start`** — Conditionally injects source-filter troubleshooting guidance
262
+ - **`session_start` / `agent_end`** — Refreshes config and clears in-session tracking state
263
+ - **Registered `/rtk` command** — Handles settings, status, verification, stats, and reset subcommands
254
264
 
255
265
  ### Windows Compatibility
256
266
 
package/package.json CHANGED
@@ -1,64 +1,64 @@
1
- {
2
- "name": "pi-rtk-optimizer",
3
- "version": "0.5.4",
4
- "description": "Pi extension that optimizes RTK command rewriting and tool output compaction for the coding agent.",
5
- "type": "module",
6
- "main": "./index.ts",
7
- "exports": {
8
- ".": "./index.ts"
9
- },
10
- "files": [
11
- "index.ts",
12
- "src",
13
- "config/config.example.json",
14
- "README.md",
15
- "CHANGELOG.md",
16
- "LICENSE"
17
- ],
18
- "scripts": {
19
- "build": "npx --yes -p typescript@5.7.3 tsc -p tsconfig.json --noCheck",
20
- "lint": "npm run build",
21
- "typecheck": "npx --yes -p typescript@5.7.3 tsc -p tsconfig.json",
22
- "test": "bun ./src/output-compactor-test.ts && bun ./src/command-rewriter-test.ts && bun ./src/runtime-guard-test.ts && bun ./src/additional-coverage-test.ts && bun ./src/config-modal-test.ts && bun ./src/index-test.ts",
23
- "check": "npm run lint && npm run typecheck && npm run test",
24
- "build:check": "bunx esbuild ./index.ts --bundle --platform=node --format=esm --outfile=./.pi-rtk-optimizer-check.mjs --external:@mariozechner/pi-coding-agent --external:@mariozechner/pi-tui && bun -e \"import { unlinkSync } from 'node:fs'; unlinkSync('./.pi-rtk-optimizer-check.mjs');\""
25
- },
26
- "keywords": [
27
- "pi-package",
28
- "pi",
29
- "pi-extension",
30
- "pi-coding-agent",
31
- "coding-agent",
32
- "rtk",
33
- "token-optimization",
34
- "tool-compaction",
35
- "output-compaction",
36
- "command-rewrite",
37
- "optimization"
38
- ],
39
- "author": "MasuRii",
40
- "license": "MIT",
41
- "repository": {
42
- "type": "git",
43
- "url": "git+https://github.com/MasuRii/pi-rtk-optimizer.git"
44
- },
45
- "bugs": {
46
- "url": "https://github.com/MasuRii/pi-rtk-optimizer/issues"
47
- },
48
- "homepage": "https://github.com/MasuRii/pi-rtk-optimizer#readme",
49
- "engines": {
50
- "node": ">=20"
51
- },
52
- "publishConfig": {
53
- "access": "public"
54
- },
55
- "pi": {
56
- "extensions": [
57
- "./index.ts"
58
- ]
59
- },
60
- "peerDependencies": {
61
- "@mariozechner/pi-coding-agent": "^0.68.1",
62
- "@mariozechner/pi-tui": "^0.68.1"
63
- }
64
- }
1
+ {
2
+ "name": "pi-rtk-optimizer",
3
+ "version": "0.5.5",
4
+ "description": "Pi extension that optimizes RTK command rewriting and tool output compaction for the coding agent.",
5
+ "type": "module",
6
+ "main": "./index.ts",
7
+ "exports": {
8
+ ".": "./index.ts"
9
+ },
10
+ "files": [
11
+ "index.ts",
12
+ "src",
13
+ "config/config.example.json",
14
+ "README.md",
15
+ "CHANGELOG.md",
16
+ "LICENSE"
17
+ ],
18
+ "scripts": {
19
+ "build": "npx --yes -p typescript@5.7.3 tsc -p tsconfig.json --noCheck",
20
+ "lint": "npm run build",
21
+ "typecheck": "npx --yes -p typescript@5.7.3 tsc -p tsconfig.json",
22
+ "test": "bun ./src/output-compactor-test.ts && bun ./src/command-rewriter-test.ts && bun ./src/runtime-guard-test.ts && bun ./src/additional-coverage-test.ts && bun ./src/config-modal-test.ts && bun ./src/index-test.ts",
23
+ "check": "npm run lint && npm run typecheck && npm run test",
24
+ "build:check": "bunx esbuild ./index.ts --bundle --platform=node --format=esm --outfile=./.pi-rtk-optimizer-check.mjs --external:@mariozechner/pi-coding-agent --external:@mariozechner/pi-tui && bun -e \"import { unlinkSync } from 'node:fs'; unlinkSync('./.pi-rtk-optimizer-check.mjs');\""
25
+ },
26
+ "keywords": [
27
+ "pi-package",
28
+ "pi",
29
+ "pi-extension",
30
+ "pi-coding-agent",
31
+ "coding-agent",
32
+ "rtk",
33
+ "token-optimization",
34
+ "tool-compaction",
35
+ "output-compaction",
36
+ "command-rewrite",
37
+ "optimization"
38
+ ],
39
+ "author": "MasuRii",
40
+ "license": "MIT",
41
+ "repository": {
42
+ "type": "git",
43
+ "url": "git+https://github.com/MasuRii/pi-rtk-optimizer.git"
44
+ },
45
+ "bugs": {
46
+ "url": "https://github.com/MasuRii/pi-rtk-optimizer/issues"
47
+ },
48
+ "homepage": "https://github.com/MasuRii/pi-rtk-optimizer#readme",
49
+ "engines": {
50
+ "node": ">=20"
51
+ },
52
+ "publishConfig": {
53
+ "access": "public"
54
+ },
55
+ "pi": {
56
+ "extensions": [
57
+ "./index.ts"
58
+ ]
59
+ },
60
+ "peerDependencies": {
61
+ "@mariozechner/pi-coding-agent": "^0.70.0",
62
+ "@mariozechner/pi-tui": "^0.70.0"
63
+ }
64
+ }
@@ -1,11 +1,20 @@
1
1
  import assert from "node:assert/strict";
2
- import { mock } from "bun:test";
3
2
  import { existsSync, readFileSync, unlinkSync, writeFileSync } from "node:fs";
3
+ import { mock } from "bun:test";
4
4
 
5
- const MOCK_AGENT_DIR = "C:/tmp/.pi/agent";
5
+ import { clearOutputMetrics, getOutputMetricsSummary, trackOutputSavings } from "./output-metrics.ts";
6
+ import { runTest } from "./test-helpers.ts";
7
+ import { matchesCommandPatterns, normalizeCommandForDetection } from "./techniques/command-detection.ts";
8
+ import { compactPath } from "./techniques/path-utils.ts";
9
+ import { applyWindowsBashCompatibilityFixes } from "./windows-command-helpers.ts";
10
+ import { applyRewrittenCommandShellSafetyFixups } from "./rewrite-pipeline-safety.ts";
11
+ import { applyRtkCommandEnvironment } from "./rtk-command-environment.ts";
12
+ import { sanitizeStreamingBashExecutionResult } from "./tool-execution-sanitizer.ts";
13
+ import { sanitizeRtkEmojiOutput } from "./techniques/emoji.ts";
14
+ import { stripRtkHookWarnings } from "./techniques/rtk.ts";
6
15
 
7
16
  mock.module("@mariozechner/pi-coding-agent", () => ({
8
- getAgentDir: () => MOCK_AGENT_DIR,
17
+ getAgentDir: () => "/tmp/.pi/agent",
9
18
  }));
10
19
 
11
20
  const {
@@ -15,16 +24,6 @@ const {
15
24
  normalizeRtkIntegrationConfig,
16
25
  saveRtkIntegrationConfig,
17
26
  } = await import("./config-store.ts");
18
- import { clearOutputMetrics, getOutputMetricsSummary, trackOutputSavings } from "./output-metrics.ts";
19
- import { runTest } from "./test-helpers.ts";
20
- import { matchesCommandPatterns, normalizeCommandForDetection } from "./techniques/command-detection.ts";
21
- import { compactPath } from "./techniques/path-utils.ts";
22
- import { applyWindowsBashCompatibilityFixes } from "./windows-command-helpers.ts";
23
- import { applyRewrittenCommandShellSafetyFixups } from "./rewrite-pipeline-safety.ts";
24
- import { applyRtkCommandEnvironment } from "./rtk-command-environment.ts";
25
- import { sanitizeStreamingBashExecutionResult } from "./tool-execution-sanitizer.ts";
26
- import { sanitizeRtkEmojiOutput } from "./techniques/emoji.ts";
27
- import { stripRtkHookWarnings } from "./techniques/rtk.ts";
28
27
 
29
28
  function makeTempConfigPath(): string {
30
29
  return `${getRtkIntegrationConfigPath()}.test-${Date.now()}-${Math.random().toString(16).slice(2)}.json`;
@@ -4,6 +4,7 @@ import { mock } from "bun:test";
4
4
  import { cloneDefaultConfig, runTest } from "./test-helpers.ts";
5
5
 
6
6
  mock.module("@mariozechner/pi-coding-agent", () => ({
7
+ getAgentDir: () => "/tmp/.pi/agent",
7
8
  getSettingsListTheme: () => ({}),
8
9
  }));
9
10
 
package/src/index-test.ts CHANGED
@@ -28,7 +28,38 @@ mock.module("@mariozechner/pi-tui", () => ({
28
28
  visibleWidth: (text: string) => text.length,
29
29
  }));
30
30
 
31
- const { createBoundedNoticeTracker } = await import("./index.ts");
31
+ const { createBoundedNoticeTracker, shouldInjectSourceFilterTroubleshootingNote } = await import("./index.ts");
32
+ const { DEFAULT_RTK_INTEGRATION_CONFIG } = await import("./types.ts");
33
+
34
+ function configWith(overrides: {
35
+ enabled?: boolean;
36
+ compactionEnabled?: boolean;
37
+ sourceFilteringEnabled?: boolean;
38
+ sourceFilteringLevel?: "none" | "minimal" | "aggressive";
39
+ smartTruncateEnabled?: boolean;
40
+ truncateEnabled?: boolean;
41
+ }): typeof DEFAULT_RTK_INTEGRATION_CONFIG {
42
+ const base = DEFAULT_RTK_INTEGRATION_CONFIG;
43
+ return {
44
+ ...base,
45
+ enabled: overrides.enabled ?? base.enabled,
46
+ outputCompaction: {
47
+ ...base.outputCompaction,
48
+ enabled: overrides.compactionEnabled ?? base.outputCompaction.enabled,
49
+ sourceCodeFilteringEnabled:
50
+ overrides.sourceFilteringEnabled ?? base.outputCompaction.sourceCodeFilteringEnabled,
51
+ sourceCodeFiltering: overrides.sourceFilteringLevel ?? base.outputCompaction.sourceCodeFiltering,
52
+ smartTruncate: {
53
+ ...base.outputCompaction.smartTruncate,
54
+ enabled: overrides.smartTruncateEnabled ?? base.outputCompaction.smartTruncate.enabled,
55
+ },
56
+ truncate: {
57
+ ...base.outputCompaction.truncate,
58
+ enabled: overrides.truncateEnabled ?? base.outputCompaction.truncate.enabled,
59
+ },
60
+ },
61
+ };
62
+ }
32
63
 
33
64
  runTest("bounded notice tracker evicts old entries and supports reset", () => {
34
65
  const tracker = createBoundedNoticeTracker(2);
@@ -52,4 +83,52 @@ runTest("bounded notice tracker coerces invalid limits to a safe minimum", () =>
52
83
  assert.equal(tracker.remember("alpha"), true);
53
84
  });
54
85
 
86
+ runTest("source-filter note injected when source filtering is active", () => {
87
+ assert.equal(
88
+ shouldInjectSourceFilterTroubleshootingNote(
89
+ configWith({ sourceFilteringEnabled: true, sourceFilteringLevel: "minimal" }),
90
+ ),
91
+ true,
92
+ );
93
+ assert.equal(
94
+ shouldInjectSourceFilterTroubleshootingNote(
95
+ configWith({ sourceFilteringEnabled: true, sourceFilteringLevel: "aggressive" }),
96
+ ),
97
+ true,
98
+ );
99
+ });
100
+
101
+ runTest("source-filter note skipped when extension is disabled", () => {
102
+ assert.equal(shouldInjectSourceFilterTroubleshootingNote(configWith({ enabled: false })), false);
103
+ });
104
+
105
+ runTest("source-filter note skipped when compaction is disabled", () => {
106
+ assert.equal(shouldInjectSourceFilterTroubleshootingNote(configWith({ compactionEnabled: false })), false);
107
+ });
108
+
109
+ runTest("source-filter note skipped when source filtering flag is off", () => {
110
+ assert.equal(
111
+ shouldInjectSourceFilterTroubleshootingNote(configWith({ sourceFilteringEnabled: false })),
112
+ false,
113
+ );
114
+ });
115
+
116
+ runTest("source-filter note skipped when filtering level is 'none'", () => {
117
+ assert.equal(
118
+ shouldInjectSourceFilterTroubleshootingNote(
119
+ configWith({ sourceFilteringEnabled: true, sourceFilteringLevel: "none" }),
120
+ ),
121
+ false,
122
+ );
123
+ });
124
+
125
+ runTest("source-filter note skipped when all read filtering safeguards are disabled", () => {
126
+ assert.equal(
127
+ shouldInjectSourceFilterTroubleshootingNote(
128
+ configWith({ smartTruncateEnabled: false, truncateEnabled: false }),
129
+ ),
130
+ false,
131
+ );
132
+ });
133
+
55
134
  console.log("All index tests passed.");
package/src/index.ts CHANGED
@@ -30,6 +30,17 @@ function trimMessage(raw: string, maxLength = 220): string {
30
30
  const SOURCE_FILTER_TROUBLESHOOTING_NOTE =
31
31
  "RTK note: If file edits repeatedly fail because old text does not match, ask the user to manually run '/rtk' in the Pi TUI, disable 'Read source filtering enabled', re-read the file, apply the edit, then ask the user to manually re-enable it in the Pi TUI.";
32
32
 
33
+ export function shouldInjectSourceFilterTroubleshootingNote(config: RtkIntegrationConfig): boolean {
34
+ const compaction = config.outputCompaction;
35
+ return (
36
+ config.enabled &&
37
+ compaction.enabled &&
38
+ compaction.sourceCodeFilteringEnabled &&
39
+ compaction.sourceCodeFiltering !== "none" &&
40
+ (compaction.smartTruncate.enabled || compaction.truncate.enabled)
41
+ );
42
+ }
43
+
33
44
  function mergeCompactionDetails(
34
45
  existingDetails: unknown,
35
46
  compaction: ToolResultCompactionMetadata,
@@ -316,6 +327,10 @@ export default function rtkIntegrationExtension(pi: ExtensionAPI): void {
316
327
  await ensureRuntimeStatusFresh();
317
328
  maybeWarnRtkMissing(ctx);
318
329
 
330
+ if (!shouldInjectSourceFilterTroubleshootingNote(config)) {
331
+ return {};
332
+ }
333
+
319
334
  if (event.systemPrompt.includes(SOURCE_FILTER_TROUBLESHOOTING_NOTE)) {
320
335
  return {};
321
336
  }
@@ -1,13 +1,13 @@
1
1
  import assert from "node:assert/strict";
2
- import { mock } from "bun:test";
3
2
  import { join } from "node:path";
3
+ import { mock } from "bun:test";
4
4
 
5
5
  import { cloneDefaultConfig, runTest } from "./test-helpers.ts";
6
6
 
7
- const MOCK_AGENT_DIR = "C:/tmp/.pi/agent";
7
+ const TEST_AGENT_DIR = "/tmp/.pi/agent";
8
8
 
9
9
  mock.module("@mariozechner/pi-coding-agent", () => ({
10
- getAgentDir: () => MOCK_AGENT_DIR,
10
+ getAgentDir: () => TEST_AGENT_DIR,
11
11
  }));
12
12
 
13
13
  const { compactToolResult } = await import("./output-compactor.ts");
@@ -223,7 +223,7 @@ runTest("skill reads stay exact when preserveExactSkillReads is enabled for user
223
223
  const result = compactToolResult(
224
224
  {
225
225
  toolName: "read",
226
- input: { path: join(MOCK_AGENT_DIR, "skills", "example", "SKILL.md") },
226
+ input: { path: join(TEST_AGENT_DIR, "skills", "example", "SKILL.md") },
227
227
  content: [{ type: "text", text: content }],
228
228
  },
229
229
  config,