fix(agent): guard permission auto-approve against empty options#2246
Merged
Conversation
Both auto-approve fallbacks indexed params.options[0].optionId without checking length, so an empty options array (which the SDK can produce) threw a TypeError out of requestPermission and crashed the in-flight tool call. Extract a buildAutoApproveOutcome helper that prefers an allow option, falls back to the first option, and returns a cancelled outcome when options is empty. Generated-By: PostHog Code Task-Id: a82e2900-b7ab-4f98-8036-48c9e8f9e966
Contributor
Prompt To Fix All With AIFix the following 1 code review issue. Work through them one at a time, proposing concise fixes.
---
### Issue 1 of 1
apps/code/src/main/services/agent/service.test.ts:465-496
These four tests all call the same function with different inputs and check different outputs — a textbook case for a parameterised test. The team's convention is to prefer `it.each` for this pattern, which reduces boilerplate and makes adding new cases trivial.
```suggestion
describe("buildAutoApproveOutcome", () => {
it.each([
[
"prefers an allow_once option",
[
{ optionId: "reject", kind: "reject_once", name: "Reject" },
{ optionId: "allow", kind: "allow_once", name: "Allow" },
],
{ outcome: "selected", optionId: "allow" },
],
[
"prefers an allow_always option",
[
{ optionId: "reject", kind: "reject_once", name: "Reject" },
{ optionId: "allow_always", kind: "allow_always", name: "Always" },
],
{ outcome: "selected", optionId: "allow_always" },
],
[
"falls back to the first option when no allow option exists",
[
{ optionId: "first", kind: "reject_once", name: "First" },
{ optionId: "second", kind: "reject_always", name: "Second" },
],
{ outcome: "selected", optionId: "first" },
],
["returns a cancelled outcome when options is empty", [], { outcome: "cancelled" }],
] as const)("%s", (_, options, expected) => {
expect(buildAutoApproveOutcome(options)).toEqual(expected);
});
});
```
Reviews (1): Last reviewed commit: "fix(agent): guard permission auto-approv..." | Re-trigger Greptile |
charlesvien
approved these changes
May 20, 2026
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
Both auto-approve fallbacks in
requestPermission(the MCP-trusted-tool branch and the no-toolCallIdfallback) indexedparams.options[0].optionIdwithout checking length. If the SDK sent a permission request with an emptyoptionsarray, the.optionIdaccess threw aTypeErrorout of therequestPermissioncallback, crashing the in-flight tool call and taking the agent session with it.Fix
buildAutoApproveOutcome(options)next to the other top-level helpers inservice.ts.allow_once/allow_always, falls back to the first option, and returns{ outcome: "cancelled" }(a valid ACP outcome) whenoptionsis empty.Test plan
pnpm --filter code typecheckpnpm --filter code test(1352 tests, all passing — includes 4 new tests forbuildAutoApproveOutcomecovering the allow / first-fallback / empty-options paths)Created with PostHog Code