Skip to content

Commit 510aa79

Browse files
authored
fix(core): store forked and imported session media as blobs (#60118)
1 parent 28a3974 commit 510aa79

7 files changed

Lines changed: 459 additions & 68 deletions

File tree

‎packages/core/src/session.ts‎

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -280,16 +280,7 @@ const layer = Layer.effect(
280280
const environments = yield* SessionEnvironment.Service
281281
const locations = yield* LocationServiceMap.Service
282282
const sessions = yield* Session.make()
283-
// `experimental.media_blob` as the session's own location configures it, where its media is written.
284-
const keepsMediaRefs = (session: SessionSchema.Info) =>
285-
SessionBlob.enabled.pipe(
286-
instances.provide(session),
287-
Effect.catch((cause) =>
288-
Effect.logWarning("could not resolve experimental.media_blob; serving media inline", { cause }).pipe(
289-
Effect.as(false),
290-
),
291-
),
292-
)
283+
const mediaBlob = (session: SessionSchema.Info) => SessionBlob.enabledFor(instances, session)
293284
const isDurableSessionEvent = (item: Bus.LogItem): item is SessionEvent.DurableEvent =>
294285
Object.hasOwn(SessionEvent.Durable.cases, item.type)
295286

@@ -378,12 +369,16 @@ const layer = Layer.effect(
378369
// The fork adopts the parent's newest instruction values rather than the
379370
// values in effect at the boundary; copied history may contain frozen
380371
// instruction-update text the initial baseline already reflects.
381-
yield* bus.publish(SessionEvent.Forked, {
382-
sessionID,
383-
parentID: parent.id,
384-
boundary: { type: input.before ? "before" : "through", messageID: boundary.id },
385-
...inherited,
386-
})
372+
// The parent's location, not the caller's, decides whether the copy's media is stored as blobs.
373+
const media = yield* mediaBlob(parent)
374+
yield* bus
375+
.publish(SessionEvent.Forked, {
376+
sessionID,
377+
parentID: parent.id,
378+
boundary: { type: input.before ? "before" : "through", messageID: boundary.id },
379+
...inherited,
380+
})
381+
.pipe(Effect.provideService(SessionBlob.ForkMedia, media))
387382
return yield* result.get(sessionID).pipe(Effect.orDie)
388383
}),
389384
get: (sessionID) => sessions.forSession(sessionID).get(),
@@ -437,13 +432,13 @@ const layer = Layer.effect(
437432
message: (input) => sessions.forSession(input.sessionID).message(input.messageID),
438433
messagesWithRefs: Effect.fn("Session.messagesWithRefs")(function* (input) {
439434
const session = yield* result.get(input.sessionID)
440-
if (!(yield* keepsMediaRefs(session))) return yield* store.messages(input)
435+
if (!(yield* mediaBlob(session))) return yield* store.messages(input)
441436
return yield* store.messagesWithRefs(input)
442437
}),
443438
messageWithRefs: Effect.fn("Session.messageWithRefs")(function* (input) {
444439
const session = yield* result.get(input.sessionID).pipe(Effect.option)
445440
if (Option.isNone(session)) return undefined
446-
if (!(yield* keepsMediaRefs(session.value))) return yield* result.message(input)
441+
if (!(yield* mediaBlob(session.value))) return yield* result.message(input)
447442
const stored = yield* store.messageWithRefs(input.messageID)
448443
return stored?.sessionID === input.sessionID ? stored.message : undefined
449444
}),
@@ -465,7 +460,7 @@ const layer = Layer.effect(
465460
inbox: (sessionID) => sessions.forSession(sessionID).inbox(),
466461
inboxWithRefs: Effect.fn("Session.inboxWithRefs")(function* (sessionID) {
467462
const session = yield* result.get(sessionID)
468-
if (!(yield* keepsMediaRefs(session))) return yield* result.inbox(sessionID)
463+
if (!(yield* mediaBlob(session))) return yield* result.inbox(sessionID)
469464
return yield* SessionInbox.listWithRefs(db, sessionID)
470465
}),
471466
cancelInbox: (input) => sessions.forSession(input.sessionID).cancelInbox(input.inboxID),

‎packages/core/src/session/blob.ts‎

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ export * as SessionBlob from "./blob.js"
22

33
import { createHash } from "node:crypto"
44
import { and, eq, exists, gte, inArray, notExists, notInArray, type SQL, sql } from "drizzle-orm"
5-
import { Effect, Option, Schema } from "effect"
5+
import { Cause, Context, Effect, Option, Schema } from "effect"
66
import { map } from "effect/Array"
77
import type { Attachment, FileAttachment } from "@opencode/schema/prompt"
88
import type { Tool } from "@opencode/schema/tool"
99
import { Config } from "../config.js"
1010
import type { Database } from "../database/database.js"
11+
import type { Instance } from "../instance/service.js"
1112
import type { SessionMessage } from "./message.js"
1213
import type { SessionSchema } from "./schema.js"
1314
import { BlobRefTable, BlobTable, SessionInboxTable, SessionMessageTable } from "./sql.js"
@@ -258,6 +259,28 @@ export const enabled = Effect.gen(function* () {
258259
return Config.latest(yield* config.value.entries(), "experimental")?.media_blob === true
259260
})
260261

262+
/**
263+
* {@link enabled} as `session`'s location configures it, where its media is written. Off when that location can't
264+
* boot or load its config, which surfaces as defects too, so a broken location never fails the caller.
265+
*/
266+
export const enabledFor = (instances: Instance.Interface, session: SessionSchema.Info) =>
267+
enabled.pipe(
268+
instances.provide(session),
269+
Effect.catchCause((cause) =>
270+
Cause.hasInterrupts(cause)
271+
? Effect.failCause(Cause.fromReasons<never>(cause.reasons.filter(Cause.isInterruptReason)))
272+
: Effect.logWarning("could not resolve experimental.media_blob; keeping media inline", { cause }).pipe(
273+
Effect.as(false),
274+
),
275+
),
276+
)
277+
278+
/**
279+
* Whether a fork's projection stores the child's copied media as blobs. `Session.fork` sets it from the parent's
280+
* location; the projection ignores the caller's own config, and a replayed fork without it copies media inline.
281+
*/
282+
export const ForkMedia = Context.Reference<boolean>("@opencode/SessionBlob/ForkMedia", { defaultValue: () => false })
283+
261284
const DATA_URI = /^data:[^,]*;base64,(.*)$/s
262285

263286
function take(pending: Pending, raw: Buffer, mime: string) {
@@ -285,11 +308,15 @@ function toolPartRefs(pending: Pending, part: AssistantPart): AssistantPart {
285308
return { ...part, state: { ...part.state, content: map(content, (item) => toolRef(pending, item)) } }
286309
}
287310

288-
const save = Effect.fnUntraced(function* (db: DatabaseService, pending: Pending) {
289-
if (pending.size === 0) return
311+
/** Inserts blobs, keeping any that already exist. */
312+
export const save = Effect.fnUntraced(function* (
313+
db: DatabaseService,
314+
blobs: ReadonlyArray<typeof BlobTable.$inferInsert>,
315+
) {
316+
if (blobs.length === 0) return
290317
yield* db
291318
.insert(BlobTable)
292-
.values([...pending.values()])
319+
.values([...blobs])
293320
.onConflictDoNothing()
294321
.run()
295322
.pipe(Effect.orDie)
@@ -303,7 +330,7 @@ export const externalizePayload = Effect.fnUntraced(function* <
303330
const pending: Pending = new Map()
304331
const files = input.files.map((file) => fileRef(pending, file))
305332
if (pending.size === 0) return input
306-
yield* save(db, pending)
333+
yield* save(db, [...pending.values()])
307334
return { ...input, files }
308335
})
309336

@@ -320,7 +347,7 @@ export const externalize = Effect.fnUntraced(function* (
320347
const pending: Pending = new Map()
321348
const content = message.content.map((part) => toolPartRefs(pending, part))
322349
if (pending.size === 0) return message
323-
yield* save(db, pending)
350+
yield* save(db, [...pending.values()])
324351
return { ...message, content }
325352
})
326353

@@ -341,6 +368,9 @@ function storedPartRefs(pending: Pending, part: Part): Part {
341368
* Moves a stored row's inline media into blobs whatever the flag says, for offline backfills. Returns the row to
342369
* store (the same object when nothing moved), the blobs to insert, and every hash the row then names.
343370
*/
371+
export function split<T extends MessageRow["data"] | InboxRow["payload"]>(
372+
data: T,
373+
): { data: T; moved: boolean; blobs: Array<typeof BlobTable.$inferInsert>; hashes: Set<string> }
344374
export function split(data: MessageRow["data"] | InboxRow["payload"]) {
345375
const pending: Pending = new Map()
346376
const stored =
@@ -353,13 +383,35 @@ export function split(data: MessageRow["data"] | InboxRow["payload"]) {
353383
return { data: result, moved: pending.size > 0, blobs: [...pending.values()], hashes: hashesOf(result) }
354384
}
355385

386+
/**
387+
* {@link split} for a message row copied into another session by a fork: saves the moved blobs and returns the row
388+
* to store. Callers check {@link ForkMedia} and record the copy's refs.
389+
*/
390+
export const externalizeRow = Effect.fnUntraced(function* (db: DatabaseService, data: MessageRow["data"]) {
391+
const result = split(data)
392+
if (!result.moved) return data
393+
yield* save(db, result.blobs)
394+
return result.data
395+
})
396+
356397
/** Records that `owner` references each blob its stored row names, so release keeps them until it lets go. */
357398
export const recordRefs = Effect.fnUntraced(function* (
358399
db: DatabaseService,
359400
owner: Owner,
360401
data: MessageRow["data"] | InboxRow["payload"],
361402
) {
362-
const hashes = hashesOf(data)
403+
yield* recordHashes(db, owner, hashesOf(data))
404+
})
405+
406+
/**
407+
* {@link recordRefs} for the given hashes only, such as the blobs an import took out of its own inline data, so
408+
* refs it carried in can't keep or expose blobs it never held.
409+
*/
410+
export const recordHashes = Effect.fnUntraced(function* (
411+
db: DatabaseService,
412+
owner: Owner,
413+
hashes: ReadonlySet<string>,
414+
) {
363415
if (hashes.size === 0) return
364416
// A ref whose blob is already gone keeps nothing alive; reads render it as unavailable.
365417
const found = yield* db

‎packages/core/src/session/projector.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ const projectFork = Effect.fn("SessionProjector.projectFork")(function* (
187187
yield* InstructionEntry.initialize(db, event.data.sessionID, event.data.instructionEntries, event.created)
188188

189189
const forkedID = (seq: number) => SessionMessage.ID.make(`${SessionMessage.ID.fromEvent(event.id)}_${seq}`)
190+
// The child stores the parent's inline media as blobs; the parent's rows are never rewritten.
191+
const media = yield* SessionBlob.ForkMedia
190192
let cursor = -1
191193
while (copiedSeq !== undefined) {
192194
// A parent message whose parts live in session_part is copied with them inline, so the fork reads the same.
@@ -219,7 +221,7 @@ const projectFork = Effect.fn("SessionProjector.projectFork")(function* (
219221
seq: row.seq,
220222
time_created: row.time_created,
221223
time_updated: row.time_updated,
222-
data: row.data,
224+
data: media ? yield* SessionBlob.externalizeRow(db, row.data) : row.data,
223225
}
224226
// A summary references the first message it kept, which the fork copied under a new ID.
225227
const keepID = row.type === "compaction" ? row.data.metadata?.keepID : undefined

‎packages/core/src/session/transfer.ts‎

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ import { makeGlobalNode } from "@opencode/util/effect/app-node"
1111
import { App } from "../app.js"
1212
import { Bus } from "../bus.js"
1313
import { Database } from "../database/database.js"
14+
import { Instance } from "../instance/service.js"
1415
import { Location } from "../location.js"
1516
import { Project } from "../project.js"
1617
import { upsertProject } from "../project/sql.js"
1718
import { AbsolutePath, RelativePath } from "../schema.js"
1819
import { Session } from "../session.js"
1920
import { Slug } from "../util/slug.js"
21+
import { SessionBlob } from "./blob.js"
2022
import { SessionEvent } from "./event.js"
2123
import { SessionMessage } from "./message.js"
2224
import { SessionProjector } from "./projector.js"
@@ -49,6 +51,7 @@ const layer = Layer.effect(
4951
const app = yield* App.Metadata
5052
const bus = yield* Bus.Service
5153
const { db } = yield* Database.Service
54+
const instances = yield* Instance.Service
5255
const projects = yield* Project.Service
5356
const sessions = yield* Session.Service
5457
const encodeMessage = Schema.encodeSync(SessionMessage.Info)
@@ -74,6 +77,9 @@ const layer = Layer.effect(
7477
const project = yield* projects.resolve(input.location.directory)
7578
yield* upsertProject(db, project).pipe(Effect.orDie)
7679
const importedAt = yield* Clock.currentTimeMillis
80+
const subpath = RelativePath.make(
81+
path.relative(project.directory, input.location.directory).replaceAll("\\", "/"),
82+
)
7783
const messages = input.data.messages.filter(isSettled).map((message, index) => {
7884
const encoded = encodeMessage(message)
7985
const { id: _, type, ...data } = encoded
@@ -86,6 +92,17 @@ const layer = Layer.effect(
8692
data,
8793
}
8894
})
95+
// Only inline media the import carries becomes blobs, and only its blobs get refs: refs it carried in
96+
// could otherwise keep or expose blobs it never held. The destination's config is loaded only when needed.
97+
const split = messages.map((message) => SessionBlob.split(message.data))
98+
const media =
99+
split.some((row) => row.moved) &&
100+
(yield* SessionBlob.enabledFor(instances, {
101+
...input.data.info,
102+
projectID: project.id,
103+
location: input.location,
104+
subpath,
105+
}))
89106
yield* bus
90107
.publish(
91108
SessionEvent.Created,
@@ -96,9 +113,7 @@ const layer = Layer.effect(
96113
version: app.version,
97114
projectID: project.id,
98115
location: input.location,
99-
subpath: RelativePath.make(
100-
path.relative(project.directory, input.location.directory).replaceAll("\\", "/"),
101-
),
116+
subpath,
102117
title: input.data.info.title,
103118
agent: input.data.info.agent,
104119
model: input.data.info.model,
@@ -110,7 +125,22 @@ const layer = Layer.effect(
110125
commit: (seq) =>
111126
Effect.gen(function* () {
112127
if (messages.length > 0) {
113-
yield* db.insert(SessionMessageTable).values(messages).run().pipe(Effect.orDie)
128+
if (media) yield* Effect.forEach(split, (row) => SessionBlob.save(db, row.blobs), { discard: true })
129+
const stored = media
130+
? messages.map((message, i) => ({ ...message, data: split[i]!.data }))
131+
: messages
132+
yield* db.insert(SessionMessageTable).values(stored).run().pipe(Effect.orDie)
133+
if (media)
134+
yield* Effect.forEach(
135+
stored,
136+
(message, i) =>
137+
SessionBlob.recordHashes(
138+
db,
139+
{ sessionID, messageID: message.id, seq: message.seq },
140+
new Set(split[i]!.blobs.map((blob) => blob.hash)),
141+
),
142+
{ discard: true },
143+
)
114144
yield* Bus.reserveSequence(db, sessionID, seq + messages.length)
115145
}
116146
yield* db
@@ -159,7 +189,7 @@ const layer = Layer.effect(
159189
export const node = makeGlobalNode({
160190
service: Service,
161191
layer,
162-
deps: [App.node, Bus.node, Database.node, Project.node, Session.node],
192+
deps: [App.node, Bus.node, Database.node, Instance.node, Project.node, Session.node],
163193
})
164194

165195
function isSettled(message: SessionMessage.Info) {

0 commit comments

Comments
 (0)