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 [
@@ -968,4 +968,153 @@ describe("LLMNative.request", () => {
expect(json).not.toContain("cachePoint")
expect(json).not.toContain("ephemeral")
}))
// Encrypted reasoning round-trip. OpenCode persists the encrypted blob in
// `MessageV2.ReasoningPart.metadata` using the AI-SDK's provider-keyed
// shape (`metadata.anthropic.signature`,
// `metadata.openai.reasoningEncryptedContent`) for sessions started on the
// AI-SDK path. Future LLM-native sessions will store it as a top-level
// `metadata.encrypted` string. The bridge probes both conventions and
// populates `LLM.ReasoningPart.encrypted` so adapters can lower it to the
// wire (Anthropic `thinking.signature`, Bedrock `reasoningText.signature`).
const reasoningPartWithMetadata = (
messageID: MessageID,
text: string,
metadata: Record<string, unknown>,
): MessageV2.ReasoningPart => ({
id: PartID.ascending(),
sessionID,
messageID,
type: "reasoning",
text,
metadata,
time: { start: 1 },
})
it.effect("extracts AI-SDK Anthropic signature into LLM.ReasoningPart.encrypted", () =>
Effect.gen(function* () {
const mdl = anthropicModel()
const userID = MessageID.ascending()
const assistantID = MessageID.ascending()
const request = yield* LLMNative.request({
provider: ProviderTest.info({ id: ProviderID.make("anthropic"), key: "anthropic-key" }, mdl),
model: mdl,
messages: [
userMessage(mdl, userID, [textPart(userID, "think about it")]),
assistantMessage(mdl, assistantID, userID, [
reasoningPartWithMetadata(assistantID, "thinking...", {
anthropic: { signature: "ant-signature-abc" },
}),
]),
],
})
// The bridge surfaces `encrypted` on the LLM IR's ReasoningPart.
expect(request.messages[1].content[0]).toMatchObject({
type: "reasoning",
text: "thinking...",
encrypted: "ant-signature-abc",
})
}))
it.effect("lowers encrypted reasoning to Anthropic thinking.signature end-to-end", () =>
Effect.gen(function* () {
const mdl = anthropicModel()
const userID = MessageID.ascending()
const assistantID = MessageID.ascending()
const request = yield* LLMNative.request({
provider: ProviderTest.info({ id: ProviderID.make("anthropic"), key: "anthropic-key" }, mdl),
model: mdl,
messages: [
userMessage(mdl, userID, [textPart(userID, "think about it")]),
assistantMessage(mdl, assistantID, userID, [
reasoningPartWithMetadata(assistantID, "thinking...", {
anthropic: { signature: "ant-signature-abc" },
}),
]),
],
})
const prepared = yield* LLMClient.make({
adapters: [AnthropicMessages.adapter],
patches: ProviderPatch.defaults,
}).prepare(request)
expect(prepared.target).toMatchObject({
messages: [
{ role: "user" },
{
role: "assistant",
content: [{ type: "thinking", thinking: "thinking...", signature: "ant-signature-abc" }],
},
],
})
}))
it.effect("extracts AI-SDK OpenAI reasoningEncryptedContent into LLM.ReasoningPart.encrypted", () =>
Effect.gen(function* () {
const mdl = anthropicModel() // any cache-irrelevant cache-capable model works for the bridge check
const userID = MessageID.ascending()
const assistantID = MessageID.ascending()
const request = yield* LLMNative.request({
provider: ProviderTest.info({ id: ProviderID.make("anthropic"), key: "anthropic-key" }, mdl),
model: mdl,
messages: [
userMessage(mdl, userID, [textPart(userID, "think")]),
assistantMessage(mdl, assistantID, userID, [
reasoningPartWithMetadata(assistantID, "internal", {
openai: { reasoningEncryptedContent: "openai-blob-xyz" },
}),
]),
],
})
expect(request.messages[1].content[0]).toMatchObject({
type: "reasoning",
encrypted: "openai-blob-xyz",
})
}))
it.effect("extracts a top-level metadata.encrypted string", () =>
Effect.gen(function* () {
const mdl = anthropicModel()
const userID = MessageID.ascending()
const assistantID = MessageID.ascending()
const request = yield* LLMNative.request({
provider: ProviderTest.info({ id: ProviderID.make("anthropic"), key: "anthropic-key" }, mdl),
model: mdl,
messages: [
userMessage(mdl, userID, [textPart(userID, "think")]),
assistantMessage(mdl, assistantID, userID, [
reasoningPartWithMetadata(assistantID, "internal", { encrypted: "native-blob" }),
]),
],
})
expect(request.messages[1].content[0]).toMatchObject({
type: "reasoning",
encrypted: "native-blob",
})
}))
it.effect("leaves encrypted unset when reasoning metadata carries no known key", () =>
Effect.gen(function* () {
const mdl = anthropicModel()
const userID = MessageID.ascending()
const assistantID = MessageID.ascending()
const request = yield* LLMNative.request({
provider: ProviderTest.info({ id: ProviderID.make("anthropic"), key: "anthropic-key" }, mdl),
model: mdl,
messages: [
userMessage(mdl, userID, [textPart(userID, "think")]),
assistantMessage(mdl, assistantID, userID, [
reasoningPartWithMetadata(assistantID, "internal", { somethingElse: "x" }),
]),
],
})
const reasoning = request.messages[1].content[0]
expect(reasoning).toMatchObject({ type: "reasoning", text: "internal" })
if (reasoning.type === "reasoning") expect(reasoning.encrypted).toBeUndefined()
}))
})