@@ -2,12 +2,13 @@ export * as SessionBlob from "./blob.js"
22
33import { createHash } from "node:crypto"
44import { 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"
66import { map } from "effect/Array"
77import type { Attachment , FileAttachment } from "@opencode/schema/prompt"
88import type { Tool } from "@opencode/schema/tool"
99import { Config } from "../config.js"
1010import type { Database } from "../database/database.js"
11+ import type { Instance } from "../instance/service.js"
1112import type { SessionMessage } from "./message.js"
1213import type { SessionSchema } from "./schema.js"
1314import { 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+
261284const DATA_URI = / ^ d a t a : [ ^ , ] * ; b a s e 6 4 , ( .* ) $ / s
262285
263286function 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 > }
344374export 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. */
357398export 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
0 commit comments