feat(core): tell the agent who sent a message (experimental)

This commit is contained in:
Kit Langton
2026-11-03 16:15:59 -08:00
Unverified
parent 3fcdcb35e8
commit 623331de33
17 changed files with 570 additions and 16 deletions
+2 -1
View File
@@ -2546,7 +2546,8 @@ export type ConfigEntry =
media_blob?: boolean
image_generate?: boolean | { model?: string | { providerID: string; model: string; variant?: string } }
schedules?: boolean
artifacts?: boolean
artifacts?: boolean;
message_context?: boolean
subagent_depth?: number
location_idle_timeout?: string | false
policies?: Array<{
+9
View File
@@ -522,6 +522,15 @@ function normalizeExperimental(
)
if (value !== undefined) result.artifacts = value
}
if (own(experimental, "message_context")) {
const value = decodeEncoded(
ConfigExperimental.Info.fields.message_context,
experimental.message_context,
["experimental", "message_context"],
diagnostics,
)
if (value !== undefined) result.message_context = value
}
if (own(experimental, "subagent_depth")) {
const value = decodeEncoded(
ConfigExperimental.Info.fields.subagent_depth,
@@ -11,6 +11,7 @@ import { Agent } from "../../agent.js"
import { Config } from "../../config.js"
import { Location } from "../../location.js"
import { Session } from "../../session.js"
import { MessageContext } from "../../session/message-context.js"
import { SubagentJob } from "../../session/subagent-job.js"
import { ShellSelect } from "../../shell/select.js"
import { FSUtil } from "@opencode/util/fs-util"
@@ -83,6 +84,7 @@ export const Plugin = define({
...input.prompt,
sessionID: child.id,
text: ["You are a subagent spawned by another session.", text].join("\n"),
metadata: MessageContext.fromSession(parent.id),
resume: false,
})
const recovery = {
+1 -1
View File
@@ -567,7 +567,7 @@ export const layer = Layer.effect(
const send = (request: LLMRequest) => {
if (ai.canCompact(request, { mechanism: "trigger" })) {
return Effect.gen(function* () {
const history = yield* SessionHistory.load(db, context.session.id, "local").pipe(Effect.orDie)
const history = yield* SessionHistory.loadForModel(db, context.session.id, "local").pipe(Effect.orDie)
const retained = recentUserMessages(history, context.model, keep)
const response = yield* llm.compact(request, { ...prepared.options, mechanism: "trigger" })
return yield* toResult([...retained, Message.assistant(response.checkpoint)], response.usage)
+56 -9
View File
@@ -3,9 +3,11 @@ import { Message } from "@opencode/ai/effect"
import { Effect, Option, Schema } from "effect"
import type { Model } from "@opencode/schema/model"
import { SessionLegacy } from "@opencode/schema/session-legacy"
import { Config } from "../config.js"
import { Database } from "../database/database.js"
import { MessageDecodeError } from "./error.js"
import { SessionMessage } from "./message.js"
import { MessageContext } from "./message-context.js"
import type { SessionSchema } from "./schema.js"
import { Instructions } from "../instructions/index.js"
import { InstructionState } from "./instruction-state.js"
@@ -172,10 +174,28 @@ export const decodeMessageRowsWithRefs = Effect.fnUntraced(function* (
)
})
/**
* Reads `experimental.message_context` from the Config in scope; without one, it is off. Only readers that build
* model requests call it, inside the Session's own location.
*/
const messageContextEnabled = Effect.gen(function* () {
const config = yield* Effect.serviceOption(Config.Service)
if (Option.isNone(config)) return false
return Config.latest(yield* config.value.entries(), "experimental")?.message_context === true
})
/**
* User metadata without the keys the prompt builders render as message context. Model requests read history
* through it while `experimental.message_context` is off, in memory only, so they render nothing.
*/
const withoutMessageContext = (metadata: Readonly<Record<string, unknown>>) =>
Object.fromEntries(Object.entries(metadata).filter(([key]) => !MessageContext.keys.includes(key)))
const messageEntries = Effect.fnUntraced(function* (
db: DatabaseService,
sessionID: SessionSchema.ID,
boundary: Boundary,
keepContext: boolean,
before?: number,
): Effect.fn.Return<
ReadonlyArray<{ readonly seq: number; readonly message: SessionMessage.Info }>,
@@ -202,16 +222,21 @@ const messageEntries = Effect.fnUntraced(function* (
// A window that cannot replay leaves nothing to start from, so read from the compaction before it.
const start = decoded[0]
if (compaction && start && !replayable(start.message, boundary))
return yield* messageEntries(db, sessionID, boundary, compaction.seq)
const entries = compaction ? yield* withRecentText(db, sessionID, decoded) : decoded
return yield* messageEntries(db, sessionID, boundary, keepContext, compaction.seq)
const entries = compaction ? yield* withRecentText(db, sessionID, decoded, keepContext) : decoded
// Re-expansion may cross a native checkpoint whose completion already advanced the instruction
// epoch: the baseline supersedes the chronological updates before it. Forks seed their baseline
// at sequence 0 but retain parent sequences, so the copied checkpoint still retires them.
const native = entries.findLast((entry) => isNativeCompaction(entry.message))
// Skipped native checkpoints are not textual summaries. Their original transcript remains available.
return entries.filter(
(entry) =>
!(entry.message.type === "system" && native && entry.seq < native.seq) && replayable(entry.message, boundary),
return entries.flatMap((entry) =>
!(entry.message.type === "system" && native && entry.seq < native.seq) && replayable(entry.message, boundary)
? [
keepContext || entry.message.type !== "user" || entry.message.metadata === undefined
? entry
: { ...entry, message: { ...entry.message, metadata: withoutMessageContext(entry.message.metadata) } },
]
: [],
)
})
@@ -224,6 +249,7 @@ const withRecentText = Effect.fnUntraced(function* (
db: DatabaseService,
sessionID: SessionSchema.ID,
entries: ReadonlyArray<{ readonly seq: number; readonly message: SessionMessage.Info }>,
keepContext: boolean,
) {
const first = entries[0]
const compaction = first?.message
@@ -252,16 +278,35 @@ const withRecentText = Effect.fnUntraced(function* (
.pipe(Effect.orDie)
// The recent text names attachments by mime and name only, so their bytes are never read.
const kept = yield* Effect.forEach(yield* withParts(db, rows), decodeStoredRow, { concurrency: 1 })
const recent = kept.map(SessionRecentText.messageToText).filter(Boolean).join("\n\n")
const recent = kept
.map((message) =>
SessionRecentText.messageToText(
keepContext || message.type !== "user" || message.metadata === undefined
? message
: { ...message, metadata: withoutMessageContext(message.metadata) },
),
)
.filter(Boolean)
.join("\n\n")
return [{ ...first, message: { ...compaction, metadata: { ...compaction.metadata, recent } } }, ...entries.slice(1)]
})
/** The Session's history with metadata as stored. Model requests read {@link loadForModel} instead. */
export const load = Effect.fn("SessionHistory.load")(function* (
db: DatabaseService,
sessionID: SessionSchema.ID,
boundary: Boundary,
) {
return (yield* messageEntries(db, sessionID, boundary)).map((entry) => entry.message)
return (yield* messageEntries(db, sessionID, boundary, true)).map((entry) => entry.message)
})
/** {@link load} for a model request, with message context as `experimental.message_context` configures it. */
export const loadForModel = Effect.fn("SessionHistory.loadForModel")(function* (
db: DatabaseService,
sessionID: SessionSchema.ID,
boundary: Boundary,
) {
return (yield* messageEntries(db, sessionID, boundary, yield* messageContextEnabled)).map((entry) => entry.message)
})
export const entriesForRunner = Effect.fn("SessionHistory.entriesForRunner")(function* (
@@ -270,10 +315,11 @@ export const entriesForRunner = Effect.fn("SessionHistory.entriesForRunner")(fun
instructions: Instructions.List,
boundary: Boundary,
) {
const keepContext = yield* messageContextEnabled
return yield* db
.transaction(() =>
Effect.gen(function* () {
const messages = yield* messageEntries(db, sessionID, boundary)
const messages = yield* messageEntries(db, sessionID, boundary, keepContext)
return {
initial: yield* InstructionState.initial(db, sessionID, instructions),
entries: messages,
@@ -290,10 +336,11 @@ export const preview = Effect.fn("SessionHistory.preview")(function* (
boundary: Boundary,
) {
const observed = yield* Instructions.read(instructions)
const keepContext = yield* messageContextEnabled
return yield* db
.transaction(() =>
Effect.gen(function* () {
const messages = yield* messageEntries(db, sessionID, boundary)
const messages = yield* messageEntries(db, sessionID, boundary, keepContext)
// An active assistant may contain an unresolved tool call, so only preview the settled prefix.
const unsettled = messages.findIndex(
(entry) => entry.message.type === "assistant" && entry.message.time.completed === undefined,
@@ -0,0 +1,86 @@
export * as MessageContext from "./message-context.js"
import { Session } from "@opencode/schema/session"
import { Option, Schema } from "effect"
/** Who sent a user message, stored under {@link ORIGIN} in its metadata. */
export const ORIGIN = "opencode.origin"
export const SessionOrigin = Schema.Struct({
kind: Schema.Literal("session"),
sessionID: Session.ID,
// The sender's title when the message arrived, set by the server. It never changes afterwards, so the line the
// agent reads stays the same and the prompt cache holds; clients show the sender's current title instead.
title: Schema.optional(Schema.String),
})
export type SessionOrigin = typeof SessionOrigin.Type
export const ToolOrigin = Schema.Struct({
kind: Schema.Literal("tool"),
name: Schema.String,
})
export const Origin = Schema.Union([SessionOrigin, ToolOrigin])
export type Origin = typeof Origin.Type
export const decodeOrigin = Schema.decodeUnknownOption(Origin)
/** Prompt metadata for a message one session sends another. */
export const fromSession = (sessionID: Session.ID) => ({
[ORIGIN]: { kind: "session", sessionID } satisfies Origin,
})
type Formatter = (value: unknown) => string | undefined
const MAX_NAME_LENGTH = 100
/** A title or name on one line, trimmed and at most 100 characters, or undefined when nothing is left. */
export const cleanName = (value: string | undefined) => {
const cleaned = Array.from((value ?? "").replace(/\s+/g, " ").trim())
if (cleaned.length === 0) return undefined
return cleaned.slice(0, MAX_NAME_LENGTH).join("").trim()
}
const escapeAttribute = (value: string) =>
value
.replace(/[\r\n]+/g, " ")
.replaceAll("&", "&amp;")
.replaceAll('"', "&quot;")
.replaceAll("<", "&lt;")
.replaceAll(">", "&gt;")
const tag = (name: string, attributes: Record<string, string>) =>
`<${name} ${Object.entries(attributes)
.map(([key, value]) => `${key}="${escapeAttribute(value)}"`)
.join(" ")}/>`
const origin: Formatter = (value) => {
const decoded = Option.getOrUndefined(decodeOrigin(value))
if (decoded === undefined) return undefined
if (decoded.kind === "tool") {
const name = cleanName(decoded.name)
return name === undefined ? undefined : tag("message-origin", { tool: name })
}
const title = cleanName(decoded.title)
return tag("message-origin", { session: decoded.sessionID, ...(title === undefined ? {} : { title }) })
}
// Metadata keys the agent reads, each with a pure formatter. History drops them all when
// `experimental.message_context` is off, so a formatter never needs to check the flag.
const formatters: Record<string, Formatter> = {
[ORIGIN]: origin,
}
export const keys = Object.keys(formatters)
/** The lines the agent reads before a user message's text, or "" when its metadata has none. */
export const format = (metadata?: Readonly<Record<string, unknown>>) =>
metadata === undefined
? ""
: Object.entries(formatters)
.flatMap(([key, line]) => line(metadata[key]) ?? [])
.join("\n")
/** A user message's text with its context lines before it. */
export const withText = (metadata: Readonly<Record<string, unknown>> | undefined, text: string) =>
[format(metadata), text].filter(Boolean).join("\n")
+30 -1
View File
@@ -6,6 +6,8 @@ import { SessionInbox } from "@opencode/schema/session-inbox"
import type { Session } from "@opencode/schema/session"
import type { SessionMessage } from "@opencode/schema/session-message"
import { FSUtil } from "@opencode/util/fs-util"
import { isFallbackTitle } from "@opencode/util/session-title-fallback"
import { eq } from "drizzle-orm"
import { Effect, Option, Schema } from "effect"
import path from "path"
import { fileURLToPath } from "url"
@@ -17,6 +19,8 @@ import { PluginHooks } from "../plugin/hooks.js"
import { Skill } from "../skill.js"
import { SessionBlob } from "./blob.js"
import { AttachmentError, SkillNotFoundError } from "./error.js"
import { MessageContext } from "./message-context.js"
import { SessionTable } from "./sql.js"
import { SessionStored } from "./stored.js"
// Image types the resizer can decode; others such as HEIC and AVIF go to the provider unchanged.
@@ -84,12 +88,37 @@ export const prepare = Effect.fn("SessionPrompt.prepare")(function* (request: {
agents: input.agents,
files,
skills: selected?.length ? selected : undefined,
metadata: event.metadata,
metadata: yield* withSenderTitle(event.metadata),
}),
delivery: event.delivery,
} satisfies SessionInbox.Item
})
// A session origin carries its sender's title from when it arrived, taken here and never from the caller. The agent
// reads only this copy, so its line never changes once sent; an untitled sender stays untitled.
const withSenderTitle = Effect.fnUntraced(function* (metadata: Record<string, unknown> | undefined) {
if (metadata === undefined) return metadata
const origin = Option.getOrUndefined(MessageContext.decodeOrigin(metadata[MessageContext.ORIGIN]))
if (origin?.kind !== "session") return metadata
const { db } = yield* Database.Service
const row = yield* db
.select({ title: SessionTable.title })
.from(SessionTable)
.where(eq(SessionTable.id, origin.sessionID))
.get()
.pipe(Effect.orDie)
const stored = row?.title ?? undefined
const title = isFallbackTitle(stored) ? undefined : MessageContext.cleanName(stored)
return {
...metadata,
[MessageContext.ORIGIN]: {
kind: origin.kind,
sessionID: origin.sessionID,
...(title === undefined ? {} : { title }),
},
}
})
const materializeAttachment = Effect.fn("SessionPrompt.materializeAttachment")(function* (
input: PromptInput.FileAttachment,
) {
+2 -1
View File
@@ -1,5 +1,6 @@
export * as SessionRecentText from "./recent-text.js"
import { MessageContext } from "./message-context.js"
import type { SessionMessage } from "./message.js"
import type { SessionStored } from "./stored.js"
@@ -30,7 +31,7 @@ export const messageToText = (message: SessionMessage.Info | SessionStored.User)
const name = file.name ?? (file.source.type === "uri" ? file.source.uri : "inline attachment")
return `[Attached ${file.mime}: ${name}]`
}) ?? []
return [...skills, `[User]: ${message.text}`, ...files].join("\n")
return [...skills, `[User]: ${MessageContext.withText(message.metadata, message.text)}`, ...files].join("\n")
}
case "location-switched":
+3 -1
View File
@@ -317,7 +317,9 @@ const layer = Layer.effect(
// The Session is about to go idle, which leaves the background summary time to finish.
if (!outcome.needsContinuation)
yield* compaction.precompute(
store.context(sessionID).pipe(Effect.map((messages) => ({ ...loaded, messages }))),
SessionHistory.loadForModel(db, sessionID, "latest").pipe(
Effect.map((messages) => ({ ...loaded, messages })),
),
)
return outcome.needsContinuation
}),
@@ -14,6 +14,7 @@ import { Option, Schema } from "effect"
import { fileURLToPath } from "url"
import { SessionMessage } from "../message.js"
import { SessionHistory } from "../history.js"
import { MessageContext } from "../message-context.js"
import { SessionRecentText } from "../recent-text.js"
import type { FileAttachment } from "@opencode/schema/prompt"
@@ -239,9 +240,10 @@ function toLLMMessage(message: SessionMessage.Info, model: Model.Ref, providerMe
}),
]
case "user":
const text = MessageContext.withText(message.metadata, message.text)
const content = [
...(message.skills ?? []).flatMap((skill) => (skill.text === undefined ? [] : [Message.text(skill.text)])),
...(message.text === "" ? [] : [Message.text(message.text)]),
...(text === "" ? [] : [Message.text(text)]),
...userAttachmentContent(message.files ?? []),
]
if (content.length === 0) return []
@@ -10,6 +10,7 @@ import { Job } from "../../job.js"
import { Model } from "../../model.js"
import { Permission } from "../../permission.js"
import { Session } from "../../session.js"
import { MessageContext } from "../../session/message-context.js"
import { SessionSchema } from "../../session/schema.js"
import { SubagentCompletion } from "../../session/subagent-completion.js"
import { SubagentJob } from "../../session/subagent-job.js"
@@ -209,6 +210,7 @@ export const Plugin = {
existing === undefined
? ["You are a subagent spawned by another session.", input.prompt].join("\n")
: input.prompt,
metadata: MessageContext.fromSession(context.sessionID),
...(background && existing === undefined ? { resume: false } : {}),
})
.pipe(
@@ -96,6 +96,12 @@ describe("command subagents", () => {
expect((yield* sessions.context(child.id)).filter((message) => message.type === "user")).toMatchObject([
{ text: "You are a subagent spawned by another session.\nReview changes: ready" },
])
// The stored message says which session sent it; the model reads it only with experimental.message_context.
expect(
(yield* sessions.messages({ sessionID: child.id })).flatMap((message) =>
message.type === "user" ? [message.metadata] : [],
),
).toEqual([{ "opencode.origin": { kind: "session", sessionID: parent.id, title: "Parent session" } }])
yield* gate.release
yield* llm.wait(2)
yield* sessions.wait(parent.id)
@@ -0,0 +1,288 @@
import { describe, expect, test } from "bun:test"
import { Message } from "@opencode/ai/effect"
import { Bus } from "@opencode/core/bus"
import { Config } from "@opencode/core/config"
import { Database } from "@opencode/core/database/database"
import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder"
import { Project } from "@opencode/core/project"
import { ProjectTable } from "@opencode/core/project/sql"
import { AbsolutePath } from "@opencode/core/schema"
import { SessionEvent } from "@opencode/core/session/event"
import { SessionHistory } from "@opencode/core/session/history"
import { SessionInbox } from "@opencode/core/session/inbox"
import { SessionMessage } from "@opencode/core/session/message"
import { MessageContext } from "@opencode/core/session/message-context"
import { SessionProjector } from "@opencode/core/session/projector"
import { SessionRecentText } from "@opencode/core/session/recent-text"
import { toLLMMessages } from "@opencode/core/session/runner/to-llm-message"
import { SessionSchema } from "@opencode/core/session/schema"
import { SessionTable } from "@opencode/core/session/sql"
import { SessionStore } from "@opencode/core/session/store"
import { Document, Info } from "@opencode/schema/config"
import { ConfigExperimental } from "@opencode/schema/config/experimental"
import { Model } from "@opencode/schema/model"
import { Provider } from "@opencode/schema/provider"
import { LayerNode } from "@opencode/util/effect/layer-node"
import { eq } from "drizzle-orm"
import { DateTime, Effect } from "effect"
import { testEffect } from "./lib/effect"
const created = DateTime.makeUnsafe(0)
const model = Model.Ref.make({ id: Model.ID.make("model"), providerID: Provider.ID.make("provider") })
const senderID = SessionSchema.ID.make("ses_context_sender")
const receiverID = SessionSchema.ID.make("ses_context_receiver")
const ORIGIN = MessageContext.ORIGIN
const user = (text: string, metadata?: Record<string, unknown>) =>
SessionMessage.User.make({
id: SessionMessage.ID.make("msg_context_user"),
type: "user",
text,
metadata,
time: { created },
})
describe("MessageContext.format", () => {
test("renders a session origin with its title, or the session alone without one", () => {
expect(MessageContext.format({ [ORIGIN]: { kind: "session", sessionID: senderID, title: "Fix login" } })).toBe(
`<message-origin session="${senderID}" title="Fix login"/>`,
)
expect(MessageContext.format({ [ORIGIN]: { kind: "session", sessionID: senderID } })).toBe(
`<message-origin session="${senderID}"/>`,
)
expect(MessageContext.format({ [ORIGIN]: { kind: "session", sessionID: senderID, title: " " } })).toBe(
`<message-origin session="${senderID}"/>`,
)
})
test("renders a tool origin by name", () => {
expect(MessageContext.format({ [ORIGIN]: { kind: "tool", name: "release-bot" } })).toBe(
`<message-origin tool="release-bot"/>`,
)
})
test("keeps titles and tool names on one line, trimmed and at most 100 characters", () => {
expect(
MessageContext.format({ [ORIGIN]: { kind: "session", sessionID: senderID, title: " Fix\nlogin\r\n now " } }),
).toBe(`<message-origin session="${senderID}" title="Fix login now"/>`)
expect(MessageContext.format({ [ORIGIN]: { kind: "tool", name: "release\nbot" } })).toBe(
`<message-origin tool="release bot"/>`,
)
expect(MessageContext.format({ [ORIGIN]: { kind: "tool", name: " \n " } })).toBe("")
expect(MessageContext.format({ [ORIGIN]: { kind: "session", sessionID: senderID, title: "a".repeat(150) } })).toBe(
`<message-origin session="${senderID}" title="${"a".repeat(100)}"/>`,
)
expect(MessageContext.cleanName("😀".repeat(101))).toBe("😀".repeat(100))
})
test("escapes attribute values", () => {
expect(
MessageContext.format({ [ORIGIN]: { kind: "session", sessionID: senderID, title: `Fix "<b>" & more` } }),
).toBe(`<message-origin session="${senderID}" title="Fix &quot;&lt;b&gt;&quot; &amp; more"/>`)
})
test("renders nothing for metadata without context or a value that doesn't decode", () => {
expect(MessageContext.format(undefined)).toBe("")
expect(MessageContext.format({ source: "api" })).toBe("")
for (const value of [
"ses_context_sender",
{ kind: "session" },
{ kind: "session", sessionID: "not-a-session" },
{ kind: "tool" },
{ kind: "agent", name: "build" },
null,
])
expect(MessageContext.format({ [ORIGIN]: value })).toBe("")
})
})
describe("prompt builders", () => {
const origin = { [ORIGIN]: { kind: "session", sessionID: senderID, title: "Fix login" } }
const line = `<message-origin session="${senderID}" title="Fix login"/>`
test("toLLMMessages puts the context line before the text the person typed", () => {
expect(toLLMMessages([user("rebase the PRs", origin)], model)[0]?.content).toEqual([
Message.text(`${line}\nrebase the PRs`),
])
expect(toLLMMessages([user("", origin)], model)[0]?.content).toEqual([Message.text(line)])
})
test("toLLMMessages output is unchanged without context", () => {
expect(toLLMMessages([user("rebase the PRs", { source: "api" })], model)).toEqual([
Message.make({
id: SessionMessage.ID.make("msg_context_user"),
role: "user",
content: [Message.text("rebase the PRs")],
metadata: { source: "api" },
}),
])
})
test("compaction's recent text gives the message one [User]: line", () => {
const text = SessionRecentText.messageToText(user("rebase the PRs", origin))
expect(text).toBe(`[User]: ${line}\nrebase the PRs`)
expect(text.match(/\[User\]:/g)).toHaveLength(1)
})
})
const it = testEffect(
AppNodeBuilder.build(
LayerNode.group([Database.node, Bus.node, SessionProjector.node, SessionInbox.node, SessionStore.node]),
[Bus.node.replace(Bus.configured({ persist: true }))],
),
)
const flag = (message_context: boolean) =>
Config.testLayer([
new Document({
type: "document",
info: new Info({ experimental: new ConfigExperimental.Info({ message_context }) }),
}),
])
const setup = (title: string | undefined) =>
Effect.gen(function* () {
const { db } = yield* Database.Service
const bus = yield* Bus.Service
const inbox = yield* SessionInbox.Service
yield* db
.insert(ProjectTable)
.values({ id: Project.ID.global, worktree: AbsolutePath.make("/project"), sandboxes: [] })
.run()
for (const [sessionID, slug] of [
[senderID, "sender"],
[receiverID, "receiver"],
] as const)
yield* bus.publish(SessionEvent.Created, {
sessionID,
projectID: Project.ID.global,
location: { directory: AbsolutePath.make("/project") },
slug,
...(sessionID === senderID && title !== undefined ? { title } : {}),
version: "test",
})
const prompt = Effect.fnUntraced(function* (text: string, metadata?: Record<string, unknown>) {
const id = SessionMessage.ID.create()
yield* inbox.admit({
id,
sessionID: receiverID,
item: { type: "user", payload: { text, metadata }, delivery: "steer" },
})
yield* bus.publish(SessionEvent.InboxDelivered, { sessionID: receiverID, inboxID: id })
return id
})
const texts = (enabled: boolean) =>
SessionHistory.loadForModel(db, receiverID, "local").pipe(
Effect.provide(flag(enabled)),
Effect.map((history) =>
toLLMMessages(history, model).flatMap((message) =>
message.content.flatMap((part) => (part.type === "text" ? [part.text] : [])),
),
),
)
return { db, bus, prompt, texts }
})
const tag = (title?: string) =>
title === undefined
? `<message-origin session="${senderID}"/>`
: `<message-origin session="${senderID}" title="${title}"/>`
describe("SessionHistory message context", () => {
it.effect("model reads drop the key with the flag off, so the request matches a message without it", () =>
Effect.gen(function* () {
const s = yield* setup("Fix login")
yield* s.prompt("hello", { [ORIGIN]: { kind: "session", sessionID: senderID, title: "Fix login" } })
for (const history of [
yield* SessionHistory.loadForModel(s.db, receiverID, "local").pipe(Effect.provide(flag(false))),
// Without any Config in scope the flag is off too.
yield* SessionHistory.loadForModel(s.db, receiverID, "local"),
]) {
const [message] = history
if (message?.type !== "user") return yield* Effect.die("Expected a user message")
expect(message.metadata).toEqual({})
const { metadata: _, ...plain } = message
expect(toLLMMessages(history, model)).toEqual(toLLMMessages([plain], model))
}
}),
)
it.effect("stored reads return metadata as stored whatever the flag", () =>
Effect.gen(function* () {
const s = yield* setup("Fix login")
const store = yield* SessionStore.Service
const origin = { kind: "session", sessionID: senderID, title: "Fix login" }
yield* s.prompt("hello", { [ORIGIN]: origin, source: "api" })
for (const messages of [
yield* store.context(receiverID),
yield* store.context(receiverID).pipe(Effect.provide(flag(false))),
yield* SessionHistory.load(s.db, receiverID, "latest").pipe(Effect.provide(flag(true))),
])
expect(messages.map((message) => message.metadata)).toEqual([{ [ORIGIN]: origin, source: "api" }])
}),
)
it.effect("with the flag on, renders the saved title, which never changes after the message is sent", () =>
Effect.gen(function* () {
const s = yield* setup("Fix login")
yield* s.prompt("hello", { [ORIGIN]: { kind: "session", sessionID: senderID, title: "Fix login" } })
yield* s.prompt("tool says hi", { [ORIGIN]: { kind: "tool", name: "release-bot" } })
yield* s.prompt("typed by a person")
const expected = [
`${tag("Fix login")}\nhello`,
`<message-origin tool="release-bot"/>\ntool says hi`,
"typed by a person",
]
expect(yield* s.texts(true)).toEqual(expected)
// Renaming or deleting the sender leaves earlier lines alone, so the receiver's prompt cache holds.
yield* s.bus.publish(SessionEvent.Renamed, { sessionID: senderID, title: "Login fixes" })
expect(yield* s.texts(true)).toEqual(expected)
yield* s.db.delete(SessionTable).where(eq(SessionTable.id, senderID)).run()
expect(yield* s.texts(true)).toEqual(expected)
expect(yield* s.texts(false)).toEqual(["hello", "tool says hi", "typed by a person"])
}),
)
it.effect("renders a sender untitled at admission by its session alone, even once it gets a title", () =>
Effect.gen(function* () {
const s = yield* setup(undefined)
yield* s.prompt("early", { [ORIGIN]: { kind: "session", sessionID: senderID } })
yield* s.bus.publish(SessionEvent.Renamed, { sessionID: senderID, title: "Auto title" })
expect(yield* s.texts(true)).toEqual([`${tag()}\nearly`])
}),
)
it.effect("leaves a value that doesn't decode unrendered", () =>
Effect.gen(function* () {
const s = yield* setup("Fix login")
yield* s.prompt("hello", { [ORIGIN]: { kind: "session", sessionID: 42 } })
expect(yield* s.texts(true)).toEqual(["hello"])
}),
)
it.effect("builds a compaction's recent text with one [User]: line, as the flag configures", () =>
Effect.gen(function* () {
const s = yield* setup("Fix login")
const keepID = yield* s.prompt("kept", {
[ORIGIN]: { kind: "session", sessionID: senderID, title: "Fix login" },
})
yield* s.bus.publish(
SessionEvent.Compaction.Ended,
{ sessionID: receiverID, reason: "manual", model, text: "summary" },
{ metadata: { keepID } },
)
const recent = (enabled: boolean) =>
SessionHistory.loadForModel(s.db, receiverID, "latest").pipe(
Effect.provide(flag(enabled)),
Effect.map((history) => {
const compaction = history[0]
return compaction?.type === "compaction" && compaction.status === "completed"
? SessionRecentText.kept(compaction)
: undefined
}),
)
expect(yield* recent(true)).toBe(`[User]: ${tag("Fix login")}\nkept`)
expect(yield* recent(false)).toBe("[User]: kept")
}),
)
})
+64 -1
View File
@@ -84,6 +84,13 @@ const mediaBlob = Config.testLayer([
info: new Info({ experimental: new ConfigExperimental.Info({ media_blob: true }) }),
}),
])
const contextDirectory = "/context"
const messageContext = Config.testLayer([
new Document({
type: "document",
info: new Info({ experimental: new ConfigExperimental.Info({ message_context: true }) }),
}),
])
const locations = makeGlobalNode({
service: LocationServiceMap.Service,
layer: Layer.effect(
@@ -95,7 +102,11 @@ const locations = makeGlobalNode({
// oxlint-disable-next-line typescript-eslint/no-unsafe-type-assertion
Layer.mergeAll(
// Every real instance has its own config, so the caller's never shows through.
ref.directory === mediaDirectory ? mediaBlob : Config.testLayer([]),
ref.directory === mediaDirectory
? mediaBlob
: ref.directory === contextDirectory
? messageContext
: Config.testLayer([]),
ref.directory === brokenDirectory ? Layer.effectDiscard(Effect.die("instance boot failed")) : Layer.empty,
LayerNode.compile(LayerNode.group([PluginHooks.node, Skill.node]), {
replacements: [Bus.node.replace(Layer.succeed(Bus.Service, bus))],
@@ -535,6 +546,58 @@ describe("Session.prompt", () => {
}),
)
it.effect("sets a session origin's title from the sender at admission, never from the caller", () =>
Effect.gen(function* () {
yield* setup
const { db } = yield* Database.Service
const session = yield* Session.Service
const insert = (id: Session.ID, directory: string, title: string | null) =>
db
.insert(SessionTable)
.values({ id, project_id: Project.ID.global, slug: id, directory, title, version: "test" })
.run()
.pipe(Effect.orDie)
const receiverID = Session.ID.make("ses_prompt_context")
const untitledID = Session.ID.make("ses_prompt_untitled")
const messyID = Session.ID.make("ses_prompt_messy")
yield* insert(receiverID, contextDirectory, "receiver")
yield* insert(untitledID, "/project", null)
yield* insert(messyID, "/project", ` Fix\nlogin ${"x".repeat(120)}`)
const send = (receiver: Session.ID, sender: Session.ID) =>
session
.prompt({
sessionID: receiver,
text: "from another session",
metadata: { "opencode.origin": { kind: "session", sessionID: sender, title: "Spoofed" }, source: "api" },
resume: false,
})
.pipe(Effect.map((message) => message.payload))
// The text is stored as sent; only the origin's title is the server's.
expect(yield* send(receiverID, sessionID)).toMatchObject({
text: "from another session",
metadata: { "opencode.origin": { kind: "session", sessionID, title: "test" }, source: "api" },
})
// The flag only decides what the agent reads, so the stored title is set the same way without it.
expect((yield* send(sessionID, sessionID)).metadata).toEqual({
"opencode.origin": { kind: "session", sessionID, title: "test" },
source: "api",
})
expect((yield* send(receiverID, untitledID)).metadata).toEqual({
"opencode.origin": { kind: "session", sessionID: untitledID },
source: "api",
})
expect((yield* send(receiverID, Session.ID.make("ses_prompt_deleted"))).metadata).toEqual({
"opencode.origin": { kind: "session", sessionID: "ses_prompt_deleted" },
source: "api",
})
expect((yield* send(receiverID, messyID)).metadata).toEqual({
"opencode.origin": { kind: "session", sessionID: messyID, title: `Fix login ${"x".repeat(90)}` },
source: "api",
})
}),
)
it.live("admits captured image bytes with their source path after the source disappears", () =>
Effect.gen(function* () {
yield* setup
+9
View File
@@ -517,6 +517,15 @@ describe("SubagentTool", () => {
message.type === "user" ? [message.payload.text] : [],
),
).toEqual(["You are a subagent spawned by another session.\nreview this", "continue this"])
// Both the first prompt and the continuation say which session sent them.
expect(
(yield* sessions.inbox(childID)).flatMap((message) =>
message.type === "user" ? [message.payload.metadata] : [],
),
).toEqual([
{ "opencode.origin": { kind: "session", sessionID: parent.id } },
{ "opencode.origin": { kind: "session", sessionID: parent.id } },
])
expect(second.content).toEqual([{ type: "text", text: completedOutput(childID) }])
}),
),
+3
View File
@@ -16709,6 +16709,9 @@
"artifacts": {
"type": "boolean"
},
"message_context": {
"type": "boolean"
},
"subagent_depth": {
"type": "integer",
"minimum": 0,
@@ -40,6 +40,10 @@ export class Info extends Schema.Class<Info>("ConfigExperimental.Info")({
artifacts: Schema.Boolean.pipe(optional).annotate({
description: "Enable experimental artifacts backed by pluggable providers. Defaults to false.",
}),
message_context: Schema.Boolean.pipe(optional).annotate({
description:
"Tell the agent who sent each message, such as another session or a tool, from the message's metadata. Defaults to false.",
}),
subagent_depth: NonNegativeInt.pipe(optional).annotate({
description: "Maximum subagent nesting depth. Defaults to 1.",
}),