diff --git a/packages/core/src/plugin/fork-notice.ts b/packages/core/src/plugin/fork-notice.ts new file mode 100644 index 00000000000..ab317f813a5 --- /dev/null +++ b/packages/core/src/plugin/fork-notice.ts @@ -0,0 +1,30 @@ +export * as ForkNoticePlugin from "./fork-notice.js" + +import { define } from "@opencode/plugin/effect/plugin" +import { SessionMessage } from "@opencode/schema/session-message" +import { Effect } from "effect" + +const notice = ` +This conversation is a fork of another conversation, copied up to the previous message. The original conversation keeps going on its own and may be active right now, as may other forks of it. +- Work started before the fork belongs to the original: background subagents, background shells, schedules, and follow-ups promised there. Their results and notifications go to the original, never here, and you can't continue those subagents. Don't wait for them, redo them, or report their results as yours; if it matters, check what already happened (files, git, PRs, CI). +- Files, repositories, branches and external services are shared with the original. Before changing something the original was working on (its uncommitted changes, branches, worktrees, PRs, deploys, messages), check its current state, and put work that diverges from it on a new branch or worktree. +- Otherwise work normally: new tasks need no extra checks, branches or questions. +- If it's unclear whether this fork or the original should do something, ask. +` + +export const Plugin = define({ + id: "opencode.fork-notice", + effect: Effect.fn(function* (ctx) { + yield* ctx.session.hook("fork", ({ sessionID }) => + ctx.session + .synthetic({ + sessionID, + id: SessionMessage.ID.make(`msg_fork_notice_${sessionID}`), + text: notice, + description: "Fork notice", + resume: false, + }) + .pipe(Effect.asVoid, Effect.orDie), + ) + }), +}) diff --git a/packages/core/src/plugin/internal.ts b/packages/core/src/plugin/internal.ts index f6239be4cbd..335c16124cb 100644 --- a/packages/core/src/plugin/internal.ts +++ b/packages/core/src/plugin/internal.ts @@ -90,6 +90,7 @@ import { AgentPlugin } from "./agent.js" import BrowserPlugin from "@opencode/plugin-browser" import { CommandPlugin } from "./command.js" import { IdentityPlugin } from "./identity.js" +import { ForkNoticePlugin } from "./fork-notice.js" import { PlanPlugin } from "./plan.js" import { ModelsDevPlugin } from "./models-dev.js" import { McpCodeModeDefaultsPlugin } from "./mcp-codemode-defaults.js" @@ -221,6 +222,7 @@ const pre = [ WellKnownPlugin.Plugin, VcsGitPlugin.Plugin, AgentPlugin.Plugin, + ForkNoticePlugin.Plugin, PlanPlugin.Plugin, CommandPlugin.Plugin, SkillPlugin.Plugin, diff --git a/packages/core/src/session.ts b/packages/core/src/session.ts index 39f5f36d3a5..7a0a5d97a23 100644 --- a/packages/core/src/session.ts +++ b/packages/core/src/session.ts @@ -13,6 +13,8 @@ import { SessionMessage } from "./session/message.js" import { PromptInput } from "@opencode/schema/prompt-input" import { Bus } from "./bus.js" import { Instance } from "./instance/service.js" +import { Plugin } from "./plugin/service.js" +import { PluginHooks } from "./plugin/hooks.js" import { Database } from "./database/database.js" import { SessionProjector } from "./session/projector.js" import { SessionMessageTable } from "./session/sql.js" @@ -355,7 +357,13 @@ const layer = Layer.effect( boundary: { type: input.before ? "before" : "through", messageID: boundary.id }, ...inherited, }) - return yield* result.get(sessionID).pipe(Effect.orDie) + const forked = yield* result.get(sessionID).pipe(Effect.orDie) + yield* Effect.gen(function* () { + yield* Plugin.awaitActivation + const hooks = yield* PluginHooks.Service + yield* hooks.trigger("session", "fork", { sessionID }) + }).pipe(instances.provide(forked), Effect.orDie) + return forked }), get: (sessionID) => sessions.forSession(sessionID).get(), environment: Effect.fn("Session.environment")(function* (input) { diff --git a/packages/core/test/session-create.test.ts b/packages/core/test/session-create.test.ts index edb83d48f8a..2aa258920bc 100644 --- a/packages/core/test/session-create.test.ts +++ b/packages/core/test/session-create.test.ts @@ -607,6 +607,31 @@ describe("Session.create", () => { }), ) + liveIt.live("queues a fork notice after copied history and before the first prompt", () => + withTmp((directory) => + Effect.gen(function* () { + const session = yield* Session.Service + const bus = yield* Bus.Service + const { db } = yield* Database.Service + const parent = yield* session.create({ + location: Location.Ref.make({ directory: AbsolutePath.make(directory) }), + }) + yield* session.prompt({ sessionID: parent.id, text: "Original", resume: false }) + yield* SessionInbox.promote(db, bus, parent.id, "steer") + const fork = yield* session.fork({ sessionID: parent.id }) + expect(yield* session.inbox(fork.id)).toMatchObject([{ type: "synthetic", delivery: "steer" }]) + yield* session.prompt({ sessionID: fork.id, text: "Continue", resume: false }) + yield* SessionInbox.promote(db, bus, fork.id, "steer") + expect(yield* session.context(fork.id)).toMatchObject([ + Expected.user("Original"), + { type: "synthetic", text: expect.stringContaining("") }, + Expected.user("Continue"), + ]) + expect(yield* session.context(parent.id)).toMatchObject([Expected.user("Original")]) + }), + ), + ) + it.effect("keeps a fork untitled when its parent is untitled", () => Effect.gen(function* () { const session = yield* Session.Service diff --git a/packages/plugin/src/effect/session.ts b/packages/plugin/src/effect/session.ts index b9f19a6b724..cbe42260a1a 100644 --- a/packages/plugin/src/effect/session.ts +++ b/packages/plugin/src/effect/session.ts @@ -136,6 +136,7 @@ export interface SessionRetry { } export interface SessionHooks { + readonly fork: { readonly sessionID: Session.ID } readonly prompt: SessionPrompt readonly context: SessionContext readonly compaction: SessionCompaction diff --git a/packages/plugin/src/promise/session.ts b/packages/plugin/src/promise/session.ts index 9794147461d..0485baa9864 100644 --- a/packages/plugin/src/promise/session.ts +++ b/packages/plugin/src/promise/session.ts @@ -136,6 +136,7 @@ export interface SessionRetry { } export interface SessionHooks { + readonly fork: { readonly sessionID: Session.ID } readonly prompt: SessionPrompt readonly context: SessionContext readonly compaction: SessionCompaction diff --git a/packages/sdk/test/instances.test.ts b/packages/sdk/test/instances.test.ts index 87857bd0838..ae4e47e7008 100644 --- a/packages/sdk/test/instances.test.ts +++ b/packages/sdk/test/instances.test.ts @@ -118,7 +118,10 @@ test("Promise instances are lazy, share by key and Location, and stay isolated b expect(fork.fork?.sessionID).toBe(sessionID) const inherited = await first.sessions.prompt({ sessionID: fork.id, text: "Review the fork", resume: false }) expect(inherited.payload.text).toBe(`first:alpha@${directory.path}: Review the fork`) - expect(await first.sessions.inbox.list({ sessionID: fork.id })).toEqual([inherited]) + expect(await first.sessions.inbox.list({ sessionID: fork.id })).toEqual([ + expect.objectContaining({ type: "synthetic", sessionID: fork.id, id: `msg_fork_notice_${fork.id}` }), + inherited, + ]) expect(configured.toSorted()).toEqual(["first:alpha", "first:alpha", "first:beta", "second:alpha"]) expect(setups.toSorted()).toEqual(