From 7b5b7a8020040d7ffc9cf59d4b5c6d7f8d33d3e0 Mon Sep 17 00:00:00 2001 From: Dax Date: Sun, 25 Oct 2026 18:38:40 -0400 Subject: [PATCH 01/15] ci: name workflows by kind (check-*, bot-*, triage-*) (#59077) --- .../workflows/{docs-locale-sync.yml => bot-docs-locale.yml} | 2 +- .github/workflows/{nix-hashes.yml => bot-nix-hashes.yml} | 4 ++-- .github/workflows/{nix-eval.yml => check-nix.yml} | 2 +- .github/workflows/{storybook.yml => check-storybook.yml} | 6 +++--- .github/workflows/{test.yml => check-test.yml} | 2 +- .../workflows/{close-issues.yml => triage-close-issues.yml} | 2 +- .github/workflows/{close-prs.yml => triage-close-prs.yml} | 2 +- .../{compliance-close.yml => triage-compliance-close.yml} | 2 +- .../workflows/{pr-management.yml => triage-contributor.yml} | 2 +- .../workflows/{pr-standards.yml => triage-pr-standards.yml} | 2 +- 10 files changed, 13 insertions(+), 13 deletions(-) rename .github/workflows/{docs-locale-sync.yml => bot-docs-locale.yml} (99%) rename .github/workflows/{nix-hashes.yml => bot-nix-hashes.yml} (98%) rename .github/workflows/{nix-eval.yml => check-nix.yml} (99%) rename .github/workflows/{storybook.yml => check-storybook.yml} (88%) rename .github/workflows/{test.yml => check-test.yml} (99%) rename .github/workflows/{close-issues.yml => triage-close-issues.yml} (95%) rename .github/workflows/{close-prs.yml => triage-close-prs.yml} (98%) rename .github/workflows/{compliance-close.yml => triage-compliance-close.yml} (99%) rename .github/workflows/{pr-management.yml => triage-contributor.yml} (96%) rename .github/workflows/{pr-standards.yml => triage-pr-standards.yml} (99%) diff --git a/.github/workflows/docs-locale-sync.yml b/.github/workflows/bot-docs-locale.yml similarity index 99% rename from .github/workflows/docs-locale-sync.yml rename to .github/workflows/bot-docs-locale.yml index 5f921e8bb71..82e81de204b 100644 --- a/.github/workflows/docs-locale-sync.yml +++ b/.github/workflows/bot-docs-locale.yml @@ -1,4 +1,4 @@ -name: docs-locale-sync +name: bot-docs-locale on: push: diff --git a/.github/workflows/nix-hashes.yml b/.github/workflows/bot-nix-hashes.yml similarity index 98% rename from .github/workflows/nix-hashes.yml rename to .github/workflows/bot-nix-hashes.yml index 63a58ec22cb..e63309e4674 100644 --- a/.github/workflows/nix-hashes.yml +++ b/.github/workflows/bot-nix-hashes.yml @@ -1,4 +1,4 @@ -name: nix-hashes +name: bot-nix-hashes permissions: contents: write @@ -16,7 +16,7 @@ on: - "nix/node_modules.nix" - "nix/scripts/**" - "patches/**" - - ".github/workflows/nix-hashes.yml" + - ".github/workflows/bot-nix-hashes.yml" concurrency: group: ${{ github.workflow }}-${{ github.ref }} diff --git a/.github/workflows/nix-eval.yml b/.github/workflows/check-nix.yml similarity index 99% rename from .github/workflows/nix-eval.yml rename to .github/workflows/check-nix.yml index a015cd15278..b52c03220e6 100644 --- a/.github/workflows/nix-eval.yml +++ b/.github/workflows/check-nix.yml @@ -1,4 +1,4 @@ -name: nix-eval +name: check-nix on: push: diff --git a/.github/workflows/storybook.yml b/.github/workflows/check-storybook.yml similarity index 88% rename from .github/workflows/storybook.yml rename to .github/workflows/check-storybook.yml index 9412291fc7a..1fd1dd8674f 100644 --- a/.github/workflows/storybook.yml +++ b/.github/workflows/check-storybook.yml @@ -1,10 +1,10 @@ -name: storybook +name: check-storybook on: push: branches: [dev] paths: - - ".github/workflows/storybook.yml" + - ".github/workflows/check-storybook.yml" - "package.json" - "bun.lock" - "packages/storybook/**" @@ -14,7 +14,7 @@ on: branches: [dev] types: [opened, synchronize, reopened, ready_for_review] paths: - - ".github/workflows/storybook.yml" + - ".github/workflows/check-storybook.yml" - "package.json" - "bun.lock" - "packages/storybook/**" diff --git a/.github/workflows/test.yml b/.github/workflows/check-test.yml similarity index 99% rename from .github/workflows/test.yml rename to .github/workflows/check-test.yml index 3af291eaecf..3b34ae0bdab 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/check-test.yml @@ -1,4 +1,4 @@ -name: test +name: check-test on: push: diff --git a/.github/workflows/close-issues.yml b/.github/workflows/triage-close-issues.yml similarity index 95% rename from .github/workflows/close-issues.yml rename to .github/workflows/triage-close-issues.yml index b8a2e3f575d..c2898ba8e06 100644 --- a/.github/workflows/close-issues.yml +++ b/.github/workflows/triage-close-issues.yml @@ -1,4 +1,4 @@ -name: close-issues +name: triage-close-issues on: schedule: diff --git a/.github/workflows/close-prs.yml b/.github/workflows/triage-close-prs.yml similarity index 98% rename from .github/workflows/close-prs.yml rename to .github/workflows/triage-close-prs.yml index a1e603a8810..3c4dad94c00 100644 --- a/.github/workflows/close-prs.yml +++ b/.github/workflows/triage-close-prs.yml @@ -1,4 +1,4 @@ -name: close-prs +name: triage-close-prs on: schedule: diff --git a/.github/workflows/compliance-close.yml b/.github/workflows/triage-compliance-close.yml similarity index 99% rename from .github/workflows/compliance-close.yml rename to .github/workflows/triage-compliance-close.yml index cc09925c5b7..5abac832ddf 100644 --- a/.github/workflows/compliance-close.yml +++ b/.github/workflows/triage-compliance-close.yml @@ -1,4 +1,4 @@ -name: compliance-close +name: triage-compliance-close on: schedule: diff --git a/.github/workflows/pr-management.yml b/.github/workflows/triage-contributor.yml similarity index 96% rename from .github/workflows/pr-management.yml rename to .github/workflows/triage-contributor.yml index 01d8bca1c24..e4f6742150a 100644 --- a/.github/workflows/pr-management.yml +++ b/.github/workflows/triage-contributor.yml @@ -1,4 +1,4 @@ -name: pr-management +name: triage-contributor on: pull_request_target: diff --git a/.github/workflows/pr-standards.yml b/.github/workflows/triage-pr-standards.yml similarity index 99% rename from .github/workflows/pr-standards.yml rename to .github/workflows/triage-pr-standards.yml index 4f9a5932043..e91a88692d2 100644 --- a/.github/workflows/pr-standards.yml +++ b/.github/workflows/triage-pr-standards.yml @@ -1,4 +1,4 @@ -name: pr-standards +name: triage-pr-standards on: pull_request_target: From e12fad949b716fb529d6610d864b7603849f43a8 Mon Sep 17 00:00:00 2001 From: Kit Langton Date: Sun, 25 Oct 2026 15:48:45 -0700 Subject: [PATCH 02/15] feat(core): opt-in blob writes for session media behind experimental.media_blob (#58741) --- .../client/src/promise/generated/types.ts | 1 + packages/core/src/config/normalize.ts | 9 + packages/core/src/session/blob.ts | 179 +++++++++++- packages/core/src/session/command.ts | 29 +- packages/core/src/session/inbox.ts | 25 +- packages/core/src/session/projector.ts | 59 ++-- packages/core/src/session/prompt.ts | 103 ++++--- packages/core/src/session/session.ts | 25 +- packages/core/src/session/stored.ts | 5 +- .../core/test/config/normalization.test.ts | 2 + packages/core/test/session-owned.test.ts | 2 +- packages/core/test/session-projector.test.ts | 256 ++++++++++++++++++ packages/core/test/session-prompt.test.ts | 63 ++++- packages/protocol/openapi.json | 3 + packages/schema/src/config/experimental.ts | 4 + 15 files changed, 652 insertions(+), 113 deletions(-) diff --git a/packages/client/src/promise/generated/types.ts b/packages/client/src/promise/generated/types.ts index 8aba08b6696..4279704ae2e 100644 --- a/packages/client/src/promise/generated/types.ts +++ b/packages/client/src/promise/generated/types.ts @@ -2365,6 +2365,7 @@ export type ConfigEntry = portable_shell_scanner?: boolean todo?: boolean background_compaction?: boolean + media_blob?: boolean subagent_depth?: number location_idle_timeout?: string | false policies?: Array<{ diff --git a/packages/core/src/config/normalize.ts b/packages/core/src/config/normalize.ts index e2b4ad4e00b..fac36c1f3f0 100644 --- a/packages/core/src/config/normalize.ts +++ b/packages/core/src/config/normalize.ts @@ -485,6 +485,15 @@ function normalizeExperimental( ) if (value !== undefined) result.background_compaction = value } + if (own(experimental, "media_blob")) { + const value = decodeEncoded( + ConfigExperimental.Info.fields.media_blob, + experimental.media_blob, + ["experimental", "media_blob"], + diagnostics, + ) + if (value !== undefined) result.media_blob = value + } if (own(experimental, "subagent_depth")) { const value = decodeEncoded( ConfigExperimental.Info.fields.subagent_depth, diff --git a/packages/core/src/session/blob.ts b/packages/core/src/session/blob.ts index 72b4bcc61da..9208b799d5f 100644 --- a/packages/core/src/session/blob.ts +++ b/packages/core/src/session/blob.ts @@ -1,12 +1,16 @@ export * as SessionBlob from "./blob.js" -import { inArray } from "drizzle-orm" -import { Effect, Schema } from "effect" +import { createHash } from "node:crypto" +import { and, eq, gte, inArray, notExists, notInArray, type SQL, sql } from "drizzle-orm" +import { Effect, Option, Schema } from "effect" import { map } from "effect/Array" import type { FileAttachment } from "@opencode/schema/prompt" import type { Tool } from "@opencode/schema/tool" +import { Config } from "../config.js" import type { Database } from "../database/database.js" -import { BlobTable, SessionInboxTable, SessionMessageTable } from "./sql.js" +import type { SessionMessage } from "./message.js" +import type { SessionSchema } from "./schema.js" +import { BlobRefTable, BlobTable, SessionInboxTable, SessionMessageTable } from "./sql.js" import { SessionStored } from "./stored.js" type DatabaseService = Database.Interface["db"] @@ -105,3 +109,172 @@ export const hydrateInbox = Effect.fnUntraced(function* (db: DatabaseService, ro return { ...row, payload: { ...payload, files: payload.files.map((file) => inlineFile(file, blobs)) } } }) }) + +/** A blob's hash is the lowercase hex SHA-256 of its raw bytes. */ +export const hashBytes = (raw: Uint8Array) => createHash("sha256").update(raw).digest("hex") + +/** The row a ref keeps its blob alive for: a message at its `seq`, or a pending inbox row at its `enqueued_seq`. */ +export interface Owner { + readonly sessionID: SessionSchema.ID + readonly messageID: SessionMessage.ID + readonly seq: number +} + +/** Blobs taken out of one row, by hash. */ +type Pending = Map +type AssistantPart = SessionMessage.Assistant["content"][number] + +/** Reads `experimental.media_blob` from the Config in scope on every write; without one, media stays inline. */ +const writeEnabled = Effect.gen(function* () { + const config = yield* Effect.serviceOption(Config.Service) + if (Option.isNone(config)) return false + return Config.latest(yield* config.value.entries(), "experimental")?.media_blob === true +}) + +const DATA_URI = /^data:[^,]*;base64,(.*)$/s + +function take(pending: Pending, raw: Buffer, mime: string) { + const hash = hashBytes(raw) + if (!pending.has(hash)) pending.set(hash, { hash, mime, bytes: raw.byteLength, data: raw }) + return SessionStored.ref(hash) +} + +function fileRef(pending: Pending, file: SessionStored.File): SessionStored.File { + if (isFileRef(file)) return file + const { data, ...rest } = file + return { ...rest, uri: take(pending, Buffer.from(data, "base64"), file.mime) } +} + +function toolRef(pending: Pending, item: Tool.Content): Tool.Content { + if (item.type !== "file") return item + const data = DATA_URI.exec(item.uri)?.[1] + return data === undefined ? item : { ...item, uri: take(pending, Buffer.from(data, "base64"), item.mime) } +} + +function toolPartRefs(pending: Pending, part: AssistantPart): AssistantPart { + if (part.type !== "tool" || (part.state.status !== "completed" && part.state.status !== "error")) return part + const content = part.state.content + if (!content) return part + return { ...part, state: { ...part.state, content: map(content, (item) => toolRef(pending, item)) } } +} + +const save = Effect.fnUntraced(function* (db: DatabaseService, pending: Pending) { + if (pending.size === 0) return + yield* db + .insert(BlobTable) + .values([...pending.values()]) + .onConflictDoNothing() + .run() + .pipe(Effect.orDie) +}) + +/** {@link externalize} for a user message or pending inbox user payload. */ +export const externalizePayload = Effect.fnUntraced(function* < + T extends { readonly files?: ReadonlyArray }, +>(db: DatabaseService, input: T): Effect.fn.Return { + if (input.files === undefined || !(yield* writeEnabled)) return input + const pending: Pending = new Map() + const files = input.files.map((file) => fileRef(pending, file)) + if (pending.size === 0) return input + yield* save(db, pending) + return { ...input, files } +}) + +/** + * With `experimental.media_blob` on, moves inline user files and tool `data:` files into `blob` and returns the + * message to store with refs in their place. Existing refs pass through. + */ +export const externalize = Effect.fnUntraced(function* ( + db: DatabaseService, + message: SessionMessage.Info | SessionStored.User, +) { + if (message.type === "user") return yield* externalizePayload(db, message) + if (message.type !== "assistant" || !(yield* writeEnabled)) return message + const pending: Pending = new Map() + const content = message.content.map((part) => toolPartRefs(pending, part)) + if (pending.size === 0) return message + yield* save(db, pending) + return { ...message, content } +}) + +const hashesOf = (data: MessageRow["data"] | InboxRow["payload"]) => { + const hashes = new Set() + collect(data, hashes) + return hashes +} + +/** Records that `owner` references each blob its stored row names, so release keeps them until it lets go. */ +export const recordRefs = Effect.fnUntraced(function* ( + db: DatabaseService, + owner: Owner, + data: MessageRow["data"] | InboxRow["payload"], +) { + const hashes = hashesOf(data) + if (hashes.size === 0) return + // A ref whose blob is already gone keeps nothing alive; reads render it as unavailable. + const found = yield* db + .select({ hash: BlobTable.hash }) + .from(BlobTable) + .where(inArray(BlobTable.hash, [...hashes])) + .all() + .pipe(Effect.orDie) + if (found.length === 0) return + yield* db + .insert(BlobRefTable) + .values( + found.map(({ hash }) => ({ session_id: owner.sessionID, message_id: owner.messageID, hash, seq: owner.seq })), + ) + .onConflictDoUpdate({ + target: [BlobRefTable.session_id, BlobRefTable.message_id, BlobRefTable.hash], + set: { seq: owner.seq }, + }) + .run() + .pipe(Effect.orDie) +}) + +/** Records the refs a rewritten message names and releases the ones it no longer does. */ +export const syncRefs = Effect.fnUntraced(function* (db: DatabaseService, owner: Owner, data: MessageRow["data"]) { + yield* recordRefs(db, owner, data) + const present = [...hashesOf(data)] + const own = [eq(BlobRefTable.session_id, owner.sessionID), eq(BlobRefTable.message_id, owner.messageID)] as const + yield* present.length === 0 ? release(db, ...own) : release(db, ...own, notInArray(BlobRefTable.hash, present)) +}) + +/** Deletes the matching refs, then every blob among theirs that no ref names anymore. */ +const release = Effect.fnUntraced(function* (db: DatabaseService, ...where: readonly [SQL, ...SQL[]]) { + const released = yield* db + .delete(BlobRefTable) + .where(and(...where)) + .returning({ hash: BlobRefTable.hash }) + .all() + .pipe(Effect.orDie) + const hashes = [...new Set(released.map((ref) => ref.hash))] + for (let offset = 0; offset < hashes.length; offset += 500) + yield* db + .delete(BlobTable) + .where( + and( + inArray(BlobTable.hash, hashes.slice(offset, offset + 500)), + notExists( + db + .select({ one: sql`1` }) + .from(BlobRefTable) + .where(eq(BlobRefTable.hash, BlobTable.hash)), + ), + ), + ) + .run() + .pipe(Effect.orDie) +}) + +/** Releases the blobs a removed message or cancelled inbox row referenced. */ +export const releaseMessage = (db: DatabaseService, sessionID: SessionSchema.ID, messageID: SessionMessage.ID) => + release(db, eq(BlobRefTable.session_id, sessionID), eq(BlobRefTable.message_id, messageID)) + +/** Releases the blobs referenced at or after `seq`, for a revert. */ +export const releaseFrom = (db: DatabaseService, sessionID: SessionSchema.ID, seq: number) => + release(db, eq(BlobRefTable.session_id, sessionID), gte(BlobRefTable.seq, seq)) + +/** Releases every blob a deleted session referenced. */ +export const releaseSession = (db: DatabaseService, sessionID: SessionSchema.ID) => + release(db, eq(BlobRefTable.session_id, sessionID)) diff --git a/packages/core/src/session/command.ts b/packages/core/src/session/command.ts index 302f12e40c4..bf79c2392b3 100644 --- a/packages/core/src/session/command.ts +++ b/packages/core/src/session/command.ts @@ -18,18 +18,21 @@ export const execute = Effect.fn("SessionCommand.execute")(function* (input: { delivery?: SessionInbox.Delivery }) { const instances = yield* Instance.Service - const commands = yield* Plugin.awaitActivation.pipe(Effect.andThen(Command.Service), instances.provide(input.session)) - yield* commands.execute({ - name: input.command, - invocation: { - sessionID: input.session.id, - prompt: { - text: input.text, - files: input.files, - agents: input.agents, - skills: input.skills, + yield* Effect.gen(function* () { + yield* Plugin.awaitActivation + const commands = yield* Command.Service + yield* commands.execute({ + name: input.command, + invocation: { + sessionID: input.session.id, + prompt: { + text: input.text, + files: input.files, + agents: input.agents, + skills: input.skills, + }, + delivery: input.delivery ?? "steer", }, - delivery: input.delivery ?? "steer", - }, - }) + }) + }).pipe(instances.provide(input.session)) }) diff --git a/packages/core/src/session/inbox.ts b/packages/core/src/session/inbox.ts index 36eb12d34af..9be6743c35e 100644 --- a/packages/core/src/session/inbox.ts +++ b/packages/core/src/session/inbox.ts @@ -52,7 +52,7 @@ export type Promotable = "input" | "steer" const decodeUser = Schema.decodeUnknownSync(UserPayload) const decodeStoredUser = Schema.decodeUnknownSync(SessionStored.UserPayload) -const encodeUser = Schema.encodeSync(UserPayload) +const encodeStoredUser = Schema.encodeSync(SessionStored.UserPayload) const decodeSynthetic = Schema.decodeUnknownSync(SyntheticPayload) const encodeSynthetic = Schema.encodeSync(SyntheticPayload) const decodeCompaction = Schema.decodeUnknownSync(CompactionPayload) @@ -306,20 +306,21 @@ export const projectAdmitted = Effect.fn("SessionInbox.projectAdmitted")(functio .get() .pipe(Effect.orDie) if (message !== undefined) return yield* Effect.die(new LifecycleConflict({ id: request.id })) + const payload = + request.item.type === "user" + ? encodeStoredUser(yield* SessionBlob.externalizePayload(db, request.item.payload)) + : request.item.type === "synthetic" + ? encodeSynthetic(request.item.payload) + : request.item.type === "compaction" + ? encodeCompaction(request.item.payload) + : encodeMove(request.item.payload) const stored = yield* db .insert(SessionInboxTable) .values({ id: request.id, session_id: request.sessionID, type: request.item.type, - payload: - request.item.type === "user" - ? encodeUser(request.item.payload) - : request.item.type === "synthetic" - ? encodeSynthetic(request.item.payload) - : request.item.type === "compaction" - ? encodeCompaction(request.item.payload) - : encodeMove(request.item.payload), + payload, delivery: request.item.delivery, enqueued_seq: request.enqueuedSeq, time_created: request.timeCreated, @@ -329,6 +330,11 @@ export const projectAdmitted = Effect.fn("SessionInbox.projectAdmitted")(functio .get() .pipe(Effect.orDie) if (!stored) return yield* Effect.die(new LifecycleConflict({ id: request.id })) + yield* SessionBlob.recordRefs( + db, + { sessionID: request.sessionID, messageID: request.id, seq: request.enqueuedSeq }, + payload, + ) }) /** @@ -379,6 +385,7 @@ export const projectCancelled = Effect.fn("SessionInbox.projectCancelled")(funct .get() .pipe(Effect.orDie) if (!deleted) return yield* Effect.die(new LifecycleConflict({ id: input.id })) + yield* SessionBlob.releaseMessage(db, input.sessionID, input.id) }) const projectDelivery = Effect.fn("SessionInbox.projectDelivery")(function* ( diff --git a/packages/core/src/session/projector.ts b/packages/core/src/session/projector.ts index e5dba0813ac..7cce5b808a9 100644 --- a/packages/core/src/session/projector.ts +++ b/packages/core/src/session/projector.ts @@ -12,6 +12,7 @@ import { SessionMessage } from "./message.js" import { SessionMessageUpdater } from "./message-updater.js" import { SessionInbox } from "./inbox.js" import { InstructionState } from "./instruction-state.js" +import { SessionBlob } from "./blob.js" import { SessionInboxTable, SessionMessageTable, SessionTable } from "./sql.js" import { SessionStored } from "./stored.js" import { InstructionEntry } from "./instruction-entry.js" @@ -236,6 +237,16 @@ const projectFork = Effect.fn("SessionProjector.projectFork")(function* ( }), ) yield* db.insert(SessionMessageTable).values(values).run().pipe(Effect.orDie) + yield* Effect.forEach( + values, + (value) => + SessionBlob.recordRefs( + db, + { sessionID: event.data.sessionID, messageID: value.id, seq: value.seq }, + value.data, + ), + { discard: true }, + ) cursor = rows.at(-1)!.seq } @@ -246,21 +257,23 @@ const projectFork = Effect.fn("SessionProjector.projectFork")(function* ( function run(db: DatabaseService, event: MessageEvent) { return Effect.gen(function* () { - const updateMessage = (message: SessionMessage.Info) => { - const encoded = encodeMessage(message) - const { id: _, type, ...data } = encoded - return db + const updateMessage = Effect.fnUntraced(function* (message: SessionMessage.Info) { + const stored = yield* SessionBlob.externalize(db, message) + const { id: _, type, ...data } = encodeStored(stored) + const updated = yield* db .update(SessionMessageTable) .set({ type, time_created: DateTime.toEpochMillis(message.time.created), data }) - .where( - and( - eq(SessionMessageTable.id, message.id), - eq(SessionMessageTable.session_id, event.data.sessionID), - ), - ) - .run() + .where(and(eq(SessionMessageTable.id, message.id), eq(SessionMessageTable.session_id, event.data.sessionID))) + .returning({ seq: SessionMessageTable.seq }) + .get() .pipe(Effect.orDie) - } + if (!updated) return + const owner = { sessionID: event.data.sessionID, messageID: message.id, seq: updated.seq } + // Most updates only carry existing refs forward; a content rewrite may also drop some. `externalize` returns + // the same message when it extracted nothing, so streaming updates skip the ref tables entirely. + if (event.type === SessionEvent.MessageContentUpdated.type) yield* SessionBlob.syncRefs(db, owner, data) + else if (stored !== message) yield* SessionBlob.recordRefs(db, owner, data) + }) const appendMessage = (message: SessionMessage.Info) => insertMessage(db, event, message) const adapter: SessionMessageUpdater.Adapter = { getAgent() { @@ -400,13 +413,16 @@ function run(db: DatabaseService, event: MessageEvent) { }) } -function insertMessage( +const encodeStored = (message: SessionMessage.Info | SessionStored.User) => + message.type === "user" ? encodeStoredUser(message) : encodeMessage(message) + +const insertMessage = Effect.fnUntraced(function* ( db: DatabaseService, event: SessionEvent.DurableEvent, message: SessionMessage.Info | SessionStored.User, ) { - const { id: _, type, ...data } = message.type === "user" ? encodeStoredUser(message) : encodeMessage(message) - return db + const { id: _, type, ...data } = encodeStored(yield* SessionBlob.externalize(db, message)) + yield* db .insert(SessionMessageTable) .values({ id: message.id, @@ -418,7 +434,12 @@ function insertMessage( }) .run() .pipe(Effect.orDie) -} + yield* SessionBlob.recordRefs( + db, + { sessionID: event.data.sessionID, messageID: message.id, seq: event.durable.seq }, + data, + ) +}) function projectIdle( db: DatabaseService, @@ -561,7 +582,10 @@ const layer = Layer.effectDiscard( }), ) yield* bus.project(SessionEvent.Deleted, (event) => - db.delete(SessionTable).where(eq(SessionTable.id, event.data.sessionID)).run().pipe(Effect.orDie), + Effect.gen(function* () { + yield* SessionBlob.releaseSession(db, event.data.sessionID) + yield* db.delete(SessionTable).where(eq(SessionTable.id, event.data.sessionID)).run().pipe(Effect.orDie) + }), ) yield* bus.project(SessionEvent.AgentSelected, (event) => Effect.gen(function* () { @@ -815,6 +839,7 @@ const layer = Layer.effectDiscard( .run() .pipe(Effect.orDie) yield* InstructionState.rewind(db, event.data.sessionID, boundary.seq) + yield* SessionBlob.releaseFrom(db, event.data.sessionID, boundary.seq) }), ) yield* bus.subscribe([SessionEvent.Step.Ended, SessionEvent.Step.Failed, SessionEvent.UsageRecorded]).pipe( diff --git a/packages/core/src/session/prompt.ts b/packages/core/src/session/prompt.ts index d721a7454ed..20246af780d 100644 --- a/packages/core/src/session/prompt.ts +++ b/packages/core/src/session/prompt.ts @@ -10,7 +10,6 @@ import { Effect } from "effect" import path from "path" import { fileURLToPath } from "url" import { Image } from "../image.js" -import { Instance } from "../instance/service.js" import { Mime } from "../mime.js" import { Plugin } from "../plugin/service.js" import { PluginHooks } from "../plugin/hooks.js" @@ -32,60 +31,56 @@ export const prepare = Effect.fn("SessionPrompt.prepare")(function* (request: { input: Input }) { const fs = yield* FSUtil.Service - const instances = yield* Instance.Service - - return yield* Effect.gen(function* () { - yield* Plugin.awaitActivation - const hooks = yield* PluginHooks.Service - const event = yield* hooks.trigger("session", "prompt", { - sessionID: request.session.id, - messageID: request.messageID, - prompt: structuredClone({ - text: request.input.text, - files: request.input.files?.slice(), - agents: request.input.agents?.slice(), - skills: request.input.skills?.slice(), + yield* Plugin.awaitActivation + const hooks = yield* PluginHooks.Service + const event = yield* hooks.trigger("session", "prompt", { + sessionID: request.session.id, + messageID: request.messageID, + prompt: structuredClone({ + text: request.input.text, + files: request.input.files?.slice(), + agents: request.input.agents?.slice(), + skills: request.input.skills?.slice(), + }), + metadata: structuredClone(request.input.metadata), + delivery: request.input.delivery ?? "steer", + }) + const input = event.prompt + const files = input.files + ? yield* Effect.forEach(input.files, materializeAttachment, { concurrency: 8 }) + : undefined + const requested = input.skills + const selected = yield* Effect.gen(function* () { + if (!requested?.length) return undefined + const skillService = yield* Skill.Service + const prepared = new Map() + return yield* Effect.forEach(requested, (attachment) => + Effect.gen(function* () { + const name = prepared.get(attachment.id) + if (name !== undefined) return { id: attachment.id, name, mention: attachment.mention } + const skill = yield* skillService.get(attachment.id) + if (!skill) return yield* new SkillNotFoundError({ skill: attachment.id }) + prepared.set(skill.id, skill.name) + return { + id: skill.id, + name: skill.name, + text: (yield* Skill.prepare(fs, skill).pipe(Effect.orDie)).output, + mention: attachment.mention, + } }), - metadata: structuredClone(request.input.metadata), - delivery: request.input.delivery ?? "steer", - }) - const input = event.prompt - const files = input.files - ? yield* Effect.forEach(input.files, materializeAttachment, { concurrency: 8 }) - : undefined - const requested = input.skills - const selected = yield* Effect.gen(function* () { - if (!requested?.length) return undefined - const skillService = yield* Skill.Service - const prepared = new Map() - return yield* Effect.forEach(requested, (attachment) => - Effect.gen(function* () { - const name = prepared.get(attachment.id) - if (name !== undefined) return { id: attachment.id, name, mention: attachment.mention } - const skill = yield* skillService.get(attachment.id) - if (!skill) return yield* new SkillNotFoundError({ skill: attachment.id }) - prepared.set(skill.id, skill.name) - return { - id: skill.id, - name: skill.name, - text: (yield* Skill.prepare(fs, skill).pipe(Effect.orDie)).output, - mention: attachment.mention, - } - }), - ) - }) - return { - type: "user", - payload: SessionInbox.UserPayload.make({ - text: input.text, - agents: input.agents, - files, - skills: selected?.length ? selected : undefined, - metadata: event.metadata, - }), - delivery: event.delivery, - } satisfies SessionInbox.Item - }).pipe(instances.provide(request.session)) + ) + }) + return { + type: "user", + payload: SessionInbox.UserPayload.make({ + text: input.text, + agents: input.agents, + files, + skills: selected?.length ? selected : undefined, + metadata: event.metadata, + }), + delivery: event.delivery, + } satisfies SessionInbox.Item }) const materializeAttachment = Effect.fn("SessionPrompt.materializeAttachment")(function* ( diff --git a/packages/core/src/session/session.ts b/packages/core/src/session/session.ts index 06c2b241e6d..d7153134fae 100644 --- a/packages/core/src/session/session.ts +++ b/packages/core/src/session/session.ts @@ -165,19 +165,18 @@ export const make = Effect.fn("Session.make")(function* () { delivery: input.delivery ?? "steer", }) if (existing) return existing - const item = yield* restore( - SessionPrompt.prepare({ session, messageID, input }).pipe( - Effect.provideService(Instance.Service, instances), - Effect.provideService(FSUtil.Service, fs), - ), - ) - // Commit a staged revert only after preparation succeeds, before admitting new work. - if (session.revert) yield* SessionRevert.commit(bus, session) - return yield* admission.admit({ - id: messageID, - sessionID: session.id, - item, - }) + // Preparation, the staged revert commit and admission share one acquisition of the session's location + // services, so projections see its config whoever called prompt. + return yield* Effect.gen(function* () { + const item = yield* restore(SessionPrompt.prepare({ session, messageID, input })) + // Commit a staged revert only after preparation succeeds, before admitting new work. + if (session.revert) yield* SessionRevert.commit(bus, session) + return yield* admission.admit({ + id: messageID, + sessionID: session.id, + item, + }) + }).pipe(instances.provide(session), Effect.provideService(FSUtil.Service, fs)) }).pipe( Effect.catchTag("SessionInbox.LifecycleConflict", () => new PromptConflictError({ sessionID, messageID })), ) diff --git a/packages/core/src/session/stored.ts b/packages/core/src/session/stored.ts index 717a28e2a7a..f040801d6dc 100644 --- a/packages/core/src/session/stored.ts +++ b/packages/core/src/session/stored.ts @@ -9,7 +9,7 @@ import { SessionMessage } from "./message.js" /** * Stored rows may reference a `blob` row as `media:` and the lowercase hex SHA-256 of its raw bytes. The blob - * row holds the bytes, size and dimensions; the ref names no server route. + * row holds the bytes and size; the ref names no server route. */ export const Ref = Schema.String.check(Schema.isPattern(/^media:[0-9a-f]{64}$/)) export type Ref = typeof Ref.Type @@ -17,6 +17,9 @@ export type Ref = typeof Ref.Type /** The `blob` hash a {@link Ref} names. */ export const refHash = (ref: Ref) => ref.slice("media:".length) +/** The {@link Ref} naming a `blob` hash. */ +export const ref = (hash: string): Ref => `media:${hash}` + const { data: _, ...attachment } = FileAttachment.fields /** A user file whose bytes live in `blob`. */ diff --git a/packages/core/test/config/normalization.test.ts b/packages/core/test/config/normalization.test.ts index 41a86e261aa..d61e70065f9 100644 --- a/packages/core/test/config/normalization.test.ts +++ b/packages/core/test/config/normalization.test.ts @@ -526,6 +526,7 @@ describe("ConfigNormalize", () => { portable_shell_scanner: true, todo: true, background_compaction: true, + media_blob: true, subagent_depth: 0, location_idle_timeout: "2 hours", policies: [{ action: "provider.use", resource: "custom", effect: "allow" }], @@ -536,6 +537,7 @@ describe("ConfigNormalize", () => { portable_shell_scanner: true, todo: true, background_compaction: true, + media_blob: true, subagent_depth: 0, location_idle_timeout: "7200000 millis", }, diff --git a/packages/core/test/session-owned.test.ts b/packages/core/test/session-owned.test.ts index 327071b5677..e1322cfb6f4 100644 --- a/packages/core/test/session-owned.test.ts +++ b/packages/core/test/session-owned.test.ts @@ -751,7 +751,7 @@ describe("SessionPrompt preparation", () => { input, } const items = yield* Effect.forEach([0, 1], () => SessionPrompt.prepare(request)).pipe( - Effect.provideService(Instance.Service, fixture.instances), + fixture.instances.provide(request.session), Effect.satisfiesServicesType(), ) diff --git a/packages/core/test/session-projector.test.ts b/packages/core/test/session-projector.test.ts index dd17856b790..df8780fd18c 100644 --- a/packages/core/test/session-projector.test.ts +++ b/packages/core/test/session-projector.test.ts @@ -24,10 +24,15 @@ import { fromRow } from "@opencode/core/session/info" import { SessionInbox } from "@opencode/core/session/inbox" import { SessionStore } from "@opencode/core/session/store" import { SessionStored } from "@opencode/core/session/stored" +import { SessionBlob } from "@opencode/core/session/blob" +import { Config } from "@opencode/core/config" +import { Document, Info } from "@opencode/schema/config" +import { ConfigExperimental } from "@opencode/schema/config/experimental" import { SessionHistory } from "@opencode/core/session/history" import { Shell } from "@opencode/schema/shell" import { InstructionStateTable, + BlobRefTable, BlobTable, SessionInboxTable, SessionMessageTable, @@ -48,6 +53,22 @@ const created = DateTime.makeUnsafe(0) const model = { id: Model.ID.make("model"), providerID: Provider.ID.make("provider") } const previousModel = { ...model, variant: Model.VariantID.make("medium") } const encodeMessage = Schema.encodeSync(SessionMessage.Info) +const mediaBlob = Config.testLayer([ + new Document({ + type: "document", + info: new Info({ experimental: new ConfigExperimental.Info({ media_blob: true }) }), + }), +]) +const png = Buffer.from( + "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=", + "base64", +) +const attachment = (name: string, bytes = png, mime = "image/png") => ({ + data: bytes.toString("base64"), + mime, + source: { type: "inline" as const }, + name, +}) const build = Agent.defaultID const assistantRow = ( @@ -1303,4 +1324,239 @@ describe("SessionProjector", () => { ]) }), ) + + it.effect("keeps media inline unless experimental.media_blob is on", () => + Effect.gen(function* () { + const db = yield* seedSession() + const inbox = yield* SessionInbox.Service + const id = SessionMessage.ID.make("msg_media_inline") + yield* inbox.admit({ + id, + sessionID, + item: { type: "user", payload: { text: "inline", files: [attachment("shot.png")] }, delivery: "queue" }, + }) + + const row = yield* db + .select() + .from(SessionInboxTable) + .where(eq(SessionInboxTable.id, id)) + .get() + .pipe(Effect.orDie) + expect(row?.payload).toEqual({ text: "inline", files: [attachment("shot.png")] }) + expect(yield* db.select().from(BlobTable).all().pipe(Effect.orDie)).toEqual([]) + expect(yield* db.select().from(BlobRefTable).all().pipe(Effect.orDie)).toEqual([]) + }), + ) + + it.effect("stores media once and refs it from inbox, message, and tool rows when experimental.media_blob is on", () => + Effect.gen(function* () { + const db = yield* seedSession() + const bus = yield* Bus.Service + const inbox = yield* SessionInbox.Service + const store = yield* SessionStore.Service + const hash = SessionBlob.hashBytes(png) + const uri = SessionStored.ref(hash) + const id = SessionMessage.ID.make("msg_media_user") + const files = [attachment("shot.png"), attachment("copy.png")] + const refs = [ + { mime: "image/png", source: { type: "inline" }, name: "shot.png", uri }, + { mime: "image/png", source: { type: "inline" }, name: "copy.png", uri }, + ] + + yield* inbox.admit({ id, sessionID, item: { type: "user", payload: { text: "look", files }, delivery: "steer" } }) + const queued = yield* db + .select() + .from(SessionInboxTable) + .where(eq(SessionInboxTable.id, id)) + .get() + .pipe(Effect.orDie) + expect(queued?.payload).toEqual({ text: "look", files: refs }) + expect(yield* inbox.list(sessionID)).toMatchObject([{ id, payload: { text: "look", files } }]) + + const delivered = yield* bus.publish(SessionEvent.InboxDelivered, { sessionID, inboxID: id }) + const message = yield* db + .select() + .from(SessionMessageTable) + .where(eq(SessionMessageTable.id, id)) + .get() + .pipe(Effect.orDie) + expect(message?.data).toMatchObject({ text: "look", files: refs }) + expect((yield* store.message(id))?.message).toMatchObject({ type: "user", files }) + + const assistantMessageID = SessionMessage.ID.make("msg_media_tool") + yield* db.insert(SessionMessageTable).values(assistantRow(assistantMessageID, 0)).run().pipe(Effect.orDie) + yield* bus.publish(SessionEvent.Tool.Input.Started, { sessionID, assistantMessageID, id: "shot", name: "shot" }) + yield* bus.publish(SessionEvent.Tool.Called, { + sessionID, + assistantMessageID, + id: "shot", + input: {}, + executed: true, + }) + yield* bus.publish(SessionEvent.Tool.Success, { + sessionID, + assistantMessageID, + id: "shot", + content: [{ type: "file", uri: `data:image/png;base64,${png.toString("base64")}`, mime: "image/png" }], + executed: true, + }) + const tool = yield* db + .select() + .from(SessionMessageTable) + .where(eq(SessionMessageTable.id, assistantMessageID)) + .get() + .pipe(Effect.orDie) + expect(tool?.data).toMatchObject({ + content: [{ state: { status: "completed", content: [{ type: "file", uri, mime: "image/png" }] } }], + }) + expect((yield* store.message(assistantMessageID))?.message).toMatchObject({ + content: [ + { + state: { + content: [{ type: "file", uri: `data:image/png;base64,${png.toString("base64")}`, mime: "image/png" }], + }, + }, + ], + }) + + expect( + yield* db + .select({ + hash: BlobTable.hash, + mime: BlobTable.mime, + bytes: BlobTable.bytes, + }) + .from(BlobTable) + .all() + .pipe(Effect.orDie), + ).toEqual([{ hash, mime: "image/png", bytes: png.byteLength }]) + expect( + yield* db + .select({ message: BlobRefTable.message_id, hash: BlobRefTable.hash, seq: BlobRefTable.seq }) + .from(BlobRefTable) + .orderBy(asc(BlobRefTable.seq)) + .all() + .pipe(Effect.orDie), + ).toEqual([ + { message: assistantMessageID, hash, seq: 0 }, + { message: id, hash, seq: delivered.durable?.seq }, + ]) + }).pipe(Effect.provide(mediaBlob)), + ) + + it.effect("deletes a cancelled inbox row's blob refs and blobs", () => + Effect.gen(function* () { + const db = yield* seedSession() + const inbox = yield* SessionInbox.Service + const id = SessionMessage.ID.make("msg_media_cancel") + yield* inbox.admit({ + id, + sessionID, + item: { type: "user", payload: { text: "later", files: [attachment("shot.png")] }, delivery: "queue" }, + }) + expect(yield* db.select().from(BlobRefTable).all().pipe(Effect.orDie)).toHaveLength(1) + expect(yield* db.select().from(BlobTable).all().pipe(Effect.orDie)).toHaveLength(1) + + yield* inbox.cancel({ id, sessionID }) + expect(yield* db.select().from(BlobRefTable).all().pipe(Effect.orDie)).toEqual([]) + expect(yield* db.select().from(BlobTable).all().pipe(Effect.orDie)).toEqual([]) + }).pipe(Effect.provide(mediaBlob)), + ) + + it.effect("deletes reverted messages' blob refs and the blobs only they referenced", () => + Effect.gen(function* () { + const db = yield* seedSession() + const bus = yield* Bus.Service + const inbox = yield* SessionInbox.Service + const text = Buffer.from("reverted attachment") + const deliver = (id: SessionMessage.ID, files: ReturnType[]) => + Effect.gen(function* () { + yield* inbox.admit({ id, sessionID, item: { type: "user", payload: { text: id, files }, delivery: "steer" } }) + yield* bus.publish(SessionEvent.InboxDelivered, { sessionID, inboxID: id }) + }) + const kept = SessionMessage.ID.make("msg_media_kept") + const reverted = SessionMessage.ID.make("msg_media_reverted") + const shared = SessionMessage.ID.make("msg_media_shared") + yield* deliver(kept, [attachment("shot.png")]) + yield* deliver(reverted, [attachment("notes.txt", text, "text/plain")]) + yield* deliver(shared, [attachment("again.png")]) + expect(yield* db.select().from(BlobRefTable).all().pipe(Effect.orDie)).toHaveLength(3) + + yield* bus.publish(SessionEvent.RevertEvent.Committed, { sessionID, to: reverted }) + expect( + yield* db + .select({ message: BlobRefTable.message_id, hash: BlobRefTable.hash }) + .from(BlobRefTable) + .all() + .pipe(Effect.orDie), + ).toEqual([{ message: kept, hash: SessionBlob.hashBytes(png) }]) + expect(yield* db.select({ hash: BlobTable.hash }).from(BlobTable).all().pipe(Effect.orDie)).toEqual([ + { hash: SessionBlob.hashBytes(png) }, + ]) + }).pipe(Effect.provide(mediaBlob)), + ) + + it.effect("keeps a blob while a fork references it and deletes it with the last session", () => + Effect.gen(function* () { + const db = yield* seedSession() + const bus = yield* Bus.Service + const inbox = yield* SessionInbox.Service + const id = SessionMessage.ID.make("msg_media_forked") + const childID = Session.ID.make("ses_media_fork") + yield* inbox.admit({ + id, + sessionID, + item: { type: "user", payload: { text: "fork me", files: [attachment("shot.png")] }, delivery: "steer" }, + }) + yield* bus.publish(SessionEvent.InboxDelivered, { sessionID, inboxID: id }) + yield* bus.publish(SessionEvent.Forked, { + sessionID: childID, + parentID: sessionID, + boundary: { type: "through", messageID: id }, + }) + expect( + yield* db.select({ session: BlobRefTable.session_id }).from(BlobRefTable).all().pipe(Effect.orDie), + ).toEqual(expect.arrayContaining([{ session: sessionID }, { session: childID }])) + + yield* bus.publish(SessionEvent.Deleted, { sessionID }) + expect( + yield* db.select({ session: BlobRefTable.session_id }).from(BlobRefTable).all().pipe(Effect.orDie), + ).toEqual([{ session: childID }]) + expect(yield* db.select().from(BlobTable).all().pipe(Effect.orDie)).toHaveLength(1) + + yield* bus.publish(SessionEvent.Deleted, { sessionID: childID }) + expect(yield* db.select().from(BlobRefTable).all().pipe(Effect.orDie)).toEqual([]) + expect(yield* db.select().from(BlobTable).all().pipe(Effect.orDie)).toEqual([]) + }).pipe(Effect.provide(mediaBlob)), + ) + + it.effect("releases a tool's blob when a content update drops its ref", () => + Effect.gen(function* () { + const db = yield* seedSession() + const bus = yield* Bus.Service + const assistantMessageID = SessionMessage.ID.make("msg_media_rewritten") + yield* db.insert(SessionMessageTable).values(assistantRow(assistantMessageID, 0)).run().pipe(Effect.orDie) + yield* bus.publish(SessionEvent.Tool.Input.Started, { sessionID, assistantMessageID, id: "shot", name: "shot" }) + yield* bus.publish(SessionEvent.Tool.Called, { + sessionID, + assistantMessageID, + id: "shot", + input: {}, + executed: true, + }) + yield* bus.publish(SessionEvent.Tool.Success, { + sessionID, + assistantMessageID, + id: "shot", + content: [{ type: "file", uri: `data:image/png;base64,${png.toString("base64")}`, mime: "image/png" }], + executed: true, + }) + expect(yield* db.select().from(BlobRefTable).all().pipe(Effect.orDie)).toHaveLength(1) + expect(yield* db.select().from(BlobTable).all().pipe(Effect.orDie)).toHaveLength(1) + + yield* bus.publish(SessionEvent.MessageContentUpdated, { sessionID, messageID: assistantMessageID, content: [] }) + expect(yield* db.select().from(BlobRefTable).all().pipe(Effect.orDie)).toEqual([]) + expect(yield* db.select().from(BlobTable).all().pipe(Effect.orDie)).toEqual([]) + }).pipe(Effect.provide(mediaBlob)), + ) }) diff --git a/packages/core/test/session-prompt.test.ts b/packages/core/test/session-prompt.test.ts index d6a20188945..631e5e5cbda 100644 --- a/packages/core/test/session-prompt.test.ts +++ b/packages/core/test/session-prompt.test.ts @@ -23,7 +23,13 @@ import { SessionMessage } from "@opencode/core/session/message" import { SessionProjector } from "@opencode/core/session/projector" import { SessionExecution } from "@opencode/core/session/execution" import { SessionInbox } from "@opencode/core/session/inbox" -import { SessionInboxTable, SessionMessageTable, SessionTable } from "@opencode/core/session/sql" +import { + BlobRefTable, + BlobTable, + SessionInboxTable, + SessionMessageTable, + SessionTable, +} from "@opencode/core/session/sql" import { SessionStore } from "@opencode/core/session/store" import { LocationServiceMap } from "@opencode/core/location-service-map" import type { LocationServices } from "@opencode/core/location-services" @@ -34,6 +40,10 @@ import { Snapshot } from "@opencode/core/snapshot" import { Skill } from "@opencode/core/skill" import { tmpdirScoped } from "./fixture/tmpdir" import { testEffect } from "./lib/effect" +import { Config } from "@opencode/core/config" +import { Document, Info } from "@opencode/schema/config" +import { ConfigExperimental } from "@opencode/schema/config/experimental" +import { SessionStored } from "@opencode/core/session/stored" const executionCalls: Session.ID[] = [] const interruptCalls: Session.ID[] = [] @@ -62,6 +72,13 @@ const execution = Layer.succeed( awaitIdle: () => Effect.void, }), ) +const mediaDirectory = "/media" +const mediaBlob = Config.testLayer([ + new Document({ + type: "document", + info: new Info({ experimental: new ConfigExperimental.Info({ media_blob: true }) }), + }), +]) const locations = makeGlobalNode({ service: LocationServiceMap.Service, layer: Layer.effect( @@ -69,9 +86,10 @@ const locations = makeGlobalNode({ Effect.gen(function* () { const bus = yield* Bus.Service return yield* LayerMap.make( - (_ref: Location.Ref) => + (ref: Location.Ref) => // oxlint-disable-next-line typescript-eslint/no-unsafe-type-assertion Layer.mergeAll( + ref.directory === mediaDirectory ? mediaBlob : Layer.empty, LayerNode.compile(LayerNode.group([PluginHooks.node, Skill.node]), { replacements: [Bus.node.replace(Layer.succeed(Bus.Service, bus))], }), @@ -432,6 +450,47 @@ describe("Session.prompt", () => { }), ) + it.effect("stores media refs at admission when the session's config enables media_blob", () => + Effect.gen(function* () { + yield* setup + const { db } = yield* Database.Service + const mediaSessionID = Session.ID.make("ses_prompt_media") + yield* db + .insert(SessionTable) + .values({ + id: mediaSessionID, + project_id: Project.ID.global, + slug: "media", + directory: mediaDirectory, + title: "media", + version: "test", + }) + .run() + .pipe(Effect.orDie) + const bytes = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=" + + const message = yield* (yield* Session.Service).prompt({ + sessionID: mediaSessionID, + text: "Inspect this image", + files: [{ uri: `data:image/png;base64,${bytes}` }], + resume: false, + }) + + const row = yield* db + .select() + .from(SessionInboxTable) + .where(eq(SessionInboxTable.id, message.id)) + .get() + .pipe(Effect.orDie) + const [hash] = (yield* db.select().from(BlobTable).all().pipe(Effect.orDie)).map((blob) => blob.hash) + expect(hash).toBeDefined() + expect(row?.payload).toMatchObject({ files: [{ uri: SessionStored.ref(hash!), mime: "image/png" }] }) + expect(yield* db.select().from(BlobRefTable).all().pipe(Effect.orDie)).toEqual([ + { session_id: mediaSessionID, message_id: message.id, seq: expect.any(Number), hash }, + ]) + }), + ) + it.effect("normalizes large image content before validating persisted Base64", () => Effect.gen(function* () { yield* setup diff --git a/packages/protocol/openapi.json b/packages/protocol/openapi.json index 9e362f333b2..e124f4505fb 100644 --- a/packages/protocol/openapi.json +++ b/packages/protocol/openapi.json @@ -14778,6 +14778,9 @@ "background_compaction": { "type": "boolean" }, + "media_blob": { + "type": "boolean" + }, "subagent_depth": { "type": "integer", "minimum": 0 diff --git a/packages/schema/src/config/experimental.ts b/packages/schema/src/config/experimental.ts index 4e703eae37c..633a03e58d4 100644 --- a/packages/schema/src/config/experimental.ts +++ b/packages/schema/src/config/experimental.ts @@ -15,6 +15,10 @@ export class Info extends Schema.Class("ConfigExperimental.Info")({ description: "Enable experimental background compaction: summarize the conversation in the background as it nears the compaction threshold, so automatic compaction does not wait for the summary. Background summaries that are never used still cost tokens. Defaults to false.", }), + media_blob: Schema.Boolean.pipe(optional).annotate({ + description: + "Store new session media once in a deduplicated blob table instead of inline in every message row. Defaults to false.", + }), subagent_depth: NonNegativeInt.pipe(optional).annotate({ description: "Maximum subagent nesting depth. Defaults to 1.", }), From 6820420d642c413421899f30666b07a8304a6400 Mon Sep 17 00:00:00 2001 From: Kit Langton Date: Sun, 25 Oct 2026 15:50:56 -0700 Subject: [PATCH 03/15] test(core): guard boot work counts for catalog lookups, Azure clones and git spawns (#58892) --- packages/core/test/boot-work.test.ts | 214 +++++++++++++++++++++++++++ 1 file changed, 214 insertions(+) create mode 100644 packages/core/test/boot-work.test.ts diff --git a/packages/core/test/boot-work.test.ts b/packages/core/test/boot-work.test.ts new file mode 100644 index 00000000000..1f2636c8fa0 --- /dev/null +++ b/packages/core/test/boot-work.test.ts @@ -0,0 +1,214 @@ +// Counts the work a cold Location boot and one session turn do, so a speedup can't silently regress: +// catalog lookups per step (#58601), Azure model edits per model rebuild (#58600) and git spawns (#58602). +// These are exact counts, not timings. Changing one is fine when the change is intended; update the expected +// number here and say why in the PR. +import { describe, expect } from "bun:test" +import path from "path" +import whichPkg from "which" +import { Effect, Layer } from "effect" +import { ChildProcessSpawner } from "effect/process" +import { LanguageModel } from "@opencode/ai/effect" +import { OpenAIChat } from "@opencode/ai/protocols/openai-chat" +import { TestLLM } from "@opencode/ai/testing" +import { Agent } from "@opencode/core/agent" +import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder" +import { LayerNodePlatform } from "@opencode/core/effect/app-node-platform" +import { Watcher } from "@opencode/core/filesystem/watcher" +import { LocationServiceMap } from "@opencode/core/location-service-map" +import { Model } from "@opencode/core/model" +import { Plugin } from "@opencode/core/plugin" +import { Provider } from "@opencode/core/provider" +import { AbsolutePath } from "@opencode/core/schema" +import { Session } from "@opencode/core/session" +import { SessionRunnerModel } from "@opencode/core/session/runner/model" +import { gitExecutable } from "@opencode/core/util/git-executable" +import { CrossSpawnSpawner } from "@opencode/util/cross-spawn-spawner" +import { LayerNode } from "@opencode/util/effect/layer-node" +import { Global } from "@opencode/util/global" +import { tempGlobalLayer } from "./fixture/global" +import { initRepo } from "./fixture/git" +import { offlineModels } from "./fixture/models" +import { tmpdirScoped } from "./fixture/tmpdir" +import { testEffect } from "./lib/effect" + +const counters = { catalogLookups: 0, azureModelEdits: 0, gitSpawns: [] as string[], gitLookups: 0 } + +const isGit = (command: string) => path.basename(command).replace(/\.exe$/i, "") === "git" + +const countingSpawner = CrossSpawnSpawner.node.replace( + CrossSpawnSpawner.node.mapLayer((base) => + Layer.effect( + ChildProcessSpawner.ChildProcessSpawner, + Effect.map(ChildProcessSpawner.ChildProcessSpawner, (spawner) => + ChildProcessSpawner.make((command) => { + if (command._tag === "StandardCommand" && isGit(command.command)) + counters.gitSpawns.push([command.command, ...command.args].join(" ")) + return spawner.spawn(command) + }), + ), + ).pipe(Layer.provide(base)), + ), +) + +const isAzure = (provider: Provider.Info) => + provider.id === Provider.ID.azure || provider.package.startsWith("@opencode/ai/providers/azure/") + +// `ctx.model.list()` reads `Model.available()`. Every editor list/get/update clones the models it touches. +const countingModels = Model.node.replace( + Model.node.mapLayer((base) => + Layer.effect( + Model.Service, + Effect.map(Model.Service, (models) => + Model.Service.of({ + ...models, + available: () => + Effect.suspend(() => { + counters.catalogLookups++ + return models.available() + }), + transform: (update) => + models.transform((editor) => { + const azure = new Set( + editor.provider + .list() + .filter((item) => isAzure(item.provider)) + .map((item) => item.provider.id), + ) + update({ + ...editor, + list: (providerID) => { + const result = editor.list(providerID) + counters.azureModelEdits += result.filter((model) => azure.has(model.providerID)).length + return result + }, + get: (providerID, modelID) => { + if (azure.has(providerID)) counters.azureModelEdits++ + return editor.get(providerID, modelID) + }, + update: (providerID, modelID, edit) => { + if (azure.has(providerID)) counters.azureModelEdits++ + editor.update(providerID, modelID, edit) + }, + }) + }), + }), + ), + ).pipe(Layer.provide(base)), + ), +) + +// Counts PATH lookups of git: resolving the git executable (macOS and Windows) and finding Git Bash (Windows). +const countingGitLookups = Effect.acquireRelease( + Effect.sync(() => { + const sync = whichPkg.sync + whichPkg.sync = Object.assign((cmd: string, options?: Parameters[1]) => { + if (cmd === "git") counters.gitLookups++ + return sync(cmd, options as never) + }, sync) as typeof sync + return sync + }), + (sync) => Effect.sync(() => void (whichPkg.sync = sync)), +) + +const llmLayer = TestLLM.testLayer({ fallback: TestLLM.text("Done", "done") }) +const it = testEffect( + Layer.merge( + llmLayer, + AppNodeBuilder.build(LayerNode.group([Session.node, LocationServiceMap.node]), [ + Global.node.replace(tempGlobalLayer), + offlineModels, + Watcher.node.replace(Watcher.configured({ enabled: false })), + LayerNodePlatform.llmClient.replace(llmLayer), + SessionRunnerModel.node.replace( + Layer.succeed(SessionRunnerModel.Service, { + resolve: () => + Effect.succeed( + SessionRunnerModel.resolved( + LanguageModel.make({ id: "boot", provider: "test", route: OpenAIChat.route }), + { + capabilities: { tools: true, input: ["text"], output: ["text"] }, + cost: [], + limit: { context: 200_000, output: 32_000 }, + }, + ), + ), + }), + ), + countingSpawner, + countingModels, + ]), + ), +) + +const expected = { + // Folder startup: project identity, VCS branch/remote reads and the Location's repository probe. + "boot: git spawns": 8, + // The step's snapshot: repository probe, snapshot init, diff, untracked files, ignore check, index and tree. + "turn: git spawns": 9, + // Git is resolved once per process, before this test, and every spawn uses that resolution. On Windows the shell + // tool's ShellSelect.gitbash() looks up git once to find Git Bash (cached per process), so one lookup is allowed. + "boot + turn: git lookups beyond one": 0, + "boot + turn: git spawns not using the resolved executable": 0, + // One step resolves the prompt hook model once, shared by the context, compaction and generate hooks. + "turn: catalog lookups per step": 1, + // Azure is enabled without a resource name, so its model transform has nothing to rewrite. + "model rebuild: Azure model edits": 0, +} + +describe("boot work", () => { + it.live("counts catalog lookups, Azure model edits and git spawns over a cold boot and one turn", () => + Effect.gen(function* () { + const tmp = yield* tmpdirScoped() + yield* Effect.promise(() => initRepo(tmp.path)) + yield* Effect.promise(() => + Bun.write(path.join(tmp.path, "opencode.json"), JSON.stringify({ providers: { azure: {} } })), + ) + const executable = gitExecutable() + counters.gitSpawns = [] + counters.gitLookups = 0 + yield* countingGitLookups + + const sessions = yield* Session.Service + const locations = yield* LocationServiceMap.Service + const session = yield* sessions.create({ + location: { directory: AbsolutePath.make(tmp.path) }, + agent: Agent.ID.make("build"), + model: Model.Ref.make({ id: Model.ID.make("boot"), providerID: Provider.ID.make("test") }), + }) + const location = locations.get(session.location) + yield* Plugin.Service.use((plugins) => plugins.awaitActivation).pipe(Effect.provide(location)) + const boot = counters.gitSpawns.length + + counters.catalogLookups = 0 + yield* sessions.prompt({ sessionID: session.id, text: "hello" }) + yield* sessions.wait(session.id) + const steps = (yield* sessions.context(session.id)).filter((message) => message.type === "assistant").length + const lookups = counters.catalogLookups + const spawns = [...counters.gitSpawns] + + counters.azureModelEdits = 0 + yield* Model.Service.use((models) => models.reload().pipe(Effect.andThen(models.available()))).pipe( + Effect.provide(location), + ) + + const measured: Record = { + "boot: git spawns": boot, + "turn: git spawns": spawns.length - boot, + "boot + turn: git lookups beyond one": Math.max(0, counters.gitLookups - 1), + "boot + turn: git spawns not using the resolved executable": spawns.filter( + (spawn) => !spawn.startsWith(`${executable} `), + ).length, + "turn: catalog lookups per step": steps === 0 ? lookups : lookups / steps, + "model rebuild: Azure model edits": counters.azureModelEdits, + } + const drift = Object.entries(expected).flatMap(([name, want]) => { + const got = measured[name as keyof typeof expected] + if (got === want) return [] + return [`${name}: expected ${want}, got ${got} (${got > want ? "+" : ""}${got - want})`] + }) + const report = drift.length === 0 ? "" : [...drift, "", "git spawns, boot then turn:", ...spawns].join("\n") + expect(steps).toBe(1) + expect(report).toBe("") + }), + ) +}) From 71d29ef4ba22071414c1ba87a42c9a64a1d09f8c Mon Sep 17 00:00:00 2001 From: Kit Langton Date: Sun, 25 Oct 2026 15:53:00 -0700 Subject: [PATCH 04/15] feat(console): serve apple-app-site-association for iOS pair links (#58540) --- .../app/src/lib/apple-app-site-association.ts | 20 ++++++++++ packages/console/app/src/middleware.ts | 4 ++ .../app/test/appleAppSiteAssociation.test.ts | 40 +++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 packages/console/app/src/lib/apple-app-site-association.ts create mode 100644 packages/console/app/test/appleAppSiteAssociation.test.ts diff --git a/packages/console/app/src/lib/apple-app-site-association.ts b/packages/console/app/src/lib/apple-app-site-association.ts new file mode 100644 index 00000000000..48afe7483ea --- /dev/null +++ b/packages/console/app/src/lib/apple-app-site-association.ts @@ -0,0 +1,20 @@ +// Lets https://opencode.ai/pair links open the OpenCode iOS app. Only /pair may open the app. +// Served from middleware because the file router skips dot directories like /.well-known. +const body = JSON.stringify({ + applinks: { + details: [ + { + appIDs: ["5NZ4Q7NXJ4.ai.opencode.mobilecode"], + components: [{ "/": "/pair", comment: "pairing links, payload in the fragment" }, { "/": "/pair/*" }], + }, + ], + }, +}) + +export function appleAppSiteAssociation(request: Request) { + if (request.method !== "GET" && request.method !== "HEAD") return + if (new URL(request.url).pathname !== "/.well-known/apple-app-site-association") return + return new Response(request.method === "HEAD" ? null : body, { + headers: { "Content-Type": "application/json", "Cache-Control": "public, max-age=3600" }, + }) +} diff --git a/packages/console/app/src/middleware.ts b/packages/console/app/src/middleware.ts index 195f6ec5adb..a248267e3e3 100644 --- a/packages/console/app/src/middleware.ts +++ b/packages/console/app/src/middleware.ts @@ -1,9 +1,13 @@ import { createMiddleware } from "@solidjs/start/middleware" +import { appleAppSiteAssociation } from "~/lib/apple-app-site-association" import { LOCALE_HEADER, cookie, fromPathname, strip } from "~/lib/language" import { sanitizeServerActionRequest } from "~/lib/server-action" export default createMiddleware({ async onRequest(event) { + const association = appleAppSiteAssociation(event.request) + if (association) return association + event.request = sanitizeServerActionRequest(event.request) const url = new URL(event.request.url) diff --git a/packages/console/app/test/appleAppSiteAssociation.test.ts b/packages/console/app/test/appleAppSiteAssociation.test.ts new file mode 100644 index 00000000000..6adc0ba90f4 --- /dev/null +++ b/packages/console/app/test/appleAppSiteAssociation.test.ts @@ -0,0 +1,40 @@ +import { describe, expect, test } from "bun:test" +import { appleAppSiteAssociation } from "../src/lib/apple-app-site-association" + +describe("apple app site association", () => { + test("serves JSON that opens only /pair links in the iOS app", async () => { + const response = appleAppSiteAssociation(new Request("https://opencode.ai/.well-known/apple-app-site-association")) + + expect(response?.status).toBe(200) + expect(response?.headers.get("content-type")).toBe("application/json") + expect(await response?.json()).toEqual({ + applinks: { + details: [ + { + appIDs: ["5NZ4Q7NXJ4.ai.opencode.mobilecode"], + components: [{ "/": "/pair", comment: "pairing links, payload in the fragment" }, { "/": "/pair/*" }], + }, + ], + }, + }) + }) + + test("answers HEAD without a body", async () => { + const response = appleAppSiteAssociation( + new Request("https://opencode.ai/.well-known/apple-app-site-association", { method: "HEAD" }), + ) + + expect(response?.headers.get("content-type")).toBe("application/json") + expect(await response?.text()).toBe("") + }) + + test("ignores other paths and methods", () => { + expect( + [ + new Request("https://opencode.ai/.well-known/apple-app-site-association.json"), + new Request("https://opencode.ai/pair"), + new Request("https://opencode.ai/.well-known/apple-app-site-association", { method: "POST" }), + ].map(appleAppSiteAssociation), + ).toEqual([undefined, undefined, undefined]) + }) +}) From 8b813d362c96e9f1ef06178d4ed99d80b2f0c80c Mon Sep 17 00:00:00 2001 From: Luke Parker <10430890+Hona@users.noreply.github.com> Date: Mon, 26 Oct 2026 08:54:00 +1000 Subject: [PATCH 05/15] feat(core): watch files open in the side panel (#59081) Co-authored-by: James Long --- packages/client/src/effect/api/api.ts | 16 ++ .../client/src/effect/generated/client.ts | 25 +- .../client/src/promise/generated/client.ts | 30 +++ .../client/src/promise/generated/types.ts | 14 ++ packages/client/test/promise.test.ts | 2 +- packages/core/src/filesystem/file-watch.ts | 164 ++++++++++++++ packages/core/src/filesystem/watcher.ts | 27 ++- .../core/test/filesystem/file-watch.test.ts | 213 ++++++++++++++++++ packages/core/test/filesystem/watcher.test.ts | 10 + packages/protocol/openapi.json | 203 +++++++++++++++++ packages/protocol/src/groups/fs.ts | 39 +++- packages/server/src/handlers/fs.ts | 39 +++- packages/server/src/routes.ts | 3 + packages/server/test/fs.test.ts | 29 +++ 14 files changed, 800 insertions(+), 14 deletions(-) create mode 100644 packages/core/src/filesystem/file-watch.ts create mode 100644 packages/core/test/filesystem/file-watch.test.ts diff --git a/packages/client/src/effect/api/api.ts b/packages/client/src/effect/api/api.ts index ebf11286169..583d78f9427 100644 --- a/packages/client/src/effect/api/api.ts +++ b/packages/client/src/effect/api/api.ts @@ -1840,9 +1840,25 @@ export type FileFindInput = { export type FileFindOutput = { readonly location: Location.PublicRef; readonly data: ReadonlyArray } export type FileFindOperation = (input: FileFindInput) => Effect.Effect +export type FileWatchInput = { + readonly location?: { readonly directory?: string | undefined } | undefined + readonly path: string +} +export type FileWatchOutput = void +export type FileWatchOperation = (input: FileWatchInput) => Effect.Effect + +export type FileUnwatchInput = { + readonly location?: { readonly directory?: string | undefined } | undefined + readonly path: string +} +export type FileUnwatchOutput = void +export type FileUnwatchOperation = (input: FileUnwatchInput) => Effect.Effect + export interface FileApi { readonly list: FileListOperation readonly find: FileFindOperation + readonly watch: FileWatchOperation + readonly unwatch: FileUnwatchOperation } export type CommandListInput = { readonly location?: { readonly directory?: string | undefined } | undefined } diff --git a/packages/client/src/effect/generated/client.ts b/packages/client/src/effect/generated/client.ts index 69c1136c0da..7612a10ba8a 100644 --- a/packages/client/src/effect/generated/client.ts +++ b/packages/client/src/effect/generated/client.ts @@ -195,6 +195,10 @@ import type { FileListOutput, FileFindInput, FileFindOutput, + FileWatchInput, + FileWatchOutput, + FileUnwatchInput, + FileUnwatchOutput, CommandListInput, CommandListOutput, SkillListInput, @@ -1276,7 +1280,26 @@ const EndpointFileFind = (raw: RawClient["server.fs"]) => (input: FileFindInput) }).pipe(Effect.mapError(mapClientError)), ) -const adaptGroupFile = (raw: RawClient["server.fs"]) => ({ list: EndpointFileList(raw), find: EndpointFileFind(raw) }) +const EndpointFileWatch = (raw: RawClient["server.fs"]) => (input: FileWatchInput) => + preserveEffect()( + raw["fs.watch"]({ query: { location: input["location"] }, payload: { path: input["path"] } }).pipe( + Effect.mapError(mapClientError), + ), + ) + +const EndpointFileUnwatch = (raw: RawClient["server.fs"]) => (input: FileUnwatchInput) => + preserveEffect()( + raw["fs.unwatch"]({ query: { location: input["location"] }, payload: { path: input["path"] } }).pipe( + Effect.mapError(mapClientError), + ), + ) + +const adaptGroupFile = (raw: RawClient["server.fs"]) => ({ + list: EndpointFileList(raw), + find: EndpointFileFind(raw), + watch: EndpointFileWatch(raw), + unwatch: EndpointFileUnwatch(raw), +}) const EndpointCommandList = (raw: RawClient["server.command"]) => (input?: CommandListInput) => preserveEffect()( diff --git a/packages/client/src/promise/generated/client.ts b/packages/client/src/promise/generated/client.ts index 8c19478c024..d29a5268579 100644 --- a/packages/client/src/promise/generated/client.ts +++ b/packages/client/src/promise/generated/client.ts @@ -193,6 +193,10 @@ import type { FileFindOutput, FileWriteInput, FileWriteOutput, + FileWatchInput, + FileWatchOutput, + FileUnwatchInput, + FileUnwatchOutput, CommandListInput, CommandListOutput, SkillListInput, @@ -1734,6 +1738,32 @@ export function make(options: ClientOptions) { }, requestOptions, ), + watch: (input: FileWatchInput, requestOptions?: RequestOptions) => + request( + { + method: "POST", + path: `/api/experimental/fs/watch`, + query: { location: input["location"] }, + body: { path: input["path"] }, + successStatus: 204, + declaredStatuses: [400, 401, 404, 503], + empty: true, + }, + requestOptions, + ), + unwatch: (input: FileUnwatchInput, requestOptions?: RequestOptions) => + request( + { + method: "DELETE", + path: `/api/experimental/fs/watch`, + query: { location: input["location"] }, + body: { path: input["path"] }, + successStatus: 204, + declaredStatuses: [400, 401, 404], + empty: true, + }, + requestOptions, + ), }, command: { list: (input?: CommandListInput, requestOptions?: RequestOptions) => diff --git a/packages/client/src/promise/generated/types.ts b/packages/client/src/promise/generated/types.ts index 4279704ae2e..303f8e07afa 100644 --- a/packages/client/src/promise/generated/types.ts +++ b/packages/client/src/promise/generated/types.ts @@ -5612,6 +5612,20 @@ export type FileWriteInput = { export type FileWriteOutput = { location: LocationPublicRef; data: FileSystemWrite } +export type FileWatchInput = { + readonly location?: { readonly location?: { readonly directory?: string | undefined } | undefined }["location"] + readonly path: { readonly path: string }["path"] +} + +export type FileWatchOutput = void + +export type FileUnwatchInput = { + readonly location?: { readonly location?: { readonly directory?: string | undefined } | undefined }["location"] + readonly path: { readonly path: string }["path"] +} + +export type FileUnwatchOutput = void + export type CommandListInput = { readonly location?: { readonly location?: { readonly directory?: string | undefined } | undefined }["location"] } diff --git a/packages/client/test/promise.test.ts b/packages/client/test/promise.test.ts index 3283ddaaaa1..2a975d57ffc 100644 --- a/packages/client/test/promise.test.ts +++ b/packages/client/test/promise.test.ts @@ -49,7 +49,7 @@ test("exposes every standard HTTP API group", () => { expect(Object.keys(client.websearch)).toEqual(["providers", "query"]) expect(Object.keys(client.voice)).toEqual(["models", "session"]) expect(Object.keys(client.usage)).toEqual(["limits"]) - expect(Object.keys(client.file)).toEqual(["read", "list", "find", "write"]) + expect(Object.keys(client.file)).toEqual(["read", "list", "find", "write", "watch", "unwatch"]) expect(Object.keys(client.vcs)).toEqual(["init", "get", "base", "status", "branch", "diff"]) expect(Object.keys(client.pty)).toEqual(["list", "create", "get", "update", "remove", "connect"]) expect(Object.keys(client.pty.connect)).toEqual(["token"]) diff --git a/packages/core/src/filesystem/file-watch.ts b/packages/core/src/filesystem/file-watch.ts new file mode 100644 index 00000000000..011e2319bed --- /dev/null +++ b/packages/core/src/filesystem/file-watch.ts @@ -0,0 +1,164 @@ +export * as FileWatch from "./file-watch.js" + +import { makeGlobalNode } from "@opencode/util/effect/app-node" +import { FSUtil } from "@opencode/util/fs-util" +import { FileSystem } from "@opencode/schema/filesystem" +import type { Location } from "@opencode/schema/location" +import { + Cause, + Clock, + Context, + Duration, + Effect, + Exit, + Layer, + Schedule, + Schema, + Scope, + Semaphore, + Stream, +} from "effect" +import { Bus } from "../bus.js" +import { Watcher } from "./watcher.js" + +export const TTL = Duration.hours(24) +export const SWEEP_INTERVAL = Duration.minutes(5) +export const MAX_PER_LOCATION = 256 + +export class DisabledError extends Schema.TaggedError()("FileWatch.DisabledError", {}) { + override get message() { + return "File watching is disabled" + } +} + +export type Input = { + readonly location: Location.Ref + /** Absolute path of the file to watch. */ + readonly file: string +} + +export interface Interface { + /** Starts watching a file, or resets its expiry when it is already watched. */ + readonly watch: (input: Input) => Effect.Effect + readonly unwatch: (input: Input) => Effect.Effect +} + +export class Service extends Context.Service()("@opencode/FileWatch") {} + +export const Options = Schema.Struct({ + enabled: Schema.optional(Schema.Boolean), +}) +export type Options = typeof Options.Type + +type Entry = { expires: number; scope: Scope.Closeable } + +// Global rather than location-scoped so watches outlive idle location eviction. +export const layer = (options?: Options) => + Layer.effect( + Service, + Effect.gen(function* () { + if (options?.enabled === false) { + return Service.of({ watch: () => Effect.fail(new DisabledError()), unwatch: () => Effect.void }) + } + const watcher = yield* Watcher.Service + const bus = yield* Bus.Service + const fs = yield* FSUtil.Service + const scope = yield* Effect.scope + const lock = yield* Semaphore.make(1) + const locations = new Map>() + const locationKey = (location: Location.Ref) => JSON.stringify([location.directory, location.workspaceID]) + + const remove = (key: string, file: string) => { + const files = locations.get(key) + const entry = files?.get(file) + if (!files || !entry) return + files.delete(file) + if (files.size === 0) locations.delete(key) + return entry + } + + const stop = (key: string, file: string) => + Effect.suspend(() => { + const entry = remove(key, file) + if (!entry) return Effect.void + return Scope.close(entry.scope, Exit.void) + }) + + const start = Effect.fnUntraced(function* (key: string, input: Input) { + const child = yield* Scope.fork(scope) + yield* Effect.gen(function* () { + const updates = yield* watcher.subscribe({ path: input.file, type: "file" }) + yield* Stream.runForEach(updates, () => + Effect.gen(function* () { + const exists = yield* fs.existsSafe(input.file) + yield* bus.publish( + FileSystem.Event.Changed, + { file: input.file, event: exists ? "change" : "unlink" }, + { location: input.location }, + ) + }), + ) + }).pipe( + Effect.catchCauseIf( + (cause) => !Cause.hasInterrupts(cause), + (cause) => Effect.logError("file watch failed", { file: input.file, cause }), + ), + // The watch ended or failed (e.g. its directory vanished): forget it so the next watch starts a new one. + // Interrupts skip this. The scope closes from another fiber because closing it interrupts this one. + Effect.andThen( + lock.withPermit( + Effect.suspend(() => { + if (locations.get(key)?.get(input.file)?.scope !== child) return Effect.void + remove(key, input.file) + return Scope.close(child, Exit.void).pipe(Effect.forkIn(scope), Effect.asVoid) + }), + ), + ), + Effect.forkIn(child, { startImmediately: true }), + ) + return child + }) + + const sweep = lock.withPermit( + Effect.gen(function* () { + const now = yield* Clock.currentTimeMillis + const expired = [...locations].flatMap(([key, files]) => + [...files].filter(([, entry]) => entry.expires <= now).map(([file]) => [key, file] as const), + ) + yield* Effect.forEach(expired, ([key, file]) => stop(key, file), { discard: true }) + }), + ) + yield* sweep.pipe(Effect.repeat(Schedule.spaced(SWEEP_INTERVAL)), Effect.forkScoped) + + return Service.of({ + watch: (input) => + lock.withPermit( + Effect.gen(function* () { + const key = locationKey(input.location) + const expires = (yield* Clock.currentTimeMillis) + Duration.toMillis(TTL) + const existing = locations.get(key)?.get(input.file) + if (existing) { + existing.expires = expires + return + } + const full = locations.get(key) + if (full && full.size >= MAX_PER_LOCATION) { + const soonest = [...full].reduce((a, b) => (b[1].expires < a[1].expires ? b : a)) + yield* stop(key, soonest[0]) + } + const entry = { expires, scope: yield* start(key, input) } + const files = locations.get(key) ?? new Map() + files.set(input.file, entry) + locations.set(key, files) + }), + ), + unwatch: (input) => lock.withPermit(stop(locationKey(input.location), input.file)), + }) + }), + ) + +export function configured(options?: Options) { + return makeGlobalNode({ service: Service, layer: layer(options), deps: [Watcher.node, Bus.node, FSUtil.node] }) +} + +export const node = configured() diff --git a/packages/core/src/filesystem/watcher.ts b/packages/core/src/filesystem/watcher.ts index f33413bfb02..ae359e36658 100644 --- a/packages/core/src/filesystem/watcher.ts +++ b/packages/core/src/filesystem/watcher.ts @@ -203,20 +203,27 @@ export const nativeLayer = Layer.succeed( Native.of({ subscribe: (input) => { if (input.type === "file" || input.type === "entries") { - return Effect.sync(() => { - const directory = input.type === "file" ? path.dirname(input.target) : input.target - const names = new Set(input.type === "file" ? [path.basename(input.target)] : input.names) - const subscription = watch(directory, { recursive: false }, (_event, file) => { + const directory = input.type === "file" ? path.dirname(input.target) : input.target + const names = new Set(input.type === "file" ? [path.basename(input.target)] : input.names) + return Effect.try(() => + watch(directory, { recursive: false }, (_event, file) => { if (file && !names.has(file)) return for (const name of file ? [file] : names) { input.publish({ path: path.join(directory, name), type: "update" }) } - }) - subscription.on("error", (error: unknown) => - Effect.runFork(Effect.logError("watcher callback failed", { path: directory, error })), - ) - return { unsubscribe: () => Promise.resolve(subscription.close()), backend: "node" } - }) + }), + ).pipe( + Effect.map((subscription): Subscription => { + subscription.on("error", (error: unknown) => + Effect.runFork(Effect.logError("watcher callback failed", { path: directory, error })), + ) + return { unsubscribe: () => Promise.resolve(subscription.close()), backend: "node" } + }), + // A missing directory (ENOENT) ends subscriber streams instead of crashing the watch. + Effect.catch((error) => + Effect.logError("failed to watch directory", { path: directory, error }).pipe(Effect.as(undefined)), + ), + ) } return subscribeDirectory(watcher(), getBackend(), input.target, input.ignore, input.publish) }, diff --git a/packages/core/test/filesystem/file-watch.test.ts b/packages/core/test/filesystem/file-watch.test.ts new file mode 100644 index 00000000000..52efcb85282 --- /dev/null +++ b/packages/core/test/filesystem/file-watch.test.ts @@ -0,0 +1,213 @@ +import { describe, expect } from "bun:test" +import fs from "fs/promises" +import path from "path" +import { Deferred, Duration, Effect, Fiber, Layer, Schedule, Stream } from "effect" +import { TestClock } from "effect/testing" +import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder" +import { Bus } from "@opencode/core/bus" +import { FileWatch } from "@opencode/core/filesystem/file-watch" +import { Watcher } from "@opencode/core/filesystem/watcher" +import { FileSystem } from "@opencode/schema/filesystem" +import { AbsolutePath } from "@opencode/core/schema" +import { FSUtil } from "@opencode/util/fs-util" +import { LayerNode } from "@opencode/util/effect/layer-node" +import { tmpdirScoped } from "../fixture/tmpdir" +import { testEffect } from "../lib/effect" + +const services = AppNodeBuilder.build(LayerNode.group([FSUtil.node, Bus.node])) +const it = testEffect(Layer.provideMerge(FileWatch.layer(), Watcher.testLayer).pipe(Layer.provideMerge(services))) +const native = testEffect( + FileWatch.layer().pipe( + Layer.provideMerge(Watcher.layer().pipe(Layer.provide(Watcher.nativeLayer))), + Layer.provideMerge(services), + ), +) +const describeNative = process.env.CI ? describe.skip : describe + +const location = { directory: AbsolutePath.make("/project") } +const file = (name: string) => ({ location, file: path.join(location.directory, name) }) + +const subscriptions = Effect.gen(function* () { + const test = yield* Watcher.Test + return (yield* test.subscriptions()).length +}) + +describe("FileWatch", () => { + it.effect("shares one watch per file and resubscribes after unwatch", () => + Effect.gen(function* () { + const fileWatch = yield* FileWatch.Service + yield* fileWatch.watch(file("a.ts")) + yield* fileWatch.watch(file("a.ts")) + expect(yield* subscriptions).toBe(1) + yield* fileWatch.unwatch(file("a.ts")) + yield* fileWatch.watch(file("a.ts")) + expect(yield* subscriptions).toBe(2) + }), + ) + + it.effect("expires a watch after 24 hours unless it is watched again", () => + Effect.gen(function* () { + const fileWatch = yield* FileWatch.Service + yield* fileWatch.watch(file("a.ts")) + yield* fileWatch.watch(file("b.ts")) + yield* TestClock.adjust(Duration.hours(23)) + yield* fileWatch.watch(file("a.ts")) + yield* TestClock.adjust(Duration.hours(2)) + expect(yield* subscriptions).toBe(2) + yield* fileWatch.watch(file("a.ts")) + expect(yield* subscriptions).toBe(2) + yield* fileWatch.watch(file("b.ts")) + expect(yield* subscriptions).toBe(3) + }), + ) + + it.effect("evicts the watch closest to expiring when a location is full", () => + Effect.gen(function* () { + const fileWatch = yield* FileWatch.Service + yield* fileWatch.watch(file("oldest.ts")) + yield* TestClock.adjust(Duration.minutes(1)) + yield* Effect.forEach( + Array.from({ length: FileWatch.MAX_PER_LOCATION - 1 }, (_, index) => file(`${index}.ts`)), + (input) => fileWatch.watch(input), + { discard: true }, + ) + yield* fileWatch.watch({ location: { directory: AbsolutePath.make("/other") }, file: "/other/a.ts" }) + expect(yield* subscriptions).toBe(FileWatch.MAX_PER_LOCATION + 1) + yield* fileWatch.watch(file("newest.ts")) + yield* fileWatch.watch(file("0.ts")) + expect(yield* subscriptions).toBe(FileWatch.MAX_PER_LOCATION + 2) + yield* fileWatch.watch(file("oldest.ts")) + expect(yield* subscriptions).toBe(FileWatch.MAX_PER_LOCATION + 3) + }), + ) + + it.effect("publishes filesystem.changed on the watch's location", () => + Effect.gen(function* () { + const fileWatch = yield* FileWatch.Service + const test = yield* Watcher.Test + const bus = yield* Bus.Service + const tmp = yield* tmpdirScoped() + const target = path.join(tmp.path, "a.ts") + yield* Effect.promise(() => fs.writeFile(target, "a")) + const input = { location: { directory: AbsolutePath.make(tmp.path) }, file: target } + const events = yield* bus + .subscribe(FileSystem.Event.Changed) + .pipe(Stream.take(2), Stream.runCollect, Effect.forkScoped({ startImmediately: true })) + yield* fileWatch.watch(input) + yield* test.emit({ path: target, type: "update" }) + yield* Effect.promise(() => fs.rm(target)) + yield* test.emit({ path: target, type: "update" }) + const received = yield* Fiber.join(events) + expect(received.map((event) => ({ directory: event.location?.directory, ...event.data }))).toEqual([ + { directory: AbsolutePath.make(tmp.path), file: target, event: "change" }, + { directory: AbsolutePath.make(tmp.path), file: target, event: "unlink" }, + ]) + }), + ) +}) + +describe("FileWatch dead watches", () => { + const counts = { subscribes: 0 } + // A native watch that cannot start, like one on a directory that no longer exists. + const failing = testEffect( + FileWatch.layer().pipe( + Layer.provide( + Watcher.layer().pipe( + Layer.provide( + Layer.succeed( + Watcher.Native, + Watcher.Native.of({ subscribe: () => Effect.sync(() => void counts.subscribes++) }), + ), + ), + ), + ), + Layer.provide(services), + ), + ) + failing.effect("starts a new watch when the previous one ended", () => + Effect.gen(function* () { + const fileWatch = yield* FileWatch.Service + yield* fileWatch.watch(file("a.ts")) + yield* Effect.repeat(Effect.yieldNow, { times: 10 }) + yield* fileWatch.watch(file("a.ts")) + expect(counts.subscribes).toBe(2) + }), + ) + + const failures = { subscribes: 0 } + const broken = testEffect( + FileWatch.layer().pipe( + Layer.provide( + Layer.succeed( + Watcher.Service, + Watcher.Service.of({ + subscribe: () => Effect.sync(() => (failures.subscribes++, Stream.die(new Error("watch failed")))), + }), + ), + ), + Layer.provide(services), + ), + ) + broken.effect("starts a new watch when the previous one failed", () => + Effect.gen(function* () { + const fileWatch = yield* FileWatch.Service + yield* fileWatch.watch(file("a.ts")) + yield* Effect.repeat(Effect.yieldNow, { times: 10 }) + yield* fileWatch.watch(file("a.ts")) + expect(failures.subscribes).toBe(2) + }), + ) +}) + +describe("FileWatch disabled", () => { + const disabled = testEffect( + FileWatch.layer({ enabled: false }).pipe(Layer.provide(Watcher.testLayer), Layer.provide(services)), + ) + disabled.effect("fails watch requests", () => + Effect.gen(function* () { + const fileWatch = yield* FileWatch.Service + const error = yield* fileWatch.watch(file("a.ts")).pipe(Effect.flip) + expect(error).toBeInstanceOf(FileWatch.DisabledError) + }), + ) +}) + +describeNative("FileWatch native", () => { + native.live("reports in-place edits and atomic temp + rename saves", () => + Effect.gen(function* () { + const fileWatch = yield* FileWatch.Service + const bus = yield* Bus.Service + const tmp = yield* tmpdirScoped() + const target = path.join(tmp.path, "a.ts") + yield* Effect.promise(() => fs.writeFile(target, "start")) + yield* fileWatch.watch({ location: { directory: AbsolutePath.make(tmp.path) }, file: target }) + + const changed = (write: () => Promise) => + Effect.gen(function* () { + const seen = yield* Deferred.make() + const listener = yield* bus.subscribe(FileSystem.Event.Changed).pipe( + Stream.filter((event) => event.data.file === target && event.data.event === "change"), + Stream.runForEach(() => Deferred.succeed(seen, undefined)), + Effect.forkScoped({ startImmediately: true }), + ) + // Native watches arm asynchronously, so repeat the write until one is seen. + const writes = yield* Effect.promise(write).pipe( + Effect.repeat(Schedule.spaced("20 millis")), + Effect.forkScoped({ startImmediately: true }), + ) + yield* Deferred.await(seen).pipe( + Effect.timeoutOrElse({ duration: "5 seconds", orElse: () => Effect.fail(new Error("no change event")) }), + Effect.ensuring(Fiber.interrupt(writes)), + Effect.ensuring(Fiber.interrupt(listener)), + ) + }) + + yield* changed(() => fs.writeFile(target, `inplace-${Math.random()}`)) + yield* changed(async () => { + const temp = path.join(tmp.path, `.a.ts.${Math.random().toString(36).slice(2)}.tmp`) + await fs.writeFile(temp, `atomic-${Math.random()}`) + await fs.rename(temp, target) + }) + }), + ) +}) diff --git a/packages/core/test/filesystem/watcher.test.ts b/packages/core/test/filesystem/watcher.test.ts index c880c31ec72..b50ac8b9978 100644 --- a/packages/core/test/filesystem/watcher.test.ts +++ b/packages/core/test/filesystem/watcher.test.ts @@ -54,6 +54,16 @@ function countingNative() { } describe("Watcher lifecycle", () => { + it.live("ends the stream when the watched directory is missing", () => + withTmp((directory) => + Effect.gen(function* () { + const watcher = yield* Watcher.Service + const updates = yield* watcher.subscribe({ path: path.join(directory, "missing", "a.ts"), type: "file" }) + expect(Option.isNone(yield* Stream.runHead(updates))).toBe(true) + }).pipe(Effect.provide(AppNodeBuilder.build(Watcher.node))), + ), + ) + it.effect("signals readiness after acquisition and buffers updates published by the ready callback", () => Effect.gen(function* () { const publish = yield* Deferred.make<(update: Watcher.Update) => void>() diff --git a/packages/protocol/openapi.json b/packages/protocol/openapi.json index e124f4505fb..ec858694639 100644 --- a/packages/protocol/openapi.json +++ b/packages/protocol/openapi.json @@ -9564,6 +9564,209 @@ } } }, + "/api/experimental/fs/watch": { + "post": { + "tags": ["filesystem"], + "operationId": "experimental.fs.watch", + "parameters": [ + { + "name": "location", + "in": "query", + "schema": { + "anyOf": [ + { + "type": "object", + "properties": { + "directory": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ] + } + }, + "additionalProperties": false + }, + { + "type": "null" + } + ] + }, + "required": false, + "style": "deepObject", + "explode": true + } + ], + "security": [], + "responses": { + "204": { + "description": "" + }, + "400": { + "description": "InvalidRequestError", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/InvalidRequestErrorEncoded" + } + } + } + }, + "401": { + "description": "UnauthorizedError", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/UnauthorizedErrorEncoded" + } + } + } + }, + "404": { + "description": "FileNotFoundError | LocationNotFoundError", + "content": { + "application/json": { + "schema": { + "anyOf": [ + { + "$ref": "#/components/schemas/FileNotFoundErrorEncoded" + }, + { + "$ref": "#/components/schemas/LocationNotFoundErrorEncoded" + } + ] + } + } + } + }, + "503": { + "description": "ServiceUnavailableError", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ServiceUnavailableErrorEncoded" + } + } + } + } + }, + "description": "Watch one file relative to the requested location for 24 hours, publishing filesystem.changed on that location when it changes. Watching an already watched file resets its expiry. Paths follow read confinement. Experimental: may change without compatibility guarantees.", + "summary": "Watch file", + "requestBody": { + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "path": { + "type": "string", + "description": "A file path relative to the requested location." + } + }, + "required": ["path"], + "additionalProperties": false + } + } + }, + "required": true + } + }, + "delete": { + "tags": ["filesystem"], + "operationId": "experimental.fs.unwatch", + "parameters": [ + { + "name": "location", + "in": "query", + "schema": { + "anyOf": [ + { + "type": "object", + "properties": { + "directory": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ] + } + }, + "additionalProperties": false + }, + { + "type": "null" + } + ] + }, + "required": false, + "style": "deepObject", + "explode": true + } + ], + "security": [], + "responses": { + "204": { + "description": "" + }, + "400": { + "description": "InvalidRequestError", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/InvalidRequestErrorEncoded" + } + } + } + }, + "401": { + "description": "UnauthorizedError", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/UnauthorizedErrorEncoded" + } + } + } + }, + "404": { + "description": "LocationNotFoundError", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/LocationNotFoundErrorEncoded" + } + } + } + } + }, + "description": "Stop watching one file relative to the requested location. Experimental: may change without compatibility guarantees.", + "summary": "Unwatch file", + "requestBody": { + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "path": { + "type": "string", + "description": "A file path relative to the requested location." + } + }, + "required": ["path"], + "additionalProperties": false + } + } + }, + "required": true + } + } + }, "/api/command": { "get": { "tags": ["command"], diff --git a/packages/protocol/src/groups/fs.ts b/packages/protocol/src/groups/fs.ts index 612e8d39211..f41fa74bc83 100644 --- a/packages/protocol/src/groups/fs.ts +++ b/packages/protocol/src/groups/fs.ts @@ -3,7 +3,7 @@ import { Location } from "@opencode/schema/location" import { PositiveInt } from "@opencode/schema/schema" import { Schema } from "effect" import { HttpApiEndpoint, HttpApiGroup, HttpApiSchema, OpenApi } from "effect/http-api" -import { FileNotFoundError } from "../errors.js" +import { FileNotFoundError, ServiceUnavailableError } from "../errors.js" import { LocationQuery, locationQueryOpenApi } from "./location.js" const ListQuery = Schema.Struct({ @@ -21,6 +21,10 @@ const WriteQuery = Schema.Struct({ }), }) +const WatchPayload = Schema.Struct({ + path: Schema.String.annotate({ description: "A file path relative to the requested location." }), +}) + const FindQuery = Schema.Struct({ ...LocationQuery.fields, query: FileSystem.FindInput.fields.query, @@ -91,6 +95,39 @@ export const FileSystemGroup = HttpApiGroup.make("server.fs") }), ), ) + .add( + HttpApiEndpoint.post("fs.watch", "/api/experimental/fs/watch", { + query: LocationQuery, + payload: WatchPayload, + success: HttpApiSchema.NoContent, + error: [FileNotFoundError, ServiceUnavailableError], + }) + .annotateMerge(locationQueryOpenApi) + .annotateMerge( + OpenApi.annotations({ + identifier: "experimental.fs.watch", + summary: "Watch file", + description: + "Watch one file relative to the requested location for 24 hours, publishing filesystem.changed on that location when it changes. Watching an already watched file resets its expiry. Paths follow read confinement. Experimental: may change without compatibility guarantees.", + }), + ), + ) + .add( + HttpApiEndpoint.delete("fs.unwatch", "/api/experimental/fs/watch", { + query: LocationQuery, + payload: WatchPayload, + success: HttpApiSchema.NoContent, + }) + .annotateMerge(locationQueryOpenApi) + .annotateMerge( + OpenApi.annotations({ + identifier: "experimental.fs.unwatch", + summary: "Unwatch file", + description: + "Stop watching one file relative to the requested location. Experimental: may change without compatibility guarantees.", + }), + ), + ) .annotateMerge( OpenApi.annotations({ title: "filesystem", diff --git a/packages/server/src/handlers/fs.ts b/packages/server/src/handlers/fs.ts index 11225d42b71..41d009a18ba 100644 --- a/packages/server/src/handlers/fs.ts +++ b/packages/server/src/handlers/fs.ts @@ -1,14 +1,18 @@ import { FileSystem } from "@opencode/core/filesystem" +import { FileWatch } from "@opencode/core/filesystem/file-watch" +import { Location } from "@opencode/core/location" import { RelativePath } from "@opencode/core/schema" -import { FileNotFoundError } from "@opencode/protocol/errors" +import { FileNotFoundError, ServiceUnavailableError } from "@opencode/protocol/errors" import { Effect, Option, Stream } from "effect" import { type HttpServerRequest, HttpServerResponse } from "effect/http" import { HttpApiBuilder } from "effect/http-api" +import path from "path" import { Api } from "../api" import { response } from "../location" export const FileSystemHandler = HttpApiBuilder.group(Api, "server.fs", (handlers) => Effect.gen(function* () { + const fileWatch = yield* FileWatch.Service return handlers .handleRaw("fs.read", (ctx) => Effect.gen(function* () { @@ -54,6 +58,39 @@ export const FileSystemHandler = HttpApiBuilder.group(Api, "server.fs", (handler }), ), ) + .handle("fs.watch", (ctx) => + Effect.gen(function* () { + const fs = yield* FileSystem.Service + const location = yield* Location.Service + // Same confinement as read: the file must exist inside the location. + yield* fs + .read({ path: RelativePath.make(ctx.payload.path) }) + .pipe( + Effect.mapError( + (error) => new FileNotFoundError({ path: error.path, message: `File not found: ${error.path}` }), + ), + ) + yield* fileWatch + .watch({ + location: { directory: location.directory, workspaceID: location.workspaceID }, + file: path.resolve(location.directory, ctx.payload.path), + }) + .pipe( + Effect.mapError( + (error) => new ServiceUnavailableError({ message: error.message, service: "filewatcher" }), + ), + ) + }), + ) + .handle("fs.unwatch", (ctx) => + Effect.gen(function* () { + const location = yield* Location.Service + yield* fileWatch.unwatch({ + location: { directory: location.directory, workspaceID: location.workspaceID }, + file: path.resolve(location.directory, ctx.payload.path), + }) + }), + ) }), ) diff --git a/packages/server/src/routes.ts b/packages/server/src/routes.ts index c0d08f90ff0..19122cfe789 100644 --- a/packages/server/src/routes.ts +++ b/packages/server/src/routes.ts @@ -34,6 +34,7 @@ import { SdkPlugins } from "@opencode/core/plugin/sdk" import { WellKnown } from "@opencode/core/wellknown" import { Workspace } from "@opencode/core/workspace" import { Watcher } from "@opencode/core/filesystem/watcher" +import { FileWatch } from "@opencode/core/filesystem/file-watch" import { HttpRouter } from "effect/http" import { HttpApiBuilder } from "effect/http-api" import { Context, Effect, Layer, Option } from "effect" @@ -77,6 +78,7 @@ const applicationServiceNodes = [ LocationActivity.node, SessionRestart.node, Workspace.node, + FileWatch.node, ] as const const applicationServices = LayerNode.group(applicationServiceNodes) @@ -122,6 +124,7 @@ function makeRoutes( App.node.replace(App.configured(options.app)), ModelsDev.node.replace(ModelsDev.configured(options.models)), Watcher.node.replace(Watcher.configured({ enabled: options.fs?.filewatcher })), + FileWatch.node.replace(FileWatch.configured({ enabled: options.fs?.filewatcher })), FileSystemSearch.node.replace(FileSystemSearch.configured({ fff: options.fs?.fff })), Global.node.replace(Global.layerWith(options.config?.directory ? { config: options.config.directory } : {})), Config.node.replace( diff --git a/packages/server/test/fs.test.ts b/packages/server/test/fs.test.ts index 75bb3305141..432ee5a2713 100644 --- a/packages/server/test/fs.test.ts +++ b/packages/server/test/fs.test.ts @@ -335,3 +335,32 @@ it.live("opens no file for HEAD and closes it when a download is aborted", () => ).toBe(0) }), ) + +it.live("confines watch requests like read and reports a disabled watcher", () => + Effect.gen(function* () { + const tmp = yield* tmpdirScoped() + const project = path.join(tmp.path, "project") + yield* Effect.promise(async () => { + await fs.mkdir(project) + await fs.writeFile(path.join(project, "a.ts"), "a") + await fs.writeFile(path.join(tmp.path, "outside.ts"), "outside") + }) + const server = yield* startServer(path.join(tmp.path, "config")) + const watch = (method: string, file: string) => + Effect.promise(async () => { + const url = new URL("/api/experimental/fs/watch", server.base) + url.searchParams.set("location[directory]", project) + const response = await fetch(url, { + method, + headers: { ...server.headers, "content-type": "application/json" }, + body: JSON.stringify({ path: file }), + }) + return response.status + }) + + expect(yield* watch("POST", "../outside.ts")).toBe(404) + expect(yield* watch("POST", "missing.ts")).toBe(404) + expect(yield* watch("POST", "a.ts")).toBe(503) + expect(yield* watch("DELETE", "a.ts")).toBe(204) + }), +) From f6991636a5fe816b4f54b13b7ea46c9d2304c00e Mon Sep 17 00:00:00 2001 From: Kit Langton Date: Sun, 25 Oct 2026 15:54:33 -0700 Subject: [PATCH 06/15] fix(client): register running sessions missed while disconnected (#53012) --- packages/client/src/solid/data.ts | 5 +++++ packages/client/test/solid-data.test.ts | 19 ++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/packages/client/src/solid/data.ts b/packages/client/src/solid/data.ts index 5115f667da0..a4fa31fc04d 100644 --- a/packages/client/src/solid/data.ts +++ b/packages/client/src/solid/data.ts @@ -652,6 +652,11 @@ export function createData(config: CreateDataInput) { }) activeUpdates = undefined setStore("session", "active", reconcile(Object.fromEntries(snapshot))) + // Sessions created before this connection (while disconnected, or before the client started) + // never delivered `session.created`; without their info a running child never joins its root's family. + snapshot.forEach((status, id) => { + if (status === "running" && !store.session.info[id]) refresh(() => result.session.sync(id)) + }) }) .catch(() => { if (activeUpdates === updates) activeUpdates = undefined diff --git a/packages/client/test/solid-data.test.ts b/packages/client/test/solid-data.test.ts index 7ee5d17795c..ba40318720a 100644 --- a/packages/client/test/solid-data.test.ts +++ b/packages/client/test/solid-data.test.ts @@ -1186,6 +1186,21 @@ test.each([ } }) +test("adds running sessions created while disconnected to their family", async () => { + const root = { ...session(0), id: "ses_root" } + const child = { ...session(0), id: "ses_child", parentID: "ses_root" } + const setup = activityFixture(() => Response.json({ data: { ses_child: { type: "running" } } }), [child]) + + try { + setup.data.session.remember(root) + setup.emit({ type: "server.connected", data: {} }) + await wait(() => setup.data.session.family("ses_root").includes("ses_child")) + expect(setup.data.session.status("ses_child")).toBe("running") + } finally { + setup.dispose() + } +}) + test("ignores activity snapshots from an older connection", async () => { const reads: ReturnType>[] = [] const setup = activityFixture(() => { @@ -1257,7 +1272,7 @@ function modelInfo(name: string): ModelInfo { } } -function activityFixture(read: () => Response | Promise) { +function activityFixture(read: () => Response | Promise, sessions: SessionInfo[] = []) { const listeners = new Set[0]>() const api = OpenCode.make({ baseUrl: "http://opencode.local", @@ -1265,6 +1280,8 @@ function activityFixture(read: () => Response | Promise) { const request = input instanceof Request ? input : new Request(input, init) const path = new URL(request.url).pathname if (path === "/api/session/active") return read() + const known = sessions.find((item) => path === `/api/session/${item.id}`) + if (known) return Response.json({ data: known }) if (path === "/api/project") return Response.json([]) if (path === "/api/location") return Response.json({ directory: "/project" }) return Response.json({ location: { directory: "/project" }, data: { branch: "main" } }) From 61163cb1341d562e48edcfbb0aeae2207bf0b392 Mon Sep 17 00:00:00 2001 From: Luke Parker <10430890+Hona@users.noreply.github.com> Date: Mon, 26 Oct 2026 08:55:28 +1000 Subject: [PATCH 07/15] fix(server): let agent and provider lists wait for plugins (#59082) Co-authored-by: Kit Langton --- .../client/src/promise/generated/client.ts | 2 +- packages/protocol/openapi.json | 10 +++++++ packages/protocol/src/groups/agent.ts | 3 +- packages/server/src/handlers/agent.ts | 3 +- packages/server/src/handlers/provider.ts | 3 +- packages/server/test/provider.test.ts | 30 ++++++++++++++----- 6 files changed, 39 insertions(+), 12 deletions(-) diff --git a/packages/client/src/promise/generated/client.ts b/packages/client/src/promise/generated/client.ts index d29a5268579..cb45acafb89 100644 --- a/packages/client/src/promise/generated/client.ts +++ b/packages/client/src/promise/generated/client.ts @@ -492,7 +492,7 @@ export function make(options: ClientOptions) { path: `/api/agent`, query: { location: input?.["location"] }, successStatus: 200, - declaredStatuses: [400, 401, 404], + declaredStatuses: [400, 401, 404, 503], empty: false, }, requestOptions, diff --git a/packages/protocol/openapi.json b/packages/protocol/openapi.json index ec858694639..48964f54d3e 100644 --- a/packages/protocol/openapi.json +++ b/packages/protocol/openapi.json @@ -359,6 +359,16 @@ } } } + }, + "503": { + "description": "ServiceUnavailableError", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ServiceUnavailableErrorEncoded" + } + } + } } }, "description": "Retrieve currently registered agents.", diff --git a/packages/protocol/src/groups/agent.ts b/packages/protocol/src/groups/agent.ts index 485d0ae37cb..eecf67af085 100644 --- a/packages/protocol/src/groups/agent.ts +++ b/packages/protocol/src/groups/agent.ts @@ -3,13 +3,14 @@ import { Location } from "@opencode/schema/location" import { Schema } from "effect" import { HttpApiEndpoint, HttpApiGroup, OpenApi } from "effect/http-api" import { LocationQuery, locationQueryOpenApi } from "./location.js" -import { AgentNotFoundError } from "../errors.js" +import { AgentNotFoundError, ServiceUnavailableError } from "../errors.js" export const AgentGroup = HttpApiGroup.make("server.agent") .add( HttpApiEndpoint.get("agent.list", "/api/agent", { query: LocationQuery, success: Location.response(Schema.Array(Agent.Info)), + error: ServiceUnavailableError, }) .annotateMerge(locationQueryOpenApi) .annotateMerge( diff --git a/packages/server/src/handlers/agent.ts b/packages/server/src/handlers/agent.ts index f54148af226..eff3bb9497a 100644 --- a/packages/server/src/handlers/agent.ts +++ b/packages/server/src/handlers/agent.ts @@ -2,13 +2,14 @@ import { Agent } from "@opencode/core/agent" import { Effect } from "effect" import { HttpApiBuilder } from "effect/http-api" import { Api } from "../api" -import { response } from "../location" +import { awaitPlugins, response } from "../location" import { AgentNotFoundError } from "@opencode/protocol/errors" export const AgentHandler = HttpApiBuilder.group(Api, "server.agent", (handlers) => handlers .handle("agent.list", () => Effect.gen(function* () { + yield* awaitPlugins return yield* response(Agent.Service.use((agent) => agent.list())) }), ) diff --git a/packages/server/src/handlers/provider.ts b/packages/server/src/handlers/provider.ts index ab9c2201a5e..3b0bdf4a495 100644 --- a/packages/server/src/handlers/provider.ts +++ b/packages/server/src/handlers/provider.ts @@ -3,7 +3,7 @@ import { ProviderNotFoundError } from "@opencode/protocol/errors" import { Effect } from "effect" import { HttpApiBuilder } from "effect/http-api" import { Api } from "../api" -import { response } from "../location" +import { awaitPlugins, response } from "../location" export const ProviderHandler = HttpApiBuilder.group(Api, "server.provider", (handlers) => Effect.gen(function* () { @@ -11,6 +11,7 @@ export const ProviderHandler = HttpApiBuilder.group(Api, "server.provider", (han .handle( "provider.list", Effect.fn(function* () { + yield* awaitPlugins const providers = yield* Provider.Service return yield* response(providers.available()) }), diff --git a/packages/server/test/provider.test.ts b/packages/server/test/provider.test.ts index 9da592289ee..4e132e7fd45 100644 --- a/packages/server/test/provider.test.ts +++ b/packages/server/test/provider.test.ts @@ -8,7 +8,7 @@ import { it } from "../../core/test/lib/effect" import { createRoutes } from "../src/routes" it.live( - "provider reads stay nonblocking while authentication discovery waits for plugins", + "agent and provider lists wait for plugins while single provider reads stay nonblocking", () => Effect.gen(function* () { const tmp = yield* tmpdirScoped("opencode-provider-endpoints-") @@ -32,6 +32,7 @@ it.live( models: { chat: {} }, }, }, + agents: { reviewer: { description: "Reviews changes" } }, }), }, }).pipe(Layer.provide(HttpServer.layerServices)), @@ -62,22 +63,35 @@ it.live( }), ) }) - // Config providers activate after SDK plugins; reads must return the current snapshot without waiting. - const list = yield* request("GET", "/api/provider").pipe(Effect.timeout("2 seconds")) + // Config providers and agents activate after SDK plugins, so the lists wait rather than answer with a partial + // catalog that a client would cache as loaded. + const providers = yield* request("GET", "/api/provider").pipe(Effect.forkScoped) + const agents = yield* request("GET", "/api/agent").pipe(Effect.forkScoped) yield* Deferred.await(started) const integrations = yield* request("GET", "/api/integration").pipe(Effect.forkScoped) - expect(list.status).toBe(200) - expect(yield* Effect.promise(() => list.json())).toMatchObject({ - location: { directory: tmp.path }, - data: expect.not.arrayContaining([expect.objectContaining({ id: "custom" })]), - }) const get = yield* request("GET", "/api/provider/custom").pipe(Effect.timeout("2 seconds")) expect(get.status).toBe(404) expect(yield* Effect.promise(() => get.json())).toMatchObject({ _tag: "ProviderNotFoundError", providerID: "custom", }) + expect(providers.pollUnsafe()).toBeUndefined() + expect(agents.pollUnsafe()).toBeUndefined() yield* Deferred.succeed(release, undefined) + const list = yield* Fiber.join(providers) + expect(list.status).toBe(200) + expect(yield* Effect.promise(() => list.json())).toMatchObject({ + location: { directory: tmp.path }, + data: expect.arrayContaining([expect.objectContaining({ id: "custom" })]), + }) + const agentList = yield* Fiber.join(agents) + expect(agentList.status).toBe(200) + expect(yield* Effect.promise(() => agentList.json())).toMatchObject({ + data: expect.arrayContaining([ + expect.objectContaining({ id: "build" }), + expect.objectContaining({ id: "reviewer" }), + ]), + }) const discovered = yield* Fiber.join(integrations) expect(discovered.status).toBe(200) expect(yield* Effect.promise(() => discovered.json())).toMatchObject({ From c8cf3a544f53283eec45bd353105ecc01d6249d2 Mon Sep 17 00:00:00 2001 From: Luke Parker <10430890+Hona@users.noreply.github.com> Date: Mon, 26 Oct 2026 08:57:36 +1000 Subject: [PATCH 08/15] fix(server): stop form polling from booting idle locations (#59083) Co-authored-by: Kit Langton --- packages/core/src/effect/app-node-builder.ts | 3 +- packages/core/src/instance/service.ts | 2 + packages/sdk/src/internal/instances.ts | 4 +- .../server/src/middleware/form-location.ts | 8 ++- packages/server/test/fetch.test.ts | 1 - packages/server/test/session-form.test.ts | 50 +++++++++++++++++++ 6 files changed, 62 insertions(+), 6 deletions(-) create mode 100644 packages/server/test/session-form.test.ts diff --git a/packages/core/src/effect/app-node-builder.ts b/packages/core/src/effect/app-node-builder.ts index dfefbf8d662..427b5259828 100644 --- a/packages/core/src/effect/app-node-builder.ts +++ b/packages/core/src/effect/app-node-builder.ts @@ -2,7 +2,7 @@ import { buildLocationServiceMap } from "../location-services.js" import { LocationServiceMap } from "../location-service-map.js" import { LayerNode } from "@opencode/util/effect/layer-node" import { makeGlobalNode } from "@opencode/util/effect/app-node" -import { Effect, Layer } from "effect" +import { Effect, Layer, RcMap } from "effect" import { Instance } from "../instance/service.js" const instances = makeGlobalNode({ @@ -13,6 +13,7 @@ const instances = makeGlobalNode({ const locations = yield* LocationServiceMap.Service return Instance.Service.of({ provide: (session) => Effect.provide(locations.get(session.location)), + live: (session) => RcMap.has(locations.rcMap, LocationServiceMap.canonical(session.location)), }) }), ), diff --git a/packages/core/src/instance/service.ts b/packages/core/src/instance/service.ts index f23d28f49f2..8299cfd16a2 100644 --- a/packages/core/src/instance/service.ts +++ b/packages/core/src/instance/service.ts @@ -12,6 +12,8 @@ export interface Interface { readonly provide: ( session: Session.Info, ) => (effect: Effect.Effect) => Effect.Effect> + /** Whether `provide` would reuse built or booting capabilities. Selectors without it are assumed live. */ + readonly live?: (session: Session.Info) => Effect.Effect } export class Service extends Context.Service()("@opencode/Instance") {} diff --git a/packages/sdk/src/internal/instances.ts b/packages/sdk/src/internal/instances.ts index ad2a6c6d460..f48440a6a5b 100644 --- a/packages/sdk/src/internal/instances.ts +++ b/packages/sdk/src/internal/instances.ts @@ -7,7 +7,7 @@ import type { InstancePlugins } from "@opencode/core/plugin/instance" import type { Session } from "@opencode/schema/session" import { makeGlobalNode } from "@opencode/util/effect/app-node" import type { LayerNode } from "@opencode/util/effect/layer-node" -import { Context, Duration, Effect, Layer, LayerMap, Scope } from "effect" +import { Context, Duration, Effect, Layer, LayerMap, RcMap, Scope } from "effect" export interface Configuration { readonly plugins: InstancePlugins.List @@ -101,7 +101,7 @@ export function layer(options: Options, replacements: () => LayerNode.Replacemen ), { idleTimeToLive: Duration.infinity }, ) - return Instance.Service.of({ provide }) + return Instance.Service.of({ provide, live: (session) => RcMap.has(instances.rcMap, key(session)) }) }), ) } diff --git a/packages/server/src/middleware/form-location.ts b/packages/server/src/middleware/form-location.ts index b975d1aacfb..3bf93b33f66 100644 --- a/packages/server/src/middleware/form-location.ts +++ b/packages/server/src/middleware/form-location.ts @@ -3,7 +3,7 @@ import { LocationServiceMap } from "@opencode/core/location-services" import { Session } from "@opencode/core/session" import { LocationNotFoundError, InvalidRequestError, SessionNotFoundError } from "@opencode/protocol/errors" import { Effect, Layer } from "effect" -import { HttpRouter, HttpServerRequest } from "effect/http" +import { HttpRouter, HttpServerRequest, HttpServerResponse } from "effect/http" import { HttpApiMiddleware } from "effect/http-api" import { locationErrors, requestRef, sessionInfo, type LocationServices } from "../location" @@ -21,7 +21,7 @@ export const formLocationLayer = Layer.effect( const instances = yield* Instance.Service const locations = yield* LocationServiceMap.Service - return FormLocationMiddleware.of((effect) => + return FormLocationMiddleware.of((effect, options) => Effect.gen(function* () { const route = yield* HttpRouter.RouteContext if (route.params.sessionID === "global") { @@ -33,6 +33,10 @@ export const formLocationLayer = Layer.effect( } const session = yield* sessionInfo(sessions, route.params.sessionID) + // Forms live in memory, so a Session without live capabilities has none pending. Listing must + // not boot its Location: clients poll whole subagent trees, including deleted worktrees. + if (options.endpoint.identifier === "session.form.list" && instances.live && !(yield* instances.live(session))) + return HttpServerResponse.jsonUnsafe({ data: [] }) return yield* effect.pipe(instances.provide(session), locationErrors) }), ) diff --git a/packages/server/test/fetch.test.ts b/packages/server/test/fetch.test.ts index 35307b96532..56f8a1dbf96 100644 --- a/packages/server/test/fetch.test.ts +++ b/packages/server/test/fetch.test.ts @@ -42,7 +42,6 @@ it.live("returns LocationNotFoundError for a missing folder and recovers once it "/api/model", "/api/integration", `/api/session/${session.id}/permission`, - `/api/session/${session.id}/form`, "/api/session/global/form", ] for (const endpoint of endpoints) { diff --git a/packages/server/test/session-form.test.ts b/packages/server/test/session-form.test.ts new file mode 100644 index 00000000000..1f22d8ffa07 --- /dev/null +++ b/packages/server/test/session-form.test.ts @@ -0,0 +1,50 @@ +import { expect } from "bun:test" +import fs from "fs/promises" +import { Effect } from "effect" +import { tmpdirScoped } from "../../core/test/fixture/tmpdir" +import { it } from "../../core/test/lib/effect" +import { ServerFetch } from "../src/fetch" + +it.live("lists no forms for a Session whose Location is not live, and pending forms once it is", () => + Effect.gen(function* () { + const handler = yield* ServerFetch.make({ + app: { version: "test" }, + database: { path: ":memory:" }, + fs: { filewatcher: false }, + models: { fetch: false }, + }) + const request = (path: string, body?: unknown) => + Effect.promise(async () => { + const response = await handler( + new Request(`http://opencode.local${path}`, { + method: body === undefined ? "GET" : "POST", + headers: { "content-type": "application/json" }, + body: body === undefined ? undefined : JSON.stringify(body), + }), + ) + return { status: response.status, body: await response.json() } + }) + const create = (directory: string) => + request("/api/session", { location: { directory } }).pipe( + Effect.map((response) => (response.body as { data: { id: string } }).data.id), + ) + + // A subagent whose worktree was deleted must not boot or 404. + const deleted = yield* tmpdirScoped() + const orphan = yield* create(deleted.path) + yield* Effect.promise(() => fs.rm(deleted.path, { recursive: true, force: true })) + expect(yield* request(`/api/session/${orphan}/form`)).toEqual({ status: 200, body: { data: [] } }) + + const directory = yield* tmpdirScoped() + const live = yield* create(directory.path) + const created = yield* request(`/api/session/${live}/form`, { + title: "Pick one", + fields: [{ key: "answer", type: "string" }], + }) + expect(created.status).toBe(200) + expect(yield* request(`/api/session/${live}/form`)).toEqual({ + status: 200, + body: { data: [(created.body as { data: unknown }).data] }, + }) + }).pipe(Effect.scoped), +) From 01cd723db9e5762c32b1be154a49e283aab8ddb8 Mon Sep 17 00:00:00 2001 From: Luke Parker <10430890+Hona@users.noreply.github.com> Date: Mon, 26 Oct 2026 08:58:29 +1000 Subject: [PATCH 09/15] fix(core): publish catalog updates only when they change (#59084) Co-authored-by: Kit Langton --- packages/core/src/agent.ts | 42 +++--- packages/core/src/command.ts | 15 ++- packages/core/src/integration.ts | 8 +- packages/core/src/plugin.ts | 14 +- packages/core/src/reference.ts | 7 +- packages/core/src/skill.ts | 5 +- packages/core/src/state.ts | 19 +++ packages/core/src/websearch.ts | 13 +- packages/core/test/config/reload.test.ts | 159 +++++++++++++++++++++++ packages/core/test/config/skill.test.ts | 3 +- packages/core/test/state.test.ts | 31 +++++ 11 files changed, 273 insertions(+), 43 deletions(-) diff --git a/packages/core/src/agent.ts b/packages/core/src/agent.ts index d574e7528b0..5100b353e11 100644 --- a/packages/core/src/agent.ts +++ b/packages/core/src/agent.ts @@ -50,6 +50,24 @@ const layer = Layer.effect( Service, Effect.gen(function* () { const bus = yield* Bus.Service + const selectable = (agent: Info | undefined) => + agent && agent.mode !== "subagent" && !agent.hidden ? agent : undefined + const selectedDefault = (data: Data) => { + const configured = data.default ? selectable(data.agents.get(data.default)) : undefined + if (configured) return configured + const build = selectable(data.agents.get(defaultID)) + if (build) return build + for (const agent of data.agents.values()) { + const fallback = selectable(agent) + if (fallback) return fallback + } + } + const list = (data: Data) => { + const agents = Array.fromIterable(data.agents.values()) + const selected = selectedDefault(data) + if (!selected) return agents + return [selected, ...agents.filter((agent) => agent.id !== selected.id)] + } const state = State.create({ name: "agent", initial: () => ({ agents: new Map() }), @@ -69,21 +87,8 @@ const layer = Layer.effect( editor.agents.delete(id) }, }), - notify: () => bus.publish(Agent.Event.Updated, {}).pipe(Effect.asVoid), + notify: State.changes(list, () => bus.publish(Agent.Event.Updated, {}).pipe(Effect.asVoid)), }) - const selectable = (agent: Info | undefined) => - agent && agent.mode !== "subagent" && !agent.hidden ? agent : undefined - const selectedDefault = () => { - const data = state.get() - const configured = data.default ? selectable(data.agents.get(data.default)) : undefined - if (configured) return configured - const build = selectable(data.agents.get(defaultID)) - if (build) return build - for (const agent of data.agents.values()) { - const fallback = selectable(agent) - if (fallback) return fallback - } - } return Service.of({ transform: state.transform, @@ -93,18 +98,15 @@ const layer = Layer.effect( }), resolve: Effect.fnUntraced(function* (id) { if (id !== undefined) return state.get().agents.get(ID.make(id)) - return selectedDefault() + return selectedDefault(state.get()) }), select: Effect.fn("Agent.select")(function* (id) { if (id !== undefined) return { id, info: state.get().agents.get(id) } - const info = selectedDefault() + const info = selectedDefault(state.get()) return { id: info?.id ?? defaultID, info } }), list: Effect.fn("Agent.list")(function* () { - const agents = Array.fromIterable(state.get().agents.values()) - const selected = selectedDefault() - if (!selected) return agents - return [selected, ...agents.filter((agent) => agent.id !== selected.id)] + return list(state.get()) }), }) }), diff --git a/packages/core/src/command.ts b/packages/core/src/command.ts index 8cf1b06bcfb..5678fafbe51 100644 --- a/packages/core/src/command.ts +++ b/packages/core/src/command.ts @@ -54,19 +54,22 @@ export const layer = Layer.effect( Service, Effect.gen(function* () { const bus = yield* Bus.Service + const info = (definition: Definition) => + Info.make({ + name: definition.name, + description: definition.description, + }) const state = State.create, Editor>({ name: "command", initial: () => new Map(), editor: (editor) => ({ add: (definition) => editor.set(definition.name, definition), }), - notify: () => bus.publish(Command.Event.Updated, {}).pipe(Effect.asVoid), + notify: State.changes( + (commands) => Array.from(commands.values(), info), + () => bus.publish(Command.Event.Updated, {}).pipe(Effect.asVoid), + ), }) - const info = (definition: Definition) => - Info.make({ - name: definition.name, - description: definition.description, - }) return Service.of({ reload: state.reload, diff --git a/packages/core/src/integration.ts b/packages/core/src/integration.ts index dc929331db3..d8fc7ed8f60 100644 --- a/packages/core/src/integration.ts +++ b/packages/core/src/integration.ts @@ -376,7 +376,13 @@ const layer = Layer.effect( }, }, }), - notify: () => bus.publish(Integration.Event.Updated, {}).pipe(Effect.asVoid), + notify: State.changes( + (data) => + Array.from(data.integrations.values(), (entry) => project(entry, resolveConnections(entry, []))).toSorted( + (a, b) => a.name.localeCompare(b.name), + ), + () => bus.publish(Integration.Event.Updated, {}).pipe(Effect.asVoid), + ), }) const createCredential = Effect.fnUntraced(function* (input: Parameters[0]) { diff --git a/packages/core/src/plugin.ts b/packages/core/src/plugin.ts index 94ca4c0463d..f94867388d2 100644 --- a/packages/core/src/plugin.ts +++ b/packages/core/src/plugin.ts @@ -40,6 +40,10 @@ const layer = Layer.effect( const pendingFailures = yield* Queue.unbounded() let discovered: readonly Failure[] = [] let inventory: Plugin.Info[] = [] + const publish = State.changes( + (items: readonly Plugin.Info[]) => items, + () => bus.publish(Plugin.Event.Updated, {}).pipe(Effect.asVoid), + ) const list = Effect.fn("Plugin.list")(function* () { return inventory }) @@ -115,10 +119,8 @@ const layer = Layer.effect( if (entry) active.set(definition.id, { ...entry, plugin: definition }) } if (prefix === definitions.length && active.size === definitions.length) { - const nextInventory = [...Array.from(active.values()).map(slotInfo), ...failures] - if (JSON.stringify(inventory) === JSON.stringify(nextInventory)) return - inventory = nextInventory - yield* bus.publish(Plugin.Event.Updated, {}) + inventory = [...Array.from(active.values()).map(slotInfo), ...failures] + yield* publish(inventory) return } @@ -172,7 +174,7 @@ const layer = Layer.effect( inventory = [...Array.from(active.values()).map(slotInfo), ...failures] }), ) - yield* bus.publish(Plugin.Event.Updated, {}) + yield* publish(inventory) }), ), (release) => release, @@ -195,7 +197,7 @@ const layer = Layer.effect( // cannot disable a replacement and teardown need not wait for this worker. inventory = [...Array.from(active.values()).map(slotInfo), ...discovered] const refreshed = yield* State.batch(item.refresh).pipe(Effect.exit) - yield* bus.publish(Plugin.Event.Updated, {}) + yield* publish(inventory) if (Exit.isFailure(refreshed)) yield* Effect.logWarning("failed to refresh state after disabling plugin", { "plugin.id": item.plugin.id, diff --git a/packages/core/src/reference.ts b/packages/core/src/reference.ts index d567abd5f5d..c1bdcb376ef 100644 --- a/packages/core/src/reference.ts +++ b/packages/core/src/reference.ts @@ -48,8 +48,8 @@ const layer = Layer.effect( const bus = yield* Bus.Service const cache = yield* RepositoryCache.Service const scope = yield* Scope.Scope - const list = (): Info[] => - Array.from(state.get().sources).flatMap(([name, source]) => { + const list = (data: Data = state.get()): Info[] => + Array.from(data.sources).flatMap(([name, source]) => { const info = { name, source, @@ -101,11 +101,12 @@ const layer = Layer.effect( list: () => Array.from(editor.sources), get: (name) => editor.sources.get(name), }), - notify: () => + notify: State.changes(list, () => Effect.gen(function* () { yield* refresh().pipe(Effect.forkIn(scope)) yield* bus.publish(Reference.Event.Updated, {}) }), + ), }) // Check independently of session activity; the shared cache throttles Git work daily. diff --git a/packages/core/src/skill.ts b/packages/core/src/skill.ts index 8f7909e0e2e..e0d7981d15a 100644 --- a/packages/core/src/skill.ts +++ b/packages/core/src/skill.ts @@ -117,7 +117,10 @@ const layer = Layer.effect( editor.skills.delete(ID.make(id)) }, }), - notify: () => bus.publish(Skill.Event.Updated, {}).pipe(Effect.asVoid), + notify: State.changes( + (data) => Array.from(data.skills.values()), + () => bus.publish(Skill.Event.Updated, {}).pipe(Effect.asVoid), + ), }) return Service.of({ diff --git a/packages/core/src/state.ts b/packages/core/src/state.ts index eafee2696e5..2d1690ecec4 100644 --- a/packages/core/src/state.ts +++ b/packages/core/src/state.ts @@ -126,6 +126,25 @@ export function reconcile( }) } +/** + * Runs `notify` only when the serialized projection of `project(state)` differs from the last + * notified value. The first notification always runs so a freshly booted Location announces its + * current catalog even when empty. + */ +export function changes( + project: (state: State) => unknown, + notify: (state: State) => Effect.Effect, +): (state: State) => Effect.Effect { + let published: string | undefined + return (state) => + Effect.suspend(() => { + const next = JSON.stringify(project(state)) ?? "" + if (next === published) return Effect.void + published = next + return notify(state) + }) +} + export const inherit = Effect.fnUntraced(function* () { const batch = yield* CurrentBatch return (effect: Effect.Effect) => Effect.provideService(effect, CurrentBatch, batch) diff --git a/packages/core/src/websearch.ts b/packages/core/src/websearch.ts index dc37dd45c05..f8f8dd3073b 100644 --- a/packages/core/src/websearch.ts +++ b/packages/core/src/websearch.ts @@ -99,6 +99,12 @@ const layer = Layer.effect( ), Effect.forkScoped({ startImmediately: true }), ) + const providers = (data: Data) => + Array.from(data.providers.values(), (provider) => ({ + id: provider.id, + name: provider.name, + })).toSorted((a, b) => a.name.localeCompare(b.name)) + const state = State.create({ initial: () => ({ providers: new Map() }), editor: (editor) => ({ @@ -108,7 +114,7 @@ const layer = Layer.effect( set: (selection) => (editor.selection = selection), }, }), - notify: () => bus.publish(WebSearch.Event.Updated, {}).pipe(Effect.asVoid), + notify: State.changes(providers, () => bus.publish(WebSearch.Event.Updated, {}).pipe(Effect.asVoid)), }) const requireProvider = (providers: Map, providerID: ID) => { @@ -162,10 +168,7 @@ const layer = Layer.effect( transform: state.transform, reload: state.reload, providers: Effect.fn("WebSearch.providers")(function* () { - return Array.from(state.get().providers.values(), (provider) => ({ - id: provider.id, - name: provider.name, - })).toSorted((a, b) => a.name.localeCompare(b.name)) + return providers(state.get()) }), default: Effect.fn("WebSearch.defaultInfo")(function* () { const provider = yield* defaultProvider(yield* selection()) diff --git a/packages/core/test/config/reload.test.ts b/packages/core/test/config/reload.test.ts index f6acae19fc7..7fd0de8abca 100644 --- a/packages/core/test/config/reload.test.ts +++ b/packages/core/test/config/reload.test.ts @@ -8,10 +8,12 @@ import { Command } from "@opencode/core/command" import { Config } from "@opencode/core/config" import { ConfigAgentPlugin } from "@opencode/core/config/plugin/agent" import { ConfigCommandPlugin } from "@opencode/core/config/plugin/command" +import { ConfigPolicyPlugin } from "@opencode/core/config/plugin/policy" import { ConfigProviderPlugin } from "@opencode/core/config/plugin/provider" import { ConfigReferencePlugin } from "@opencode/core/config/plugin/reference" import { ConfigCompatibilityPlugin } from "@opencode/core/config/plugin/compatibility" import { ConfigSkillPlugin } from "@opencode/core/config/plugin/skill" +import { ConfigWebSearchPlugin } from "@opencode/core/config/plugin/websearch" import { Bus } from "@opencode/core/bus" import { Integration } from "@opencode/core/integration" import { Plugin } from "@opencode/core/plugin" @@ -20,6 +22,8 @@ import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder" import { Provider } from "@opencode/core/provider" import { Reference } from "@opencode/core/reference" import { Skill } from "@opencode/core/skill" +import { State } from "@opencode/core/state" +import { WebSearch } from "@opencode/core/websearch" import { ShellSelect } from "@opencode/core/shell/select" import { Job } from "@opencode/core/job" import { SubagentJob } from "@opencode/core/session/subagent-job" @@ -323,6 +327,161 @@ describe("config plugin reloads", () => { Effect.provideService(Global.Service, Global.Service.of(Global.make())), ), ) + + it.live("publishes catalog updates on boot and real changes but skips unchanged reloads", () => + Effect.gen(function* () { + const bus = yield* Bus.Service + const integrations = yield* Integration.Service + const plugins = yield* Plugin.Service + const websearch = yield* WebSearch.Service + const baseHost = yield* PluginHost.make(plugins) + const test = yield* Config.Test + const tracked = new Set([ + Agent.Event.Updated.type, + Command.Event.Updated.type, + Skill.Event.Updated.type, + Reference.Event.Updated.type, + WebSearch.Event.Updated.type, + Integration.Event.Updated.type, + Credential.Event.Updated.type, + Credential.Event.Switched.type, + ]) + const counts = new Map() + const reloads = { agent: 0, command: 0, skill: 0, reference: 0, websearch: 0, integration: 0 } + const count = (type: string) => counts.get(type) ?? 0 + yield* Effect.acquireRelease( + bus.listen((event) => + Effect.sync(() => { + if (!tracked.has(event.type)) return + counts.set(event.type, count(event.type) + 1) + }), + ), + (unsubscribe) => unsubscribe, + ) + const host: typeof baseHost = { + ...baseHost, + agent: { + ...baseHost.agent, + reload: () => baseHost.agent.reload().pipe(Effect.tap(() => Effect.sync(() => void reloads.agent++))), + }, + command: { + ...baseHost.command, + reload: () => baseHost.command.reload().pipe(Effect.tap(() => Effect.sync(() => void reloads.command++))), + }, + skill: { + ...baseHost.skill, + reload: () => baseHost.skill.reload().pipe(Effect.tap(() => Effect.sync(() => void reloads.skill++))), + }, + reference: { + ...baseHost.reference, + reload: () => baseHost.reference.reload().pipe(Effect.tap(() => Effect.sync(() => void reloads.reference++))), + }, + websearch: { + ...baseHost.websearch, + reload: () => baseHost.websearch.reload().pipe(Effect.tap(() => Effect.sync(() => void reloads.websearch++))), + }, + integration: { + ...baseHost.integration, + reload: () => + baseHost.integration.reload().pipe(Effect.tap(() => Effect.sync(() => void reloads.integration++))), + }, + } + + let providerName = "First search" + yield* State.batch( + Effect.gen(function* () { + yield* websearch.transform((editor) => + editor.add({ id: WebSearch.ID.make("search"), name: providerName, execute: () => Effect.succeed([]) }), + ) + yield* ConfigAgentPlugin.Plugin.effect(host) + yield* ConfigCommandPlugin.Plugin.effect(host) + yield* ConfigSkillPlugin.Plugin.effect(host) + yield* ConfigPolicyPlugin.Plugin.effect(host) + yield* ConfigReferencePlugin.Plugin.effect(host) + yield* ConfigWebSearchPlugin.Plugin.effect(host) + yield* ConfigProviderPlugin.Plugin.effect(host) + }), + ) + expect(Object.fromEntries(counts)).toEqual({ + [Agent.Event.Updated.type]: 1, + [Command.Event.Updated.type]: 1, + [Skill.Event.Updated.type]: 1, + [Reference.Event.Updated.type]: 1, + [WebSearch.Event.Updated.type]: 1, + [Integration.Event.Updated.type]: 1, + }) + + counts.clear() + yield* Effect.yieldNow + yield* bus.publish(Event.Updated, {}) + yield* waitUntil( + Effect.sync( + () => + reloads.agent === 1 && + reloads.command === 1 && + reloads.skill === 2 && + reloads.reference === 1 && + reloads.websearch === 1 && + reloads.integration === 1, + ), + ) + expect(Object.fromEntries(counts)).toEqual({}) + + yield* integrations.connection.key({ + integrationID: Integration.ID.make("first"), + key: "sk-test", + label: "Work", + }) + const connected = yield* integrations.get(Integration.ID.make("first")) + expect(connected?.connections).toMatchObject([{ type: "credential", label: "Work", method: "key" }]) + expect(Object.fromEntries(counts)).toEqual({ + [Credential.Event.Updated.type]: 1, + [Credential.Event.Switched.type]: 1, + }) + + counts.clear() + yield* bus.publish(Event.Updated, {}) + yield* waitUntil( + Effect.sync( + () => + reloads.agent === 2 && + reloads.command === 2 && + reloads.skill === 4 && + reloads.reference === 2 && + reloads.websearch === 2 && + reloads.integration === 2, + ), + ) + expect(Object.fromEntries(counts)).toEqual({}) + + yield* test.setEntries([config("second")]) + providerName = "Second search" + yield* host.websearch.reload() + yield* bus.publish(Event.Updated, {}) + yield* waitUntil( + Effect.sync( + () => + reloads.agent === 3 && + reloads.command === 3 && + reloads.skill === 6 && + reloads.reference === 3 && + reloads.websearch === 4 && + reloads.integration === 3, + ), + ) + expect(Object.fromEntries(counts)).toEqual({ + [Agent.Event.Updated.type]: 1, + [Command.Event.Updated.type]: 1, + [Skill.Event.Updated.type]: 1, + [Reference.Event.Updated.type]: 1, + [WebSearch.Event.Updated.type]: 1, + [Integration.Event.Updated.type]: 1, + }) + }).pipe( + Effect.provide(Config.testLayer([config("first")])), + Effect.provideService(Global.Service, Global.Service.of(Global.make())), + ), + ) }) function liveConfig(directory: string, native?: Watcher.NativeInterface, options: Config.Options = { global: false }) { diff --git a/packages/core/test/config/skill.test.ts b/packages/core/test/config/skill.test.ts index 5568eda0f41..4dc455b8446 100644 --- a/packages/core/test/config/skill.test.ts +++ b/packages/core/test/config/skill.test.ts @@ -415,7 +415,8 @@ describe("ConfigSkillPlugin.Plugin", () => { expect(yield* watcher.subscriptions()).toEqual([{ path: path.join(tmp.path, "generated"), type: "file" }]) yield* Effect.promise(() => fs.mkdir(path.join(tmp.path, "generated"))) - yield* emitAndWait({ type: "create", path: path.join(tmp.path, "generated") }) + yield* watcher.emit({ type: "create", path: path.join(tmp.path, "generated") }) + while ((yield* watcher.subscriptions()).at(-1)?.path !== source) yield* Effect.sleep("10 millis") yield* Effect.promise(async () => { await fs.mkdir(path.join(source, "deploy"), { recursive: true }) await write(source, "deploy", "Deploy") diff --git a/packages/core/test/state.test.ts b/packages/core/test/state.test.ts index 1a73acf2891..0fc0f325c63 100644 --- a/packages/core/test/state.test.ts +++ b/packages/core/test/state.test.ts @@ -202,6 +202,37 @@ describe("State", () => { expect(finalized).toBe(2) }), ) + + it.effect("publishes the initial projection and skips unchanged reloads", () => + Effect.gen(function* () { + let notifications = 0 + let label = "first" + const state = State.create({ + initial: () => ({ items: [] as { name: string; execute: () => void }[] }), + editor: (editor) => ({ + add: (name: string) => editor.items.push({ name, execute: () => {} }), + }), + notify: State.changes( + (data) => data.items.map((item) => ({ name: item.name })), + () => Effect.sync(() => void notifications++), + ), + }) + + yield* state.transform(() => {}) + expect(notifications).toBe(1) + yield* state.reload() + expect(notifications).toBe(1) + + yield* state.transform((editor) => editor.add(label)) + expect(notifications).toBe(2) + yield* state.reload() + expect(notifications).toBe(2) + + label = "second" + yield* state.reload() + expect(notifications).toBe(3) + }), + ) }) describe("State rebuild", () => { From eba3399588ebe653783725e78bf9ce2902af131a Mon Sep 17 00:00:00 2001 From: Luke Parker <10430890+Hona@users.noreply.github.com> Date: Mon, 26 Oct 2026 09:03:21 +1000 Subject: [PATCH 10/15] fix(core): reload compatibility skills only for their own sources (#59086) Co-authored-by: Kit Langton --- .../core/src/config/plugin/compatibility.ts | 21 +++++++- packages/core/test/config/skill.test.ts | 50 +++++++++++++++++++ 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/packages/core/src/config/plugin/compatibility.ts b/packages/core/src/config/plugin/compatibility.ts index 2ca898ace73..d1d97513a30 100644 --- a/packages/core/src/config/plugin/compatibility.ts +++ b/packages/core/src/config/plugin/compatibility.ts @@ -20,6 +20,8 @@ export const Plugin = define({ const changes = yield* PubSub.sliding(1) const lock = Semaphore.makeUnsafe(1) const skills: Skill.Info[] = [] + const compatibility = () => + config.compatibility ? config.compatibility() : Effect.succeed({ claude: [], agents: [] }) const watch = Effect.fn("ConfigCompatibilityPlugin.watch")(function* ( target: string, @@ -39,7 +41,7 @@ export const Plugin = define({ const refresh = Effect.fn("ConfigCompatibilityPlugin.refresh")( function* () { yield* FiberMap.clear(watches) - const roots = config.compatibility ? yield* config.compatibility() : { claude: [], agents: [] } + const roots = yield* compatibility() const directories = [...roots.claude, ...roots.agents].map((root) => path.join(root, "skills")) const loaded = new Map() for (const directory of directories) { @@ -66,7 +68,10 @@ export const Plugin = define({ Effect.forkScoped({ startImmediately: true }), ) yield* config.changes().pipe( - Stream.runForEach(() => reload), + Stream.filterEffect((update) => + Effect.map(compatibility(), (roots) => isCompatibilitySource(roots, update.path)), + ), + Stream.runForEach((update) => PubSub.publish(changes, update.path).pipe(Effect.asVoid)), Effect.forkScoped({ startImmediately: true }), ) yield* refresh() @@ -75,3 +80,15 @@ export const Plugin = define({ }) }), }) + +const sourceDirectories = [".claude", ".agents"] as const + +function isCompatibilitySource( + roots: { readonly claude: readonly string[]; readonly agents: readonly string[] }, + file: string, +) { + return ( + sourceDirectories.some((name) => path.basename(file) === name) || + [...roots.claude, ...roots.agents].some((root) => FSUtil.contains(root, file)) + ) +} diff --git a/packages/core/test/config/skill.test.ts b/packages/core/test/config/skill.test.ts index 4dc455b8446..66d0ef6c16e 100644 --- a/packages/core/test/config/skill.test.ts +++ b/packages/core/test/config/skill.test.ts @@ -21,6 +21,7 @@ import { Skill } from "@opencode/core/skill" import { SkillDiscovery } from "@opencode/core/skill/discovery" import { WellKnown } from "@opencode/core/wellknown" import { emptyCredentialNode, emptyWellknownNode } from "../fixture/config-nodes" +import { advance, drain } from "../lib/clock" import { tmpdir } from "../fixture/tmpdir" import { location } from "../fixture/location" import { testEffect } from "../lib/effect" @@ -427,4 +428,53 @@ describe("ConfigSkillPlugin.Plugin", () => { ), ), ) + + it.effect("ignores unrelated config updates and coalesces compatibility bursts into one reload", () => + Effect.acquireDisposable(Effect.promise(() => tmpdir())).pipe( + Effect.flatMap((tmp) => + Effect.gen(function* () { + const claude = path.join(tmp.path, ".claude") + const skills = path.join(claude, "skills") + const service = yield* Skill.Service + const configTest = yield* Config.Test + let reloads = 0 + yield* ConfigCompatibilityPlugin.Plugin.effect( + host({ + skill: { + list: () => Effect.die("unused skill.list"), + transform: service.transform, + reload: () => service.reload().pipe(Effect.tap(() => Effect.sync(() => reloads++))), + }, + }), + ) + yield* Effect.yieldNow + + yield* configTest.emitChange({ type: "update", path: path.join(tmp.path, "opencode.json") }) + yield* configTest.emitChange({ type: "create", path: path.join(tmp.path, "commands", "review.md") }) + yield* configTest.emitChange({ type: "create", path: path.join(tmp.path, "agents", "reviewer.md") }) + yield* drain + expect(reloads).toBe(0) + + yield* Effect.promise(async () => { + await fs.mkdir(path.join(skills, "review"), { recursive: true }) + await write(skills, "review", "Review once") + }) + yield* configTest.emitChange({ type: "create", path: claude }) + yield* configTest.emitChange({ type: "create", path: path.join(skills, "review", "SKILL.md") }) + yield* configTest.emitChange({ type: "update", path: path.join(skills, "review", "SKILL.md") }) + yield* advance(() => reloads >= 1) + yield* drain + expect(reloads).toBe(1) + expect((yield* service.list()).find((item) => item.id === "review")?.description).toBe("Review once") + }).pipe( + Effect.provide( + Config.testLayer( + [new Directory({ type: "directory", path: AbsolutePath.make(tmp.path) })], + { claude: [AbsolutePath.make(path.join(tmp.path, ".claude"))], agents: [] }, + ), + ), + ), + ), + ), + ) }) From c156d3c5e018610586f729409d24cc5e3194d9b4 Mon Sep 17 00:00:00 2001 From: Luke Parker <10430890+Hona@users.noreply.github.com> Date: Mon, 26 Oct 2026 09:03:25 +1000 Subject: [PATCH 11/15] fix(core): canonicalize location in activity tracking (#59087) Co-authored-by: Kit Langton --- packages/core/src/location-activity.ts | 5 ++-- packages/core/test/location-activity.test.ts | 26 ++++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/packages/core/src/location-activity.ts b/packages/core/src/location-activity.ts index c98319027a2..7aa3ed68dc9 100644 --- a/packages/core/src/location-activity.ts +++ b/packages/core/src/location-activity.ts @@ -51,9 +51,8 @@ export function layer(options: { readonly timeToLive?: Duration.Input; readonly ) const unsubscribe = yield* bus.listen((event) => { - if (!isSessionEvent(event)) return Effect.void - const location = event.location - if (!location) return Effect.void + if (!isSessionEvent(event) || !event.location) return Effect.void + const location = LocationServiceMap.canonical(event.location) return RcMap.has(locations.rcMap, location).pipe( Effect.flatMap((active) => (active ? touch(location) : Effect.void)), ) diff --git a/packages/core/test/location-activity.test.ts b/packages/core/test/location-activity.test.ts index e7a10ed1517..7f20d05704c 100644 --- a/packages/core/test/location-activity.test.ts +++ b/packages/core/test/location-activity.test.ts @@ -286,4 +286,30 @@ describe("LocationActivity eviction", () => { ) }), ) + + it.effect("refreshes inactivity deadline for session events with non-canonical location refs", () => + Effect.gen(function* () { + const bus = yield* Bus.Service + const map = yield* LocationServiceMap.Service + const raw = Location.Ref.make({ directory: AbsolutePath.make("/project") }) + const ref = LocationServiceMap.canonical(raw) + + yield* Location.Service.pipe(Effect.provide(map.get(raw)), Effect.scoped) + expect(yield* RcMap.has(map.rcMap, raw)).toBe(false) + expect(yield* RcMap.has(map.rcMap, ref)).toBe(true) + + yield* TestClock.adjust("31 minutes") + yield* bus.publish( + SessionEvent.Execution.Started, + { sessionID: Session.ID.make("ses_activity") }, + { location: raw }, + ) + + yield* TestClock.adjust("32 minutes") + expect(Array.from(yield* RcMap.keys(map.rcMap))).toEqual([ref]) + + yield* TestClock.adjust("30 minutes") + expect(Array.from(yield* RcMap.keys(map.rcMap))).toEqual([]) + }), + ) }) From 14d8b1d621ebc7aed7caf69a14f6fb184cb8ab96 Mon Sep 17 00:00:00 2001 From: Luke Parker <10430890+Hona@users.noreply.github.com> Date: Mon, 26 Oct 2026 09:03:52 +1000 Subject: [PATCH 12/15] fix(core): use the index when loading turn diffs (#59088) Co-authored-by: Kit Langton --- packages/core/src/session/diff.ts | 59 +++++++++++++++---------- packages/core/test/session-diff.test.ts | 8 ++++ 2 files changed, 43 insertions(+), 24 deletions(-) diff --git a/packages/core/src/session/diff.ts b/packages/core/src/session/diff.ts index 2956acfdc3c..a389d192743 100644 --- a/packages/core/src/session/diff.ts +++ b/packages/core/src/session/diff.ts @@ -1,6 +1,6 @@ export * as SessionDiff from "./diff.js" -import { and, asc, eq, gt, inArray, lt, or, sql } from "drizzle-orm" +import { and, asc, desc, eq, gt, lt, sql } from "drizzle-orm" import { Context, Effect, Schema } from "effect" import { Location } from "@opencode/schema/location" import { Database } from "../database/database.js" @@ -48,32 +48,43 @@ export const turn = Effect.fn("SessionDiff.turn")(function* ( }, ) { const sessionID = input.session.id - const rows = yield* db - .select({ id: SessionMessageTable.id, type: SessionMessageTable.type, seq: SessionMessageTable.seq }) - .from(SessionMessageTable) - .where( - and( - eq(SessionMessageTable.session_id, sessionID), - or( - inArray(SessionMessageTable.type, ["user", "idle"]), - input.from ? eq(SessionMessageTable.id, input.from) : undefined, - input.to ? eq(SessionMessageTable.id, input.to) : undefined, - ), - ), - ) - .orderBy(asc(SessionMessageTable.seq)) - .all() - .pipe(Effect.orDie) - const users = rows.filter((row) => row.type === "user") - const markers = rows.filter((row) => row.type === "idle") + // Selecting only seq lets the (session_id, type, seq) index answer these without reading message rows, + // which can be megabytes each. Matching the requested IDs here with OR would scan the whole Session. + const boundaries = (type: "user" | "idle") => + db + .select({ seq: SessionMessageTable.seq }) + .from(SessionMessageTable) + .where(and(eq(SessionMessageTable.session_id, sessionID), eq(SessionMessageTable.type, type))) + .orderBy(asc(SessionMessageTable.seq)) + .all() + .pipe( + Effect.map((rows) => rows.map((row) => row.seq)), + Effect.orDie, + ) + const users = yield* boundaries("user") + const markers = yield* boundaries("idle") const resolve = Effect.fn(function* (field: "from" | "to", id: SessionMessage.ID) { - const row = rows.find((row) => row.id === id) + const row = yield* db + .select({ id: SessionMessageTable.id, type: SessionMessageTable.type, seq: SessionMessageTable.seq }) + .from(SessionMessageTable) + .where(and(eq(SessionMessageTable.id, id), eq(SessionMessageTable.session_id, sessionID))) + .get() + .pipe(Effect.orDie) if (!row) return yield* new MessageNotFoundError({ sessionID, messageID: id }) if (row.type !== "user") return yield* new TurnRangeError({ sessionID, field, message: `Message ${id} is not a user message` }) return row }) - const anchor = input.from ? yield* resolve("from", input.from) : users[users.length - 1] + const anchor = input.from + ? yield* resolve("from", input.from) + : yield* db + .select({ id: SessionMessageTable.id, seq: SessionMessageTable.seq }) + .from(SessionMessageTable) + .where(and(eq(SessionMessageTable.session_id, sessionID), eq(SessionMessageTable.type, "user"))) + .orderBy(desc(SessionMessageTable.seq)) + .limit(1) + .get() + .pipe(Effect.orDie) if (!anchor) return [] const last = input.to ? yield* resolve("to", input.to) : anchor if (last.seq < anchor.seq) @@ -81,9 +92,9 @@ export const turn = Effect.fn("SessionDiff.turn")(function* ( // Without any marker, history predates idle markers and a prompt's turn ends at the next prompt. const legacy = markers.length === 0 // The turn opens with the first prompt after the previous idle marker; the anchor itself is the latest candidate. - const opened = markers.findLast((row) => row.seq < anchor.seq)?.seq ?? -1 - const start = legacy ? anchor.seq : (users.find((row) => row.seq > opened)?.seq ?? anchor.seq) - const end = legacy ? users.find((row) => row.seq > last.seq)?.seq : markers.find((row) => row.seq > last.seq)?.seq + const opened = markers.findLast((seq) => seq < anchor.seq) ?? -1 + const start = legacy ? anchor.seq : (users.find((seq) => seq > opened) ?? anchor.seq) + const end = legacy ? users.find((seq) => seq > last.seq) : markers.find((seq) => seq > last.seq) const steps = yield* db .select({ seq: SessionMessageTable.seq, diff --git a/packages/core/test/session-diff.test.ts b/packages/core/test/session-diff.test.ts index 83b5898cb27..4d9ff30a815 100644 --- a/packages/core/test/session-diff.test.ts +++ b/packages/core/test/session-diff.test.ts @@ -161,6 +161,10 @@ describe("Session.diff", () => { _tag: "Session.TurnRangeError", field: "from", }) + expect(yield* diff({ from: first, to: firstStep }).pipe(Effect.flip)).toMatchObject({ + _tag: "Session.TurnRangeError", + field: "to", + }) expect(yield* diff({ from: SessionMessage.ID.create() }).pipe(Effect.flip)).toMatchObject({ _tag: "Session.MessageNotFoundError", }) @@ -191,6 +195,10 @@ describe("Session.diff", () => { expect((yield* sessions.diff({ sessionID: forked.id, context: 0 })).map(summarize)).toEqual([ ["third.txt", "added", 1, 0], ]) + // Messages are scoped to their own Session. + expect(yield* sessions.diff({ sessionID: forked.id, from: first }).pipe(Effect.flip)).toMatchObject({ + _tag: "Session.MessageNotFoundError", + }) }).pipe(Effect.provide(LocationServiceMap.Service.get(created.location))) }), // Real Location/plugin startup and Git snapshots can exceed five seconds under CI load. From 79469efc98bec3c8397ddb7b5f5e3d67f0f71cc7 Mon Sep 17 00:00:00 2001 From: Luke Parker <10430890+Hona@users.noreply.github.com> Date: Mon, 26 Oct 2026 09:15:22 +1000 Subject: [PATCH 13/15] feat(server): report the machine's hostname in /api/info (#59090) Co-authored-by: Dax Raad --- packages/cli/test/service.test.ts | 1 + packages/client/src/effect/api/api.ts | 1 + packages/client/src/promise/generated/types.ts | 1 + packages/protocol/openapi.json | 10 ++++++++++ packages/protocol/src/groups/server.ts | 2 ++ packages/server/src/handlers/server.ts | 2 ++ 6 files changed, 17 insertions(+) diff --git a/packages/cli/test/service.test.ts b/packages/cli/test/service.test.ts index d69cfd86c60..c1e3a2ad6ee 100644 --- a/packages/cli/test/service.test.ts +++ b/packages/cli/test/service.test.ts @@ -431,6 +431,7 @@ test("concurrent service processes elect one server", async () => { ).toEqual({ version: info.version, pid: info.pid, + hostname: os.hostname(), urls: [info.url], // The server reports the canonical tmp directory; Windows os.tmpdir() can be an 8.3 short name. paths: { tmp: await fs.realpath(path.join(os.tmpdir(), "opencode")) }, diff --git a/packages/client/src/effect/api/api.ts b/packages/client/src/effect/api/api.ts index 583d78f9427..d26ce6e5009 100644 --- a/packages/client/src/effect/api/api.ts +++ b/packages/client/src/effect/api/api.ts @@ -46,6 +46,7 @@ import type { Config } from "@opencode/schema/config" export type ServerInfoOutput = { readonly version: string readonly pid: number + readonly hostname?: string | undefined readonly urls: ReadonlyArray readonly paths: { readonly tmp: string } readonly capabilities?: { readonly persistentPty?: boolean | undefined } | undefined diff --git a/packages/client/src/promise/generated/types.ts b/packages/client/src/promise/generated/types.ts index 303f8e07afa..c10f30e54e2 100644 --- a/packages/client/src/promise/generated/types.ts +++ b/packages/client/src/promise/generated/types.ts @@ -3,6 +3,7 @@ export type JsonValue = null | boolean | number | string | Array | { export type ServerInfo = { version: string pid: number + hostname?: string | undefined urls: Array paths: { tmp: string } capabilities?: { persistentPty?: boolean | undefined } | undefined diff --git a/packages/protocol/openapi.json b/packages/protocol/openapi.json index 48964f54d3e..39457a317ff 100644 --- a/packages/protocol/openapi.json +++ b/packages/protocol/openapi.json @@ -19138,6 +19138,16 @@ "type": "integer", "minimum": 0 }, + "hostname": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ] + }, "urls": { "type": "array", "items": { diff --git a/packages/protocol/src/groups/server.ts b/packages/protocol/src/groups/server.ts index 4b4cab189bf..d78813bfc8b 100644 --- a/packages/protocol/src/groups/server.ts +++ b/packages/protocol/src/groups/server.ts @@ -6,6 +6,8 @@ export const ServerInfo = Schema.Struct({ version: Schema.String, // 0 means the runtime has no OS process identity (e.g. workerd). pid: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)), + // The machine's name, absent where the runtime has none (e.g. workerd). + hostname: Schema.optional(Schema.String), urls: Schema.Array(Schema.String), paths: Schema.Struct({ tmp: Schema.String, diff --git a/packages/server/src/handlers/server.ts b/packages/server/src/handlers/server.ts index d85594b38d4..7487581e70d 100644 --- a/packages/server/src/handlers/server.ts +++ b/packages/server/src/handlers/server.ts @@ -1,3 +1,4 @@ +import { hostname } from "node:os" import { Duration, Effect } from "effect" import { HttpServerRequest, HttpServerResponse } from "effect/http" import { HttpApiBuilder } from "effect/http-api" @@ -20,6 +21,7 @@ export const ServerHandler = HttpApiBuilder.group(Api, "server.server", (handler return { version: info.app.version ?? "unknown", pid: process.pid ?? 0, + hostname: hostname() || undefined, urls: info.urls(), paths: info.paths, capabilities: { persistentPty: PersistentPty.available }, From 6f861e2623b055caaafbe97a246036ab9b39472b Mon Sep 17 00:00:00 2001 From: Luke Parker <10430890+Hona@users.noreply.github.com> Date: Mon, 26 Oct 2026 09:18:45 +1000 Subject: [PATCH 14/15] feat(core): show detected project icons (#59093) Co-authored-by: Kit Langton --- packages/core/src/project.ts | 9 +- packages/core/src/project/icon.ts | 165 +++++++++++++++++++++++ packages/core/test/project-icon.test.ts | 168 ++++++++++++++++++++++++ 3 files changed, 341 insertions(+), 1 deletion(-) create mode 100644 packages/core/src/project/icon.ts create mode 100644 packages/core/test/project-icon.test.ts diff --git a/packages/core/src/project.ts b/packages/core/src/project.ts index 9922166a89e..23c07d5f38e 100644 --- a/packages/core/src/project.ts +++ b/packages/core/src/project.ts @@ -1,6 +1,6 @@ export * as Project from "./project.js" -import { Cache, Context, Duration, Effect, Layer, Schedule, Schema } from "effect" +import { Cache, Context, Duration, Effect, Layer, Schedule, Schema, Scope } from "effect" import { ChildProcess } from "effect/process" import { and, asc, desc, eq, gte, isNotNull, isNull, lt, lte, ne, sql } from "drizzle-orm" import path from "path" @@ -15,6 +15,7 @@ import { makeGlobalNode } from "@opencode/util/effect/app-node" import { Hash } from "@opencode/util/hash" import { ProjectSchema } from "./project/schema.js" import { ProjectTable, upsertProject } from "./project/sql.js" +import { ProjectIcon } from "./project/icon.js" import { WorktreeTable } from "./worktree/sql.js" export const ID = ProjectSchema.ID @@ -117,6 +118,7 @@ const layer = Layer.effect( const proc = yield* AppProcess.Service const bus = yield* Bus.Service const db = (yield* Database.Service).db + const scope = yield* Scope.Scope const announcing = new Set() const persist = Effect.fnUntraced(function* (project: Resolved) { @@ -149,6 +151,11 @@ const layer = Layer.effect( : undefined const changed = replaced ?? unarchived if (changed) yield* bus.publish(ProjectSchema.Event.Updated, fromRow(changed)) + yield* ProjectIcon.refresh(fs, db, project.id).pipe( + Effect.flatMap((row) => (row ? bus.publish(ProjectSchema.Event.Updated, fromRow(row)) : Effect.void)), + Effect.ignoreCause, + Effect.forkIn(scope), + ) if (!project.vcs) return project const directories: Array<{ projectID: ID; directory: AbsolutePath; strategy?: Worktree.StrategyID }> = [ { projectID: project.id, directory: project.canonical }, diff --git a/packages/core/src/project/icon.ts b/packages/core/src/project/icon.ts new file mode 100644 index 00000000000..c90e8c8c79b --- /dev/null +++ b/packages/core/src/project/icon.ts @@ -0,0 +1,165 @@ +export * as ProjectIcon from "./icon.js" + +import path from "path" +import { Effect, Option, Schema } from "effect" +import { eq } from "drizzle-orm" +import { FSUtil } from "@opencode/util/fs-util" +import { Hash } from "@opencode/util/hash" +import type { EffectDrizzleSqlite } from "../database/drizzle.js" +import { ProjectTable } from "./sql.js" +import type { ProjectSchema } from "./schema.js" + +const IMAGES = new Set([".svg", ".png", ".ico", ".jpg", ".jpeg", ".webp"]) +const CANDIDATES = [ + "Assets.xcassets/AppIcon.appiconset", + "ios/App/Assets.xcassets/AppIcon.appiconset", + ...[ + "assets/app-icon", + "build/icon", + "src-tauri/icons/icon", + "resources/icon", + "icons/icon", + "extension/icons/icon", + ].flatMap((b) => [`${b}.svg`, `${b}.png`]), + ...["favicon.svg", "icon.svg", "favicon.png", "apple-touch-icon.png", "icon.png", "favicon.ico"].flatMap((f) => + ["public/", "app/", "src/app/", "site/public/", ""].map((d) => `${d}${f}`), + ), + ...["assets/icon", "assets/logo", "public/logo", "logo"].flatMap((b) => [`${b}.svg`, `${b}.png`]), +] +const composer = Schema.decodeUnknownOption( + Schema.fromJsonString( + Schema.Struct({ + groups: Schema.Array( + Schema.Struct({ layers: Schema.Array(Schema.Struct({ "image-name": Schema.optional(Schema.String) })) }), + ), + fill: Schema.optional( + Schema.Struct({ + solid: Schema.optional(Schema.String), + "linear-gradient": Schema.optional(Schema.Array(Schema.String)), + }), + ), + }), + ), +) + +export const refresh = Effect.fn("ProjectIcon.refresh")(function* ( + fs: FSUtil.Interface, + db: EffectDrizzleSqlite.EffectSQLiteDatabase, + id: ProjectSchema.ID, +) { + const project = yield* db.select().from(ProjectTable).where(eq(ProjectTable.id, id)).get() + if (!project || id === "global") return + const image = yield* discover(fs, project.worktree) + if (!image || (project.icon_url && Hash.fast(project.icon_url) === Hash.fast(image.url))) return + return yield* db.update(ProjectTable).set({ icon_url: image.url }).where(eq(ProjectTable.id, id)).returning().get() +}) + +export const discover = Effect.fn("ProjectIcon.discover")(function* (fs: FSUtil.Interface, directory: string) { + const root = yield* fs.realPath(directory) + const children = yield* fs.readDirectoryEntries(root).pipe(Effect.orElseSucceed(() => [])) + const catalogs = children + .filter((e) => e.type === "directory" && !e.name.startsWith(".")) + .map((e) => e.name) + .toSorted() + .slice(0, 128) + .map((name) => `${name}/Assets.xcassets/AppIcon.appiconset`) + // Prefer flattened icons: a small Composer layer may be only a glow beneath an oversized foreground. + for (const name of [ + ...CANDIDATES, + "AppIcon.icon", + "app/AppIcon.icon", + "assets/AppIcon.icon", + ...catalogs, + "resources/windows", + ]) { + const candidate = yield* fs.realPath(path.join(root, name)).pipe(Effect.orElseSucceed(() => undefined)) + if (!candidate || candidate === root || !FSUtil.contains(root, candidate)) continue + if (name.endsWith(".icon")) { + const manifest = yield* read(fs, root, path.join(candidate, "icon.json"), new Set([".json"])) + const config = manifest ? Option.getOrUndefined(composer(manifest.bytes.toString("utf8"))) : undefined + for (const layer of config?.groups.flatMap((group) => group.layers) ?? []) { + if (!layer["image-name"]) continue + const image = yield* read( + fs, + root, + path.join(candidate, "Assets", layer["image-name"]), + new Set([".svg", ".png"]), + ) + if (!image) continue + const plain = dataURL(image.file, image.bytes) + const url = image.file.endsWith(".svg") ? backdrop(plain, config?.fill) : plain + if (url.length <= 64 * 1024) return { path: path.relative(root, image.file), url } + if (plain.length <= 64 * 1024) return { path: path.relative(root, image.file), url: plain } + } + continue + } + const windows = name === "resources/windows" + const entries = + name.endsWith(".appiconset") || windows + ? yield* fs.readDirectoryEntries(candidate).pipe(Effect.orElseSucceed(() => [])) + : undefined + const rank = (file: string) => + windows ? Math.abs(Number(file.match(/^icon-(\d+)\.png$/i)?.[1]) - 128) : /128|256/.test(file) ? 0 : 1 + const files = entries + ?.flatMap((e) => + e.type === "file" && + (windows ? /^icon-\d+\.png$/i.test(e.name) : IMAGES.has(path.extname(e.name).toLowerCase())) + ? [e.name] + : [], + ) + .toSorted() + .sort((a, b) => rank(a) - rank(b)) + .slice(0, 128) + .map((file) => path.join(candidate, file)) ?? [candidate] + for (const file of files) { + const image = yield* read(fs, root, file, IMAGES) + if (!image) continue + const url = dataURL(image.file, image.bytes) + if (url.length <= 64 * 1024) return { path: path.relative(root, image.file), url } + } + } +}) + +const read = Effect.fn("ProjectIcon.read")( + function* (fs: FSUtil.Interface, root: string, name: string, extensions: ReadonlySet) { + const file = yield* fs.realPath(name) + if (file === root || !FSUtil.contains(root, file) || !extensions.has(path.extname(file).toLowerCase())) return + const info = yield* fs.stat(file) + if (info.type !== "File" || !Number(info.size) || Number(info.size) > 64 * 1024) return + const bytes = yield* fs.readFile(file) + if (bytes.byteLength && bytes.byteLength <= 64 * 1024) return { file, bytes: Buffer.from(bytes) } + }, + Effect.orElseSucceed(() => undefined), +) + +function dataURL(file: string, bytes: Buffer) { + const mime = path.extname(file).toLowerCase() === ".ico" ? "image/x-icon" : FSUtil.mimeType(file) + return `data:${mime};base64,${bytes.toString("base64")}` +} + +function backdrop(url: string, fill: { solid?: string; "linear-gradient"?: ReadonlyArray } | undefined) { + const colors = (fill?.solid ? [fill.solid] : (fill?.["linear-gradient"] ?? [])).map((color) => { + const match = /^(gray|srgb|display-p3):([\d.,]+)$/.exec(color) + const values = match?.[2]?.split(",").map(Number) ?? [] + if ( + !match || + values.length !== (match[1] === "gray" ? 2 : 4) || + values.some((n) => !Number.isFinite(n) || n < 0 || n > 1) + ) + return + const rgb = match[1] === "gray" ? [values[0], values[0], values[0]] : values.slice(0, 3) + return `color(${match[1] === "display-p3" ? "display-p3" : "srgb"} ${rgb.join(" ")} / ${values.at(-1)})` + }) + if (!colors.length || colors.some((color) => !color)) return url + const gradient = + colors.length > 1 + ? `${colors.map((color, i) => ``).join("")}` + : "" + // Embed the layer as an image, never as SVG markup: its content stays in an inert image document. + return dataURL( + "icon.svg", + Buffer.from( + `${gradient}`, + ), + ) +} diff --git a/packages/core/test/project-icon.test.ts b/packages/core/test/project-icon.test.ts new file mode 100644 index 00000000000..3a5fce254b8 --- /dev/null +++ b/packages/core/test/project-icon.test.ts @@ -0,0 +1,168 @@ +import { expect } from "bun:test" +import path from "path" +import fs from "fs/promises" +import { Effect, Fiber, Stream } from "effect" +import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder" +import { LayerNode } from "@opencode/util/effect/layer-node" +import { Project } from "@opencode/core/project" +import { ProjectIcon } from "@opencode/core/project/icon" +import { ProjectSchema } from "@opencode/core/project/schema" +import { ProjectTable } from "@opencode/core/project/sql" +import { Database } from "@opencode/core/database/database" +import { Bus } from "@opencode/core/bus" +import { AbsolutePath } from "@opencode/core/schema" +import { FSUtil } from "@opencode/util/fs-util" +import { tmpdirScoped } from "./fixture/tmpdir" +import { testEffect } from "./lib/effect" + +const it = testEffect(AppNodeBuilder.build(LayerNode.group([Project.node, Database.node, Bus.node, FSUtil.node]))) +const svg = '' +const url = `data:image/svg+xml;base64,${Buffer.from(svg).toString("base64")}` + +for (const kind of ["ranked appiconset", "escaping symlink", "oversized file"] as const) { + it.live(`discovers the expected icon with a ${kind}`, () => + Effect.gen(function* () { + const tmp = yield* tmpdirScoped() + const root = path.join(tmp.path, "project") + const filesystem = yield* FSUtil.Service + const db = (yield* Database.Service).db + const id = Project.ID.make("icons") + yield* Effect.promise(() => Bun.write(path.join(root, "public/favicon.svg"), svg)) + if (kind === "ranked appiconset") { + yield* Effect.promise(() => Bun.write(path.join(root, "Assets.xcassets/AppIcon.appiconset/000.png"), "other")) + yield* Effect.promise(() => + Bun.write(path.join(root, "Assets.xcassets/AppIcon.appiconset/icon256.svg"), svg + " "), + ) + } + if (kind === "escaping symlink") { + yield* Effect.promise(() => Bun.write(path.join(tmp.path, "outside.svg"), "outside")) + yield* Effect.promise(() => fs.mkdir(path.join(root, "assets"))) + yield* Effect.promise(() => + fs.symlink(path.join(tmp.path, "outside.svg"), path.join(root, "assets/app-icon.svg")), + ) + } + if (kind === "oversized file") + yield* Effect.promise(() => Bun.write(path.join(root, "assets/app-icon.svg"), "x".repeat(64 * 1024 + 1))) + yield* db + .insert(ProjectTable) + .values({ id, worktree: AbsolutePath.make(root), sandboxes: [] }) + .run() + const row = yield* ProjectIcon.refresh(filesystem, db, id) + expect(row?.icon_url).toBe( + kind === "ranked appiconset" ? `data:image/svg+xml;base64,${Buffer.from(svg + " ").toString("base64")}` : url, + ) + }), + ) +} + +it.live("resolves in the background, preserves overrides, and only writes changed content", () => + Effect.gen(function* () { + const tmp = yield* tmpdirScoped() + const project = yield* Project.Service + const bus = yield* Bus.Service + const filesystem = yield* FSUtil.Service + const db = (yield* Database.Service).db + yield* Effect.promise(() => Bun.write(path.join(tmp.path, "favicon.svg"), svg)) + const updated = yield* bus + .subscribe(ProjectSchema.Event.Updated) + .pipe(Stream.take(1), Stream.runCollect, Effect.forkScoped({ startImmediately: true })) + const resolved = yield* project.resolve(AbsolutePath.make(tmp.path)) + expect((yield* Fiber.join(updated))[0]?.data.icon?.url).toBe(url) + yield* project.update({ projectID: resolved.id, icon: { override: "my-icon", color: "blue" } }) + const before = (yield* project.list())[0] + expect(yield* ProjectIcon.refresh(filesystem, db, resolved.id)).toBeUndefined() + expect((yield* project.list())[0]).toEqual(before) + yield* Effect.promise(() => Bun.write(path.join(tmp.path, "favicon.svg"), svg + " ")) + const changed = yield* ProjectIcon.refresh(filesystem, db, resolved.id) + expect(changed?.icon_url).not.toBe(url) + expect(changed).toMatchObject({ icon_url_override: "my-icon", icon_color: "blue" }) + }), +) + +for (const fixture of [ + { name: "root Composer SVG", file: "AppIcon.icon/Assets/logo.svg", manifest: "AppIcon.icon/icon.json" }, + { name: "app Composer PNG", file: "app/AppIcon.icon/Assets/logo.png", manifest: "app/AppIcon.icon/icon.json" }, + { + name: "assets Composer SVG", + file: "assets/AppIcon.icon/Assets/logo.svg", + manifest: "assets/AppIcon.icon/icon.json", + }, + { name: "direct-child Xcode catalog", file: "OpenCode/Assets.xcassets/AppIcon.appiconset/icon128.svg" }, + { name: "Windows nearest usable size", file: "resources/windows/icon-96.png" }, + { name: "Scrawl oversized flat icon", file: "site/public/icon.svg" }, +]) { + it.live(`discovers ${fixture.name} without a database`, () => + Effect.gen(function* () { + const tmp = yield* tmpdirScoped() + const filesystem = yield* FSUtil.Service + yield* Effect.promise(() => + Bun.write( + path.join(tmp.path, fixture.file), + fixture.file.endsWith(".png") + ? Buffer.from( + "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+aWZkAAAAASUVORK5CYII=", + "base64", + ) + : svg, + ), + ) + if (fixture.manifest) + yield* Effect.promise(() => + Bun.write( + path.join(tmp.path, fixture.manifest), + JSON.stringify({ + groups: [{ layers: [{ "image-name": path.basename(fixture.file) }] }], + }), + ), + ) + if (fixture.name === "Windows nearest usable size") { + yield* Effect.promise(() => Bun.write(path.join(tmp.path, "resources/windows/icon-128.png"), "x".repeat(65537))) + yield* Effect.promise(() => Bun.write(path.join(tmp.path, "resources/windows/icon-32.png"), "other")) + } + if (fixture.name === "Scrawl oversized flat icon") + yield* Effect.promise(() => Bun.write(path.join(tmp.path, "assets/app-icon.png"), "x".repeat(65537))) + expect((yield* ProjectIcon.discover(filesystem, tmp.path))?.path.split(path.sep).join("/")).toBe(fixture.file) + }), + ) +} + +it.live("Composer backgrounds stay inert and malformed, oversized or escaping manifests fall through", () => + Effect.gen(function* () { + const tmp = yield* tmpdirScoped() + const filesystem = yield* FSUtil.Service + const manifest = path.join(tmp.path, "AppIcon.icon/icon.json") + yield* Effect.promise(() => Bun.write(path.join(tmp.path, "AppIcon.icon/Assets/logo.svg"), svg)) + for (const fill of [ + { solid: "display-p3:0.075,0.063,0.063,1" }, + { "linear-gradient": ["gray:0.2,1", "srgb:0,0,0,1"] }, + ]) { + yield* Effect.promise(() => + Bun.write(manifest, JSON.stringify({ fill, groups: [{ layers: [{ "image-name": "logo.svg" }] }] })), + ) + const icon = yield* ProjectIcon.discover(filesystem, tmp.path) + if (!icon) throw new Error("Expected Composer icon") + const markup = Buffer.from(icon.url.slice(icon.url.indexOf(",") + 1), "base64").toString() + expect(markup).toContain(` Bun.write(manifest, content)) + expect(yield* ProjectIcon.discover(filesystem, tmp.path)).toBeUndefined() + } + const outside = yield* tmpdirScoped() + yield* Effect.promise(() => + Bun.write( + path.join(outside.path, "icon.json"), + JSON.stringify({ groups: [{ layers: [{ "image-name": "logo.svg" }] }] }), + ), + ) + yield* Effect.promise(() => fs.rm(manifest)) + yield* Effect.promise(() => fs.symlink(path.join(outside.path, "icon.json"), manifest)) + expect(yield* ProjectIcon.discover(filesystem, tmp.path)).toBeUndefined() + }), +) From d1cf7dd04a1dae3e151eced1fb8b3ca7f594adcd Mon Sep 17 00:00:00 2001 From: Luke Parker <10430890+Hona@users.noreply.github.com> Date: Mon, 26 Oct 2026 09:34:34 +1000 Subject: [PATCH 15/15] fix(core): send unchanged hooked requests without copying the body (#59097) --- packages/core/src/session/model-request.ts | 25 +++++----- .../test/session-model-request-hooks.test.ts | 48 +++++++++++++++++++ 2 files changed, 62 insertions(+), 11 deletions(-) diff --git a/packages/core/src/session/model-request.ts b/packages/core/src/session/model-request.ts index bf622dc4812..0a4853a1b21 100644 --- a/packages/core/src/session/model-request.ts +++ b/packages/core/src/session/model-request.ts @@ -321,17 +321,20 @@ export const layer = Layer.effect( const http: StreamOptions["http"] = hasHttpHooks ? (req, handler) => Effect.gen(function* () { - const before = yield* hooks.trigger("session", "http.request", { - ...scope, - request: yield* HttpClientRequest.toWeb(req), - }) - let sent = HttpClientRequest.fromWeb(before.request) - if (before.request.body) - sent = HttpClientRequest.bodyUint8Array( - sent, - new Uint8Array(yield* Effect.promise(() => before.request.clone().arrayBuffer())), - before.request.headers.get("content-type") ?? undefined, - ) + const request = yield* HttpClientRequest.toWeb(req) + const before = yield* hooks.trigger("session", "http.request", { ...scope, request }) + // A hook that kept the request object cannot have replaced its body, so the original body is sent as is. + // Reading it back would hold a second full copy of the request, screenshots included, for the whole call. + const sent = + before.request === request + ? HttpClientRequest.setBody(HttpClientRequest.fromWeb(request), req.body) + : before.request.body + ? HttpClientRequest.bodyUint8Array( + HttpClientRequest.fromWeb(before.request), + new Uint8Array(yield* Effect.promise(() => before.request.clone().arrayBuffer())), + before.request.headers.get("content-type") ?? undefined, + ) + : HttpClientRequest.fromWeb(before.request) const res = yield* handler(sent) const after = yield* hooks.trigger("session", "http.response", { ...scope, diff --git a/packages/core/test/session-model-request-hooks.test.ts b/packages/core/test/session-model-request-hooks.test.ts index 01ff4ea74de..32b6eab685c 100644 --- a/packages/core/test/session-model-request-hooks.test.ts +++ b/packages/core/test/session-model-request-hooks.test.ts @@ -81,6 +81,54 @@ describe("SessionModelRequest HTTP hooks", () => { }).pipe(Effect.provideService(SessionModelTransport.Service, transport)), ) + it.effect("sends a kept request's original body and a replaced request's new body", () => + Effect.gen(function* () { + const hooks = yield* PluginHooks.Service + let replace = false + yield* hooks.register("session", "http.request", (event) => + Effect.sync(() => { + if (replace) { + event.request = new Request(event.request.url, { method: "POST", body: '{"replaced":true}' }) + return + } + event.request.headers.set("x-plugin", "kept") + }), + ) + const requests = yield* SessionModelRequest.Service.pipe(Effect.provide(SessionModelRequest.layer)) + const prepared = yield* requests.primary({ + session, + agent: Agent.ID.make("build"), + model, + system: [], + messages: [], + }) + const http = prepared.options.http + if (!http) throw new Error("Expected HTTP middleware") + const original = HttpClientRequest.post("https://example.test/v1/chat/completions").pipe( + HttpClientRequest.bodyText('{"messages":[]}', "application/json"), + ) + const send = () => { + const sent: HttpClientRequest.HttpClientRequest[] = [] + return http(original, (request) => + Effect.sync(() => { + sent.push(request) + return HttpClientResponse.fromWeb(request, new Response("{}", { status: 200 })) + }), + ).pipe(Effect.map(() => sent[0]!)) + } + + const kept = yield* send() + expect(kept.body).toBe(original.body) + expect(kept.headers["x-plugin"]).toBe("kept") + + replace = true + const replaced = yield* send() + expect(replaced.body._tag).toBe("Uint8Array") + if (replaced.body._tag !== "Uint8Array") return + expect(new TextDecoder().decode(replaced.body.body)).toBe('{"replaced":true}') + }).pipe(Effect.provideService(SessionModelTransport.Service, transport)), + ) + it.effect("keeps hook edits to a tool schema out of later requests", () => Effect.gen(function* () { const hooks = yield* PluginHooks.Service