Repository navigation
fix(crag): drop caller cache_control on native history blocks and stop failover on CRAG_CACHE_CONTROL (#1120) #1129
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
Closed
+267
−3
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,92 @@ | ||
| import assert from 'node:assert/strict' | ||
| import test from 'node:test' | ||
| import { dropNativeBlockCacheAnchors, prepareCragPrintBody } from '../../src/lib/protocol/crag-print-prompt.mjs' | ||
|
|
||
| const CC = { type: 'ephemeral' } | ||
| const history = () => [ | ||
| { role: 'user', content: [{ type: 'text', text: 'run ls' }] }, | ||
| { | ||
| role: 'assistant', | ||
| content: [ | ||
| { type: 'thinking', thinking: 'x', signature: 'sig' }, | ||
| { | ||
| type: 'tool_use', | ||
| id: 'toolu_1', | ||
| name: 'Bash', | ||
| input: { command: 'ls' }, | ||
| caller: { type: 'direct' }, | ||
| cache_control: CC, | ||
| }, | ||
| ], | ||
| }, | ||
| { role: 'user', content: [{ type: 'tool_result', tool_use_id: 'toolu_1', content: 'a', cache_control: CC }] }, | ||
| ] | ||
|
|
||
| test('drops cache_control only on model-emitted (caller) assistant blocks', () => { | ||
| const input = history() | ||
| const out = dropNativeBlockCacheAnchors(input) | ||
| const toolUse = out[1].content[1] | ||
| assert.equal(toolUse.cache_control, undefined) | ||
| assert.deepEqual(toolUse.caller, { type: 'direct' }) | ||
| assert.equal(toolUse.id, 'toolu_1') | ||
| assert.deepEqual(out[1].content[0], input[1].content[0]) | ||
| assert.deepEqual(out[2].content[0].cache_control, CC, 'trailing tool_result breakpoint stays') | ||
| assert.equal(input[1].content[1].cache_control, CC, 'input not mutated') | ||
| }) | ||
|
|
||
| test('leaves blocks without caller and non-assistant turns alone', () => { | ||
| const input = history() | ||
| delete input[1].content[1].caller | ||
| assert.equal(dropNativeBlockCacheAnchors(input), input) | ||
| const user = [{ role: 'user', content: [{ type: 'text', text: 'x', caller: { type: 'direct' }, cache_control: CC }] }] | ||
| assert.equal(dropNativeBlockCacheAnchors(user), user) | ||
| }) | ||
|
|
||
| test('prepareCragPrintBody applies it', () => { | ||
| const out = prepareCragPrintBody( | ||
| { model: 'claude-opus-5-5', max_tokens: 10, messages: history() }, | ||
| { foldSystemMessages: false }, | ||
| ) | ||
| const toolUse = out.messages[1].content.find((block) => block.type === 'tool_use') | ||
| assert.equal(toolUse.id, 'toolu_1') | ||
| assert.equal(toolUse.cache_control, undefined) | ||
| assert.deepEqual(out.messages[2].content[0].cache_control, CC) | ||
| }) | ||
|
|
||
| test('drops cache_control on a tool_result that carries a tool_reference', () => { | ||
| const input = [ | ||
| { role: 'user', content: [{ type: 'text', text: 'plan' }] }, | ||
| { | ||
| role: 'assistant', | ||
| content: [ | ||
| { type: 'tool_use', id: 'toolu_s', name: 'ToolSearch', input: { query: 'x' }, caller: { type: 'direct' } }, | ||
| ], | ||
| }, | ||
| { | ||
| role: 'user', | ||
| content: [ | ||
| { | ||
| type: 'tool_result', | ||
| tool_use_id: 'toolu_s', | ||
| content: [{ type: 'tool_reference', tool_name: 'ExitPlanMode' }], | ||
| cache_control: CC, | ||
| }, | ||
| ], | ||
| }, | ||
| ] | ||
| const out = dropNativeBlockCacheAnchors(input) | ||
| assert.equal(out[2].content[0].cache_control, undefined) | ||
| assert.deepEqual(out[2].content[0].content, input[2].content[0].content) | ||
| assert.deepEqual(input[2].content[0].cache_control, CC, 'input not mutated') | ||
| }) | ||
|
|
||
| test('keeps cache_control on an ordinary tool_result and on user text', () => { | ||
| const input = [ | ||
| { | ||
| role: 'user', | ||
| content: [{ type: 'tool_result', tool_use_id: 't', content: [{ type: 'text', text: 'a' }], cache_control: CC }], | ||
| }, | ||
| { role: 'user', content: [{ type: 'text', text: 'go', cache_control: CC }] }, | ||
| ] | ||
| assert.equal(dropNativeBlockCacheAnchors(input), input) | ||
| }) |
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,65 @@ | ||
| import assert from 'node:assert/strict' | ||
| import { test } from 'node:test' | ||
| import { surfaceCragPolicyBlock } from '../../src/lib/transport/go-worker-client.mjs' | ||
| import { classifyUpstreamResult, shouldContinue } from '../../src/lib/pool/upstream-error-policy.mjs' | ||
|
|
||
| const blocked = (over = {}, result = {}) => ({ | ||
| ok: false, | ||
| status: 502, | ||
| via: 'go-worker-stream', | ||
| committed: false, | ||
| terminalState: 'incomplete', | ||
| body: { type: 'message', role: 'assistant', content: [] }, | ||
| nativeHandoff: false, | ||
| providerResult: { | ||
| _internal_v2: true, | ||
| schema_version: 2, | ||
| type: 'kin_provider_result', | ||
| source: 'crag', | ||
| provider_complete: false, | ||
| usage_complete: false, | ||
| public_status: 502, | ||
| delivery_error: { status: 502, type: 'crag_policy_error' }, | ||
| error: { type: 'crag_policy_error', message: 'Request was blocked before provider dispatch' }, | ||
| observation: { | ||
| source: 'crag', | ||
| policy_blocked: true, | ||
| capture_failed: false, | ||
| exception: { | ||
| name: 'Error', | ||
| code: 'CRAG_CACHE_CONTROL', | ||
| message: 'Caller cache anchor changed in native history', | ||
| }, | ||
| }, | ||
| ...over, | ||
| }, | ||
| ...result, | ||
| }) | ||
|
|
||
| test('CRAG cache-anchor block is surfaced and stops failover', () => { | ||
| const before = classifyUpstreamResult(blocked()) | ||
| assert.equal(shouldContinue(before), true, 'baseline: an unmarked block looks like a retryable empty hop') | ||
|
|
||
| const out = surfaceCragPolicyBlock(blocked()) | ||
| assert.equal(out.status, 502) | ||
| assert.equal(out.terminalState, 'rejected') | ||
| assert.equal(out.body.error.code, 'crag_policy_blocked') | ||
| assert.match(out.body.error.message, /CRAG_CACHE_CONTROL: Caller cache anchor changed/) | ||
| const policy = classifyUpstreamResult(out) | ||
| assert.equal(policy.action, 'stop') | ||
| assert.equal(policy.reason, 'crag_policy_blocked') | ||
| assert.equal(shouldContinue(policy), false) | ||
| }) | ||
|
|
||
| test('other results are untouched', () => { | ||
| for (const input of [ | ||
| blocked({ observation: { policy_blocked: true, exception: { code: 'CRAG_OTHER' } } }), | ||
| blocked({ source: 'provider_sse' }), | ||
| blocked({ provider_complete: true }), | ||
| blocked({}, { committed: true }), | ||
| blocked({}, { ok: true, status: 200 }), | ||
| blocked({}, { providerResult: null }), | ||
| ]) { | ||
| assert.equal(surfaceCragPolicyBlock(input), input) | ||
| } | ||
| }) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a CRAG block reaches either the non-streaming response path or the uncommitted streaming path,
handle-protocol.mjspasses it throughmapProtocolClientError()andmapUpstreamError(). The latter has no case forcrag_policy_blocked, so its fallback replaces this code withupstream_errorand discardsdetails.retryable: false. Thus API callers receive a generic 502 without the machine-readable indication that this is a deterministic, non-retryable rejection. Add a corresponding public error mapping so the new code and retryability survive beyond the transport helper.Useful? React with 👍 / 👎.