Repository navigation
feat(codemode): add timeout and background to execute behind full_codemode - #65266
Conversation
…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
There was a problem hiding this comment.
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", |
There was a problem hiding this comment.
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,
agentneedsAgent.ID.make("build"); - line 54: the notify callback must return the created message, but
notices.push+Deferred.succeedreturns a boolean; - line 77:
tool(true).inputis typed as the union, sodecodeUnknownSync(...)is typed{ code }and thetoEqual({ ..., timeout, background })literal doesn't type-check. Decoding withCodeModeTool.FullInputdirectly would fix it.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I checked a161248 and it fixes the timing problem I raised.
- Background tests: the
Date.now()bounds are gone. The tests now check thejobID, that the job is stillrunning, and that the notice arrives. Therunningcheck 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 checkspollUnsafe()is stillundefined, then advances to 150 ms and joins the fiber. That gets the job ID back, and it checks the job isrunning. 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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
Issue for this PR
Closes #
Type of change
What does this PR do?
With
full_codemodeon, the Code Modeexecutetool takes optionaltimeoutandbackgroundinputs, mirroring the shell tool. With the flag off its schema stays{ code }.background: truestarts 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;timeoutnever stops a script. Withbackground: truethe call returns immediately andtimeouthas no effect.The schema is picked where the tool is defined (
CodeModeTool.creategets session services only withfull_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 withoutOPENCODE_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: 3000script moving to the background after 3 s.Checklist
Added by the opencode bot (AI-generated).
Public API: adds optional
timeoutandbackgroundinputs to the Code Modeexecutetool behindfull_codemode(backwards compatible)