Skip to content

Shake after handoff only when pruning is not enough - #16

Merged
HugoLopes45 merged 3 commits into
mainfrom
fix/shake-gate
Nov 11, 2026
Merged

HugoLopes45 merged 3 commits into
mainfrom
fix/shake-gate

Conversation

@HugoLopes45

Copy link
Copy Markdown
Owner

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 Support Pi 1.5 and edit the context only at the threshold #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).

turn_end checked Pi's raw token count, not the planned result, before shaking after a handoff. When pruning alone reached the target, the default methodOrder still elided old code blocks and broke the cache once more.

The README said old file reads are still replaced with Pi's compaction off. Since edits happen only at the threshold, nothing changes then; the README now says so.

Install dependencies without lifecycle scripts in CI and release: the release job can request an npm publish token, and no dependency needs its install script for the checks.
@HugoLopes45 HugoLopes45 self-assigned this Nov 11, 2026
@HugoLopes45
HugoLopes45 merged commit 5235c3d into main Nov 11, 2026
6 checks passed
@HugoLopes45
HugoLopes45 deleted the fix/shake-gate branch November 11, 2026 14:00
HugoLopes45 added a commit that referenced this pull request Nov 11, 2026
## Why

The `turn_end` handler had a cyclomatic complexity of 35. A change to
one strategy risked another, as the shake-gate bug fixed in #16 showed.
This PR adds pi-claude-pro's complexity lint (ESLint `complexity` at
most 10, run on Node's type-stripped source) to `npm run check`, and
splits the five functions above the limit.

## Changes

- `scripts/lint.ts`, `npm run lint`, and `check` now runs the lint.
`tsconfig.json` includes `scripts`. CONTRIBUTING documents the rule.
- Split without behavior change: `turn_end` and `session_before_compact`
in `extensions/index.ts`, `src/prune.ts`, `src/shake.ts`,
`src/stale.ts`. `src/tool-calls.ts` gets a shared `newestToolResults`
generator.
- Two intended deltas:
- `turn_end` returns early without a model, after
`background.invalidate`. Before, it computed a plan with no edits and
returned nothing.
- A focused `/compact` no longer cancels the background note itself,
because `handoff` cancels it before it requests a note. A new test pins
this.
- `eslint` devDependency with a `minimatch` ^11 override: every
minimatch 10.x has GHSA-g3hv-75wg-m249, and ESLint 10.14 pins ^10.2.6.
`npm audit` reports 0 vulnerabilities. The lockfile adds 67 packages
from registry.npmjs.org and removes none.

## Verification

- `npm run check`: tsc, lint clean, 97 tests passed.
- Four new characterization tests (early-threshold wait, note start
below the threshold, no retry of a failed note at Pi's threshold,
focused `/compact` cancel) pass on the code before the split. Each one
fails when its branch is removed, in the old code and in the new.
- An independent read-only review (hl-code-review) compared the code
line by line with the base and found the two deltas safe. Its two
findings are fixed in d2af352: the messages are built inside the `try`
again, and the focused-cancel test is added.
HugoLopes45 added a commit that referenced this pull request Nov 11, 2026
## Why

#16 changed the shake gate after a handoff from Pi's raw token count to
the planned result. The #16 review noted a second case that the change
affects, and no test covered it: replacing superseded reads alone brings
the context under the threshold, but not to `progressRatio` of it. Then
`turn_end` keeps only those edits and does not shake. That is the
intended behavior: one cache break instead of two, and the code blocks
stay visible until the next threshold.

## Changed behavior

None. Test only.

## Verification

- Head `e0f57ca`, repository root: `npm run check`, 1 run, tsc and lint
clean, 101 tests passed.
- The test fails when the gate uses Pi's raw token count (`tokens >
limit`), the bug fixed in #16: it finds an `Elided block` edit next to
the superseded read. 1 run.
- Calibration: the threshold is 11,000 tokens. Replacing the read leaves
about 10,500 tokens, above 8,800 (80%). With a threshold of 10,000, the
context stays above the threshold and the shake runs, so the test is
about 500 tokens from that edge. A change to Pi's token estimate can
move it. The failure would then show as a second edit, not as a silent
pass.
- The test also fails when `planMaintenance` returns the raw token count
after the stale edit: it finds 1 compaction entry where it expects 0. 1
run.
- Independent review (fresh-context reviewer, hl-code-review): Clear, 1
limited finding. A handoff compaction could pass the edit-count
assertion. Fixed in `e0f57ca` with the no-compaction and `done`
assertions.

## Risk and rollback

Low: one new test in `test/extension.test.ts`. Rollback: revert the
squash commit. Owner: HugoLopes45.
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