Skip to content

feat(codemode): add timeout and background to execute behind full_codemode - #65266

Merged
jlongster merged 5 commits into
devfrom
codemode-execute-timeout-background
Nov 20, 2026
Merged

jlongster merged 5 commits into
devfrom
codemode-execute-timeout-background

Conversation

@jlongster

@jlongster jlongster commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Issue for this PR

Closes #

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

With full_codemode on, the Code Mode execute tool takes optional timeout and background inputs, mirroring the shell tool. With the flag off its schema stays { code }.

  • background: true starts the script as a background job and returns its job ID immediately; the model is notified when it finishes.
  • timeout (positive integer ms) is only how long the call waits before moving the script to the background (replaces the 2-minute default for that call). The script keeps running in the background and the model is notified when it finishes; timeout never stops a script. With background: true the call returns immediately and timeout has no effect.

The schema is picked where the tool is defined (CodeModeTool.create gets session services only with full_codemode), and the job runner receives the options only in that case.

How did you verify your code works?

New packages/core/test/codemode-execute-options.test.ts (schema with flag off/on, background returns at once and notifies, foreground timeout moves to background and still finishes and notifies, background with a timeout returns at once and is not stopped), plus existing execute tests, with and without OPENCODE_FLAGS. Typechecked codemode, core, session-ui, app. Drove Desktop and TUI with a simulated model.

Screenshots / recordings

Board: https://dev.anoma.ly/team/stage/b/QbyrcrOCzB (scenarios prefixed "execute timeout/background · "): background script listed in the Desktop activity pill and TUI Scripts tab right away; a timeout: 3000 script moving to the background after 3 s.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Added by the opencode bot (AI-generated).

Public API: adds optional timeout and background inputs to the Code Mode execute tool behind full_codemode (backwards compatible)

…emode

User decisions:
- Add timeout and background to execute, only with full_codemode
User decisions:
- Add timeout and background to execute, only with full_codemode
- timeout only sets how long to wait before backgrounding; it never stops a script

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The approach looks sound: background skips the block, a foreground timeout replaces the 2-minute block deadline, and a background timeout limits runtime through timeoutOrElse. This matches the shell tool's semantics, and with the flag off the schema is still { code }. The new test passes and the existing Code Mode tests pass with and without full_codemode. A TUI drive with the simulated model didn't settle anything (base and PR behaved the same), so I haven't confirmed it end to end.

Public API: the model-facing execute tool input gets two optional fields, timeout (positive integer ms) and background (boolean), and its description gets one line. Both apply only with the full_codemode experimental flag and are backwards compatible. No SDK, HTTP, config or plugin surface changes.

Concerns, both in the new test file (details inline): it fails core's type check, and two of its tests depend on wall-clock timing and may be flaky.


const context = (sessionID: string): Tool.Context => ({
sessionID: Session.ID.make(sessionID),
agent: "build",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file fails tsc -b tsconfig.json tsconfig.tests.json in packages/core with 3 errors, and they're the only errors in the package:

  • here, agent needs Agent.ID.make("build");
  • line 54: the notify callback must return the created message, but notices.push + Deferred.succeed returns a boolean;
  • line 77: tool(true).input is typed as the union, so decodeUnknownSync(...) is typed { code } and the toEqual({ ..., timeout, background }) literal doesn't type-check. Decoding with CodeModeTool.FullInput directly would fix it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in a161248: Agent.ID.make("build"), the notify stub now returns the message, and the schema test decodes with CodeModeTool.FullInput. tsc -b tsconfig.json tsconfig.tests.json in core is clean.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed: at a161248, tsc -b tsconfig.json tsconfig.tests.json in packages/core passes with no errors, and bun test test/codemode-execute-options.test.ts passes all 6 tests. The Agent.ID.make, notify stub (Effect.as(input)) and CodeModeTool.FullInput changes cover all three errors I reported, so this thread can be resolved.

One side effect of the same commit: the two background: true tests no longer check elapsed time. They only check that the job is still running when the call returns. That still proves the call didn't wait for the 300 ms script, so the coverage is fine. Moving the foreground-timeout test onto TestClock is better than the old wall-clock bounds.

The shared checkout's HEAD is still at 5d3c101, so to check the new version I copied the file from a161248 into it temporarily. I've put the original file back. I didn't change anything else, and the bun.lock modification was already there.

const h = yield* harness
const started = Date.now()
const result = yield* h.run(context("ses_bg"), script(300), { background: true })
expect(Date.now() - started).toBeLessThan(250)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These wall-clock bounds (< 250 ms here; >= 140 / < 500 ms in the foreground-timeout test) may be flaky on a loaded CI runner, since the margins are only a few hundred ms over real Effect.sleeps. Checking the outcome would be enough and wouldn't depend on timing: the result has a jobID and the job is still running, then the notice arrives. Or use TestClock if the timing itself matters.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in a161248: the background tests now assert outcomes (jobID, job still running, then the notice) with no wall-clock bounds. The foreground test uses TestClock: still blocking at 149 ms, moves to the background at 150 ms, then finishes and notifies.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked a161248 and it fixes the timing problem I raised.

  • Background tests: the Date.now() bounds are gone. The tests now check the jobID, that the job is still running, and that the notice arrives. The running check is safe: the script sleeps 300 ms in real time, so it can't have finished when the call returns.
  • Foreground timeout test: it now forks the call under TestClock. At 149 ms it checks pollUnsafe() is still undefined, then advances to 150 ms and joins the fiber. That gets the job ID back, and it checks the job is running. It then advances 600 ms and waits for the completion notice. This tests the exact 150 ms cutoff without depending on real time.

What I ran: bun test test/codemode-execute-options.test.ts on a161248, three times. All 6 tests passed each time, about 1.2 s per run. I used a temporary worktree under /tmp/opencode so I didn't touch the shared checkout, and removed it afterwards. I didn't rerun with OPENCODE_FLAGS set or run the rest of the suite.

Shared checkout: repo/ is still on 5d3c101, not pr/head. bun.lock was already modified before I started, and I left it alone. I made no changes there.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new commits stop timeout from ever ending a script: a foreground timeout only moves the script to the background, and with background: true the timeout is ignored. The code and the model-facing descriptions now agree on that, and this part looks sound. The updated test and the existing Code Mode tests pass, including with full_codemode.

Public API: still only the model-facing execute input, behind full_codemode. It gains optional timeout (positive integer ms, wait before moving to the background) and background (boolean). Backwards compatible.

The earlier points are still open: the new test file still fails core's tsc (same 3 errors), and it still depends on wall-clock timing. The rewritten background test adds another < 250 ms bound of the same kind.

User decisions:
- Add timeout and background to execute, only with full_codemode
- timeout only sets how long to wait before backgrounding; it never stops a script
User decisions:
- Add timeout and background to execute, only with full_codemode
- timeout only sets how long to wait before backgrounding; it never stops a script
User decisions:
- Add timeout and background to execute, only with full_codemode
- timeout only sets how long to wait before backgrounding; it never stops a script

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both earlier points are fixed: the test file now type-checks (core tsc is clean), and the wall-clock bounds are replaced by TestClock in the foreground-timeout test and by job-status checks in the background tests. The test passed 15 runs in a row. No new concerns.

Public API: still only the model-facing execute input, behind full_codemode. It gains optional timeout (positive integer ms) and background (boolean). Backwards compatible.

@jlongster
jlongster merged commit a5fc5fd into dev Nov 20, 2026
10 checks passed
@jlongster
jlongster deleted the codemode-execute-timeout-background branch November 20, 2026 21:26
@amrkmn amrkmn mentioned this pull request Nov 23, 2026
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant