Fix: Add incremental parsing to SystemIntegrity handler - #453
Closed
anutron wants to merge 1 commit into
Closed
Conversation
Resolves performance issue where Stop hooks take 1-2 minutes in long sessions.
Problem:
- SystemIntegrity handler parses entire transcript on every Stop event
- In long sessions (953+ lines), this adds 1-2 seconds per Stop
- Transcript is reparsed from line 0 every time, wasting CPU
Solution:
- Track last_parsed_line in integrity-state.json
- Only parse new lines since last check (incremental parsing)
- Update state after each parse with new last_parsed_line
Changes:
- change-detection.ts: Updated IntegrityState interface and parseToolUseBlocks function
- Added last_parsed_line?: number to IntegrityState
- Modified parseToolUseBlocks to accept startLine parameter
- Returns {changes, lastLine} instead of just changes array
- SystemIntegrity.ts: Updated to use incremental parsing
- Import readIntegrityState from change-detection
- Read last_parsed_line from state before parsing
- Pass startLine to parseToolUseBlocks
- Update state with new lastLine after parsing
Performance Impact:
- Before: O(n) where n = total transcript lines
- After: O(m) where m = new lines since last check
- In 953-line session: ~950 lines saved per Stop event
Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
Owner
|
Thanks for this contribution! We've done a significant restructuring in the upcoming PAI v3.0 release — including a redesigned hook system (22 → ~19 hooks), a completely rewritten Algorithm (v0.5.1), and removal of the observability system. These changes supersede the fix addressed in this PR. We really appreciate the effort you put into this and encourage you to check out v3.0 when it lands — community feedback like yours directly informed our redesign. Closing as superseded, but your contribution is valued! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Resolves performance issue where Stop hooks take 1-2 minutes to complete in long sessions.
Problem
The SystemIntegrity handler parses the entire transcript on every Stop event. In long sessions (1000+ lines), this adds 1-2 minutes of overhead per Stop event. The transcript is reparsed from line 0 every time, creating unnecessary CPU overhead that compounds as sessions grow.
Solution
This PR implements incremental parsing by tracking the last parsed line number in the integrity state:
last_parsed_lineinintegrity-state.jsonlast_parsed_lineChanges
change-detection.tslast_parsed_line?: numbertoIntegrityStateinterfaceparseToolUseBlocks()to acceptstartLineparameter for incremental parsingFileChange[]to{ changes: FileChange[]; lastLine: number }SystemIntegrity.tsreadIntegrityStatefrom change-detectionlast_parsed_linefrom state before parsingstartLinetoparseToolUseBlocks()lastLineafter parsingPerformance Impact
This eliminates the transcript re-parsing overhead and makes Stop hooks complete much faster in long sessions.
Testing
Tested locally in long session. Stop hook performance improved significantly - no longer waiting 1-2 minutes for SystemIntegrity to complete.
Backwards Compatibility
Fully backwards compatible. If
last_parsed_lineis missing from state (fresh install or existing state file), it defaults to 0 and parses the entire transcript on first run.