fix: improve context overflow error with diagnostic details#15749
fix: improve context overflow error with diagnostic details#15749superlowburn wants to merge 3 commits intoopenclaw:mainfrom
Conversation
Include provider/model, context window limit, session identifier, and message count in the overflow error. Add actionable recovery steps (/reset, switch model, adjust contextTokens). Closes openclaw#9409 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
| const kind = isCompactionFailure ? "compaction_failure" : "context_overflow"; | ||
| const contextLimit = ctxInfo.tokens; | ||
| const sessionLabel = params.sessionKey ?? params.sessionId; | ||
| const overflowDetails = [ |
There was a problem hiding this comment.
Possible crash on overflow
ctxInfo.tokens is used as a number (contextLimit.toLocaleString()), but resolveContextWindowInfo isn’t guaranteed to return a numeric tokens value in all configurations. If tokens is undefined/non-number, this will throw while handling a context overflow, masking the real error and preventing an error payload from being returned.
Consider guarding/normalizing ctxInfo.tokens before calling toLocaleString().
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/pi-embedded-runner/run.ts
Line: 603:606
Comment:
**Possible crash on overflow**
`ctxInfo.tokens` is used as a number (`contextLimit.toLocaleString()`), but `resolveContextWindowInfo` isn’t guaranteed to return a numeric `tokens` value in all configurations. If `tokens` is `undefined`/non-number, this will throw while handling a context overflow, masking the real error and preventing an error payload from being returned.
Consider guarding/normalizing `ctxInfo.tokens` before calling `toLocaleString()`.
How can I resolve this? If you propose a fix, please make it concise.There was a problem hiding this comment.
Valid — ctxInfo.tokens could be undefined depending on the provider/config, which would throw on .toLocaleString(). Will add a fallback: const contextLimit = ctxInfo.tokens ?? 'unknown' and skip the .toLocaleString() when it's not a number.
src/agents/pi-embedded-runner/run.ts
Outdated
| const sessionLabel = params.sessionKey ?? params.sessionId; | ||
| const overflowDetails = [ | ||
| `Context overflow (${provider}/${modelId}, limit: ${contextLimit.toLocaleString()} tokens)`, | ||
| sessionLabel ? ` Session: ${sessionLabel}` : null, | ||
| ` Messages: ${msgCount}`, |
There was a problem hiding this comment.
Leaks raw session identifier
The user-facing overflow text now includes Session: ${params.sessionKey ?? params.sessionId}. Elsewhere in this file session identifiers are redacted before logging (redactRunIdentifier(...)), so this appears to be sensitive/internal data that shouldn’t be echoed back to end users (especially if sessionId encodes channel IDs, phone numbers, etc.).
If the goal is diagnostics, consider either redacting here too or only including a short/hashed label intended for users.
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/pi-embedded-runner/run.ts
Line: 605:609
Comment:
**Leaks raw session identifier**
The user-facing overflow text now includes `Session: ${params.sessionKey ?? params.sessionId}`. Elsewhere in this file session identifiers are redacted before logging (`redactRunIdentifier(...)`), so this appears to be sensitive/internal data that shouldn’t be echoed back to end users (especially if `sessionId` encodes channel IDs, phone numbers, etc.).
If the goal is diagnostics, consider either redacting here too or only including a short/hashed label intended for users.
How can I resolve this? If you propose a fix, please make it concise.There was a problem hiding this comment.
Good catch — the rest of this file redacts session identifiers before logging. Will remove the session line from the user-facing error. Diagnostic details should go to logs, not to the user.
src/agents/pi-embedded-runner/run.ts
Outdated
| const overflowDetails = [ | ||
| `Context overflow (${provider}/${modelId}, limit: ${contextLimit.toLocaleString()} tokens)`, | ||
| sessionLabel ? ` Session: ${sessionLabel}` : null, | ||
| ` Messages: ${msgCount}`, | ||
| "", | ||
| "Actions:", | ||
| " \u2022 Run /reset to clear context and start fresh", | ||
| " \u2022 Or switch to a model with a larger context window", | ||
| " \u2022 Consider lowering contextTokens in config to trigger earlier compaction", |
There was a problem hiding this comment.
Compaction failures misreported
This new message is returned for both context_overflow and compaction_failure (via kind), but the header always says Context overflow (...) and the suggested actions are overflow-specific. When isCompactionFailure is true (e.g., summarization/compaction request itself overflows), this text is misleading.
Consider branching the header/actions for compaction_failure vs context_overflow so users get the right recovery steps.
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/pi-embedded-runner/run.ts
Line: 606:614
Comment:
**Compaction failures misreported**
This new message is returned for both `context_overflow` and `compaction_failure` (via `kind`), but the header always says `Context overflow (...)` and the suggested actions are overflow-specific. When `isCompactionFailure` is true (e.g., summarization/compaction request itself overflows), this text is misleading.
Consider branching the header/actions for `compaction_failure` vs `context_overflow` so users get the right recovery steps.
How can I resolve this? If you propose a fix, please make it concise.There was a problem hiding this comment.
Valid — the kind variable distinguishes the two cases but the user-facing message doesn't. Will add a conditional header: "Context overflow" vs "Compaction failure" based on isCompactionFailure, with appropriate actions for each.
…ion vs overflow Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…paction vs overflow header Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
bfc1ccb to
f92900f
Compare
|
This pull request has been automatically marked as stale due to inactivity. |
Summary
Before:
After:
Closes #9409
Test plan
isError: trueanderror.kind, not exact text — unchanged🤖 Generated with Claude Code
Greptile Overview
Greptile Summary
This change updates the embedded PI agent’s context-overflow handling to return a multi-line error message with diagnostics (provider/model, context-window limit, session label, message count) and suggested recovery actions instead of the previous generic one-liner.
The behavior lives in
src/agents/pi-embedded-runner/run.tsinside the overflow/compaction recovery branch, and affects what end users see when the runner detects a context overflow and can’t recover via auto-compaction/tool-result truncation.Confidence Score: 3/5
Last reviewed commit: 281f021