Skip to content

Support Pi 1.5 and edit the context only at the threshold - #14

Merged
HugoLopes45 merged 4 commits into
mainfrom
fix/pi-1.5-cache-safe-edits
Nov 11, 2026
Merged

HugoLopes45 merged 4 commits into
mainfrom
fix/pi-1.5-cache-safe-edits

Conversation

@HugoLopes45

@HugoLopes45 HugoLopes45 commented Nov 11, 2026 •

Copy link
Copy Markdown
Owner

Why

  • Pi 1.5 no longer exports Type from @earendil-works/pi-ai, so the extension did not load on current Pi.
  • Every context edit changes the prompt prefix. The provider then reads all later text without its cache, and Anthropic drops every thinking block after the edited message. In local sessions (requests less than 4 minutes apart), the lost cached prefix after an edit was: no edit, mean 224 tokens; superseded-read edit, mean 27,190; prune, mean 135,373. Superseded-read edits below the threshold cost about 4.8 times the input tokens they saved. A second edit within a few turns of the previous one often missed the whole cache.

What

Commit 1, behavior:

  • Import Type from typebox, which Pi 1.0.3 and 1.5 both provide to extensions. Development uses Pi 1.5.2.
  • Replace superseded reads only above the threshold, in one batch with pruning, shaking or compaction, and anywhere in the context.
  • Without a compaction, keep edits only when they bring the context to the threshold or below, so a batch does not repeat at each turn.
  • Remove the staleSuffixTokens setting. A configured value now reports contextKit.staleSuffixTokens is not a setting. The next release should be a minor version.
  • Cancel obsolete background handoff requests after session-tree navigation, and state the limits of recall in the README.
  • Tests: Pi 1.0's faux provider counts a session's first prompt as both input and cache write, so the tests disable its cache and size their contexts for both versions.

Commit 2, repository (aligned with pi-claude-pro):

  • A GitHub release is the only release step. The tag sets the version, and generated notes replace CHANGELOG.md. The release script and its tests are removed. See CONTRIBUTING.md.
  • Workflow actions pinned to commit SHAs, Dependabot cooldown of 7 days, and a zizmor workflow check. zizmor runs from PyPI because this repository allows only GitHub-owned actions. CodeQL stays on GitHub's default setup, which already scans actions and TypeScript weekly; GitHub rejects an advanced CodeQL workflow while it is enabled.
  • CI also checks Pi 1.0.3, the oldest supported version. Every Pi package is pinned, because the caret ranges between them would otherwise mix in newer sub-packages.

Commit 3: vite 8.3.2 to 8.3.8 (dev dependency through vitest) for its open advisories. npm audit reports no vulnerabilities.

Not included: a cyclomatic complexity limit like pi-claude-pro's. Five functions exceed 10 (the turn_end handler is at 35); they need a refactor in a separate PR.

Checks

  • npm run check passes: 92 tests and tsc on the locked Pi 1.5.2.
  • CI: 92 tests and tsc on the locked Pi 1.5.2 (Node 22 and 24), on Pi 1.0.3, and on latest Pi.
  • actionlint and zizmor report no findings.
  • Tests first: the new tests for no edits below the threshold, for older reads anywhere in the context, and for no edit above the threshold without compaction failed on the old code.
  • The README matches the new behavior.
  • Independent review by a second agent: diagnosis from replayed real sessions; diff reviewed and npm run check rerun; approved in two rounds. Its one README finding is applied.

After merge: the npm trusted publisher and the npm environment stay as they are; the workflow file name release.yml is unchanged.

Optional repository setting: pi-claude-pro's CodeQL uses the security-and-quality query suite; the default setup here uses default.

Pi 1.5 no longer exports Type from pi-ai, so the extension imports it from typebox, which Pi 1.0.3 and 1.5 both provide to extensions. Development now uses Pi 1.5.2; the tests pass on 1.5.2 and 1.0.3. The faux provider in Pi 1.0 counts a session's first prompt as both input and cache write, so the tests disable its cache to get the same usage on every version.

Every context edit changes the prompt prefix. The provider then reads all later text without its cache, and Anthropic drops every thinking block after the edit. In local sessions, superseded-read edits below the threshold cost about 4.8 times the input tokens they saved, and a second edit within a few turns of the previous one often missed the whole cache. Superseded reads are therefore replaced only above the threshold, in the same batch as pruning and shaking, and anywhere in the context. Without a compaction, edits are kept only when they bring the context to the threshold or below, so they do not repeat at each turn. The staleSuffixTokens setting no longer has a purpose and is removed.

Also: cancel obsolete background handoff requests after session-tree navigation, and state recall's limits (current branch only, images as MIME types).
…in CI

Align the repository with pi-claude-pro. A GitHub release is now the only release step: its tag sets the version and its generated notes replace CHANGELOG.md, which removes the release PR, the manual tag push, the dispatch, and the release script with its tests. The workflow still requires the owner, a vX.Y.Z tag on main with green CI, and approval of the npm environment.

Tags can move; commit SHAs cannot, so workflow actions are pinned and Dependabot keeps them current with a 7-day cooldown. zizmor checks the workflows and CodeQL scans the TypeScript and the workflows.

CI now also checks Pi 1.0.3, the oldest supported version. Pi's packages depend on each other with caret ranges, so installing only pi-coding-agent@1.0.3 mixes in newer sub-packages; every Pi package is pinned, as in a real install.
@HugoLopes45 HugoLopes45 self-assigned this Nov 11, 2026
The locked vite 8.3.2, a dev dependency through vitest, has open advisories for files served outside server.fs.allow and for launch-editor. npm audit now reports no vulnerabilities.
This repository allows only GitHub-owned actions, so the zizmor action could not start and CI failed at startup. CodeQL's default setup already scans actions and TypeScript weekly here, and GitHub rejects an advanced CodeQL workflow while it is enabled.
@HugoLopes45
HugoLopes45 merged commit 3539e8d into main Nov 11, 2026
6 checks passed
@HugoLopes45
HugoLopes45 deleted the fix/pi-1.5-cache-safe-edits branch November 11, 2026 13:40
HugoLopes45 added a commit that referenced this pull request Nov 11, 2026
## Why

- **Shake ran when pruning was already enough.** After a handoff,
`turn_end` decided whether to shake from Pi's raw token count, not from
the planned result. With the default `methodOrder`, pruning could bring
the context under the target and old code blocks were still elided. That
is one more cache break and lost content the model may still need.
- **README was wrong about `compaction.enabled: false`.** Since #14,
edits happen only at the threshold. With Pi's compaction off there is no
threshold, so nothing changes. The README said old file reads were still
replaced.
- **Install scripts ran in the release job.** That job holds `id-token:
write` for npm trusted publishing. Three transitive dev dependencies
(`@google/genai`, `protobufjs`, `fsevents`) declare install scripts; the
checks do not need them.

## Changes

- `extensions/index.ts`: gate the shake after handoff on
`result.tokens`.
- `test/extension.test.ts`: regression test. It failed before the fix
with an `Elided block` replacement and passes after.
- `README.md`: two lines on `compaction.enabled: false`.
- `ci.yml`, `release.yml`: `npm ci --ignore-scripts`, and
`--ignore-scripts` for the Pi 1.0.3 and latest installs.

## Verification

- `npx tsc --noEmit`: pass.
- `npx vitest --run`: 93 passed.
- A local install with `npm ci --ignore-scripts` passes the full suite.

Found by an `hl-improve` survey and an `hl-security-audit` pass (no
confirmed vulnerability; this is hardening).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant