Skip to content

[WRONG BRANCH] fix(cursor): avoid quadratic replay cost by tracking entry index and bounding replay window - #348

Draft
luvs01 wants to merge 1 commit into
mainfrom
codex/fix-quadratic-cpu-exhaustion-vulnerability-9grcuk
Draft

[WRONG BRANCH] fix(cursor): avoid quadratic replay cost by tracking entry index and bounding replay window#348
luvs01 wants to merge 1 commit into
mainfrom
codex/fix-quadratic-cpu-exhaustion-vulnerability-9grcuk

Conversation

@luvs01

@luvs01 luvs01 commented Aug 28, 2026

Copy link
Copy Markdown
Owner

Motivation

  • The external-model Cursor replay deduper performed entries.indexOf(...) for every duplicate, creating attacker-controlled O(k·r) CPU behavior that could stall the server event loop.
  • The replay traversal processed the full rawMessages before pruning, so large attacker-controlled histories could trigger the quadratic path before any size limits applied.

Description

  • Replace array search with a tracked index: lastReplayEntry is replaced by lastReplayEntryIndex and duplicate replacements update the known index directly to make each dedupe operation O(1).
  • Introduce CURSOR_EXTERNAL_REPLAY_MESSAGE_LIMIT = 4096 and limit the synchronous replay construction to the newest bounded suffix for external wire models so replay processing is bounded before later wire-size pruning.
  • Reset the tracked index on user/developer turns to preserve the same-collapse semantics and keep the existing repetition-marking behavior intact.
  • Add a unit test bounds replay construction before processing an oversized history to tests/cursor-repetition-breaker.test.ts that verifies oversized histories outside the bounded replay window do not trigger the severe repetition note.

Testing

  • Ran the focused tests with bun test tests/cursor-repetition-breaker.test.ts and all 6 tests passed.
  • Ran bun run typecheck (TS check) which completed without errors.
  • Attempted a full bun run test; the repository-wide suite ran many tests and reported unrelated failures/timeouts in other subsystems, so only the focused regression and typecheck were relied on to validate this scoped fix.

Codex Task

@github-actions

Copy link
Copy Markdown

Deterministic PR hygiene checks passed.

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

coderabbitai Bot commented Aug 28, 2026

Copy link
Copy Markdown

Important

  • 🔍 Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: a33da723-8212-4fe9-9386-57f42202a368


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 changed the title fix(cursor): avoid quadratic replay cost by tracking entry index and bounding replay window [WRONG BRANCH] fix(cursor): avoid quadratic replay cost by tracking entry index and bounding replay window Aug 28, 2026
@github-actions

github-actions Bot commented Aug 28, 2026

Copy link
Copy Markdown

⏳ DRAFT

  • wrong target branch (main); retarget to dev.

What to do

  • Retarget this PR to dev — all contributions go to dev.

Its title has been prefixed with [WRONG BRANCH].
This pull request was already a draft. Its draft status will be preserved after every issue above is resolved.

@github-actions
github-actions Bot marked this pull request as draft August 28, 2026 02:37

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d16b150861

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +260 to +262
const replayStart = externalModel
? Math.max(0, replayEnd - CURSOR_EXTERNAL_REPLAY_MESSAGE_LIMIT)
: 0;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Align the capped replay window to a complete turn

When more than 4,096 messages follow the most recent historical user message, this cutoff starts on an assistant/tool-result entry. The later complete-turn selector requires a user root, so with the history constructed by the new test (old turn, 4,101 assistant messages, then the active user), it rejects the orphaned suffix and sends only the system root; the external model receives the active “continue” action with none of the conversation being continued. Anchor the bounded suffix at a user/developer boundary, or explicitly retain the preceding user message, so limiting construction cannot erase all replay context.

Useful? React with 👍 / 👎.

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

Labels

aardvark bug Something isn't working codex

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant