Repository navigation
Description
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
mutatingHookandcanUseToolinto named module-level functions that take an explicit context; - extract the remembered-grant and allowlist decisions into pure functions.
- extract
sdk-options.ts:- extract
computeToolRoster(req, allowedTools), which returns the bridge handlers and theallowedlist; - extract the environment and relay environment, and the prompt-context assembly, into separate functions;
assembleClaudeSdkTurncomposes them.
- extract
- 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.tsorsdk-options.tsexceeds ~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. -
computeToolRosterand the extracted grant decision each have a direct test. A recorded mutation of the grant decision fails its named test. -
runBackendContractpasses 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 lintContract 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
schlessera commentedon Nov 3, 2026
Claimed (
claim-2519-01727). Branchrefactor/2519-split-claude-permission-sdk-assemblyfromepic/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 inpermission-hooks.ts(~:245); this slice keeps that comment in place. #3447'sturn-*.tsfiles are not touched.schlessera commentedon Nov 3, 2026
Implementation is in #3459 (ready), which targets
epic/1342-turn-path-split@2efaf6c23; head6a89981ea.createPermissionWiringis down from 560 to 47 lines andassembleClaudeSdkTurnfrom 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:prpasses 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.test(backend-claude): pin the host-sized worker limits in the SDK opt…
test(ui-server): wait for the host's budget abort instead of a fixed …
refactor(backend-claude): split the Claude backend's createPermission…