Skip to content

backends: split the Claude backend's createPermissionWiring and assembleClaudeSdkTurn #2519

Closed

Description

@schlessera

Context

Besides startTurn (#1360), the Claude backend has two more oversized functions on the turn path. Both were measured with the TypeScript compiler API at dcf5341aa.

createPermissionWiring (packages/ui-backend-claude/src/permission-hooks.ts:42-590) is 549 lines. It contains:

  • canUseTool (:274-361, 88 lines);
  • a 140-line mutatingHook (:369-508);
  • the remembered-grant and allowlist logic around both.

This is the code that decides whether a tool call runs.

assembleClaudeSdkTurn (packages/ui-backend-claude/src/sdk-options.ts:143-482) is 340 lines. It mixes five concerns:

  • computing the tool roster;
  • the environment and relay environment;
  • sandbox notes;
  • assembling the prompt context;
  • building the SDK Options.

These are the most-changed files in the package (git log --format= --name-only -- packages/ui-backend-claude/src | sort | uniq -c | sort -rn): sdk-options.ts 48 commits, permission-hooks.ts 43, turn-runner.ts 30.

#1342's outcome covers backend turn start. Its table and #1360 name only startTurn, so these two would still exceed the bound after #1360 lands.

Scope

  • permission-hooks.ts:
    • extract mutatingHook and canUseTool into named module-level functions that take an explicit context;
    • extract the remembered-grant and allowlist decisions into pure functions.
  • sdk-options.ts:
    • extract computeToolRoster(req, allowedTools), which returns the bridge handlers and the allowed list;
    • extract the environment and relay environment, and the prompt-context assembly, into separate functions;
    • assembleClaudeSdkTurn composes them.
  • Add direct tests for each extracted unit.

Out of scope

  • startTurn (backends: split the Claude backend's startTurn #1360).
  • The per-tool MCP wrapper files (the sibling Claude tool-table issue in this review).
  • Any change to permission decisions, the tool roster, the order of the tool list, or the system prompt. The tool list and prompt must stay byte-identical (docs/decisions/provider-history.md).

Acceptance criteria

  • No function in permission-hooks.ts or sdk-options.ts exceeds ~150 lines.
  • A test asserts that the SDK Options (tools, allowed tools, system prompt) built for a fixed fictional request are byte-identical before and after the change. The expected value is captured from the tree before the change, not computed by the code under test.
  • computeToolRoster and the extracted grant decision each have a direct test. A recorded mutation of the grant decision fails its named test.
  • runBackendContract passes for the Claude backend unchanged, and the keyless provider-history guard passes.

Verification

bun run test packages/ui-backend-claude
bun run typecheck
bun run lint

Contract impact

None. This is a pure internal refactor. No tool name, schema, prompt or permission behaviour changes.

Found by a repository review on 2026-10-22.

Activity

  1. added
    type: refactor
    area: backends
    priority: p3
    agent-ready
    on Oct 21, 2026
  2. self-assigned this
    on Nov 3, 2026
  3. schlessera commented on Nov 3, 2026

    @schlessera
    OwnerAuthor

    Claimed (claim-2519-01727). Branch refactor/2519-split-claude-permission-sdk-assembly from epic/1342-turn-path-split @ 2efaf6c23; the PR targets that epic integration branch. Collision report for the cited files: closure-only overlaps with open main-targeted PRs, no path collision. Overlap on the epic: #3449 edits one comment line in permission-hooks.ts (~:245); this slice keeps that comment in place. #3447's turn-*.ts files are not touched.

  4. added this to the 0.45.0 milestone on Nov 3, 2026
  5. schlessera commented on Nov 3, 2026

    @schlessera
    OwnerAuthor

    Implementation is in #3459 (ready), which targets epic/1342-turn-path-split @ 2efaf6c23; head 6a89981ea. createPermissionWiring is down from 560 to 47 lines and assembleClaudeSdkTurn from 360 to 135; no function in either file is over 146 lines. The SDK options for six fixed requests are pinned byte-identical by a fixture captured before the split. The grant-decision and roster-order mutations each fail their named tests. check:pr passes against the epic base. The local package suite fails the same 49 worker-dependent tests as the untouched base, because this host has no unprivileged user namespaces. So the keyless provider-history guard is hosted-only and is left to the PR's hosted CI. This issue closes when the epic batch lands on main. The assignment stays while the PR is live.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

agent-readyarea: backendspriority: p3type: refactor

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions