fix(core): pass stored message metadata to session request hooks (#63590)

This commit is contained in:
Aiden Cline
2026-11-12 19:00:57 -06:00
committed by GitHub
Unverified
parent 85a043926a
commit 2f886a2a5e
7 changed files with 100 additions and 7 deletions
+22 -3
View File
@@ -24,6 +24,7 @@ import type {
SessionGenerate,
SessionRequest,
SessionRequestKind,
SessionRequestMessage,
SessionTitle,
} from "@opencode/plugin/effect/session"
import type { Agent } from "@opencode/schema/agent"
@@ -83,7 +84,7 @@ export interface Input {
readonly model: SessionRunnerModel.Resolved
readonly tools?: Tool.Snapshot
readonly system: Array<SystemPart>
readonly messages: Array<Message>
readonly messages: Array<SessionRequestMessage>
readonly toolChoice?: LLM.LLMRequestInput["toolChoice"]
/** Only the durable runner may use a stateful WebSocket. */
readonly webSocket?: "session"
@@ -123,7 +124,15 @@ export const baseTranscript = (input: {
metadata: { experimental: { instructions: input.initial } },
},
].filter((part) => part.text.length > 0),
messages: toLLMMessages(input.messages, input.model.ref, providerMetadataKey),
// Hooks read each stored message's metadata on the messages built from it; `prepare` strips it before the request.
messages: input.messages.flatMap((message) =>
toLLMMessages([message], input.model.ref, providerMetadataKey).map(
(item): SessionRequestMessage =>
message.metadata === undefined || item.id !== message.id
? item
: { ...item, metadata: message.metadata },
),
),
}
}
@@ -327,7 +336,17 @@ export const layer = Layer.effect(
promptCacheKey: /^ses_[0-9a-f]{64}$/.test(affinity) ? affinity.slice(4) : affinity,
cache: model.cache?.ttl === undefined ? undefined : { ttl: model.cache.ttl },
system: shaped.system,
messages: boundImages(unsupportedParts(shaped.messages, model.capabilities, model.model.provider)),
// Strip the stored metadata hooks could read, so it never reaches the provider.
messages: boundImages(
unsupportedParts(
shaped.messages.map((message) => {
const { metadata, ...rest } = message
return metadata === undefined ? message : Message.make(rest)
}),
model.capabilities,
model.model.provider,
),
),
tools: Array.from(hooked, ([name, t]) => ({
...t,
name,
@@ -792,7 +792,9 @@ it.effect("summary updates include the retained tail in compaction hooks and the
expect(yield* compactManually(session, messages)).toEqual({ status: "completed" })
expect(requests).toHaveLength(1)
expect(requests[0].messages.slice(0, -1)).toEqual(expected)
// Hooks read the checkpoint's stored metadata; the model request does not carry it.
expect(expected[0]?.metadata).toEqual({ recent: "[User]: Previous exact request" })
expect(requests[0].messages.slice(0, -1)).toEqual(expected.map((message) => Message.make({ ...message })))
expect(requests[0].messages.at(-1)?.content).toEqual([{ type: "text", text: SessionCompaction.buildPrompt(true) }])
const store = yield* SessionStore.Service
expect(yield* store.context(session.id)).toMatchObject([
+61 -1
View File
@@ -15,6 +15,7 @@ import {
InvalidRequestError,
RateLimitError,
UnknownProviderError,
type UserMessage,
} from "@opencode/ai/effect"
import { OpenAIChat } from "@opencode/ai/protocols/openai-chat"
import { AnthropicMessages, OpenAIResponses } from "@opencode/ai/protocols"
@@ -77,7 +78,21 @@ import { SessionSystemPrompt } from "@opencode/core/session/system-prompt"
import { ID, Model } from "@opencode/core/model"
import { Location } from "@opencode/core/location"
import { Provider } from "@opencode/core/provider"
import { Cause, Context, DateTime, Deferred, Effect, Exit, Fiber, Layer, Queue, Schema, Scope, Stream, Tracer } from "effect"
import {
Cause,
Context,
DateTime,
Deferred,
Effect,
Exit,
Fiber,
Layer,
Queue,
Schema,
Scope,
Stream,
Tracer,
} from "effect"
import { TestClock } from "effect/testing"
import { and, asc, desc, eq, sql } from "drizzle-orm"
import { testEffect } from "./lib/effect"
@@ -1176,6 +1191,51 @@ describe("SessionRunnerLLM", () => {
expect(s.executions).toEqual(["renamed"])
})
scenario("passes stored message metadata to the context hook but not to the model request", function* (s) {
const hooks = yield* PluginHooks.Service
yield* hooks.register("session", "prompt", (event) =>
Effect.sync(() => {
event.metadata = { canary: { context: ["CANARY-TOKEN"] } }
}),
)
yield* hooks.register("session", "context", (event) =>
Effect.sync(() => {
// Mirrors JavaScript plugins, which edit the payload in place.
for (const message of event.messages) {
const canary = message.metadata?.canary as { context: string[] } | undefined
if (message.role !== "user" || canary === undefined) continue
;(message.content as Array<UserMessage["content"][number]>).unshift(Message.text(canary.context.join("\n")))
}
}),
)
yield* s.llm.push(TestLLM.text("Done", "text-done"))
yield* s.runPrompt("Start")
expect(s.requests[0]?.messages.at(-1)?.content).toEqual([Message.text("CANARY-TOKEN"), Message.text("Start")])
expect(s.requests[0]?.messages.some((message) => Object.hasOwn(message, "metadata"))).toBe(false)
const compiled = yield* compileRequest(s.requests[0]!)
expect(JSON.stringify(compiled.body)).toContain("CANARY-TOKEN")
expect(JSON.stringify(compiled.body)).not.toContain('"metadata"')
})
scenario("keeps context hook edits to messages without stored metadata", function* (s) {
const hooks = yield* PluginHooks.Service
yield* hooks.register("session", "context", (event) =>
Effect.sync(() => {
for (const message of event.messages) {
if (message.role !== "user" || message.metadata !== undefined) continue
;(message.content as Array<UserMessage["content"][number]>).unshift(Message.text("EDITED"))
}
}),
)
yield* s.llm.push(TestLLM.text("Done", "text-done"))
yield* s.runPrompt("Start")
expect(s.requests[0]?.messages.at(-1)?.content).toEqual([Message.text("EDITED"), Message.text("Start")])
})
for (const resume of [false, true]) {
scenario(
`continues instead of going idle when a session idle hook admits input (resume: ${resume})`,
+4 -1
View File
@@ -22,6 +22,9 @@ export interface SessionPrompt {
/** Request overrides. Typed keys are generation settings; any other key is a provider option. */
export type SessionRequestOptions = Types.DeepMutable<GenerationOptionsFields> & Record<string, unknown>
/** A request message. `metadata` is the stored Session message's metadata, for reading only; it is not sent to the model. */
export type SessionRequestMessage = Message & { readonly metadata?: Record<string, unknown> }
export interface SessionRequest {
readonly sessionID: Session.ID
readonly model: Model.Ref
@@ -30,7 +33,7 @@ export interface SessionRequest {
* not updated when a hook edits `text`. Experimental.
*/
system: Array<SystemPart>
messages: Array<Message>
messages: Array<SessionRequestMessage>
options: SessionRequestOptions
}
+4 -1
View File
@@ -22,6 +22,9 @@ export interface SessionPrompt {
/** Request overrides. Typed keys are generation settings; any other key is a provider option. */
export type SessionRequestOptions = Types.DeepMutable<GenerationOptionsFields> & Record<string, unknown>
/** A request message. `metadata` is the stored Session message's metadata, for reading only; it is not sent to the model. */
export type SessionRequestMessage = Message & { readonly metadata?: Record<string, unknown> }
export interface SessionRequest {
readonly sessionID: Session.ID
readonly model: Model.Ref
@@ -30,7 +33,7 @@ export interface SessionRequest {
* not updated when a hook edits `text`. Experimental.
*/
system: Array<SystemPart>
messages: Array<Message>
messages: Array<SessionRequestMessage>
options: SessionRequestOptions
}
@@ -1289,6 +1289,9 @@ effect: (ctx) =>
}),
```
Messages in the `context`, `compaction`, and `generate` hooks carry their stored message's `metadata`, read-only
(for example, what a `prompt` hook set). It is not sent to the model.
The `system` part that holds OpenCode's instructions also lists each source in `metadata.experimental.instructions` as
`{ key, text }`, such as `core/date` or `core/environment`, so a hook can tell the sources apart. The list is a copy:
editing the part's `text` does not update it. This is experimental and may change.
@@ -1460,6 +1460,9 @@ await ctx.session.hook("compaction", async (event) => {
Changes affect only the outgoing model call, not persisted history or configuration. A hook that should apply to
every request must register for each kind.
Messages in the `context`, `compaction`, and `generate` hooks carry their stored message's `metadata`, read-only
(for example, what a `prompt` hook set). It is not sent to the model.
The `system` part that holds OpenCode's instructions also lists each source in `metadata.experimental.instructions` as
`{ key, text }`, such as `core/date` or `core/environment`, so a hook can tell the sources apart. The list is a copy:
editing the part's `text` does not update it. This is experimental and may change.