@softspark/ai-toolkit 4.17.0 → 4.19.0

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.
@@ -0,0 +1,710 @@
1
+ ---
2
+ title: "Plan: rtk Pack Integration"
3
+ category: planning
4
+ service: ai-toolkit
5
+ tags:
6
+ - rtk
7
+ - plugin-pack
8
+ - token-reduction
9
+ - vendored-binaries
10
+ - cross-build
11
+ - upstream-sync
12
+ doc_type: postmortem
13
+ status: completed
14
+ created: "2026-07-26"
15
+ last_updated: "2026-07-26"
16
+ completion: "100% — phases 0, 1, 2, 4, 5 delivered; phase 3 cut on the measurement"
17
+ shipped_in: "v4.18.0"
18
+ pinned_upstream: "v0.44.0"
19
+ description: "Integrate rtk as an opt-in ai-toolkit plugin pack: binaries cross-built from source in our CI with telemetry disabled, hosted on our own GitHub Release, fetched and checksum-verified at pack install, auto-update on ai-toolkit update, and an SOP for tracking upstream releases. Custom filter presets were cut on the measurement (0.008% of input tokens). Phase 0 validated the premise against v0.44.0 on the full transcript corpus before any build work."
20
+ ---
21
+
22
+ # rtk Pack Integration
23
+
24
+ **Completed 2026-07-26, shipped in v4.18.0.** Kept whole rather than summarised,
25
+ because the value here is the measurements and the things that turned out to be
26
+ false, not the plan structure. Live procedure:
27
+ `kb/procedures/rtk-upstream-sync-sop.md`.
28
+
29
+ Headline: rtk-pack ships opt-in, built from source with telemetry compiled out,
30
+ and saves a **measured 0.0615% of input tokens** against a kill number of 0.05%
31
+ published before the measurement. Phase 3 (our own filter presets) was cut at
32
+ 0.008%. The projection this plan published for itself was about twice too
33
+ optimistic, and section 10.1 says why.
34
+
35
+ ## 1. Decision
36
+
37
+ Ship `rtk` to ai-toolkit users as an **opt-in plugin pack**, with the binary
38
+ supply chain owned end to end by us.
39
+
40
+ Five choices, made and locked:
41
+
42
+ | Question | Decision |
43
+ |---|---|
44
+ | Delivery | Plugin pack (`rtk-pack`), never a native feature |
45
+ | Binary hosting | Our own GitHub Release, fetched and SHA-256 verified at pack install |
46
+ | Binary provenance | Cross-built from upstream source in our CI, `RTK_TELEMETRY_URL` unset |
47
+ | Pinned upstream tag | **v0.44.0** (2026-07-26). v0.43.0 was ruled out on evidence, see section 2 |
48
+ | Upstream tracking | An SOP that rebuilds, verifies, and re-publishes when rtk ships a release |
49
+
50
+ Upstream: https://github.com/rtk-ai/rtk, Apache-2.0, Rust 1.91 plus a C
51
+ toolchain (rusqlite `bundled` compiles SQLite from source).
52
+
53
+ ## 2. Phase 0: premise validated before building anything
54
+
55
+ The retired output filter validated its design across five phases and its
56
+ premise not at all until the fifth. This plan runs premise validation first.
57
+ Everything below is measured, not estimated.
58
+
59
+ ### 2.1 Why v0.43.0 was ruled out
60
+
61
+ Two structural facts, read from the source of both tags:
62
+
63
+ - **Custom TOML filters are not wired into the rewrite path in v0.43.0.**
64
+ `src/discover/registry.rs` at v0.43.0 contains zero references to
65
+ `toml_filter`. In v0.44.0 they appear at `registry.rs:1047,1055,1058`.
66
+ *This reason no longer applies:* section 7 cut our own presets, so nothing we
67
+ ship depends on that wiring. It is recorded because it was one of the two
68
+ arguments made at pin time, and the pin now rests on the second alone.
69
+ - **`pipeline_final_safe` does not exist in v0.43.0.** It arrives in v0.44.0
70
+ (`rules.rs:7,115,124`, `registry.rs:605,659,864,1069`). v0.43.0 rewrote the
71
+ first pipeline stage; v0.44.0 rewrites the last. On this traffic 56.5% of
72
+ `rtk grep` hits arrive through the pipeline-final path, so they do not exist
73
+ at all on v0.43.0.
74
+
75
+ ### 2.2 The port models v0.44.0, not v0.43.0
76
+
77
+ `rtk_port.py` is re-validated by extracting the `assert_eq!` assertions from
78
+ each tag's `registry.rs` `#[cfg(test)]` block and replaying them:
79
+
80
+ | Port checked against | Assertions | Exact-string agreement |
81
+ |---|---:|---:|
82
+ | **v0.44.0** | 203 | **203/203** |
83
+ | v0.43.0 | 160 | 147/160 |
84
+
85
+ The 13 v0.43.0 failures are all the same pipeline inversion. The port also
86
+ passes the 15 assertions v0.44.0 added after the port was written. Pinning
87
+ v0.44.0 makes the existing measurement correct rather than requiring rework.
88
+
89
+ `registry.rs:590-592` returns `Some(unchanged)` for a simple already-`rtk`
90
+ command; the port's equivalent bail must be mapped to that, not to `None`.
91
+
92
+ ### 2.3 The recorded numbers came from an 11% sample
93
+
94
+ `measure_rtk.py` sets `N_FILES = 134` and takes the most recently modified
95
+ transcripts. The available pool is **1224 transcripts**. At n=134 the projected
96
+ saving swings **8.8x** across windows (0.071% to 0.628%); at n>=408 it converges
97
+ to within **1.05x**. The spread recorded in earlier revisions of this plan was
98
+ sampling noise.
99
+
100
+ ### 2.4 The port over-counted, and by how much
101
+
102
+ Both real entry points call `contains_unattestable_construct` **before**
103
+ `registry::rewrite_command`: `hook_cmd.rs:149-151` (Defer) and
104
+ `rewrite_cmd.rs:54-56` (Passthrough). Any command carrying a command
105
+ substitution, a process substitution, or a redirect with a file target is
106
+ rejected whole. Upstream's rewrite tests call `rewrite_command` directly, so a
107
+ port validated only against them passes while still over-counting.
108
+
109
+ The gate is ported from `lexer.rs:295-347` and validated **35/35** against
110
+ upstream's own assertions at `lexer.rs:1186-1276`. Applying it costs 3.1%
111
+ relative coverage.
112
+
113
+ ### 2.5 Corrected numbers
114
+
115
+ Full corpus, 1224 transcripts, 552 MB, 106 MB of tool results, 26.6 MB of
116
+ successful Bash, entry gate applied:
117
+
118
+ | Metric | Earlier revision (n=134, ungated) | Phase 0 (n=1224, gated) |
119
+ |---|---:|---:|
120
+ | Coverage of successful Bash bytes | 31.5% | **35.17%** |
121
+ | Coverage of all tool-result bytes | 9.69% | **8.77%** |
122
+ | Ceiling: tool results as share of input volume | 5.52% | **4.54%** |
123
+ | Projection at rtk's 60-90% claim | 0.32-0.48% | **0.239-0.359%** |
124
+ | Projection, mechanism modelled | 0.15-0.21% | **0.117-0.164%** |
125
+ | Reach of our own TOML presets (the number that cut section 7) | 1.91% of Bash | **1.08% of Bash** |
126
+ | `Read` share of tool-result bytes | 53.8% | **62.8%** |
127
+
128
+ Modelled effectiveness per family against the claim: `rtk grep` 75% claimed and
129
+ **9.0%** modelled; `rtk read` 60% claimed and **0.0%** modelled; `rtk rg` 75%
130
+ and 30%; `rtk make` 65% and 16%; `rtk jq` 74%.
131
+
132
+ Largest unaddressed buckets, as a share of successful Bash bytes: pipeline not a
133
+ rewritable final stage **39.78%**, ignored by rtk **10.72%**, entry gate
134
+ **8.29%**, multiline script **4.71%**.
135
+
136
+ ### 2.6 Open question 3 answered: no
137
+
138
+ Patching `pipeline_final_safe` for `head` and `tail` unlocks 10.56 MB, 39.96% of
139
+ Bash bytes. **88.3% of that (9.33 MB) routes to `rtk read`, measured at 0.00%
140
+ saving** on this traffic because it returns files verbatim at the default
141
+ `--level none`. The largest reachable gap is reachable and worthless. The
142
+ argument for maintaining a fork does not survive its own measurement.
143
+
144
+ ## 3. Why a pack rather than a native feature
145
+
146
+ - **Opt-in is structural, not a policy.** Plugin packs must not be
147
+ auto-installed [PATH: kb/reference/plugin-pack-conventions.md]. The retired
148
+ output filter shipped `off` by default and still had to be wired into every
149
+ install path, which is why v4.17.0 needed a bespoke migration cleanup.
150
+ - **Removal is already solved.** `plugin remove` strips hook entries from
151
+ `settings.json`, deletes owned `plugin-<pack>-*` assets, and leaves core and
152
+ user files alone.
153
+ - **The core package stays lean.** ai-toolkit is 4.4 MB unpacked; the platform
154
+ artifacts are ~19.5 MB compressed. They never enter the base package.
155
+ - **There is a working precedent.** `memory-pack` ships hooks, a shared script
156
+ directory, an install-time init script, its own data with retention, and a
157
+ clean uninstall. `rtk-pack` is the same shape with a binary instead of a
158
+ database.
159
+
160
+ ## 4. Architecture
161
+
162
+ ```text
163
+ app/plugins/rtk-pack/
164
+ ├── plugin.json # pinned rtk version + per-platform SHA-256
165
+ ├── README.md
166
+ ├── hooks/
167
+ │ └── rewrite.sh # PreToolUse; delegates to the fetched binary
168
+ └── scripts/
169
+ ├── init.py # platform detect → fetch → verify → install
170
+ └── status.py # reports binary, digest and hook wiring
171
+ ```
172
+
173
+ Runtime layout on a user machine:
174
+
175
+ ```text
176
+ ~/.softspark/ai-toolkit/
177
+ ├── hooks/plugin-rtk-pack-rewrite.sh # installed hook
178
+ └── plugin-scripts/rtk-pack/
179
+ ├── bin/rtk # verified binary, 0755
180
+ └── version.json # pinned version + recorded digest
181
+ ```
182
+
183
+ Everything the pack writes lives under `plugin-scripts/rtk-pack/`, which
184
+ `plugin remove` deletes wholesale (`plugin.py:500-503`). The pack owns no state
185
+ outside the toolkit data directory: no `filters.toml`, no entry in rtk's trust
186
+ store, nothing in an OS config directory. That is a consequence of cutting
187
+ section 7 and it is what keeps removal complete.
188
+
189
+ Binary supply chain:
190
+
191
+ ```text
192
+ upstream tag → our CI cross-build (RTK_TELEMETRY_URL unset)
193
+ → our GitHub Release softspark-rtk-<upstream>-<build>
194
+ → SHA-256 recorded in plugin.json
195
+ → plugin install: detect platform, fetch, verify, chmod
196
+ ```
197
+
198
+ ## 5. Phase 1: build pipeline and binary release
199
+
200
+ **Outcome:** we can produce, from an upstream tag, a set of binaries that
201
+ provably make no network calls of their own.
202
+
203
+ **Targets: five, not six.** `x86_64-apple-darwin`, `aarch64-apple-darwin`,
204
+ `x86_64-unknown-linux-musl`, `aarch64-unknown-linux-gnu`,
205
+ `x86_64-pc-windows-msvc`. Earlier revisions added `x86_64-unknown-linux-gnu` on
206
+ the reasoning that upstream does not ship it. It is dropped: upstream's own
207
+ Homebrew formula routes Linux x86_64 to the **musl** tarball
208
+ (`release.yml:314-316`), a static musl binary runs on glibc, and upstream has
209
+ never validated a gnu x86_64 artifact, so we would be first to ship one and
210
+ would own all its breakage plus an uncontrolled glibc floor inherited from the
211
+ runner image.
212
+
213
+ Build constraints, all read from the pinned checkout:
214
+
215
+ - **Pin the toolchain.** Upstream uses `dtolnay/rust-toolchain@stable`
216
+ (`release.yml:65,116`) with `warnings = "deny"` (`Cargo.toml:70-72`) and ships
217
+ no `rust-toolchain` file. A future rustc lint turns our builds red with no
218
+ change on either side. We pin an exact version at or above 1.91.
219
+ - **Windows must build natively.** `build.rs:6-13` emits
220
+ `cargo:rustc-link-arg=/STACK:8388608` under `#[cfg(windows)]`, which in a
221
+ build script is a **host** predicate. Cross-building Windows from Linux
222
+ silently drops the 8 MiB stack reservation that upstream's own comment says is
223
+ what makes `rtk.exe --version`, `--help`, and hook entry points start
224
+ reliably. Build on `windows-latest`, or pass the link-arg explicitly.
225
+ - **Both darwin targets build on `macos-latest` (arm64).** `macos-13` is the
226
+ last x86_64 macOS image and GitHub is winding it down: on runs 30212577757 and
227
+ 30214341444 that job sat queued indefinitely while every other target
228
+ finished. Upstream builds both on arm64 too, but never runs its x86_64
229
+ artifact. We do: the verifier detects Rosetta 2 and executes it, and reports
230
+ `inconclusive` rather than `pass` if it cannot.
231
+ - **Every target needs a target-capable C compiler.** rusqlite `bundled`
232
+ (`Cargo.toml:26`) is not switchable off; there is no `[features]` table and no
233
+ `cfg(feature` in `src/`. There is no pure-Rust escape route.
234
+ - **aarch64-linux is the hard case, but upstream already solves it** with
235
+ `cross: true` on `ubuntu-latest` (`release.yml:51-54`). Copy that. Upstream
236
+ sets only the linker env var and lets the `cc` crate guess the C compiler;
237
+ export `CC_aarch64_unknown_linux_gnu` and `AR_aarch64_unknown_linux_gnu`
238
+ explicitly so a runner image change surfaces as a clear error.
239
+ - **Run our own blocking `cargo audit`.** Upstream's is advisory: `ci.yml:88-96`
240
+ swallows failures into a warning. Rebuilding from a tag inherits that
241
+ lockfile, and on v0.44.0 it inherits four advisories across three of its 203
242
+ crates. The audit and the build jobs apply the same remediation, so we audit
243
+ the dependency set we ship rather than a different one:
244
+
245
+ | Crate | Advisory | Disposition |
246
+ |---|---|---|
247
+ | `anyhow` 1.0.102 | RUSTSEC-2026-0190 | `cargo update` to 1.0.103, in range |
248
+ | `crossbeam-epoch` 0.9.18 | RUSTSEC-2026-0204 | `cargo update` to 0.9.20, in range |
249
+ | `quick-xml` 0.37.5 | RUSTSEC-2026-0194, RUSTSEC-2026-0195 | ignored with reasons |
250
+
251
+ The quick-xml fix lands only in 0.41.0 while rtk pins `"0.37"` as a direct
252
+ dependency (`Cargo.toml:34`), so taking it means editing `Cargo.toml` and the
253
+ calling code in `src/cmds/dotnet/dotnet_trx.rs`. That would break the promise
254
+ that the only difference from an upstream build is the undefined telemetry
255
+ endpoint. The reachable surface is narrow: quick-xml parses .NET TRX test
256
+ output only, the input is a report produced locally by the user's own test
257
+ run, and both advisories are availability-only (CVSS `C:N/I:N/A:H`). The SOP
258
+ re-checks this on every sync and deletes the ignores once upstream moves to
259
+ quick-xml 0.41 or later.
260
+ - A target that will not build is dropped, not faked.
261
+
262
+ ### 5.1 Proving the binary is silent
263
+
264
+ The endpoint is compile-time only: `option_env!("RTK_TELEMETRY_URL")` at
265
+ `telemetry.rs:16` and `telemetry_cmd.rs:176`, with the token at
266
+ `telemetry.rs:17` and `telemetry_cmd.rs:189`. Nothing supplies a default:
267
+ `build.rs` emits no `cargo:rustc-env`, `TelemetryConfig` has no URL field, and
268
+ no runtime `std::env::var` resolves the endpoint. Upstream injects it only in
269
+ `release.yml:85-86,124-125,151-152`. Building without it yields a binary whose
270
+ telemetry destination cannot be re-enabled by any env var or config at runtime.
271
+
272
+ **The acceptance criterion in earlier revisions was untestable and is replaced.**
273
+ "No telemetry symbols in the binary" cannot be checked: the guard is a runtime
274
+ `if TELEMETRY_URL.is_none()` on a const (`telemetry.rs:23-26`), not a `#[cfg]`,
275
+ so the code compiles in and is only eliminated by LLVM as an optimisation; and
276
+ `Cargo.toml:51` sets `strip = true`, which removes the symbols regardless of
277
+ whether the code is present. A symbol check would pass for the wrong reason.
278
+
279
+ What we assert instead:
280
+
281
+ 0. **The artifact was actually started.** Every other assertion is worthless on
282
+ a binary nobody executed, and two of the five targets are not native to their
283
+ runner. `aarch64-unknown-linux-gnu` runs under `qemu-user` with the cross
284
+ sysroot passed as `-L`, and `x86_64-apple-darwin` runs under Rosetta 2 on the
285
+ arm64 runner. A target that cannot be started reports `inconclusive`, never
286
+ `pass`.
287
+ 1. **Build gate.** `RTK_TELEMETRY_URL` and `RTK_TELEMETRY_TOKEN` are unset in
288
+ the build environment, asserted in CI before `cargo build`, with a clean
289
+ target directory per build.
290
+ 2. **Offline smoke run.** Each artifact runs its real command surface with no
291
+ network route available and makes zero outbound connections. This is
292
+ Linux-only: `unshare -rn` has no unprivileged equivalent on macOS or Windows
293
+ runners, and Ubuntu 24.04's
294
+ `kernel.apparmor_restrict_unprivileged_userns=1` means even there it needs
295
+ `sudo`. On the first run this assertion silently degraded to a skip on every
296
+ target while the verdict still read `pass`, so the manifest now records which
297
+ isolator was used and a target that could not be started at all reports
298
+ `inconclusive` rather than `pass`.
299
+ 3. **Filesystem assertion.** No telemetry state is created under the resolved
300
+ data directory.
301
+ 4. **Drift detection.** Record artifact size and a string-allowlist hash per
302
+ target and fail the build on unexplained drift, since whether `ureq`,
303
+ `rustls`, `ring` and `webpki-roots` are actually eliminated is an LLVM
304
+ outcome under `lto = true`, not a guarantee.
305
+
306
+ Three findings that must reach the pack README:
307
+
308
+ - **`rtk cc` shells out to `npx --yes ccusage`** when `ccusage` is not on PATH
309
+ (`ccusage.rs:104-119`). That is a runtime npm fetch and third-party code
310
+ execution. It is not telemetry and not automatic, but it is outbound network
311
+ from a binary we tell users makes no network calls.
312
+ - **`RTK_TELEMETRY_DISABLED` only accepts the exact string `1`**
313
+ (`telemetry_cmd.rs:31-33`, locked by the test at `:222-229`). `true` and `yes`
314
+ are silent no-ops. Never write anything else anywhere in the pack.
315
+ - **`rtk telemetry forget` on an endpoint-free build prints a misleading
316
+ failure** telling the user to email upstream to complete erasure
317
+ (`telemetry_cmd.rs:158-169`), when nothing was ever sent.
318
+
319
+ Apache-2.0 obligations ship alongside: upstream `LICENSE`, a `NOTICE` file
320
+ (upstream has none), and a statement that the only build-time change is leaving
321
+ the telemetry endpoint undefined. **Do not redistribute upstream
322
+ `DISCLAIMER.md` verbatim**: `DISCLAIMER.md:25` states telemetry is collected by
323
+ default, which the code contradicts and which a compliance reviewer would read
324
+ as a reason to block the pack. `docs/TELEMETRY.md:180` calls the mechanism
325
+ "compile-time gating" where "all telemetry code is dead", which is imprecise for
326
+ the same reason our own criterion changed.
327
+
328
+ **Success criteria:** five artifacts build from the pinned tag; each runs
329
+ `rtk --version` on its target; the silence assertions pass or say plainly that
330
+ they did not run; checksums published.
331
+
332
+ ### 5.2 Result, run 30214882862
333
+
334
+ All five targets build and verify. Every artifact was actually started, none
335
+ shipped on a skipped assertion.
336
+
337
+ | Target | `runs` | `offline` |
338
+ |---|---|---|
339
+ | `aarch64-apple-darwin` | native | not applicable |
340
+ | `x86_64-apple-darwin` | translated via Rosetta 2 | not applicable |
341
+ | `x86_64-unknown-linux-musl` | native | passed under `sudo -n unshare -rn` |
342
+ | `aarch64-unknown-linux-gnu` | emulated via `qemu-aarch64-static` | passed under `sudo -n unshare -rn` |
343
+ | `x86_64-pc-windows-msvc` | native | not applicable |
344
+
345
+ No TLS markers in any artifact, so `ureq`, `rustls`, `ring` and `webpki-roots`
346
+ appear to be eliminated under LTO once the endpoint const is `None`. That is an
347
+ optimisation outcome rather than a guarantee, which is why the fingerprint is
348
+ recorded per target and drift fails the build.
349
+
350
+ **Four of five targets are bit-reproducible.** Across independent runs with
351
+ identical inputs, `aarch64-apple-darwin`, `x86_64-apple-darwin`,
352
+ `aarch64-unknown-linux-gnu` and `x86_64-unknown-linux-musl` produced
353
+ byte-identical binaries. The concern about unset `trim-paths` embedding registry
354
+ paths does not materialise on these runners.
355
+
356
+ `x86_64-pc-windows-msvc` did not, and the cause was bounded: **24 bytes out of
357
+ 8,018,432 differed**, in five regions. Four were the same two-byte value at
358
+ `0x100` and in the debug directory, which is the MSVC link timestamp; the fifth
359
+ was a 16-byte CodeView GUID. The code was identical; only the link stamp varied.
360
+
361
+ `-C link-arg=/Brepro` is now set for the Windows target, which makes the linker
362
+ derive that timestamp from content rather than the clock. **The shipped
363
+ `softspark-rtk-v0.44.0-1` artifacts predate that change**, so the Windows digest
364
+ in `plugin.json` is from a non-deterministic link; the flag takes effect on the
365
+ next rebuild, which will produce a different Windows digest for the same source.
366
+
367
+ This matters for the sync SOP: a changed digest means changed input rather than
368
+ build noise. That inference holds on four targets today and on all five from the
369
+ next rebuild onward.
370
+
371
+ Note that the **archives** are not reproducible even where the binaries are,
372
+ because gzip records a timestamp. Compare extracted binaries, never the
373
+ tarballs.
374
+
375
+ Three defects the run surfaced, all now fixed and guarded:
376
+
377
+ - The gnu target is dynamically linked, so `qemu-user` needs the cross sysroot.
378
+ It is passed as `-L` rather than `QEMU_LD_PREFIX`, because the offline check
379
+ runs under `sudo` and `env_reset` drops the variable.
380
+ - The offline check compared a plain run against a namespaced one, so `sudo`'s
381
+ environment handling was confounded with the network variable and a harness
382
+ failure was reported as a behaviour difference in the binary. Both sides now
383
+ run in a namespace and differ only in `-n`.
384
+ - `7z` stored the full relative path, so the Windows zip held
385
+ `rtk-src/target/<triple>/release/rtk.exe` while every tar.gz held a bare
386
+ `rtk`. The pack installer extracts all five the same way, so this was a
387
+ silent install failure on one platform. A `collect` step now asserts every
388
+ archive holds exactly one flat entry.
389
+
390
+ ## 6. Phase 2: the pack
391
+
392
+ **Outcome:** `ai-toolkit plugin install rtk-pack` produces a working setup and
393
+ `plugin remove` leaves nothing behind.
394
+
395
+ - `scripts/init.py` detects platform and libc, fetches the matching artifact,
396
+ verifies its SHA-256 against `plugin.json`, and installs to
397
+ `plugin-scripts/rtk-pack/bin/rtk`. A mismatch aborts and removes the partial
398
+ download. No network at runtime, only at install.
399
+ - Failure to fetch is not an install failure: the pack degrades to inert and
400
+ says so, matching how the core behaves when `jq` is missing.
401
+ - The hook is wired at `PreToolUse` through the existing pack hook mechanism, so
402
+ `plugin remove` strips it via the `_source` marker.
403
+ - `plugin status` dispatches to the pack's own `scripts/status.py`, which is
404
+ generic rather than another hardcoded `if name == ...` branch. It reports the
405
+ binary, the install record, the live version, and hook wiring, and announces a
406
+ missing binary as inert rather than letting a green install imply it works.
407
+
408
+ ### 6.1 No trust step, because the pack ships no filters
409
+
410
+ Section 7 is cut, so the pack writes no `filters.toml` and takes no entry in
411
+ rtk's trust store. That removes the single highest-ranked risk this plan
412
+ carried: an untrusted or content-changed preset produces **zero output on the
413
+ command path with no warning, no stderr line, and no non-zero exit**
414
+ (`toml_filter.rs:220-221`, `:450-458`) — installed, apparently fine, doing
415
+ nothing, which is exactly the shape of failure that retired the in-house filter.
416
+
417
+ The pack instead reports whether it is actually working. `scripts/status.py`
418
+ distinguishes installed from functioning: a missing binary is announced as
419
+ `MISSING — the hook is inert and every command runs unchanged` rather than
420
+ inferred from a green install.
421
+
422
+ **If presets are ever revisited, these are the constraints, kept because they
423
+ were expensive to establish:**
424
+
425
+ - Trust is a byte-exact SHA-256 (`trust.rs:142-163`), so every write, upgrade,
426
+ repair, trailing newline, or git CRLF normalisation invalidates it.
427
+ - `rtk trust --yes` is indiscriminate: it trusts every gated file that exists,
428
+ including a `.rtk/filters.toml` an attacker committed into the CWD
429
+ (`trust.rs:262-305`, project path is CWD-relative at `:207`).
430
+ - It exits 1 both when there is nothing to trust and when the file is invalid
431
+ TOML (`trust.rs:307-312`), so `set -e` aborts on the benign case.
432
+ - There is no `rtk doctor`, and `rtk trust --list` never re-hashes
433
+ (`trust.rs:242-256`). Verification means reading the store and recomputing.
434
+ - The global path is not `~/.config/rtk/filters.toml` everywhere: it comes from
435
+ `dirs::config_dir()` (`trust.rs:206-216`), which on macOS is
436
+ `~/Library/Application Support`. rtk's own error text hardcodes the Linux path
437
+ and is wrong there.
438
+ - One global file, no include mechanism, so injection would have to follow the
439
+ merge-friendly install model [PATH: kb/reference/merge-friendly-install-model.md]
440
+ with comment markers rather than owning the file.
441
+
442
+ **Success criteria:** install, status, update, remove, and re-install are
443
+ idempotent; a machine without the pack behaves exactly as today; uninstalling
444
+ ai-toolkit removes every pack artifact.
445
+
446
+ ## 7. Phase 3: cut
447
+
448
+ **Cut on the measurement, 2026-07-26.** Custom TOML presets reach **1.08% of
449
+ Bash bytes**, which is 0.27% of tool-result bytes. At a generous 60% reduction
450
+ that is **0.008% of input tokens**, roughly one part in twelve thousand.
451
+
452
+ Against that: presets would have required a trust step that fails silently, a
453
+ doctor check that recomputes digests, marker injection into a file the user may
454
+ also edit, re-trusting after every write, and a per-upstream-release review of a
455
+ DSL whose `deny_unknown_fields` rejects the entire file on one unknown key. The
456
+ maintenance surface is large, the payoff is not measurable, and every item on
457
+ that list is a way for the pack to look installed while doing nothing.
458
+
459
+ The pack therefore ships upstream rtk's built-in filters and nothing of our own.
460
+ The 35.2% coverage quoted in section 2.5 is already what those built-ins
461
+ deliver; it does not shrink as a result of this cut.
462
+
463
+ Two consequences recorded so they are not rediscovered:
464
+
465
+ - The pack owns no state outside `~/.softspark/ai-toolkit/plugin-scripts/rtk-pack/`,
466
+ which is what makes `plugin remove` complete.
467
+ - One of the two arguments for pinning v0.44.0 was that custom filters are only
468
+ wired into the rewrite path there. That argument is now moot; the pin rests on
469
+ `pipeline_final_safe`, which carries 56.5% of `rtk grep` hits on this traffic
470
+ and does not exist in v0.43.0. See section 2.1.
471
+
472
+ Reopening this is a plan change, not a task: it needs a workload where rtk's
473
+ built-ins measurably under-perform and a preset that measurably closes the gap,
474
+ demonstrated by replay before any code is written.
475
+
476
+ ## 8. Phase 4: auto-update on `ai-toolkit update`
477
+
478
+ **Outcome:** a user who installed the pack gets the new binary by running the
479
+ update they already run.
480
+
481
+ Verified gap: `handleUpdate` in `bin/ai-toolkit.js` reads installed modules from
482
+ `state.json` and does not touch `plugins.json`. Nothing propagates to packs
483
+ today, so this is new wiring, not a configuration change.
484
+
485
+ **Done.** `ai-toolkit update` now calls `plugin update --editor all --all`
486
+ after the core update. Two things had to change first, both generic rather than
487
+ rtk-specific:
488
+
489
+ - `update_pack` was an unconditional remove-then-reinstall, so wiring it into
490
+ the core update would have refetched the binary on every run. `plugins.json`
491
+ now records the pack version installed per editor, and an update whose
492
+ manifest version matches is a silent no-op. `--force` overrides.
493
+ - State written before versions were tracked has no `versions` map, so every
494
+ pack reads as stale exactly once and is updated once. That is the intended
495
+ migration, not a bug.
496
+
497
+ `--local` leaves packs alone: they live in `~/.softspark/ai-toolkit` and are
498
+ global, while `--local` is project-local config only.
499
+
500
+ - After the core update completes, `ai-toolkit update` reads `plugins.json` and
501
+ runs the equivalent of `plugin update` for every **currently installed** pack.
502
+ Packs that are not installed stay untouched, which preserves adoption rule 1.
503
+ - The rtk-pack update path is: compare the manifest's pinned version against the
504
+ recorded pack version; if they differ, fetch and verify the new binary and
505
+ re-install the hook.
506
+ - `--dry-run` reports what would change per pack.
507
+ - A pack update failure warns and continues; it never fails the core update.
508
+ - Bats coverage in `tests/test_plugin_update.bats`: stale version replaced;
509
+ current version untouched and silent; pack absent means no work; `--dry-run`
510
+ reports without acting; legacy state with no version map updates once; a
511
+ corrupt `plugins.json` does not crash the run.
512
+
513
+ **Success criteria:** the wiring is generic across packs, not rtk-specific;
514
+ `update` remains idempotent; a failed pack update never leaves a half-installed
515
+ binary.
516
+
517
+ ## 9. Phase 5: upstream sync SOP
518
+
519
+ **Outcome:** a written procedure so tracking upstream is routine rather than a
520
+ research project each time. **Written**, at
521
+ `kb/procedures/rtk-upstream-sync-sop.md`, modelled on the existing
522
+ ecosystem-sync SOP.
523
+
524
+ The 0.43.0 to 0.44.0 bump is a worked example of why the review step exists: 200
525
+ commits, and every file the SOP names changed, including a semantic inversion in
526
+ pipeline rewriting and the arrival of the trust gate.
527
+
528
+ Steps the SOP must cover:
529
+
530
+ 1. **Detect.** Check the upstream releases feed for a tag newer than the pinned
531
+ one. Their stable cadence is roughly two to four weeks, behind a long
532
+ release-candidate train. Cadence: on demand, plus a check folded into release
533
+ preparation.
534
+ 2. **Review before building.** Read the changelog and diff `src/discover/rules.rs`,
535
+ `src/discover/registry.rs`, `src/discover/lexer.rs`, `IGNORED_PREFIXES`, the
536
+ TOML DSL structs in `src/core/toml_filter.rs`, `src/hooks/trust.rs`, and
537
+ anything touching telemetry. A change to the DSL or to trust handling is a
538
+ stop-and-think, not a rebuild.
539
+ 3. **Re-validate the port.** Re-extract the `rewrite_command` assertions from the
540
+ new tag's `registry.rs` test block and the gate assertions from `lexer.rs`,
541
+ and replay both. Anything short of full agreement invalidates every coverage
542
+ number until the port is fixed.
543
+ 4. **Rebuild** all five targets from the new tag with telemetry unset.
544
+ 5. **Verify.** Binary runs on each target; the silence assertions pass; the
545
+ pack's fixtures still produce the expected decisions.
546
+ 6. **Publish** a new release in our namespace and record the new digests.
547
+ 7. **Bump** `rtk-pack` version in `plugin.json` and note the upstream version it
548
+ tracks. The bump is what makes `plugin update` fire at all, since a pack whose
549
+ recorded version still matches is skipped silently.
550
+ 8. **Ship** in the next ai-toolkit release; installed packs pick it up through
551
+ Phase 4.
552
+ 9. **Record** the licence position if upstream relicenses or adds a NOTICE.
553
+
554
+ There is no upstream test asserting network silence, so that property can
555
+ regress on any bump without turning their CI red. We own that test and re-run it
556
+ every time.
557
+
558
+ **Success criteria:** a maintainer who has never done it can follow the SOP end
559
+ to end; the review step names specific files rather than saying "check for
560
+ breaking changes".
561
+
562
+ ## 10. Verification
563
+
564
+ The pack must be able to answer "did this help?" with a number.
565
+
566
+ **The before-and-after design is replaced.** Phase 0 measured its noise floor:
567
+ on a 134-transcript window the projected saving varies by 8.8x while the
568
+ mechanism is unchanged. A before-and-after comparison cannot detect an effect of
569
+ 0.117% to 0.164% against that. Two changes make the measurement possible:
570
+
571
+ - **Measure over the whole transcript pool, not a recency window.** At n>=408
572
+ the same metric converges to within 1.05x.
573
+ - **Measure by replay, not by elapsed calendar time.** Run real captured tool
574
+ output through the built binary and compare byte counts directly. That is the
575
+ method that produced the honest 0% which retired the in-house filter, and it
576
+ removes the confound of what work the user happened to do that week.
577
+
578
+ The harness is `replay_rtk.py` in the measurement archive, not a script inside
579
+ the pack. It pairs each successful Bash result with the command that produced
580
+ it, keeps the ones the validated port says rtk would rewrite, and pipes the
581
+ captured bytes through the shipped binary via `rtk pipe -f <filter>`.
582
+
583
+ Two limits, stated because the number is meaningless without them:
584
+
585
+ - `rtk pipe` runs the same filter code as the command path but knows less than
586
+ it does: no result caps, no exit code, no file set. For search families the
587
+ replay figure is a **lower bound** on what the command path would save.
588
+ - Families with no pipe filter (`rtk read`, `rtk ls`, `rtk wc`) cannot be
589
+ measured this way at all. They are reported as **unmeasurable**, never folded
590
+ in as zero, because a zero that is really an absence is how the previous
591
+ effort talked itself into shipping.
592
+
593
+ Session-level token accounting, when it is wanted, comes from the session JSONL
594
+ [PATH: scripts/session_token_stats.py], summing all four usage fields: in a
595
+ cached session most context tokens land in the cache fields rather than
596
+ `input_tokens`.
597
+
598
+ **Kill number, published before the measurement rather than argued after it:**
599
+ if replay on the full corpus shows the shipped binary saving less than 0.05% of
600
+ input tokens, the pack is not worth its maintenance and supply-chain surface,
601
+ and it is retired the way the output filter was.
602
+
603
+ ### 10.1 Result: measured 0.0615%, kill number survived by 23%
604
+
605
+ Full pool, 28.56 MB of successful Bash output, replayed through the published
606
+ `softspark-rtk-v0.44.0-1` binary:
607
+
608
+ | | |
609
+ |---|---:|
610
+ | Addressed by rtk | 9.99 MB, **35.00%** of Bash bytes |
611
+ | Measurable through `rtk pipe` | 5.66 MB |
612
+ | After filtering | 4.22 MB |
613
+ | **Measured saving** | **1.44 MB = 360,529 tokens** |
614
+ | As a share of tool-result bytes | 1.35% |
615
+ | **As a share of input tokens** | **0.0615%** |
616
+
617
+ Against a kill number of 0.05%, the pack survives by a factor of 1.23. That is a
618
+ pass, not a vindication.
619
+
620
+ **The projection published in section 2.5 was roughly twice too optimistic.**
621
+ It said 0.117% to 0.164%; the measurement says 0.0615%. The error has a single
622
+ identifiable cause: the model credited families it could not simulate with
623
+ rtk's own claimed 60-90%. Replayed, the families that can be measured deliver
624
+ **25.5%** in aggregate.
625
+
626
+ Per family, measured against modelled and against upstream's claim:
627
+
628
+ | Family | Measured | Modelled | Claimed |
629
+ |---|---:|---:|---:|
630
+ | `rtk grep` | **22.3%** | 9.0% | 75% |
631
+ | `rtk git` | **33.1%** | — | 70% |
632
+ | `rtk find` | **35.6%** | — | 70% |
633
+ | `rtk rg` | **7.0%** | 30% | 75% |
634
+ | `rtk ruff` | **0.0%** | — | — |
635
+
636
+ The model **understated** grep by 2.5x and the total still came out high, so the
637
+ two errors are unrelated: coverage modelling was sound, per-family effectiveness
638
+ was guesswork wherever the source was not read closely.
639
+
640
+ **What the replay does not settle.** 43.3% of addressed bytes (4.33 MB) have no
641
+ `rtk pipe` filter and are unmeasurable, the largest being `rtk read` at 1.55 MB.
642
+ That family is known from source to return files verbatim at the default
643
+ `--level none`, so the headroom is smaller than 43.3% suggests. No extrapolation
644
+ is applied: a zero that is really an absence of measurement is how the previous
645
+ effort talked itself into shipping. For search families the pipe path also lacks
646
+ the command path's result caps, making these figures a lower bound.
647
+
648
+ **One independent confirmation.** The replay measured coverage at 35.00% of Bash
649
+ bytes; the gated port measured 35.17% on the same pool by a different method.
650
+ The port is right about *which* commands rtk touches even where it was wrong
651
+ about how much each saves.
652
+
653
+ Reproduce with `replay_rtk.py --pool 1300 --rtk <binary>` in the measurement
654
+ archive.
655
+
656
+ ## 11. Licence and security obligations
657
+
658
+ - **Apache-2.0.** Ship upstream `LICENSE` with the binaries, add a `NOTICE`, and
659
+ state the build-time change. Never relabel any upstream file MIT. Do not ship
660
+ `DISCLAIMER.md` verbatim, see section 5.1.
661
+ - **Telemetry.** Proven absent per build by the four assertions in section 5.1,
662
+ re-proven on every upstream sync.
663
+ - **Supply chain.** Digests pinned in `plugin.json`, verified on fetch,
664
+ re-verified by `doctor`. This mirrors the existing checksum-pin discipline for
665
+ URL-sourced rules and hooks. Run a blocking `cargo audit` on the pinned
666
+ lockfile, because upstream's is advisory.
667
+ - **Trust boundary.** rtk rewrites commands before execution, which the retired
668
+ in-house contract explicitly forbade for itself. Adopting it is a conscious
669
+ reversal of that constraint and its own threat surface: what runs is not what
670
+ the model asked for. The pack's README must say this plainly, and
671
+ `plugin install` must not be silent about it. The README must also disclose
672
+ the `rtk cc` npx path.
673
+
674
+ The sharp edge is more specific than "the command changes". rtk evaluates the
675
+ host's permission rules against the **original** command and applies the
676
+ verdict to the **rewritten** one: `decide_hook_action(cmd, ...)` takes the
677
+ command the model asked for, and `permissionDecision: allow` is then emitted
678
+ for the substituted `rtk …` form (`hook_cmd.rs:405-436`). An allowlist entry
679
+ for `git status` therefore authorises `rtk git status`, which the user never
680
+ wrote a rule for. Upstream documents the absence case in its own test
681
+ (`hook_cmd.rs:1272-1283`): with no matching allow rule, no decision is emitted
682
+ and the normal prompt stands. Both halves belong in the README.
683
+
684
+ ## 12. Pre-mortem
685
+
686
+ | Rank | Failure mode | Probability | Impact | Mitigation |
687
+ |---:|---|:---:|:---:|---|
688
+ | — | ~~Presets install but are untrusted, so they silently do nothing~~ | — | — | **Eliminated**, not mitigated: section 7 is cut, so the pack ships no filters and takes no trust-store entry |
689
+ | 2 | Cross-building with bundled SQLite is fragile in CI | Medium | Medium | Copy upstream's `cross` setup for aarch64-linux; five targets not six; a target that will not build is dropped, not faked |
690
+ | 3 | Pre-execution rewriting changes command semantics | Medium | High | Opt-in pack, documented one-flag disable, upstream's own review process, our integration tests on real commands |
691
+ | 4 | Windows binary cross-built without the 8 MiB stack reservation | Medium | High | Build natively on `windows-latest`, or pass the link-arg explicitly. `build.rs:6-13` |
692
+ | 5 | Upstream velocity breaks the rewrite logic the coverage numbers model | Medium | Medium | Pinned version, SOP review step naming exact files, port re-validation as a gate, fork option preserved by Apache-2.0 |
693
+ | 6 | Unpinned toolchain plus `warnings = "deny"` turns builds red with no change | Medium | Low | Pin an exact rustc version; treat bumps as deliberate |
694
+ | 7 | A fetch failure leaves a half-installed pack | Medium | Medium | Verify-then-install, abort and clean on digest mismatch, `doctor` detects drift |
695
+ | 8 | Live saving lands near the projection, not the claim | High | Medium | Section 10 measures it by replay either way, against a published kill number |
696
+ | 9 | The auto-update wiring makes `update` slower or flakier | Low | Medium | Skip when versions match, warn-and-continue on failure, `--dry-run` coverage |
697
+ | 10 | The pack drifts into being installed by default | Low | High | Adoption rule 1 plus a test asserting `install` never pulls it in |
698
+
699
+ ## 13. Open questions
700
+
701
+ 1. ~~Which upstream tag do we pin first?~~ **Answered: v0.44.0.** See section 2.1.
702
+ 2. ~~User scope only, or a project-scoped preset variant too?~~ **Moot:** section 7 is cut, so the pack installs no presets at any scope.
703
+ 3. ~~Is patching `pipeline_final_safe` for `head`/`tail` in scope later?~~
704
+ **Answered: no.** See section 2.6.
705
+ 4. ~~Does Phase 3 survive its own measurement at 0.008% of input tokens?~~
706
+ **Answered: no, cut 2026-07-26.** See section 7.
707
+ 5. ~~How does the pack share rtk's single global `filters.toml`?~~ **Moot:** it
708
+ writes no filters at all. The marker-injection design and the trust
709
+ constraints that would have applied are kept in section 6.1 in case presets
710
+ are ever revisited.