feat: recover from prompt-too-long errors by compacting and retrying - #48
feat: recover from prompt-too-long errors by compacting and retrying#48kyletser wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
Adds deterministic “prompt too long” (HTTP 400) recovery to the agent’s model request path so long sessions can compact the transcript and retry instead of aborting the turn.
Changes:
- Introduces
ModelRequestError(with HTTP status) andisPromptTooLongError()detection for Anthropic-compatible 400 PTL responses. - Adds
requestWithPromptTooLongRecovery()to snip-compact and retry up toLIMITS.PTL_MAX_RETRIES. - Wires the agent loop to use the recovery helper and adds a dedicated test suite covering the retry/rethrow behaviors.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
test/prompt-too-long.test.ts |
Adds unit tests for PTL detection + compact-and-retry behavior. |
src/utils/errors.ts |
Introduces ModelRequestError and PTL message matching. |
src/prompt-too-long.ts |
Implements the compact-and-retry recovery helper around model.next(). |
src/anthropic-adapter.ts |
Throws ModelRequestError on non-2xx responses (status-aware errors). |
src/agent-loop.ts |
Wraps the model request with PTL recovery and keeps transcript/stats in sync. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
When the model API rejects the request as prompt too long (HTTP 400), the agent loop now deterministically snip-compacts the transcript (no extra model call) and retries, up to PTL_MAX_RETRIES times. Previously the whole turn aborted on such errors, wasting the work done so far. - Add ModelRequestError carrying the HTTP status, thrown by the Anthropic adapter on non-2xx responses. - Add isPromptTooLongError() matching Anthropic-compatible 400 wording. - Add requestWithPromptTooLongRecovery() helper and wire it into the agent loop request path; if nothing can be freed, the original error is rethrown untouched. Part of the model-aware context management roadmap (LiuMengxuan04#1).
04a71da to
0117192
Compare
|
Addressed both review points in 0117192. The recovery callback now receives the full SnipCompactResult and awaits the existing onSnipCompact path before retrying, so the snip boundary is persisted and session save state is reset. Recovery also marks the turn as already snipped. Added tests for awaited metadata delivery and agent-loop persistence wiring. Verification: npm run check, npm run lint, and npm test (235/235) all pass. |
|
感谢你的PR,我们正在进行版本升级,请等我们近期升级完成之后集中处理PR请求 |
What
Implements prompt-too-long recovery from the context-management roadmap in #1. Recognized HTTP 400 context-length errors trigger deterministic transcript compaction and a bounded retry.
Behavior
ModelRequestErrorand recognize context-length error wording from Anthropic-compatible endpoints.LIMITS.PTL_MAX_RETRIES(2).onSnipCompactcallback with the complete snip result before retrying, preserving boundary persistence and avoiding another regular snip in the same turn.Verification
Synced with upstream main and validated at
7d71c41:npm run check: passed.npm run lint: passed.npm test: 279 passed, no failures or skips.test/prompt-too-long.test.ts: 9 cases covering recognition, bounded retries, failure propagation, complete snip metadata, awaited persistence, fresh ephemeral context, and cancellation during a blocked retry.Refs #1