Skip to content

Keep the Cursor stream open across tool round trips - #1

Open
Yuki13929 wants to merge 1 commit into
mainfrom
devin/1786635969-tool-loop-resume
Open

Keep the Cursor stream open across tool round trips#1
Yuki13929 wants to merge 1 commit into
mainfrom
devin/1786635969-tool-loop-resume

Conversation

@Yuki13929

Copy link
Copy Markdown
Owner

Summary

Agent loops (Claude Code) used to stall because every tool_result request opened a new Cursor turn and pasted the whole conversation back as flattened Human:/Assistant: text. Cursor's harness reads that as a forged transcript, so the model re-verified earlier steps, sometimes refused, and each round trip re-paid the ~25k token system prompt.

Now a tool_use keeps its upstream stream alive and the next request answers on it:

# server.py
tool_use -> remember_session(session, [block]); keep_open = True   # do not close()

Turn.start():
    if self._resume():        # tool_result ids -> the stream that emitted them
        return                # session.send_tool_results(...) on the same turn
    ... render_history(...)   # only when that stream is gone

_live maps tool_use.id -> (session, exec_id), is reaped after CURSOR2API_RESUME_TTL (900s), and only resumes when all returned ids belong to the same stream — otherwise it falls back to replay. The replay path now labels the history as relayed context (TRANSCRIPT_HEADER) instead of a raw chat log.

Also here: response_format (json_object / json_schema) emulated with an instruction plus incremental code-fence stripping (Unfence, streaming-safe), comma-separated local API_KEYs, .env, Docker files, and tests/test_units.py (19 cases, no network).

Measured with real Claude Code 2.1.197: a fix-bug + run-command task finishes in ~27s (previously hit the 200s timeout), and a 379-line file write completes in ~67s with no mid-task disconnect. tests/test_api.py and tests/test_openai.py both exit 0.

Co-Authored-By: Test <kazumi200367@outlook.com>
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