Fix: Add incremental parsing to SystemIntegrity handler #453
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.