Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions packages/opencode/src/effort-history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -609,16 +609,16 @@ export function applyOpenCodeEffortMarkers(
}

for (const message of consumed.messages) {
if (message.transitions.length > 1) {
throw new EffortMarkerCorrelationError(
'Multiple internal Fable 5.1 effort markers on one user boundary',
)
}
const transition = message.transitions[0]
if (transition && transition.scope !== requestPlan.scope) {
throw new EffortMarkerCorrelationError(
'Fable 5.1 effort marker scope mismatch',
)
// Consecutive host user records collapse into one wire message, so a
// boundary may legitimately carry several transitions. The flat checks
// below pin their order and identity; every one still needs its scope
// verified, not just the first.
for (const transition of message.transitions) {
if (transition.scope !== requestPlan.scope) {
throw new EffortMarkerCorrelationError(
'Fable 5.1 effort marker scope mismatch',
)
}
}
if (message.anchors.length > 1) {
throw new EffortMarkerCorrelationError(
Expand Down Expand Up @@ -719,7 +719,7 @@ export function applyOpenCodeEffortMarkers(
let inserted = 0
const rewritten: unknown[] = []
for (const message of consumed.messages) {
const transition = message.transitions[0]
const transition = message.transitions.at(-1)
if (transition && applyConfig) {
rewritten.push({
role: 'system',
Expand Down
184 changes: 184 additions & 0 deletions packages/opencode/src/tests/effort-history.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,4 +579,188 @@ describe('OpenCode Fable 5.1 effort markers', () => {
expect(markOpenCodeEffortTransitions(otherModel)).toBeNull()
expect(markerTexts(otherModel)).toEqual([])
})

test('accepts a merged boundary whose transitions are a correctly-ordered run and emits the last effort', () => {
const messages = [
user('msg_low', 'ses_merged', 'claude-fable-5-1', 'low'),
user('msg_high', 'ses_merged', 'claude-fable-5-1', 'high'),
user('msg_max', 'ses_merged', 'claude-fable-5-1', 'max'),
]
const plan = markOpenCodeEffortTransitions(messages)
expect(plan).not.toBeNull()
expect(plan?.markerCount).toBe(2)

const highMarker = markerTexts([messages[1]!])[0]
const maxMarker = markerTexts([messages[2]!])[0]
const anchor = messages[2]!.parts
.map((part) => part.text)
.find((text) => text.startsWith(EFFORT_ANCHOR_PREFIX))
expect(highMarker).toBeDefined()
expect(maxMarker).toBeDefined()
expect(anchor).toBeDefined()

const body: {
model: string
output_config: { effort: string }
messages: unknown[]
} = {
model: 'claude-fable-5-1',
output_config: { effort: 'low' },
messages: [
{ role: 'user', content: [{ type: 'text', text: 'msg_low' }] },
{
role: 'user',
content: [
{ type: 'text', text: highMarker },
{ type: 'text', text: maxMarker },
{ type: 'text', text: anchor },
],
},
],
}

expect(
applyOpenCodeEffortMarkers(
body,
true,
encodeOpenCodeEffortPlan(plan as NonNullable<typeof plan>),
plan as NonNullable<typeof plan>,
),
).toEqual({ found: 2, inserted: 1 })
expect(body.messages).toContainEqual({
role: 'system',
content: [],
output_config: { effort: 'max' },
})
expect(body.messages).not.toContainEqual({
role: 'system',
content: [],
output_config: { effort: 'high' },
})
expect(JSON.stringify(body)).not.toContain('cortexkit-internal-effort')
})

test('rejects a merged boundary whose transitions are out of order', () => {
const messages = [
user('msg_low', 'ses_merged_order', 'claude-fable-5-1', 'low'),
user('msg_high', 'ses_merged_order', 'claude-fable-5-1', 'high'),
user('msg_max', 'ses_merged_order', 'claude-fable-5-1', 'max'),
]
const plan = markOpenCodeEffortTransitions(messages)
const highMarker = markerTexts([messages[1]!])[0]
const maxMarker = markerTexts([messages[2]!])[0]
const anchor = messages[2]!.parts
.map((part) => part.text)
.find((text) => text.startsWith(EFFORT_ANCHOR_PREFIX))
const body = {
model: 'claude-fable-5-1',
output_config: { effort: 'low' },
messages: [
{ role: 'user', content: [{ type: 'text', text: 'msg_low' }] },
{
role: 'user',
content: [
{ type: 'text', text: maxMarker },
{ type: 'text', text: highMarker },
{ type: 'text', text: anchor },
],
},
],
}

expect(() =>
applyOpenCodeEffortMarkers(
body,
true,
encodeOpenCodeEffortPlan(plan as NonNullable<typeof plan>),
plan as NonNullable<typeof plan>,
),
).toThrow('Fable 5.1 effort marker non-prefix loss')
})

test('rejects a merged boundary whose transitions are duplicated', () => {
const messages = [
user('msg_low', 'ses_merged_dup', 'claude-fable-5-1', 'low'),
user('msg_high', 'ses_merged_dup', 'claude-fable-5-1', 'high'),
user('msg_max', 'ses_merged_dup', 'claude-fable-5-1', 'max'),
]
const plan = markOpenCodeEffortTransitions(messages)
const highMarker = markerTexts([messages[1]!])[0]
const maxMarker = markerTexts([messages[2]!])[0]
const anchor = messages[2]!.parts
.map((part) => part.text)
.find((text) => text.startsWith(EFFORT_ANCHOR_PREFIX))
const body = {
model: 'claude-fable-5-1',
output_config: { effort: 'low' },
messages: [
{ role: 'user', content: [{ type: 'text', text: 'msg_low' }] },
{
role: 'user',
content: [
{ type: 'text', text: highMarker },
{ type: 'text', text: highMarker },
{ type: 'text', text: maxMarker },
{ type: 'text', text: anchor },
],
},
],
}

expect(() =>
applyOpenCodeEffortMarkers(
body,
true,
encodeOpenCodeEffortPlan(plan as NonNullable<typeof plan>),
plan as NonNullable<typeof plan>,
),
).toThrow('Fable 5.1 effort marker correlation failed: expected 2, found 3')
})

test('rejects a foreign-scope marker at index 1 of a merged boundary', () => {
const messages = [
user('msg_low', 'ses_merged_scope', 'claude-fable-5-1', 'low'),
user('msg_high', 'ses_merged_scope', 'claude-fable-5-1', 'high'),
user('msg_max', 'ses_merged_scope', 'claude-fable-5-1', 'max'),
]
const plan = markOpenCodeEffortTransitions(messages)
const highMarker = markerTexts([messages[1]!])[0]
const anchor = messages[2]!.parts
.map((part) => part.text)
.find((text) => text.startsWith(EFFORT_ANCHOR_PREFIX))

// A valid marker minted for a different session carries a foreign scope.
const foreignMessages = [
user('msg_foreign_low', 'ses_foreign_scope', 'claude-fable-5-1', 'low'),
user('msg_foreign_high', 'ses_foreign_scope', 'claude-fable-5-1', 'high'),
]
markOpenCodeEffortTransitions(foreignMessages)
const foreignMarker = markerTexts([foreignMessages[1]!])[0]
expect(foreignMarker).toBeDefined()

const body = {
model: 'claude-fable-5-1',
output_config: { effort: 'low' },
messages: [
{ role: 'user', content: [{ type: 'text', text: 'msg_low' }] },
{
role: 'user',
content: [
{ type: 'text', text: highMarker },
{ type: 'text', text: foreignMarker },
{ type: 'text', text: anchor },
],
},
],
}

expect(() =>
applyOpenCodeEffortMarkers(
body,
true,
encodeOpenCodeEffortPlan(plan as NonNullable<typeof plan>),
plan as NonNullable<typeof plan>,
),
).toThrow('Fable 5.1 effort marker scope mismatch')
})
})
8 changes: 6 additions & 2 deletions packages/opencode/src/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ import {
} from '@cortexkit/anthropic-auth-core'
import { SubcCallError } from '@cortexkit/subc-client'
import { getOpenCodeClaustrumEnrollmentPaths } from '../claustrum-enrollment-registry'
import { EFFORT_MARKER_PREFIX } from '../effort-history'
import { EFFORT_ANCHOR_PREFIX, EFFORT_MARKER_PREFIX } from '../effort-history'
import { AnthropicAuthPlugin } from '../index'
import { LANE_START_REQUEST_HEADER, LANE_START_TEXT } from '../lane-start'
import {
Expand Down Expand Up @@ -10542,6 +10542,9 @@ describe('Fable 5.1 request-scoped effort history', () => {
const transitionMarker = internalTexts?.find((text) =>
text.startsWith(EFFORT_MARKER_PREFIX),
)
const anchorMarker = internalTexts?.find((text) =>
text.startsWith(EFFORT_ANCHOR_PREFIX),
)
expect(transitionMarker).toBeString()
const correlatedHeaders = { headers: {} as Record<string, string> }
await plugin['chat.headers'](
Expand Down Expand Up @@ -10612,10 +10615,11 @@ describe('Fable 5.1 request-scoped effort history', () => {
const duplicateTransition = await send('ses_effort_duplicate_transition', [
transitionMarker,
transitionMarker,
anchorMarker,
])
expect(duplicateTransition.status).toBe(400)
expect((await duplicateTransition.json()).error.message).toBe(
'Multiple internal Fable 5.1 effort markers on one user boundary',
'Fable 5.1 effort marker correlation failed: expected 1, found 2',
)
expect(messagesCalled).toBe(false)
})
Expand Down
Loading