Skip to content

fix(core): report a settled shell as finished, not backgrounded - #63781

Open
cYoren wants to merge 1 commit into
anomalyco:devfrom
cYoren:oss-contrib/background-settled-race
Open

cYoren wants to merge 1 commit into
anomalyco:devfrom
cYoren:oss-contrib/background-settled-race

Conversation

@cYoren

@cYoren cYoren commented Nov 13, 2026

Copy link
Copy Markdown

Issue for this PR

Closes #63747

Type of change

  • Bug fix

What does this PR do?

A foreground shell call that reaches its timeout moves the command to the background. If the command exits between the timeout firing and the move, the call still reported "moved to the background" and the real result was lost.

The cause is in Job.background: it skips a settled job only when the job has no recovery. Shell jobs are always started with recovery, so a command that settled in that window was still marked as backgrounded. The call then registered a completion notice, but the settlement had already written its own row, so no notice ever arrived.

The fix is to stop moving work that has already settled. background now returns the settled snapshot with settled: true instead of marking it, and the shell tool reads that as a finished command: it returns the command's own output and exit code, and leaves nothing pending.

I took the fix in Job.background rather than only in the shell tool so the race is closed for every caller, and backgroundAll already skipped non-running jobs.

How did you verify your code works?

I added packages/core/test/job-background-settled.test.ts, which starts a shell job, settles it, then calls background and asserts the call sees status: "completed" and that pendingBackground stays empty. It fails on dev and passes with this change.

Three existing tests seeded a settled job through background, which only worked because of the old behaviour. I updated them to background while the job is still running, which is the real sequence.

  • bun run typecheck in packages/core: clean.
  • bun test for job, tool-shell, session-execution, shell-restart and the new test: 167 pass, 0 fail.
  • Full packages/core suite: no new failures against dev. The 9 that still fail there (config home discovery, flock on unwritable roots) fail the same way on dev in this environment.

Screenshots / recordings

Not a UI change.

Checklist

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

@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.

This doesn't fix #63747 yet. I made the race happen every time by having the timeout branch wait for the job to settle before calling jobs.background. A foreground call (sleep 0.3; echo hi; exit 3 with timeout: 10) still returns "Command moved to the background… did not finish within its timeout" with status: "running", and the real result still arrives later as a notice. That happens on base and on this PR alike. On this PR the finished job is also left in the job registry afterwards; on base it is cleaned up.

The fix is in the background: true branch, but the issue is about the foreground timeout branch. There, jobs.background(job.id) still maps any returned info to { type: "backgrounded", timedOut: true }. The new settled: true flag never reaches callers, because Job.background returns only result.info. The new job-background-settled.test.ts passes without exercising that path. A regression test should drive the shell tool through its timeout with the job settling first, and check that the call returns the command's own output and exit code with no notice. It would replace the Job-level test, which also relies on a 50 ms sleep.

The tests touching job, tool-shell and session-execution pass (163/0), and typecheck:tsc is clean.

Public API: unchanged (only internal Job behaviour and the shell tool's result in this race).

Comment thread packages/core/src/job.ts Outdated
if (job.isBackgrounded) return [{ info: snapshot(job) }, jobs]
// Work that already settled has nothing left to move. Marking it would hand the caller a background result
// whose completion notice was already written, so the call would report a running command that never notifies.
if (job.info.status !== "running") return [{ info: snapshot(job), settled: true }, jobs]

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.

settled: true is never visible outside this function: background returns only result.info, which still has a snapshot for a settled job. So the foreground timeout caller in shell.ts (jobs.background(job.id).pipe(Effect.map((info) => info ? { type: "backgrounded", ... } : undefined))) still reports a settled command as moved to the background. That is the race in #63747, and it is unchanged. That branch needs to treat a non-running result as finished (e.g. { type: "finished", info }) and go on to Deferred.await(settled), or background needs to return undefined or a tagged result for settled work.

Also, this returns early without marking or consuming the job. Since wait doesn't consume recoverable jobs either, a settled shell job reached this way stays in the registry: I saw jobs.get(id) still return it after the call, while on base it is removed. The early-return paths should consume it (as block does for a finished job).

Comment thread packages/core/src/tool/plugin/shell.ts Outdated

if (input.background === true) {
yield* jobs.background(job.id)
const moved = yield* jobs.background(job.id)

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 is the explicit background: true path, not the timeout path from the issue. Here it changes behaviour: a fast background: true command now returns inline instead of as a background handoff. That may be fine, but it's a separate change. The timeout branch further down (jobs.background(job.id) inside Effect.timeoutOption) is untouched, and it is where the reported race happens.

@cYoren

cYoren commented Nov 17, 2026

Copy link
Copy Markdown
Author

you're right on both counts: the timeout branch mapped every returned info to backgrounded, and the settled flag stopped at Job.background's return value, where it was dropped.

fixed in 620032b.

  • Job.background no longer carries a flag callers cannot see. it returns the settled snapshot as is (status, output, exit) and consumes that generation, so the finished job does not sit in the registry afterwards.
  • the timeout branch reads the returned status, the same way the background: true branch already did. a snapshot that is not running means the command settled before the move, so the call returns its own output and exit code and registers no notice. the settle-or-move decision still happens inside one modify on the job map, so it stays atomic.

on the test: job-background-settled.test.ts now waits for the settlement instead of sleeping 50ms, and pins the guard rather than the timeout path, for the reason you gave. driving the shell tool through that window from a test needs a hook in the source, which is what your reproduction did, and i did not want to add one for this. tell me how you would like the seam exposed and i will add the shell-level test on top.

checked on a clean checkout of the branch: bun test test/job-background-settled.test.ts test/job.test.ts test/tool-shell.test.ts 114 pass, 0 fail, 23 skip. bun test test/session-execution.test.ts 49 pass, 0 fail. bun run typecheck in packages/core is clean.

the branch is behind dev, but none of the three touched files moved in that range, so i left the existing commit history alone instead of rebuilding it.

@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 commit fixes the race on the foreground timeout path. I reran the same probe as last time: the timeout branch was patched to wait for the job to settle before jobs.background, then a foreground sleep 0.3; echo hi; exit 3 ran with timeout: 10. The call now returns the command's own output and exit: 3 with status: "completed". No notice is posted, nothing is left pending, and the settled job is consumed. On base the same call said "moved to the background" and the result arrived later as a notice.

The tests touching job, tool-shell, session-execution and shell-restart pass (167/0), and typecheck:tsc is clean.

Public API: unchanged.

The earlier point about the timeout branch and consuming the settled job is addressed. The background: true branch still carries its separate inline-result change (open thread).

@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.

This push only merges dev; the PR's own changes are the same as at 620032b. I reran the same probe on the merged head: the timeout branch was forced to wait for the job to settle before the move, then a foreground sleep 0.3; echo hi; exit 3 ran with timeout: 10. It still returns the command's own output and exit: 3 with status: "completed". No notice is posted, nothing is left pending, and the job is consumed.

The tests touching job, tool-shell, session-execution and shell-restart pass (167/0), and typecheck:tsc is clean.

Public API: unchanged.

Nothing new. The background: true branch still has its separate inline-result change (open thread).

A foreground shell call that hits its timeout moves the command to the
background. If the command exits between the timeout firing and the move,
Job.background still marked the settled job as backgrounded, because the
guard only skipped jobs without a recovery. Shell jobs always carry one.

The call then returned "moved to the background" and registered a
completion notice, but the settlement had already written its own row, so
the notice never arrived and the output was lost.

background now takes reportSettled for callers that read the settlement
themselves. The foreground timeout path passes it, so a job that finished
in that window comes back as the command's own result, and its generation
is consumed instead of being left pending. Callers that did not ask keep
the old behaviour, because explicitly backgrounded work still needs its
notification.
@cYoren
cYoren force-pushed the oss-contrib/background-settled-race branch from d245632 to f0c8bdc Compare November 20, 2026 13:42
@cYoren

cYoren commented Nov 20, 2026

Copy link
Copy Markdown
Author

rebased onto dev at 85951cb. the job registry changed under this (listing/update, consume), so the fix moved with it: background now takes reportSettled, and the foreground timeout path passes it. jobs that finish before the move come back as the command result and nothing is left pending. the default call still marks already-finished work, since its notification is what an explicit background call needs. 3 files, +90/-8. job and shell tests pass, typecheck clean.

@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 race is fixed. I reran the same probe on the new head: the timeout branch was forced to wait for the job to settle before the move, then a foreground sleep 0.3; echo hi; exit 3 ran with timeout: 10. It returns the command's own output and exit: 3 with status: "completed". No notice is posted, nothing is left pending, and the job is consumed. On base the same call reported "moved to the background".

The tests touching job, tool-shell, session-execution and shell-restart pass (171/0), and typecheck:tsc is clean.

Public API: unchanged. Job.background gets an optional { reportSettled } argument, which is internal to core.

The opt-in keeps the background: true path and other callers on their existing behaviour, so the earlier point about the background: true branch is addressed.

Not for this PR: Code Mode's timeout path (jobs.block(...).pipe(Effect.timeoutOption(BLOCK_TIMEOUT)), then jobs.background(id) in packages/core/src/codemode/job.ts) follows the same pattern and probably has the same race. I didn't run it.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Shell: a command that ends just as its timeout passes is reported as moved to the background

1 participant