Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/cli/src/run/noninteractive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,9 @@ export async function runNonInteractivePrompt(input: Input) {
const ended = state.ended.promise
await input.client.experimental.session.wait({ sessionID: input.sessionID })
const pending = await background()
// Image jobs can't be cancelled through the API, so waiting on them would only hold the exit.
if (!pending || (pending.length > 0 && pending.every((job) => job.kind === "image"))) return
// Image and Code Mode jobs can't be cancelled through the API, so waiting on them would only hold the exit.
if (!pending || (pending.length > 0 && pending.every((job) => job.kind === "image" || job.kind === "codemode")))
return
await ended
}
}
Expand Down
13 changes: 11 additions & 2 deletions packages/client/src/promise/generated/types.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

122 changes: 122 additions & 0 deletions packages/core/src/codemode/job.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
export * as CodeModeJob from "./job.js"

import { Identifier } from "@opencode/schema/identifier"
import { Cause, Context, Deferred, Effect, Exit, Option, type Scope, Semaphore } from "effect"
import { extension } from "mime-types"
import { Flag } from "../flag.js"
import { Job } from "../job.js"
import type { Session } from "../session.js"
import type { Tool } from "../tool.js"
import { ToolOutput } from "../tool-output.js"

export function notice(job: Pick<Job.Info, "id" | "status" | "output" | "error">) {
const state = job.status === "running" ? "cancelled" : job.status
const body = job.status === "running" ? "Interrupted by server restart." : (job.output ?? job.error ?? state)
return {
description: "Code Mode script",
text: `<codemode id="${job.id}" state="${state}">\n${body}\n</codemode>`,
metadata: { source: "codemode", jobID: job.id, state },
}
}

type Execute = (context: Tool.Context) => Effect.Effect<Tool.NormalizedResult, Tool.Error>
type Run = (context: Tool.Context, execute: Execute) => ReturnType<Execute>

/** Only a session drain can hand off work and report its result. */
export const Current = Context.Reference<Run | undefined>("@opencode/CodeModeJob", { defaultValue: () => undefined })

export const make = Effect.fnUntraced(function* (
jobs: Job.Interface,
synthetic: Session.Interface["synthetic"],
scope: Scope.Scope,
) {
// A custom session runner can drain without installing the Location's tool services.
const services = Option.all({
flags: yield* Effect.serviceOption(Flag.Service),
output: yield* Effect.serviceOption(ToolOutput.Service),
})
if (Option.isNone(services)) return
const { flags, output } = services.value
const truncate = Effect.fnUntraced(function* (text: string) {
const bounded = yield* output.truncate({ content: [{ type: "text", text }] })
return bounded.content.flatMap((part) => (part.type === "text" ? [part.text] : [])).join("\n")
})

return Effect.fn("CodeModeJob.execute")(function* (context: Tool.Context, execute: Execute) {
if (!(yield* flags.enabled("full_codemode"))) return yield* execute(context)
const id = `cm_${Identifier.ascending()}`
const sessionID = context.sessionID
const result = yield* Deferred.make<Tool.NormalizedResult, Tool.Error>()
const detached = yield* Deferred.make<void>()
const progress = Semaphore.makeUnsafe(1)
return yield* Effect.uninterruptibleMask((restore) =>
Effect.gen(function* () {
yield* jobs.start({
id,
type: "codemode",
title: "Code Mode script",
recovery: { kind: "codemode", sessionID, jobID: id },
run: Effect.gen(function* () {
const exit = yield* execute({
...context,
progress: (update) =>
progress.withPermit(
Deferred.isDone(detached).pipe(
Effect.flatMap((done) => (done ? Effect.void : context.progress(update))),
),
),
}).pipe(Effect.provideService(Job.BlockingParent, id), Effect.interruptible, Effect.exit)
yield* Deferred.done(result, exit)
if (Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause)) return yield* Effect.failCause(exit.cause)
const text = Exit.isFailure(exit)
? Cause.pretty(exit.cause)
: exit.value.content.flatMap((part) => (part.type === "text" ? [part.text] : [])).join("\n")
const body = yield* truncate(text)
if (Exit.isFailure(exit) || exit.value.metadata?.error === true) return yield* Effect.fail(new Error(body))
return body
}),
})
const blocked = yield* restore(jobs.block({ id, sessionID }).pipe(Effect.timeoutOption("10 seconds"))).pipe(
Effect.onInterrupt(() => jobs.cancel(id)),
)
if (Option.isSome(blocked) && blocked.value?.type === "finished") return yield* Deferred.await(result)
const job = yield* jobs.background(id)
if (!job?.notificationID) return yield* Effect.interrupt
const notificationID = job.notificationID
// Drain the last progress publication before the tool row becomes terminal.
yield* progress.withPermit(Deferred.succeed(detached, undefined))
yield* Effect.gen(function* () {
const { info } = yield* jobs.wait({ id })
// Session removal cancels the job and removes its outbox row.
if (!info || info.status === "cancelled") return
// Synthetic messages carry text. Keep the script's file results accessible without putting base64 in its inbox.
const exit = yield* Deferred.await(result).pipe(Effect.exit)
const files = Exit.isSuccess(exit) ? exit.value.content.filter((part) => part.type === "file") : []
const paths = yield* Effect.forEach(files, (file) => {
const prefix = `data:${file.mime};base64,`
if (!file.uri.startsWith(prefix)) return Effect.succeed(file.uri)
const ext = extension(file.mime)
return output
.save(Buffer.from(file.uri.slice(prefix.length), "base64"), ext ? `.${ext}` : "")
.pipe(Effect.catchDefect(() => Effect.succeed(`Could not save ${file.mime} attachment.`)))
})
const message = notice(
paths.length
? { ...info, output: yield* truncate([info.output ?? info.error, ...paths].join("\n")) }
: info,
)
yield* synthetic({ id: notificationID, sessionID, ...message }).pipe(
Effect.catchTag("Session.NotFoundError", () => Effect.void),
)
yield* jobs.completeBackground(notificationID)
}).pipe(Effect.interruptible, Effect.forkIn(scope))
const text = `Script ${id} is still running; its result will arrive in this session.`
return {
content: [{ type: "text" as const, text }],
output: { output: text, toolCalls: [], files: [] },
metadata: { jobID: id },
}
}),
)
}) satisfies Run
})
20 changes: 15 additions & 5 deletions packages/core/src/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ import { SessionSchema } from "./session/schema.js"

export type { Recovery }

/** A session-wide background request releases the outer blocker; its nested waits keep their own deadlines. */
export const BlockingParent = Context.Reference<string | undefined>("@opencode/Job/BlockingParent", {
defaultValue: () => undefined,
})

const Background = Schema.Struct({
id: Schema.String,
notificationID: SessionMessage.ID,
Expand Down Expand Up @@ -196,6 +201,7 @@ function recoveryTitle(job: Active, recovery: Recovery): string | null {
if (job.info.title !== undefined) return job.info.title
if (recovery.kind === "shell") return recovery.command
if (recovery.kind === "subagent") return recovery.description
if (recovery.kind === "codemode") return "Code Mode script"
return recovery.path
}

Expand Down Expand Up @@ -440,17 +446,21 @@ export const make = Effect.gen(function* () {
})

const block: Interface["block"] = Effect.fnUntraced(function* (input) {
const parent = yield* BlockingParent
const tracksSession = parent === undefined || parent === input.id
const result = yield* SynchronizedRef.modify(state.jobs, (jobs): readonly [BlockStart, Map<string, Active>] => {
const job = jobs.get(input.id)
if (!job) return [{ type: "missing" }, jobs]
if (job.info.status !== "running") return [{ type: "finished", info: snapshot(job), generation: job.scope }, jobs]
if (job.isBackgrounded) return [{ type: "backgrounded", info: snapshot(job) }, jobs]
return [
{ type: "wait", wait: { done: job.done, backgrounded: job.backgrounded, generation: job.scope } },
new Map(jobs).set(input.id, {
...job,
blockingSessions: incrementSession(job.blockingSessions, input.sessionID),
}),
tracksSession
? new Map(jobs).set(input.id, {
...job,
blockingSessions: incrementSession(job.blockingSessions, input.sessionID),
})
: jobs,
]
})
if (result.type === "missing") return undefined
Expand All @@ -464,7 +474,7 @@ export const make = Effect.gen(function* () {
Deferred.await(result.wait.backgrounded).pipe(Effect.map((info) => ({ type: "backgrounded" as const, info }))),
).pipe(
Effect.tap((outcome) => (outcome.type === "finished" ? consume(input.id, result.wait.generation) : Effect.void)),
Effect.ensuring(removeBlock(input)),
Effect.ensuring(tracksSession ? removeBlock(input) : Effect.void),
)
})

Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/job/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ function toItem(input: {
...(input.completed !== undefined ? { completed: input.completed } : {}),
}
const recovery = input.recovery
if (recovery.kind === "codemode")
return {
kind: "codemode",
id: input.id,
sessionID: recovery.sessionID,
status: input.status,
time,
...(title !== undefined ? { title } : {}),
}
if (recovery.kind === "shell") {
return {
kind: "shell",
Expand Down
9 changes: 8 additions & 1 deletion packages/core/src/job/recovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@ const subagent = {
description: Schema.String,
}

const OutstandingItem = Schema.Union([Shell, Image, Schema.Struct({ ...subagent, sessionID: SessionSchema.ID })])
const CodeMode = Schema.Struct({ kind: Schema.Literal("codemode"), sessionID: SessionSchema.ID, jobID: Schema.String })
const OutstandingItem = Schema.Union([
Shell,
Image,
CodeMode,
Schema.Struct({ ...subagent, sessionID: SessionSchema.ID }),
])
export type OutstandingItem = typeof OutstandingItem.Type

export const RecoverySchema = Schema.Union([
CodeMode,
Shell,
Image,
Schema.Struct({
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/permission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ const layer = Layer.effect(
pending.delete(input.requestID)
for (const [id, item] of pending) {
if (item.request.sessionID !== existing.request.sessionID) continue
if (item.request.source?.messageID !== existing.request.source?.messageID) continue
yield* bus.publish(Permission.Event.Replied, {
sessionID: item.request.sessionID,
requestID: item.request.id,
Expand Down
8 changes: 6 additions & 2 deletions packages/core/src/plugin/goal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,14 @@ export const Plugin = define({
if (goal?.status !== "active") return
if (goal.boot !== boot())
return yield* settle(sessionID, goal, "paused", "The server restarted while the goal was active")
// The Session's own running subagent or image job wakes it with its notice, and the hook runs again then. A
// The Session's own running subagent, image or script wakes it with its notice, and the hook runs again then. A
// grandchild reports to its own parent, so it would never wake this Session.
if (
background.some((work) => (work.kind === "subagent" || work.kind === "image") && work.sessionID === sessionID)
background.some(
(work) =>
(work.kind === "subagent" || work.kind === "image" || work.kind === "codemode") &&
work.sessionID === sessionID,
)
)
return
if (goal.rounds >= goal.maxRounds)
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/session/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export const outstanding = Effect.fnUntraced(function* (
descends(store, parents, recovery.kind === "subagent" ? recovery.parentSessionID : recovery.sessionID, sessionID),
)
const items: OutstandingItem[] = owned.map((recovery) => {
if (recovery.kind === "codemode") return recovery
if (recovery.kind === "shell")
return {
kind: "shell" as const,
Expand Down
10 changes: 8 additions & 2 deletions packages/core/src/session/execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * as SessionExecution from "./execution.js"

import { Cause, Context, Effect, Exit, Layer } from "effect"
import { Bus } from "../bus.js"
import { CodeModeJob } from "../codemode/job.js"
import { Database } from "../database/database.js"
import { Job } from "../job.js"
import { Instance } from "../instance/service.js"
Expand All @@ -13,6 +14,7 @@ import { SessionRunCoordinator } from "./run-coordinator.js"
import { SessionRunner } from "./runner/index.js"
import { SessionSchema } from "./schema.js"
import { SessionStore } from "./store.js"
import { SessionSynthetic } from "./synthetic.js"
import { toSessionError } from "./to-session-error.js"

export interface Interface {
Expand Down Expand Up @@ -60,6 +62,7 @@ export const layer = Layer.effect(
const bus = yield* Bus.Service
const jobs = yield* Job.Service
const inbox = yield* SessionInbox.Service
const scope = yield* Effect.scope
const db = (yield* Database.Service).db
const reportLifecycle = <A>(sessionID: SessionSchema.ID, effect: Effect.Effect<A>) =>
effect.pipe(
Expand All @@ -85,12 +88,14 @@ export const layer = Layer.effect(
const holder: {
coordinator?: SessionRunCoordinator.Coordinator<SessionSchema.ID, SessionRunner.RunError, InterruptReason>
} = {}
const wake = (sessionID: SessionSchema.ID) =>
Effect.suspend(() => (holder.coordinator ? holder.coordinator.wake(sessionID) : Effect.void))
const background = CodeModeJob.make(jobs, SessionSynthetic.make(store, inbox, wake), scope)
const subagentReport = SubagentReport.make({
store,
jobs,
inbox,
wake: (sessionID) =>
Effect.suspend(() => (holder.coordinator ? holder.coordinator.wake(sessionID) : Effect.void)),
wake,
draining: Effect.suspend<ReadonlySet<SessionSchema.ID>, never, never>(() =>
holder.coordinator ? holder.coordinator.draining : Effect.succeed(new Set<SessionSchema.ID>()),
),
Expand All @@ -116,6 +121,7 @@ export const layer = Layer.effect(
interruption: () => interruption(sessionID),
}),
).pipe(
Effect.provideServiceEffect(CodeModeJob.Current, background),
instances.provide(session),
Effect.tapCause((cause) =>
Cause.hasInterruptsOnly(cause)
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/session/execution/restart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Bus } from "../../bus.js"
import { Database } from "../../database/database.js"
import { Instance } from "../../instance/service.js"
import { Job } from "../../job.js"
import { CodeModeJob } from "../../codemode/job.js"
import { Session } from "../../session.js"
import { Shell } from "../../shell.js"
import { ShellTool } from "../../tool/plugin/shell.js"
Expand Down Expand Up @@ -117,6 +118,7 @@ export const layer = (options?: Options) =>
})

const restartMessage = (background: Job.Background, recovery: Exclude<Job.Recovery, { kind: "subagent" }>) => {
if (recovery.kind === "codemode") return CodeModeJob.notice(background)
const state = background.status === "running" ? "cancelled" : background.status
if (recovery.kind === "shell") {
const text =
Expand Down
Loading
Loading