Skip to content

fix(pi): guard against null/undefined event in before_agent_start (closes #439)#462

Closed
Tanmay9223 wants to merge 1 commit into
DietrichGebert:mainfrom
Tanmay9223:fix-439-before-agent-start-null-event
Closed

fix(pi): guard against null/undefined event in before_agent_start (closes #439)#462
Tanmay9223 wants to merge 1 commit into
DietrichGebert:mainfrom
Tanmay9223:fix-439-before-agent-start-null-event

Conversation

@Tanmay9223

Copy link
Copy Markdown
Contributor

🎯 What: Adds event?.systemPrompt ?? "" optional chaining in the before_agent_start handler in pi-extension/index.js, replacing the bare event.systemPrompt property access.

💡 Why: If the pi runtime calls before_agent_start with a falsy event (null or undefined), the handler immediately throws TypeError: Cannot read properties of null (reading 'systemPrompt'), crashing the agent before every session turn. The adjacent input handler already uses event?.source and event?.text defensively — before_agent_start was the only handler missing this guard (issue #439).

Verification: Added three targeted regression tests to pi-extension/test/extension.test.js covering: null event, undefined event, and a truthy event object without a systemPrompt key. 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-existing csv: correct pandas one-liner passes test, which requires a pandas install and was already failing on main before this change.

Result: The before_agent_start handler 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.

…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
@Tanmay9223

Copy link
Copy Markdown
Contributor Author

Closing this PR as a duplicate. PR #444 by @Aroool already addresses the same issue and was opened before this one. My PR search failed to catch it because it used 'Fixes #439' rather than 'Closes #439'. Apologies for the noise.

@Tanmay9223 Tanmay9223 closed this Jun 30, 2026
@Tanmay9223 Tanmay9223 deleted the fix-439-before-agent-start-null-event branch June 30, 2026 08:13
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