fix(codemode): abort on permission decline, bound execution with host limits - #348
Merged
Conversation
A user declining a permission from inside a Code Mode program produced a generic "Tool execution failed" ToolFailure and the model kept going, while the same decline on a direct tool call aborts the step. Declines travel as Effect defects on purpose (the tunnel entered in Permission.assert, consumed at SessionModelRequest.executeTool) so a blanket mapError cannot turn a user's "no" into model-facing tool output. The Code Mode tool runtime caught every non-interrupt cause and rewrote it, which closed that tunnel. Adds an opt-in `tunnelDefect` predicate to CodeMode options. A matching defect rides the existing interrupt path — uncatchable by program try/catch, like a direct call's decline — and is re-raised unchanged at the execution boundary, including when the declined call was never awaited. Unmatched defects are still sanitized. The host passes Decline.is, a single shared definition of what counts as a decline, now also used by model-request.
CodeModeTool.create built the runtime with no `limits`, and @opencode-ai/codemode defaults timeoutMs, maxToolCalls and maxOutputBytes to unlimited. A model-authored busy loop, an unbounded fan-out over tools, or an oversized return value therefore had no ceiling at all. Applies conservative host defaults — 120s, 100 tool calls, 1 MiB of retained result and logs — overridable per config through a new `codemode` key. These are ceilings, not budgets: ordinary programs never reach them, and each breach produces a model-facing diagnostic the model can act on rather than a silent hang. Tool.node now depends on Config.node to read them; test fixtures that stub Image.node (which previously pulled Config in transitively) bind an empty config layer.
|
Found 2 test failures on Blacksmith runners: Failures
|
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.
![Fix with [code]smith](https://pr-comments-assets.blacksmith.sh/codesmith/fix-with-codesmith-light.png)
Two Code Mode fixes found while building an external gateway (shuvbotta) against this server. Both are independent; fix 2 does not depend on fix 1. Written to be PR-able upstream unchanged.
Fix 1 —
fix(codemode): let a permission decline abort the stepProblem. Declining a permission from inside a Code Mode program does not abort the step. The model receives a generic
ToolFailurereadingTool execution failedand keeps going, and the program can evencatchit — while the same decline on a direct tool call aborts the step.Declines travel as Effect defects deliberately.
Permission.assertcalls it a "Deliberate defect tunnel" (packages/core/src/permission.ts), because tool leaves wrap execution in a blanketmapErrorthat must not convert a user's "no" into model-facing tool output; it is consumed once atSessionModelRequest.executeTool. The Code Mode tool runtime closed that tunnel:runHostinpackages/codemode/src/tool-runtime.tscaught every non-interrupt cause and rewrote it totoolError("Tool execution failed", error). A second layer ininterpreter/execute.tsthen turns any non-interrupt cause into anok: falseresult.Change.
@opencode-ai/codemodehas no business knowing what a permission is, so the classifier comes from the host: a new optionaltunnelDefect?: (defect: unknown) => booleanonCodeModeoptions. Absent (the default) is behaviourally identical to today — every defect is still sanitized, and the existing "sanitizes unknown host failures and defects" test is untouched.A matching defect is recorded and converted to
Effect.interrupt, then re-raised as a defect at the execution boundary. Interruption is the one channel the interpreter already treats as uncatchable —evaluateTryStatementrethrows onCause.isInterruptReason, and every promise seam re-fails onCause.hasInterruptsOnlyrather than converting to a catchable JS error — so a decline gets exactly the semantics it should have without teaching ~8 seams about a second special cause. The boundary check runs on the success path too, since an un-awaited declined call leaves the program itself succeeding.Core passes
Decline.is, a new single definition of what counts as a tunneled decline (Permission.DeclinedError|QuestionTool.CancelledError), now also used bymodel-request.tsso the two consumers cannot drift.Fix 2 —
fix(codemode): bound Code Mode execution with host limitsProblem.
CodeModeTool.createbuilds the runtime with nolimits, and@opencode-ai/codemodedefaultstimeoutMs,maxToolCallsandmaxOutputBytesto unlimited (documented in its README). Code Mode interprets model-authored TypeScript in-process, so today awhile (true) {}hangs the session indefinitely, tool fan-out is unbounded, and an arbitrarily large return value is carried whole.Change. Conservative host defaults, overridable through a new
codemodeconfig key:timeoutMscodemode.timeout_msTimeoutExceededwarning.maxToolCallscodemode.max_tool_callssearch(). Breach is a model-facingToolCallLimitExceededdiagnostic, so the model can re-plan in batches.maxOutputBytescodemode.max_output_bytesThese are ceilings, not budgets — ordinary programs never reach them.
The
Toolservice reads the mergedcodemodeblocks the same wayImagereadsattachments.image, soTool.nodegainsConfig.nodeas a declared dep. That edge previously existed only transitively throughImage.node, which ten test files stub out; each gets one[Config.node, emptyConfigLayer]binding using the existing fixture. That also keeps those tests hermetic — otherwise the real config layer boots and reads the developer's~/.config/opencode.Tests
Fix 1 —
packages/codemode: tunneled defect is re-raised; programtry/catchcannot swallow it; an un-awaited declined call still aborts; unmatched defects are still sanitized and still leak nothing.packages/core: end-to-end — a registered tool that dies withPermission.DeclinedError(the shapePermission.assertproduces), executed through the real Code Modeexecutetool, surfaces as aDeclinedErrordie reason rather than a completed result.Fix 2 — new
CodeMode execution limitssuite: the default tool-call ceiling stops an unbounded fan-out at exactlyDEFAULT_LIMITS.maxToolCalls; configuredmax_tool_calls,max_output_bytesandtimeout_mseach take effect (the timeout case is a realwhile (true) {}on the live clock).All eight new tests fail without their respective change — the busy-loop case simply runs until the test runner's own timeout.
bun testgreen in both packages (1093 and 1458 passing),tsgo --noEmitclean.