@@ -39,6 +39,7 @@ import { getCachedCatalog, ModelNotAvailableError, type CacheEntry } from './cat
3939import { anySignal , cancelBodyOnAbort } from '../../../lib/abort.js' ;
4040import { parseRetryAfterFromMessage } from '../../../lib/retry-delay.js' ;
4141import { resolveDevinApiBaseUrl } from '../../../oauth/devin/api-base.js' ;
42+ import { normalizeDevinToolParameters } from './tool-schema.js' ;
4243
4344/**
4445 * Connect-RPC streaming inactivity timeout. If the cloud sends zero bytes
@@ -175,6 +176,7 @@ export function allocateCascadeId(): string {
175176 * #3 prompt: string (text content)
176177 * #4 num_tokens: int (rough estimate)
177178 * #5 safe_for_code_telemetry: bool (1 = ok to log)
179+ * #9 tool_result_is_error: bool (tool prompts only)
178180 * #10 images: repeated ImageData (multimodal)
179181 * #11 thinking: string (assistant reasoning, replayed)
180182 * #12 signature: string (opaque attestation for #11)
@@ -218,6 +220,7 @@ function encodeChatMessagePrompt(
218220 thinking ?: string ;
219221 signature ?: string ;
220222 signatureType ?: string ;
223+ isError ?: boolean ;
221224 } ,
222225) : Buffer {
223226 const textParts = content . filter ( ( p ) : p is { type : 'text' ; text : string } => p . type === 'text' ) ;
@@ -234,6 +237,9 @@ function encodeChatMessagePrompt(
234237 if ( opts ?. toolCallId ) {
235238 parts . push ( encodeString ( 7 , opts . toolCallId ) ) ;
236239 }
240+ // Accepted live on a tool prompt, and the model reported the failure from the
241+ // flag alone, so a failed tool result needs no in-band text marker.
242+ if ( opts ?. isError ) parts . push ( encodeVarintField ( 9 , 1 ) ) ;
237243 // Assistant message with tool_calls: encode each as a ChatToolCall.
238244 if ( opts ?. toolCalls && opts . toolCalls . length > 0 ) {
239245 for ( const tc of opts . toolCalls ) {
@@ -257,21 +263,16 @@ function encodeChatMessagePrompt(
257263const SOURCE_BY_ROLE : Record < string , number > = {
258264 user : 1 ,
259265 assistant : 2 ,
260- // NOTE: do not send source=3 (SYSTEM) directly — the Codeium chat backend
261- // returns "third-party model provider is experiencing issues" when any
262- // ChatMessagePrompt has source=SYSTEM. The captured LS upstream traffic
263- // shows the IDE inlines system context into the *user* prompt (source=1)
264- // wrapped in <additional_metadata>...</additional_metadata>. We collapse
265- // role:'system' messages into the next user turn before building the
266- // proto — see `collapseSystemIntoUser` below.
266+ // Never sent as a prompt source: the leading system text goes in request #2,
267+ // and a later system message is collapsed into the next user turn below.
267268 system : 1 ,
268269 tool : 4 ,
269270} ;
270271
271272/**
272- * Collapse OpenAI-style messages so all `role:'system'` entries are inlined
273- * into the immediately-following user message, matching the wire format the
274- * IDE uses. Cognition's chat backend rejects raw role= system entries .
273+ * Collapse `role:'system'` entries that follow the conversation start into the
274+ * immediately-following user message. The leading run of system messages never
275+ * reaches here; it is the request's #2 system prompt .
275276 *
276277 * [{system: "S1"}, {system: "S2"}, {user: "U1"}, {assistant: "A1"}, {user: "U2"}]
277278 *
@@ -428,6 +429,8 @@ export interface ChatHistoryItem {
428429 thinking ?: string ;
429430 signature ?: string ;
430431 signature_type ?: string ;
432+ /** For `role: 'tool'` only — the tool failed. Encoded as ChatMessagePrompt #9. */
433+ is_error ?: boolean ;
431434}
432435
433436/**
@@ -535,8 +538,8 @@ interface BuildArgs {
535538 messages : ChatHistoryItem [ ] ;
536539 cascadeId : string ;
537540 /**
538- * GetChatMessageRequest #22 . Optional because it is omitted on a first turn;
539- * the working client only reuses one across a later tool loop.
541+ * GetChatMessageRequest #17 prompt_id . Optional because it is omitted on a
542+ * first turn; the working client only reuses one across a later tool loop.
540543 */
541544 promptId ?: string ;
542545 sessionId : string ;
@@ -639,7 +642,7 @@ export function sanitizeToolDescriptionForCognitionForTests(description: string)
639642 return sanitizeToolDescriptionForCognition ( description ) ;
640643}
641644
642- function encodeToolDef ( tool : ToolDef ) : Buffer {
645+ function encodeToolDef ( tool : ToolDef , modelUid : string ) : Buffer {
643646 const rawDesc = sanitizeToolDescriptionForCognition ( tool . description ?? '' ) ;
644647 const desc =
645648 rawDesc . length > MAX_TOOL_DESC_LEN
@@ -648,7 +651,7 @@ function encodeToolDef(tool: ToolDef): Buffer {
648651 return Buffer . concat ( [
649652 encodeString ( 1 , tool . name ) ,
650653 encodeString ( 2 , desc ) ,
651- encodeString ( 3 , JSON . stringify ( tool . parameters ?? { } ) ) ,
654+ encodeString ( 3 , JSON . stringify ( normalizeDevinToolParameters ( modelUid , tool . parameters ?? { } ) ) ) ,
652655 ] ) ;
653656}
654657
@@ -667,9 +670,20 @@ function buildGetChatMessageRequest(args: BuildArgs): Buffer {
667670 cloudChatShape : true ,
668671 } ) ;
669672
670- // System messages must be inlined into the user turn (Cognition cloud
671- // rejects source=3). See `collapseSystemIntoUser` for the format.
672- const collapsed = collapseSystemIntoUser ( args . messages ) ;
673+ // The leading system messages become request #2. Measured live on swe-1-6
674+ // with a ~6.7k-token system prompt: turn 2 read 6688 of 6715 prompt tokens
675+ // from cache in #2, against 7072 of 7097 when the same text was collapsed
676+ // into the first user prompt, so the cache ratio is unchanged and the prompt
677+ // is smaller. The model obeyed an instruction given only in #2.
678+ const firstNonSystem = args . messages . findIndex ( ( m ) => m . role !== 'system' ) ;
679+ const leadingSystem = firstNonSystem === - 1 ? args . messages : args . messages . slice ( 0 , firstNonSystem ) ;
680+ const systemPrompt = leadingSystem
681+ . map ( ( m ) => normalizeContent ( m . content )
682+ . filter ( ( p ) : p is { type : 'text' ; text : string } => p . type === 'text' )
683+ . map ( ( p ) => p . text ) . join ( '\n' ) )
684+ . filter ( Boolean )
685+ . join ( '\n\n' ) ;
686+ const collapsed = collapseSystemIntoUser ( args . messages . slice ( leadingSystem . length ) ) ;
673687 const promptParts = collapsed . map ( ( m ) =>
674688 encodeMessage (
675689 3 ,
@@ -686,6 +700,7 @@ function buildGetChatMessageRequest(args: BuildArgs): Buffer {
686700 thinking : m . role === 'assistant' ? m . thinking : undefined ,
687701 signature : m . role === 'assistant' ? m . signature : undefined ,
688702 signatureType : m . role === 'assistant' ? m . signature_type : undefined ,
703+ isError : m . role === 'tool' ? m . is_error : undefined ,
689704 } ,
690705 ) ,
691706 ) ,
@@ -694,7 +709,7 @@ function buildGetChatMessageRequest(args: BuildArgs): Buffer {
694709 const completion = encodeCompletionConfiguration ( args . completionOpts ?? { } ) ;
695710
696711 const toolParts : Buffer [ ] = ( args . tools ?? [ ] ) . map ( ( t ) =>
697- encodeMessage ( 10 , encodeToolDef ( t ) ) ,
712+ encodeMessage ( 10 , encodeToolDef ( t , args . modelUid ) ) ,
698713 ) ;
699714
700715 // Field layout from mitm capture of the LS:
@@ -704,15 +719,15 @@ function buildGetChatMessageRequest(args: BuildArgs): Buffer {
704719 // #8 completion_configuration
705720 // #10 tools (repeated ChatToolDefinition)
706721 // #13 prompt_cache_options
722+ // #15 CortexTrajectoryReference
707723 // #16 cascade_id (string)
724+ // #17 prompt_id (string)
708725 // #21 chat_model_uid (string)
709- // #22 prompt_id (string)
726+ // #22 execution_id (string)
710727 return Buffer . concat ( [
711728 encodeMessage ( 1 , metadata ) ,
712- // #2 system_prompt is always written, empty when the caller had none. The
713- // system turn is separately collapsed into the first user message because
714- // source=SYSTEM is refused; this field is the one the wire expects here.
715- encodeString ( 2 , '' ) ,
729+ // #2 system_prompt is always written, empty when the caller had none.
730+ encodeString ( 2 , systemPrompt ) ,
716731 ...promptParts ,
717732 encodeVarintField ( 7 , args . requestType ?? 5 ) ,
718733 encodeMessage ( 8 , completion ) ,
@@ -724,7 +739,7 @@ function buildGetChatMessageRequest(args: BuildArgs): Buffer {
724739 // it and records real savings; sending it unconditionally matches both the
725740 // native client and CLIProxyAPIPlus, which places it outside its tools gate.
726741 encodeMessage ( 13 , encodeVarintField ( 1 , PROMPT_CACHE_EPHEMERAL ) ) ,
727- // #15 session model config : { id, turn , 4 }. Present on every verified
742+ // #15 CortexTrajectoryReference : { id, 1 , 4 }. Present on every verified
728743 // request.
729744 encodeMessage ( 15 , Buffer . concat ( [
730745 encodeString ( 1 , crypto . randomUUID ( ) ) ,
@@ -734,9 +749,9 @@ function buildGetChatMessageRequest(args: BuildArgs): Buffer {
734749 encodeString ( 16 , args . cascadeId ) ,
735750 encodeVarintField ( 20 , 1 ) ,
736751 encodeString ( 21 , args . modelUid ) ,
737- // #22 is deliberately omitted. It is a user-exchange id that only appears
738- // from the second turn onward and is reused across that turn's tool loop; a
739- // fresh per-request uuid matches neither shape.
752+ // #17 prompt_id is deliberately omitted. It is a user-exchange id that only
753+ // appears from the second turn onward and is reused across that turn's tool
754+ // loop; a fresh per-request uuid matches neither shape.
740755 ] ) ;
741756}
742757
0 commit comments