Skip to content

fix(agent): guard permission auto-approve against empty options#2246

Merged
charlesvien merged 1 commit into
mainfrom
posthog-code/fix-permission-empty-options
May 20, 2026
Merged

fix(agent): guard permission auto-approve against empty options#2246
charlesvien merged 1 commit into
mainfrom
posthog-code/fix-permission-empty-options

Conversation

@jonathanlab
Copy link
Copy Markdown
Contributor

Summary

Both auto-approve fallbacks in requestPermission (the MCP-trusted-tool branch and the no-toolCallId fallback) indexed params.options[0].optionId without checking length. If the SDK sent a permission request with an empty options array, the .optionId access threw a TypeError out of the requestPermission callback, crashing the in-flight tool call and taking the agent session with it.

Fix

  • Extract buildAutoApproveOutcome(options) next to the other top-level helpers in service.ts.
  • It prefers allow_once/allow_always, falls back to the first option, and returns { outcome: "cancelled" } (a valid ACP outcome) when options is empty.
  • Both call sites now use the helper, so the empty case is handled in one place.

Test plan

  • pnpm --filter code typecheck
  • pnpm --filter code test (1352 tests, all passing — includes 4 new tests for buildAutoApproveOutcome covering the allow / first-fallback / empty-options paths)

Created with PostHog Code

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
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 20, 2026

Prompt To Fix All With AI
Fix 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

Comment thread apps/code/src/main/services/agent/service.test.ts
@charlesvien charlesvien merged commit 04b666a into main May 20, 2026
15 checks passed
@charlesvien charlesvien deleted the posthog-code/fix-permission-empty-options branch May 20, 2026 22:17
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.

2 participants