Skip to content

🤖 fix: keep the end of the reply in the turn-stop text - #7833

Merged
ThomasK33 merged 3 commits into
mainfrom
cursor-plugins/turn-stop-text
Oct 31, 2026
Merged

ThomasK33 merged 3 commits into
mainfrom
cursor-plugins/turn-stop-text

Conversation

@ThomasK33

Copy link
Copy Markdown
Member

Summary

Codex Stop hooks now receive the end of a long reply as last_assistant_message, not its start. lastAssistantText keeps the last 16 × 1024 UTF-16 units of the reply's trailing text run. If the cut starts on the low half of a surrogate pair, it drops that one unit, so the text never starts with a lone surrogate. The #6067 rule is unchanged: the text is the trailing text run, and it is null when the turn ends on a tool part.

This is the bottom PR of stack C for the Cursor plugin RFC (#7829). The later Cursor afterAgentResponse and stop hooks use this text.

Background

A stop-loop hook (for example a "ralph loop") ends the loop when the reply contains a completion promise such as <promise>DONE</promise>. That promise sits at the end of a reply. Today Xum keeps the first 16 × 1024 characters, so a reply longer than that loses the promise, and the hook keeps the loop running.

RFC: rfc/20261030_cursor-plugin-support.md (on the RFC branch of #7829), section "PR 5: Turn-stop text", decision D22, and the section 8 row for the turn-stop text.

Implementation

  • src/node/services/agentPlugins/promptHooks.ts: text.slice(0, N) becomes text.slice(-N).replace(LEADING_LONE_LOW_SURROGATE, ""). The whitespace-only null check still runs on the whole run, as before. The doc comment says the end is kept and why.
  • src/node/services/agentPlugins/promptHooks.test.ts: four tests for lastAssistantText:
    1. A 20,000-character run over two text parts that ends in the promise returns its last 16,384 characters.
    2. A cut inside a surrogate pair returns 16,383 characters with no lone low surrogate.
    3. A cut on a high surrogate keeps the whole pair.
    4. A turn that ends on a tool part still returns null.

lastAssistantText has one production caller (agentSession.ts, which fills TurnStopContext.lastAssistantMessage). Its only reader is the Codex Stop hook input in codexHookRunner.ts. No caller needs the start of the text.

Validation

  • Tests 1 to 3 fail on main's code and pass here. Test 4 passes on both, as intended.
  • I ran a scratch test with a real AgentSession and a real Codex Stop hook (registerCodexPluginHooks on the event spine). It recorded the hook's ${last_assistant_message} for a 20,000-character reply that comes after a tool call:
    • main: 16,384 characters that end in yyyy…, so the promise is lost.
    • this branch: 16,384 characters that end in …<promise>DONE</promise>. A turn that ends on a tool part gives null.
  • An independent verifier checked the head before publish. It found no blockers and measured the edge cases (empty text, exactly 16,384 characters, 16,385 characters, both surrogate cuts, and a turn that ends on a tool part).
  • make crap-check, make dry-check and make mutate-check (10 killed, 0 survived) pass on the changed function.

Risks

Low. Only the Codex Stop hook input changes, and only for replies longer than 16 × 1024 characters in their trailing text run. A hook that parsed the start of a long reply now sees its end. Shorter replies are unchanged. Two known edge cases remain, and both need malformed or unusual model output:

  • A reply that ends in more than 16 KiB of whitespace now gives a whitespace-only string. Main had the mirror case for leading whitespace.
  • A lone low surrogate at the very start of an uncut reply is dropped. It is not valid text.

Release note

The turn-stop text passed to hooks keeps the last 16 × 1024 characters of the reply's trailing text run instead of the first. Codex Stop hooks receive the same text.


Generated with xum • Model: anthropic:claude-opus-5-5 • Thinking: xhigh • Cost: $4.69

A completion promise such as <promise>DONE</promise> sits at the end of a reply, so a long turn-stop text must keep its last 16,384 UTF-16 units, not its first. The cut drops a leading lone low surrogate.

Signed-off-by: Thomas Kosiewski <tk@coder.com>
Name the leading low-surrogate regex instead of narrating it, keep the
constant's original doc comment, and make the high-surrogate test input
asymmetric so it also fails when the head is kept.

---

_Generated with `xum` • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_

<!-- mux-attribution: model=anthropic:claude-opus-5-5 thinking=xhigh -->
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-31T11:39:37.642558Z 5080d9a Manual request
🔒 Security Review ✅ Completed 2026-10-31T11:40:37.064646Z 5080d9a Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions

Copy link
Copy Markdown
Contributor

🟢 e2e stories (Haiku): 14 passed

22 agent steps · 7 replayed from cache · 15 model calls · 46.3k tokens (24% cached)
Spend: $0.09 of the $1.00 cap (proxy ledger)

All 14 tests in 9 files
Test Agent Time
🟢 chatRestart.e2e.ts · 1 passed 1 step · 1 call 5.2s
🟢 a chat survives a restart of the server 1 step · 1 call 5.2s
🟢 compaction.e2e.ts · 1 passed 1 step · 1 call 4.2s
🟢 /compact replaces the old chat with a summary 1 step · 1 call 4.2s
🟢 pluginTrustRestart.e2e.ts · 1 passed 4 steps · 2 calls 12.4s
🟢 a trusted plugin hook stays trusted after the server restarts 4 steps · 2 calls 12.4s
🟢 settingsRoundTrip.e2e.ts · 1 passed 2 steps · 1 call 8.7s
🟢 a chat survives trips through Settings 2 steps · 1 call 8.7s
🟢 sidebarPerWorkspace.e2e.ts · 1 passed 4 steps · 1 call 4.0s
🟢 a new workspace starts with its right sidebar collapsed while another keeps it open 4 steps · 1 call 4.0s
🟢 smoke.e2e.ts · 1 passed 2 steps · 1 call 2.4s
🟢 the agent opens the playground workspace 2 steps · 1 call 2.4s
🟢 streamErrors.e2e.ts · 1 passed 1 step · 1 call 6.4s
🟢 stream errors show in the chat, and the chat recovers 1 step · 1 call 6.4s
🟢 todoNudge.e2e.ts · 1 passed 1 step · 1 call 2.7s
🟢 the agent finishes open TODO items after the turn-end nudge 1 step · 1 call 2.7s
🟢 touchAlignment.e2e.ts · 6 passed 6 steps · 6 calls 20.0s
🟢 the Settings close button is level with the title on a touch screen 390px wide 1 step · 1 call 3.6s
🟢 the Settings close button is level with the title on a touch screen 768px wide 1 step · 1 call 3.7s
🟢 Backup settings checkboxes are small and level with their labels on a touch phone 1 step · 1 call 4.0s
🟢 the heartbeat checkbox is small and level with its label on a touch phone 1 step · 1 call 2.8s
🟢 a reply's model and time stay on one line on a touch phone 1 step · 1 call 3.0s
🟢 the workspace actions menu stays on the title row on a touch phone 1 step · 1 call 2.9s

e2e 0.20.0 · 1m 9s · web · run artifacts

🟢 e2e Electron stories (Haiku): 3 passed

3 agent steps · 3 model calls · 8.0k tokens (22% cached)
Spend: $0.02 of the $1.00 cap (proxy ledger)

All 3 tests in 3 files
Test Agent Time
🟢 electron/deepLink.e2e.ts · 1 passed 1 step · 1 call 5.6s
🟢 a deep link of a second launch starts a chat in the running app 1 step · 1 call 5.6s
🟢 electron/menuBar.e2e.ts · 1 passed 1 step · 1 call 5.4s
🟢 the workspace menu bar is the desktop title bar 1 step · 1 call 5.4s
🟢 electron/smoke.e2e.ts · 1 passed 1 step · 1 call 7.7s
🟢 Xum Electron opens the playground workspace 1 step · 1 call 7.7s

e2e 0.20.0 · 20.7s · electron · run artifacts

🟢 e2e agent repros (Haiku): no tests selected

Spend: $0.00 of the $1.00 cap (proxy ledger)

e2e 0.20.0 · 5.4s · 2 targets (web, phone) · run artifacts

@ThomasK33

Copy link
Copy Markdown
Member Author

@codex security review

Head: 5080d9a

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: 5080d9a127

ℹ️ 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".

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review · Automatically triggered

Security review completed. No security issues were found in this pull request.

Reviewed commit: 5080d9a127

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@ThomasK33
ThomasK33 added this pull request to stack #7836 October 31, 2026 11:58
@ThomasK33
ThomasK33 added this pull request to the merge queue Oct 31, 2026
Merged via the queue into main with commit fe09e9a Oct 31, 2026
64 of 67 checks passed
@ThomasK33
ThomasK33 deleted the cursor-plugins/turn-stop-text branch October 31, 2026 23:09
yermakoffivan pushed a commit to yermakoffivan/mux that referenced this pull request Nov 2, 2026
…quests skill (coder#7885)

## Summary

The `pull-requests` skill now explains the Codex security review receipt
that the `Codex Comments` check needs, when `wait_pr_codex.sh` requests
it, and why each pushed head costs two Codex review rounds.

## Background

During the Cursor plugin stack (coder#7833 to coder#7847, follow-ups in coder#7858),
agents on nine PRs hit the same stop: Codex approved the head, but
`Codex Comments` exited 10 with "No Codex security review was requested
for <head>: request one". Owners did not know the gate needed a
comment-triggered security review, or that `wait_pr_codex.sh` posts that
request only after a person has asked Codex for a review in a PR
comment. Each pushed head also used two of the six allowed rounds, which
owners did not budget for.

## Implementation

A new subsection under "Handling Codex Comments" states three facts,
each read from the current scripts:

1. Codex posts a security receipt only for a comment-triggered review
(`scripts/wait_pr_codex.sh` cites coder#7731).
2. `request_security_review_if_needed` posts a Codex security review
request that names the head, once per head. `CHECK_CODEX_STATUS_ONCE`
stops before that when no person has asked Codex for a review in a PR
comment.
3. So each pushed head costs two rounds. Batch fixes per push.

---

_Generated with `xum` • Model: `anthropic:claude-opus-5-5` • Thinking:
`high` • Cost: `$125.78`_

<!-- mux-attribution: model=anthropic:claude-opus-5-5 thinking=high
costs=125.78 -->
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