fix(pi): guard against null/undefined event in before_agent_start (closes #439)#462
Closed
Tanmay9223 wants to merge 1 commit into
Closed
Conversation
…oses DietrichGebert#439) The before_agent_start handler accessed event.systemPrompt with no null guard on the event argument. If the pi runtime calls the handler with a falsy event (null or undefined), it throws: TypeError: Cannot read properties of null (reading 'systemPrompt') This crashes the agent before every turn when the event is missing. The input handler on line 159 already uses optional chaining (event?.source, event?.text) — before_agent_start was the only handler that didn't follow this defensive pattern. Fix: - pi-extension/index.js: use event?.systemPrompt ?? "" so a null or missing event never throws; consistent with the input handler style Tests: - pi-extension/test/extension.test.js: three new regression cases: - null event does not throw and still injects instructions - undefined event does not throw and still injects instructions - truthy event without systemPrompt key produces no 'undefined' prefix
Contributor
Author
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.
🎯 What: Adds
event?.systemPrompt ?? ""optional chaining in thebefore_agent_starthandler inpi-extension/index.js, replacing the bareevent.systemPromptproperty access.💡 Why: If the pi runtime calls
before_agent_startwith a falsy event (null or undefined), the handler immediately throwsTypeError: Cannot read properties of null (reading 'systemPrompt'), crashing the agent before every session turn. The adjacentinputhandler already usesevent?.sourceandevent?.textdefensively —before_agent_startwas the only handler missing this guard (issue #439).✅ Verification: Added three targeted regression tests to
pi-extension/test/extension.test.jscovering: null event, undefined event, and a truthy event object without asystemPromptkey. All three were failing before the fix and pass after. Full pi-extension suite: 18/18 pass. Full repo suite: 68/69 pass — the single remaining failure is the pre-existingcsv: correct pandas one-liner passestest, which requires apandasinstall and was already failing onmainbefore this change.✨ Result: The
before_agent_starthandler is now robust to any event shape the pi runtime may pass, matching the defensive optional-chaining style used throughout the rest of the extension.