Repository navigation
fix(core): recover gateway context overflow on the Tobs side #4257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8f57911
fix(core): recover gateway context overflow through native compaction
60e8cc0
test(docs): include the overflow recovery event consumer
22f5a46
Merge main and retain both verification journeys
860ec93
fix(core): persist compacted context before overflow retry
b4863e4
chore: keep overflow replay within the source policy
0990d33
fix(core): confirm stored Session replacement before retry
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| --- | ||
| section: Fixed | ||
| --- | ||
|
|
||
| ## en | ||
|
|
||
| - Bots now recover from gateway token-limit errors that DSH reports as ordinary invalid requests: Tobs requests native context compaction and retries once after committed progress, instead of repeatedly failing until paused ([#4253](https://github.com/BotHarness/DeepSeekBot/issues/4253)). | ||
|
|
||
| ## zh | ||
|
|
||
| - 修复网关上下文超限错误被 DSH 归类为普通无效请求后 Bot 连续失败直至暂停的问题:Tobs 调用原生上下文压缩,在确认变更已提交后重试一次([#4253](https://github.com/BotHarness/DeepSeekBot/issues/4253))。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| import type { Agent, RequestErrorAction } from '@deepseek-ai/dsh-agent'; | ||
| import type {} from '@deepseek-ai/dsh-agent-preset-registry'; | ||
| import type { LlmFailure } from '@deepseek-ai/dsh-llm'; | ||
| import type {} from '@deepseek-ai/dsh-session-persistence'; | ||
| import { isDeepStrictEqual } from 'node:util'; | ||
|
|
||
| export interface ContextOverflowRequest { | ||
| agent: Pick<Agent, 'session' | 'ctx' | 'options'>; | ||
| turn: number; | ||
| step: number; | ||
| provider: string; | ||
| failure: LlmFailure; | ||
| signal: AbortSignal; | ||
| } | ||
|
|
||
| export interface ContextOverflowRecoveryOptions { | ||
| isBotSession(sessionId: string): boolean; | ||
| log(event: Record<string, unknown>): void; | ||
| } | ||
|
|
||
| const TOKEN_LIMIT = | ||
| /\b(?:input|prompt|request)\s+(?:is\s+)?too\s+(?:long|large):\s*model\s+limit\s+\d+\s+tokens?,\s*requested\s+\d+\s+tokens?\b/i; | ||
|
|
||
| export function createContextOverflowRecovery(options: ContextOverflowRecoveryOptions) { | ||
| const attempted = new WeakMap<object, { turn: number; step: number }>(); | ||
| return async ( | ||
| request: ContextOverflowRequest, | ||
| next: () => Promise<RequestErrorAction>, | ||
| ): Promise<RequestErrorAction> => { | ||
| const { agent, turn, step, failure, signal } = request; | ||
| if ( | ||
| failure.code !== 'INVALID_REQUEST' || | ||
| !TOKEN_LIMIT.test(failure.message) || | ||
| signal.aborted || | ||
| !options.isBotSession(agent.session.id) | ||
| ) | ||
| return next(); | ||
| const delegated = await next(); | ||
| if (delegated !== undefined || signal.aborted) return delegated; | ||
| const previous = attempted.get(agent); | ||
| if (previous?.turn === turn && previous.step === step) return undefined; | ||
| const presets = agent.ctx.get('agentPresets'); | ||
| const compaction = | ||
| typeof presets?.serviceFor === 'function' | ||
| ? presets.serviceFor(agent, 'compaction') | ||
| : agent.ctx.get('compaction'); | ||
| if (compaction === undefined) { | ||
| options.log({ | ||
| event: 'skipped', | ||
| outcome: 'declined', | ||
| reason: 'compaction-unavailable', | ||
| sessionId: agent.session.id, | ||
| }); | ||
| return undefined; | ||
| } | ||
| attempted.set(agent, { turn, step }); | ||
| const generation = agent.session.surface.replaceGeneration; | ||
| const startedAt = Date.now(); | ||
| const event = { sessionId: agent.session.id, turn, step, provider: request.provider }; | ||
| options.log({ event: 'started', ...event }); | ||
| let failed = false; | ||
| try { | ||
| await compaction.compactIfNeeded(agent, 'context-overflow', signal); | ||
| } catch { | ||
| failed = true; | ||
| } | ||
| const progress = agent.session.surface.replaceGeneration > generation; | ||
| let persisted = false; | ||
| let flushFailed = false; | ||
| if (progress && !signal.aborted) { | ||
| try { | ||
| const sessions = agent.ctx.get('sessions'); | ||
| const storage = agent.ctx.get('sessionPersistence'); | ||
| const replacement = agent.session | ||
| .snapshotEvents() | ||
| .findLast( | ||
| (entry) => typeof entry.surfaceOp === 'object' && entry.surfaceOp.op === 'replace', | ||
| ); | ||
| if ( | ||
| storage && | ||
| replacement && | ||
| sessions && | ||
| 'flush' in sessions && | ||
| typeof sessions.flush === 'function' && | ||
| (await sessions.flush(agent.session)) === true | ||
| ) { | ||
| const reader = await storage.open(agent.session.id, 'read', { signal }); | ||
| try { | ||
| const stored = await reader.read(replacement.seq, 1, { signal }); | ||
| persisted = isDeepStrictEqual(stored.events[0], replacement); | ||
| } finally { | ||
| await reader.close(); | ||
| } | ||
| } | ||
| } catch { | ||
| persisted = false; | ||
| flushFailed = true; | ||
| } | ||
| } | ||
| const retry = progress && persisted && !signal.aborted; | ||
| options.log({ | ||
| event: 'finished', | ||
| ...event, | ||
| durationMs: Date.now() - startedAt, | ||
| outcome: retry ? 'retry' : 'declined', | ||
| reason: signal.aborted | ||
| ? 'cancelled' | ||
| : progress | ||
| ? flushFailed | ||
| ? 'persistence-failed' | ||
| : persisted | ||
| ? 'context-replaced' | ||
| : 'persistence-unavailable' | ||
| : failed | ||
| ? 'compaction-failed' | ||
| : 'no-progress', | ||
| }); | ||
| return retry ? { kind: 'retry' } : undefined; | ||
| }; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.