Skip to content

fix(cursor): re-arm finalize after completion-only tool frames - #1341

Merged
Wibias merged 3 commits into
lidge-jun:devfrom
luvs01:agent/fix-cursor-finalize-rearm
Aug 10, 2026
Merged

fix(cursor): re-arm finalize after completion-only tool frames#1341
Wibias merged 3 commits into
lidge-jun:devfrom
luvs01:agent/fix-cursor-finalize-rearm

Conversation

@luvs01

@luvs01 luvs01 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Re-arm Cursor's revocable client-tool finalize timer when an interactionUpdate completion drains the open tool-call set, preventing a completed tool turn from waiting indefinitely for done.
  • Preserve compact toolCallCompleted frames that carry only callId by capturing open-call ownership before the protobuf mapper removes the call.
  • Cover both a completion-only sibling with an embedded MCP tool and a call-id-only completion, while retaining the existing parallel-sibling and exactly-once terminal assertions.

Verification

  • Bun 1.3.14: six focused Cursor transport suites — 72 passed, 0 failed.
  • Bun 1.3.14: finalize-race suite randomized and rerun 10 times — 70 passed, 0 failed.
  • Bun 1.4.0-canary.1: finalize-race + continuation suites — 16 passed, 0 failed.
  • bun run typecheck — passed on Bun 1.3.14 and Bun 1.4.0-canary.1.
  • bun run privacy:scan — passed.
  • git diff --check HEAD^ HEAD — passed.
  • Independent focused review found and verified the call-id-only completion case; the updated diff has no remaining P0-P3 findings.
  • The full repository suite is not claimed green locally; exact-head GitHub CI remains pending.

Checklist

  • Scope stays focused and avoids unrelated cleanup.
  • Docs or release notes were updated when needed. (No user-facing configuration or API behavior changes.)
  • Security-sensitive changes were reviewed for secrets, auth, and unsafe defaults.

Review readiness checklist

This PR stays in draft until every box below is ticked. Tick all four boxes once the requirements are met:

  • All CI tests are green on my local testing.

  • I pushed my PR to the latest dev commit.

  • I resolved all correct Codex and CodeRabbit findings.

  • My PR is ready for review.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed an issue where conversations could remain stuck after client tool calls completed.
    • Ensured completion updates received in different formats are handled reliably.
    • Prevented duplicate completion events and ensured streams close correctly after all tool calls finish.
    • Improved handling of simultaneous tool-call completions for consistent turn finalization.
    • Fixed cases where completion updates arriving by call ID could delay or prevent the end of a conversation turn.
    • Improved reliability when multiple tool calls complete at nearly the same time.

@coderabbitai

coderabbitai Bot commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The Cursor live transport now handles client-tool completion frames before event mapping. It re-arms turn finalization when open tools drain. Tests cover sibling completions and call-ID-only completions.

Changes

Cursor tool finalization

Layer / File(s) Summary
Re-arm finalization after client-tool completion
src/adapters/cursor/live-transport.ts
At lines 1089–1107, completion ownership is captured before mapping. Pending finalization is revoked, then re-armed when completion frames emit tool_call_end and drain the open-tool set.
Validate completion-frame race handling
tests/cursor-tool-finalize-race.test.ts
At lines 13 and 61–100, the harness adds full and call-ID-only completion frames. Lines 204–241 test finalization, event counts, and stream cancellation for both race cases.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

  • lidge-jun/opencodex#366: Both changes modify handleServerMessage in the Cursor live transport, but they cover different message-handling paths.
  • lidge-jun/opencodex#1228: Both changes modify Cursor live transport tool continuation handling, but this change covers completion-frame races.

Suggested reviewers: lidge-jun

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
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.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: re-arming Cursor finalization after completion-only tool frames.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@github-actions github-actions Bot added the bug Something isn't working label Aug 9, 2026
@github-actions

github-actions Bot commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Deterministic PR hygiene checks passed.

@github-actions

github-actions Bot commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

✅ READY

  • all PR quality gates passed; the review readiness checklist is complete.

Review readiness checklist

  • ✅ All CI tests are green on my local testing.
  • ✅ I pushed my PR to the latest dev commit.
  • ✅ I resolved all correct Codex and CodeRabbit findings.
  • ✅ My PR is ready for review.

4/4 boxes ticked.

This pull request is already Ready for Review.
The review-ready label marks this PR as ready; review automation runs independently. If no CodeRabbit review appears, comment @coderabbitai review to request one.
Maintainers: @lidge-jun @Ingwannu @Wibias

@luvs01

luvs01 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Aug 9, 2026

Copy link
Copy Markdown
Contributor
✅ 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
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

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

Inline comments:
In `@tests/cursor-tool-finalize-race.test.ts`:
- Around line 204-219: Update the test “completion-only sibling re-arms finalize
after draining the call set” to delay completedFrame("call_b", "echo_b") until
partway through the initial grace window, then wait past the original deadline
but before the re-armed deadline and assert that no done event or cancellation
occurred. Finally, wait through the new deadline and retain the existing
assertions for one done event, two tool_call_end events, and NGHTTP2_CANCEL.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 1cfbe4a9-1694-4c8d-b4db-e6b3eae9dd21

📥 Commits

Reviewing files that changed from the base of the PR and between d493c14 and 2d4c0c0.

📒 Files selected for processing (2)
  • src/adapters/cursor/live-transport.ts
  • tests/cursor-tool-finalize-race.test.ts

Comment thread tests/cursor-tool-finalize-race.test.ts

luvs01 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Aug 9, 2026

Copy link
Copy Markdown
Contributor
✅ 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
Contributor

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
tests/cursor-tool-finalize-race.test.ts (1)

204-226: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Increase the margin around the original deadline.

At Line [218], the test checks only about 20 ms after the original 200 ms deadline. If the event loop delays the stale timer callback by more than 20 ms, this assertion can pass even when the old timer was not revoked.

Use deterministic timer control, or widen the intervals so the assertion is well after the original deadline and well before the re-armed deadline.

Example with wider timing margins
-    const h = makeHarness(200, ["echo_a", "echo_b"]);
+    const h = makeHarness(300, ["echo_a", "echo_b"]);
...
-    await sleep(60);
+    await sleep(100);
...
-    await sleep(160);
+    await sleep(250);
...
-    await sleep(80);
+    await sleep(100);
🤖 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 `@tests/cursor-tool-finalize-race.test.ts` around lines 204 - 226, Increase the
timing margin in the test “completion-only sibling re-arms finalize after
draining the call set” so the intermediate assertion occurs clearly after the
original 200 ms deadline but before the re-armed finalize deadline. Adjust the
sleep intervals or use deterministic timer control, while preserving the final
single-done and cancellation assertions.
🤖 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.

Outside diff comments:
In `@tests/cursor-tool-finalize-race.test.ts`:
- Around line 204-226: Increase the timing margin in the test “completion-only
sibling re-arms finalize after draining the call set” so the intermediate
assertion occurs clearly after the original 200 ms deadline but before the
re-armed finalize deadline. Adjust the sleep intervals or use deterministic
timer control, while preserving the final single-done and cancellation
assertions.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 17f30eea-f928-444f-9b2c-3b3d0de9589b

📥 Commits

Reviewing files that changed from the base of the PR and between 2d4c0c0 and f5d4a51.

📒 Files selected for processing (1)
  • tests/cursor-tool-finalize-race.test.ts

@luvs01
luvs01 force-pushed the agent/fix-cursor-finalize-rearm branch from f5d4a51 to 9ce0af6 Compare August 9, 2026 08:23

luvs01 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Aug 9, 2026

Copy link
Copy Markdown
Contributor
✅ 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.

Wibias commented Aug 9, 2026

Copy link
Copy Markdown
Collaborator

Please rebase this PR onto the latest dev (e8ce2b93dd3ef8074d96dee407704c5dc06e2699) before merge, then rerun the exact-head CI checks.

@luvs01
luvs01 force-pushed the agent/fix-cursor-finalize-rearm branch from d3297bf to fe41ee8 Compare August 9, 2026 23:24
@Wibias
Wibias marked this pull request as ready for review August 9, 2026 23:45
@coderabbitai

coderabbitai Bot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Tip

For best results, initiate chat on the files or code changes.

@github-actions[bot] Acknowledged. The PR gate reports that PR #1341 is ready for review at fe41ee87a424f95dd36824efff96577d5b04416b.

You are interacting with an AI system.

@coderabbitai

coderabbitai Bot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Rate Limit Exceeded

@github-actions[bot] have exceeded the limit for the number of chat messages per hour. Please wait 48 minutes and 33 seconds before sending another message.

@Wibias
Wibias merged commit a522384 into lidge-jun:dev Aug 10, 2026
42 of 49 checks passed

Wibias commented Aug 10, 2026

Copy link
Copy Markdown
Collaborator

Thanks @luvs01 — nice focused fix for the Cursor completion/finalize race, especially the call-id-only completion handling and the deterministic re-arm regression coverage. Much appreciated 🙏

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

Labels

bug Something isn't working review-ready

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants