feat(opencode): round-trip encrypted reasoning content through the bridge

Closes audit gap #3. The bridge now extracts the encrypted reasoning
blob from `MessageV2.ReasoningPart.metadata` and surfaces it on
`LLM.ReasoningPart.encrypted`, where the Anthropic and Bedrock
adapters lower it to the wire — Anthropic emits `thinking.signature`,
Bedrock emits `reasoningContent.reasoningText.signature`. Without
this, multi-turn sessions with reasoning models would lose the
encrypted state on every step and break the chain.

The encrypted blob originates in three different places depending on
how the session was started:

1. AI-SDK Anthropic sessions store it as
   `metadata.anthropic.signature` (per AI SDK provider-keyed
   convention).
2. AI-SDK OpenAI sessions store it as
   `metadata.openai.reasoningEncryptedContent`.
3. Future LLM-native sessions will store it as a top-level
   `metadata.encrypted` string (cleanest shape — provider-agnostic,
   matches the LLM IR field name).

The new `encryptedReasoning` helper probes all three locations in
order, so existing OpenCode sessions can be served by the LLM-native
path without re-recording reasoning content. The full `metadata`
record continues to flow through to `LLM.ReasoningPart.metadata`
unchanged, preserving any provider-specific fields adapters might
read in the future.

OpenAI Responses encrypted reasoning round-trip is intentionally out
of scope: the LLM-package adapter doesn't yet model reasoning items
in the request body. That's a separate adapter feature requiring new
input-item schema variants and is deferred until needed.

Tests (5 new in llm-native.test.ts):
- AI-SDK Anthropic signature extracted into LLM.ReasoningPart.encrypted.
- End-to-end Anthropic lowering: bridge \u2192 client.prepare \u2192 target with
  `thinking.signature` populated correctly.
- AI-SDK OpenAI reasoningEncryptedContent extracted (forward
  compatibility — useful when the OpenAI Responses adapter gains
  reasoning-item lowering).
- Top-level metadata.encrypted extracted (LLM-native session shape).
- No known key in metadata leaves `encrypted` undefined.

Verified: 33/0/0 across native + bridge tests (was 28; +5 from the
new reasoning extraction tests).
This commit is contained in:
Kit Langton
2026-04-26 21:35:43 -04:00
parent b653261772
commit f59996362e
2 changed files with 163 additions and 1 deletions
+14 -1
View File
@@ -47,6 +47,9 @@ export type RequestInput = {
const isDefined = <T>(value: T | undefined): value is T => value !== undefined
const isRecord = (value: unknown): value is Record<string, unknown> =>
typeof value === "object" && value !== null && !Array.isArray(value)
// Match `data:<mediaType>[;param=value]*[;base64],<payload>`. Captures only the
// payload — the bridge passes it through to `MediaPart.data` (already-base64
// per the convention `ProviderShared.mediaBytes` follows). Non-data URLs
@@ -85,6 +88,16 @@ const providerMeta = (metadata: Record<string, unknown> | undefined) => {
const providerExecuted = (metadata: Record<string, unknown> | undefined) =>
metadata?.providerExecuted === true ? true : undefined
const encryptedReasoning = (metadata: Record<string, unknown> | undefined) => {
if (!metadata) return undefined
if (typeof metadata.encrypted === "string") return metadata.encrypted
if (isRecord(metadata.anthropic) && typeof metadata.anthropic.signature === "string") return metadata.anthropic.signature
if (isRecord(metadata.openai) && typeof metadata.openai.reasoningEncryptedContent === "string") {
return metadata.openai.reasoningEncryptedContent
}
return undefined
}
const isToolPart = (part: MessageV2.Part): part is MessageV2.ToolPart => part.type === "tool"
const supportsPart = (message: MessageV2.WithParts, part: MessageV2.Part) => {
@@ -116,7 +129,7 @@ const toolResultValue = (part: MessageV2.ToolPart) => {
const assistantContent = (part: MessageV2.Part): ReadonlyArray<ContentPart> => {
if (part.type === "text" && !part.ignored) return [LLM.text(part.text)]
if (part.type === "reasoning") return [{ type: "reasoning", text: part.text, metadata: part.metadata }]
if (part.type === "reasoning") return [{ type: "reasoning", text: part.text, encrypted: encryptedReasoning(part.metadata), metadata: part.metadata }]
if (part.type !== "tool") return []
return [