Skip to content

fix(runner): queue work when already running instead of discarding#10

Merged
CreatorGhost merged 2 commits into
devfrom
runner-queue-work
Jul 12, 2026
Merged

fix(runner): queue work when already running instead of discarding#10
CreatorGhost merged 2 commits into
devfrom
runner-queue-work

Conversation

@CreatorGhost

@CreatorGhost CreatorGhost commented Jul 11, 2026

Copy link
Copy Markdown
Owner

Port of upstream anomalyco/opencode#36375.

Adds a RunningThenRun state to the runner state machine so an ensureRunning call that arrives while a drain is in flight queues one follow-up run instead of being silently dropped — previously new work admitted during a drain could sit unprocessed until the next unrelated wake.

Verified: bun test test/effect/runner.test.ts (28 pass, includes new queued-run regression tests) and bun typecheck from packages/opencode. Applied cleanly.

Summary by CodeRabbit

  • New Features

    • Additional refresh requests made while work is in progress are now queued and run afterward.
    • Multiple callers can share the queued operation instead of triggering duplicate work.
    • Cancelling during queued execution now stops both the active and pending operations.
  • Bug Fixes

    • Improved handling of concurrent execution and cancellation, including consistent completion behavior for all callers.

@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@CreatorGhost, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 53 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: f9badd62-bec7-4808-b3d7-7da5e51bbbca

📥 Commits

Reviewing files that changed from the base of the PR and between f4d4f33 and 6520562.

📒 Files selected for processing (1)
  • packages/opencode/test/effect/runner.test.ts
📝 Walkthrough

Walkthrough

Runner now supports one queued execution while another run is active. Completion starts the queued work, concurrent callers share the pending handle, and cancellation interrupts the active run while failing both active and pending callers.

Changes

Runner queue lifecycle

Layer / File(s) Summary
Queue state and ensureRunning transitions
packages/opencode/src/effect/runner.ts
Adds the RunningThenRun state with active and pending handles. ensureRunning creates and awaits pending work when a run is already active.
Queued completion and cancellation
packages/opencode/src/effect/runner.ts, packages/opencode/test/effect/runner.test.ts
finishRun starts queued work after the active run completes, while cancel interrupts the active fiber and fails both deferreds. Tests cover queued execution, shared pending work, and cancellation behavior.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant Runner
  participant CurrentWork
  participant PendingWork
  Caller->>Runner: ensureRunning
  Runner->>CurrentWork: execute active work
  Caller->>Runner: ensureRunning while Running
  Runner->>PendingWork: queue second execution
  CurrentWork-->>Runner: complete
  Runner->>PendingWork: start queued work
  PendingWork-->>Caller: return pending result
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description covers the fix and verification, but it omits required template sections like Closes #, type of change, and checklist. Add the missing template sections: issue link, type of change checkbox, full What does this PR do?, verification details, and checklist items.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly states the runner now queues work when already running, matching the main behavior change.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch runner-queue-work

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@CreatorGhost

Copy link
Copy Markdown
Owner Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Jul 12, 2026

Copy link
Copy Markdown
✅ Action performed

Full review finished.


Your plan includes PR reviews subject to rate limits. More reviews will be available in 48 minutes.

@CreatorGhost

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 12, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@CreatorGhost

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 12, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@CreatorGhost

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 12, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
packages/opencode/test/effect/runner.test.ts (1)

145-178: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Assert the shared pending work executes only once.

This test tracks calls but never checks it. The whole point of "shares pending" is that both queued callers resolve from a single execution of work. Asserting calls === 1 would catch a regression where each queued caller spawns its own run.

💚 Proposed assertion
       if (Exit.isSuccess(exitB)) expect(exitB.value).toBe("queued")
       if (Exit.isSuccess(exitC)) expect(exitC.value).toBe("queued")
+      expect(yield* Ref.get(calls)).toBe(1) // pending work runs once, shared by both callers
     }),
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/opencode/test/effect/runner.test.ts` around lines 145 - 178, Update
the “third ensureRunning shares pending in RunningThenRun” test to assert that
the shared queued work executes exactly once by checking the tracked calls Ref
after both pending fibers complete. Keep the existing success and result
assertions unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@packages/opencode/test/effect/runner.test.ts`:
- Around line 145-178: Update the “third ensureRunning shares pending in
RunningThenRun” test to assert that the shared queued work executes exactly once
by checking the tracked calls Ref after both pending fibers complete. Keep the
existing success and result assertions unchanged.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 01013b0d-f121-46b9-8d6f-c6e54afde5ab

📥 Commits

Reviewing files that changed from the base of the PR and between dbd7ccd and f4d4f33.

📒 Files selected for processing (2)
  • packages/opencode/src/effect/runner.ts
  • packages/opencode/test/effect/runner.test.ts

@CreatorGhost CreatorGhost merged commit 25315bc into dev Jul 12, 2026
4 of 7 checks passed
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.

1 participant