Compare commits

..

13 Commits

Author SHA1 Message Date
Kit Langton 82370fdd63 refactor(server): reuse modal workspace client 2026-08-07 23:42:19 -04:00
Kit Langton e707ffa84f refactor(core): reuse environment test fixture 2026-08-07 23:42:15 -04:00
Kit Langton 77faeea584 refactor(core): simplify workspace lifecycle 2026-08-07 23:42:12 -04:00
Kit Langton b31cec37e3 fix(core): surface workspace wake failures as spawn errors 2026-08-07 23:30:45 -04:00
Kit Langton 2c80a77906 feat(core): bind environment by placement 2026-08-07 23:21:56 -04:00
Kit Langton ec0e5373e1 feat(server): add modal workspace driver 2026-08-07 23:21:56 -04:00
Kit Langton 334b278547 feat(core): add workspace domain 2026-08-07 23:21:56 -04:00
Kit Langton 9b7b402737 feat(server): run modal sandboxes on the vm runtime (#41177) 2026-08-07 23:21:05 -04:00
James Long 5e6370363b fix(tui): refine provider failure presentation (#41179) 2026-08-07 23:11:32 -04:00
Kit Langton dd6020656e fix(merman): tighten flowchart spacing (#41191) 2026-08-07 23:00:44 -04:00
Kit Langton 8c758e443b fix(core): reuse shared patch diff (#41186) 2026-08-07 22:50:59 -04:00
Kit Langton 0df6aed6ca fix(merman): derive neutral diagram palette (#41181) 2026-08-07 22:43:58 -04:00
Kit Langton aa05fd23b3 refactor(core): remove legacy account runtime schemas (#41173) 2026-08-07 22:40:53 -04:00
34 changed files with 1292 additions and 545 deletions
+14 -2
View File
@@ -1,4 +1,4 @@
import { Cause, Context, Effect, Layer } from "effect"
import { Cause, Context, Effect, Layer, Option, Schema } from "effect"
import {
FetchHttpClient,
Headers,
@@ -198,8 +198,20 @@ const responseBody = (body: string | void, request: HttpClientRequest.HttpClient
return { body: redacted.slice(0, BODY_LIMIT), bodyTruncated: true }
}
const decodeProviderBody = Schema.decodeUnknownOption(
Schema.fromJsonString(
Schema.Struct({
message: Schema.optionalKey(Schema.String),
error: Schema.optionalKey(Schema.Struct({ message: Schema.optionalKey(Schema.String) })),
}),
),
)
const providerMessage = (status: number, body: { readonly body?: string }) => {
if (body.body && body.body.length <= 500) return `Provider request failed with HTTP ${status}: ${body.body}`
if (body.body && body.body.length <= 500) {
const decoded = Option.getOrUndefined(decodeProviderBody(body.body))
return `Provider request failed with HTTP ${status}: ${decoded?.error?.message ?? decoded?.message ?? body.body}`
}
return `Provider request failed with HTTP ${status}`
}
+60 -105
View File
@@ -1,15 +1,11 @@
{
"version": "7",
"dialect": "sqlite",
"id": "2d214a71-3b0a-48c1-a667-741952c4e188",
"id": "2aefa7b6-6847-4490-96e8-6680159f757c",
"prevIds": [
"f14a9b18-8207-487e-a3d3-227e629ba9ad"
"6ff49c08-7759-48fd-beca-6086853fce79"
],
"ddl": [
{
"name": "workspace",
"entityType": "tables"
},
{
"name": "account_state",
"entityType": "tables"
@@ -75,84 +71,8 @@
"entityType": "tables"
},
{
"type": "text",
"notNull": false,
"autoincrement": false,
"default": null,
"generated": null,
"name": "id",
"entityType": "columns",
"table": "workspace"
},
{
"type": "text",
"notNull": true,
"autoincrement": false,
"default": null,
"generated": null,
"name": "type",
"entityType": "columns",
"table": "workspace"
},
{
"type": "text",
"notNull": true,
"autoincrement": false,
"default": "''",
"generated": null,
"name": "name",
"entityType": "columns",
"table": "workspace"
},
{
"type": "text",
"notNull": false,
"autoincrement": false,
"default": null,
"generated": null,
"name": "branch",
"entityType": "columns",
"table": "workspace"
},
{
"type": "text",
"notNull": false,
"autoincrement": false,
"default": null,
"generated": null,
"name": "directory",
"entityType": "columns",
"table": "workspace"
},
{
"type": "text",
"notNull": false,
"autoincrement": false,
"default": null,
"generated": null,
"name": "extra",
"entityType": "columns",
"table": "workspace"
},
{
"type": "text",
"notNull": true,
"autoincrement": false,
"default": null,
"generated": null,
"name": "project_id",
"entityType": "columns",
"table": "workspace"
},
{
"type": "integer",
"notNull": true,
"autoincrement": false,
"default": null,
"generated": null,
"name": "time_used",
"entityType": "columns",
"table": "workspace"
"name": "workspace",
"entityType": "tables"
},
{
"type": "integer",
@@ -1385,18 +1305,53 @@
"table": "session_v2"
},
{
"columns": [
"project_id"
],
"tableTo": "project",
"columnsTo": [
"id"
],
"onUpdate": "NO ACTION",
"onDelete": "CASCADE",
"nameExplicit": false,
"name": "fk_workspace_project_id_project_id_fk",
"entityType": "fks",
"type": "text",
"notNull": false,
"autoincrement": false,
"default": null,
"generated": null,
"name": "id",
"entityType": "columns",
"table": "workspace"
},
{
"type": "text",
"notNull": true,
"autoincrement": false,
"default": null,
"generated": null,
"name": "provider",
"entityType": "columns",
"table": "workspace"
},
{
"type": "text",
"notNull": true,
"autoincrement": false,
"default": null,
"generated": null,
"name": "binding",
"entityType": "columns",
"table": "workspace"
},
{
"type": "integer",
"notNull": true,
"autoincrement": false,
"default": null,
"generated": null,
"name": "created_at",
"entityType": "columns",
"table": "workspace"
},
{
"type": "integer",
"notNull": true,
"autoincrement": false,
"default": null,
"generated": null,
"name": "last_used_at",
"entityType": "columns",
"table": "workspace"
},
{
@@ -1564,15 +1519,6 @@
"entityType": "pks",
"table": "instruction_entry"
},
{
"columns": [
"id"
],
"nameExplicit": false,
"name": "workspace_pk",
"table": "workspace",
"entityType": "pks"
},
{
"columns": [
"id"
@@ -1690,6 +1636,15 @@
"table": "session_v2",
"entityType": "pks"
},
{
"columns": [
"id"
],
"nameExplicit": false,
"name": "workspace_pk",
"table": "workspace",
"entityType": "pks"
},
{
"columns": [
{
-101
View File
@@ -1,101 +0,0 @@
export * as Account from "./account"
import { Schema } from "effect"
import type { HttpClientError } from "effect/unstable/http"
export const ID = Schema.String.pipe(Schema.brand("AccountID"))
export type ID = Schema.Schema.Type<typeof ID>
export const OrgID = Schema.String.pipe(Schema.brand("OrgID"))
export type OrgID = Schema.Schema.Type<typeof OrgID>
export const AccessToken = Schema.String.pipe(Schema.brand("AccessToken"))
export type AccessToken = Schema.Schema.Type<typeof AccessToken>
export const RefreshToken = Schema.String.pipe(Schema.brand("RefreshToken"))
export type RefreshToken = Schema.Schema.Type<typeof RefreshToken>
export const DeviceCode = Schema.String.pipe(Schema.brand("DeviceCode"))
export type DeviceCode = Schema.Schema.Type<typeof DeviceCode>
export const UserCode = Schema.String.pipe(Schema.brand("UserCode"))
export type UserCode = Schema.Schema.Type<typeof UserCode>
export class Info extends Schema.Class<Info>("Account")({
id: ID,
email: Schema.String,
url: Schema.String,
active_org_id: Schema.NullOr(OrgID),
}) {}
export class Org extends Schema.Class<Org>("Org")({
id: OrgID,
name: Schema.String,
}) {}
export class AccountRepoError extends Schema.TaggedErrorClass<AccountRepoError>()("AccountRepoError", {
message: Schema.String,
cause: Schema.optional(Schema.Defect()),
}) {}
export class AccountServiceError extends Schema.TaggedErrorClass<AccountServiceError>()("AccountServiceError", {
message: Schema.String,
cause: Schema.optional(Schema.Defect()),
}) {}
export class AccountTransportError extends Schema.TaggedErrorClass<AccountTransportError>()("AccountTransportError", {
method: Schema.String,
url: Schema.String,
description: Schema.optional(Schema.String),
cause: Schema.optional(Schema.Defect()),
}) {
static fromHttpClientError(error: HttpClientError.TransportError): AccountTransportError {
return new AccountTransportError({
method: error.request.method,
url: error.request.url,
description: error.description,
cause: error.cause,
})
}
override get message(): string {
return [
`Could not reach ${this.method} ${this.url}.`,
`This failed before the server returned an HTTP response.`,
this.description,
`Check your network, proxy, or VPN configuration and try again.`,
]
.filter(Boolean)
.join("\n")
}
}
export type AccountError = AccountRepoError | AccountServiceError | AccountTransportError
export class Login extends Schema.Class<Login>("Login")({
code: DeviceCode,
user: UserCode,
url: Schema.String,
server: Schema.String,
expiry: Schema.Duration,
interval: Schema.Duration,
}) {}
export class PollSuccess extends Schema.TaggedClass<PollSuccess>()("PollSuccess", {
email: Schema.String,
}) {}
export class PollPending extends Schema.TaggedClass<PollPending>()("PollPending", {}) {}
export class PollSlow extends Schema.TaggedClass<PollSlow>()("PollSlow", {}) {}
export class PollExpired extends Schema.TaggedClass<PollExpired>()("PollExpired", {}) {}
export class PollDenied extends Schema.TaggedClass<PollDenied>()("PollDenied", {}) {}
export class PollError extends Schema.TaggedClass<PollError>()("PollError", {
cause: Schema.Defect(),
}) {}
export const PollResult = Schema.Union([PollSuccess, PollPending, PollSlow, PollExpired, PollDenied, PollError])
export type PollResult = Schema.Schema.Type<typeof PollResult>
+7 -10
View File
@@ -1,24 +1,21 @@
import { sqliteTable, text, integer, primaryKey } from "drizzle-orm/sqlite-core"
import { Account } from "../account"
import { Timestamps } from "../database/schema.sql"
export const AccountTable = sqliteTable("account", {
id: text().$type<Account.ID>().primaryKey(),
id: text().primaryKey(),
email: text().notNull(),
url: text().notNull(),
access_token: text().$type<Account.AccessToken>().notNull(),
refresh_token: text().$type<Account.RefreshToken>().notNull(),
access_token: text().notNull(),
refresh_token: text().notNull(),
token_expiry: integer(),
...Timestamps,
})
export const AccountStateTable = sqliteTable("account_state", {
id: integer().primaryKey(),
active_account_id: text()
.$type<Account.ID>()
.references(() => AccountTable.id, { onDelete: "set null" }),
active_org_id: text().$type<Account.OrgID>(),
active_account_id: text().references(() => AccountTable.id, { onDelete: "set null" }),
active_org_id: text(),
})
// LEGACY
@@ -27,8 +24,8 @@ export const ControlAccountTable = sqliteTable(
{
email: text().notNull(),
url: text().notNull(),
access_token: text().$type<Account.AccessToken>().notNull(),
refresh_token: text().$type<Account.RefreshToken>().notNull(),
access_token: text().notNull(),
refresh_token: text().notNull(),
token_expiry: integer(),
active: integer({ mode: "boolean" })
.notNull()
+13 -29
View File
@@ -141,7 +141,7 @@ export const layer = (options?: Options) => Layer.effect(
Effect.logWarning("failed to discover wellknown config", { error }).pipe(Effect.as([] as const)),
),
)
const resolved = yield* Effect.forEach(entries, (entry) =>
return yield* Effect.forEach(entries, (entry) =>
Effect.gen(function* () {
const auth = entry.manifest.auth
if (!auth) return []
@@ -151,29 +151,20 @@ export const layer = (options?: Options) => Layer.effect(
if (!credential || credential.value.type !== "key") return []
const variables = { [auth.env]: credential.value.key }
const configs = yield* wellknown.resolve(entry, variables).pipe(Effect.orDie)
return configs.map((config) => ({ config, source: entry.origin, variables }))
return yield* Effect.forEach(configs, (config) =>
ConfigVariable.substitute({
type: "virtual",
source: entry.origin,
dir: entry.origin,
text: JSON.stringify(config),
env: variables,
}).pipe(
Effect.flatMap((text) => parseInfo(text, entry.origin)),
Effect.map((info) => (info ? new Document({ type: "document", info }) : undefined)),
),
).pipe(Effect.map((documents) => documents.filter((document) => document !== undefined)))
}),
).pipe(Effect.map((documents) => documents.flat()))
// V1 merged authenticated configs before applying this allowlist. Give every
// migrated document the union so one source's wildcard deny cannot hide another.
const enabledProviders = Array.from(
new Set(resolved.flatMap((item) => legacyEnabledProviders(item.config) ?? [])),
)
return yield* Effect.forEach(resolved, (item) => {
const config = legacyEnabledProviders(item.config)
? { ...item.config, enabled_providers: enabledProviders }
: item.config
return ConfigVariable.substitute({
type: "virtual",
source: item.source,
dir: item.source,
text: JSON.stringify(config),
env: item.variables,
}).pipe(
Effect.flatMap((text) => parseInfo(text, item.source)),
Effect.map((info) => (info ? new Document({ type: "document", info }) : undefined)),
)
}).pipe(Effect.map((documents) => documents.filter((document) => document !== undefined)))
})
const loadDirectory = Effect.fnUntraced(function* (directory: AbsolutePath) {
@@ -369,13 +360,6 @@ export const layer = (options?: Options) => Layer.effect(
}),
)
function legacyEnabledProviders(config: WellKnown.Config) {
if (typeof config !== "object" || config === null) return
if (!Array.isArray(config.enabled_providers)) return
if (!config.enabled_providers.every((provider): provider is string => typeof provider === "string")) return
return config.enabled_providers
}
export function configured(options?: Options) {
return makeLocationNode({
service: Service,
@@ -1,20 +0,0 @@
import { sqliteTable, text, integer } from "drizzle-orm/sqlite-core"
import { ProjectTable } from "../project/sql"
import { Project } from "../project"
import { Workspace } from "../workspace"
export const WorkspaceTable = sqliteTable("workspace", {
id: text().$type<Workspace.ID>().primaryKey(),
type: text().notNull(),
name: text().notNull().default(""),
branch: text(),
directory: text(),
extra: text({ mode: "json" }),
project_id: text()
.$type<Project.ID>()
.notNull()
.references(() => ProjectTable.id, { onDelete: "cascade" }),
time_used: integer()
.notNull()
.$default(() => Date.now()),
})
+1
View File
@@ -42,5 +42,6 @@ export const migrations: DatabaseMigration.Migration[] = (
import("./migration/20260622202450_simplify_session_input"),
import("./migration/20260804233008_loose_psylocke"),
import("./migration/20260805200742_import_legacy_credentials"),
import("./migration/20260808023530_workspace_domain"),
])
).map((module) => module.default)
@@ -0,0 +1,22 @@
import { Effect } from "effect"
import type { DatabaseMigration } from "../migration"
const migration: DatabaseMigration.Migration = {
id: "20260808023530_workspace_domain",
up(tx) {
return Effect.gen(function* () {
yield* tx.run(`DROP TABLE \`workspace\`;`)
yield* tx.run(`
CREATE TABLE \`workspace\` (
\`id\` text PRIMARY KEY,
\`provider\` text NOT NULL,
\`binding\` text NOT NULL,
\`created_at\` integer NOT NULL,
\`last_used_at\` integer NOT NULL
);
`)
})
},
}
export default migration
+9 -13
View File
@@ -4,19 +4,6 @@ import type { DatabaseMigration } from "./migration"
const schema: Omit<DatabaseMigration.Migration, "id"> = {
up(tx) {
return Effect.gen(function* () {
yield* tx.run(`
CREATE TABLE \`workspace\` (
\`id\` text PRIMARY KEY,
\`type\` text NOT NULL,
\`name\` text DEFAULT '' NOT NULL,
\`branch\` text,
\`directory\` text,
\`extra\` text,
\`project_id\` text NOT NULL,
\`time_used\` integer NOT NULL,
CONSTRAINT \`fk_workspace_project_id_project_id_fk\` FOREIGN KEY (\`project_id\`) REFERENCES \`project\`(\`id\`) ON DELETE CASCADE
);
`)
yield* tx.run(`
CREATE TABLE \`account_state\` (
\`id\` integer PRIMARY KEY,
@@ -216,6 +203,15 @@ const schema: Omit<DatabaseMigration.Migration, "id"> = {
CONSTRAINT \`fk_session_v2_project_id_project_id_fk\` FOREIGN KEY (\`project_id\`) REFERENCES \`project\`(\`id\`) ON DELETE CASCADE
);
`)
yield* tx.run(`
CREATE TABLE \`workspace\` (
\`id\` text PRIMARY KEY,
\`provider\` text NOT NULL,
\`binding\` text NOT NULL,
\`created_at\` integer NOT NULL,
\`last_used_at\` integer NOT NULL
);
`)
yield* tx.run(`CREATE UNIQUE INDEX \`event_aggregate_seq_idx\` ON \`event\` (\`aggregate_id\`,\`seq\`);`)
yield* tx.run(`CREATE INDEX \`event_aggregate_type_seq_idx\` ON \`event\` (\`aggregate_id\`,\`type\`,\`seq\`);`)
yield* tx.run(
+19 -2
View File
@@ -5,6 +5,8 @@ import { ChildProcessSpawner } from "effect/unstable/process/ChildProcessSpawner
import type { Files } from "./files"
import { makeFiles } from "./index"
import { makeLocalDriver } from "./local"
import { Location } from "../location"
import { Workspace } from "../workspace"
export interface Interface {
readonly files: Files
@@ -17,10 +19,25 @@ const layer = Layer.effect(
Service,
Effect.gen(function* () {
const spawner = yield* ChildProcessSpawner
return Service.of({ files: makeFiles(makeLocalDriver(spawner)), spawner })
const location = yield* Location.Service
const workspace = yield* Workspace.Service
const driver = location.workspaceID
? yield* workspace.connect(location.workspaceID).pipe(
// Environment has no error channel; an unknown or destroyed placement is a configuration defect by design.
Effect.mapError(
(cause) => new Error(`Failed to bind Environment to workspace ${location.workspaceID}`, { cause }),
),
Effect.orDie,
)
: makeLocalDriver(spawner)
return Service.of({ files: makeFiles(driver), spawner: driver.spawner })
}),
)
export const node = makeLocationNode({ service: Service, layer, deps: [CrossSpawnSpawner.node] })
export const node = makeLocationNode({
service: Service,
layer,
deps: [CrossSpawnSpawner.node, Location.node, Workspace.node],
})
export * as EnvironmentService from "./environment"
-8
View File
@@ -16,7 +16,6 @@ import { SessionPendingTable, SessionMessageTable, SessionTable } from "./sql"
import { Slug } from "../util/slug"
import { Money } from "@opencode-ai/schema/money"
import type { SessionSchema } from "./schema"
import { WorkspaceTable } from "../control-plane/workspace.sql"
type DatabaseService = Database.Interface["db"]
type CurrentDurableEvent = Extract<SessionEvent.Event, { readonly durable: object }>
@@ -376,13 +375,6 @@ const layer = Layer.effectDiscard(
.get()
.pipe(Effect.orDie)
if (!stored) return yield* Effect.die(new SessionAlreadyProjected())
if (!event.data.location.workspaceID) return
yield* db
.update(WorkspaceTable)
.set({ time_used: Date.now() })
.where(eq(WorkspaceTable.id, event.data.location.workspaceID))
.run()
.pipe(Effect.orDie)
}),
)
yield* bus.project(SessionEvent.Moved, (event) =>
+9 -15
View File
@@ -3,7 +3,6 @@ export * as PatchTool from "./patch"
import type { Context as PluginContext } from "@opencode-ai/plugin/effect/plugin"
import { ToolFailure } from "@opencode-ai/ai"
import { FileDiff } from "@opencode-ai/schema/file-diff"
import { createTwoFilesPatch, diffLines } from "diff"
import { Effect, Result, Schema } from "effect"
import path from "path"
import { Bom } from "@opencode-ai/util/bom"
@@ -15,6 +14,7 @@ import { Location } from "../../location"
import { Patch } from "@opencode-ai/util/patch"
import { Permission } from "../../permission"
import DESCRIPTION from "../patch.txt"
import { fileDiff } from "./file-diff"
export const name = "patch"
@@ -353,22 +353,16 @@ function errorMessage(error: unknown) {
function patchFile(change: Prepared, after = change.after): typeof FileDiff.Info.Type {
const target = (change.type === "update" ? change.moveTarget : undefined)?.resource ?? change.target.resource
const patch = trimDiff(createTwoFilesPatch(change.target.absolute, change.target.absolute, change.before, after))
const counts =
change.type === "delete"
? { additions: 0, deletions: change.before.split("\n").length }
: diffLines(change.before, after).reduce(
(result, item) => ({
additions: result.additions + (item.added ? (item.count ?? 0) : 0),
deletions: result.deletions + (item.removed ? (item.count ?? 0) : 0),
}),
{ additions: 0, deletions: 0 },
)
const diff = fileDiff(
change.target.absolute,
change.before,
after,
change.type === "add" ? "added" : change.type === "delete" ? "deleted" : "modified",
)
return {
...diff,
file: target,
patch,
status: change.type === "add" ? "added" : change.type === "delete" ? "deleted" : "modified",
...counts,
patch: trimDiff(diff.patch),
}
}
+214 -1
View File
@@ -1,6 +1,219 @@
export * as Workspace from "./workspace"
import { Workspace } from "@opencode-ai/schema/workspace"
import { makeGlobalNode } from "@opencode-ai/util/effect/app-node"
import { eq } from "drizzle-orm"
import { Clock, Context, Duration, Effect, Exit, Layer, Ref, Schedule, Schema, Scope } from "effect"
import { systemError } from "effect/PlatformError"
import { make } from "effect/unstable/process/ChildProcessSpawner"
import type { Driver as EnvironmentDriver } from "./environment/driver"
import { Database } from "./database/database"
import { KeyedMutex } from "./effect/keyed-mutex"
import { WorkspaceDriver } from "./workspace/driver"
import { WorkspaceTable } from "./workspace/sql"
export const ID = Workspace.ID
export type ID = typeof ID.Type
export type ID = Workspace.ID
export class Info extends Schema.Class<Info>("Workspace.Info")({
id: ID,
provider: Schema.String,
binding: WorkspaceDriver.Binding,
createdAt: Schema.Number,
lastUsedAt: Schema.Number,
}) {}
export class NotFound extends Schema.TaggedErrorClass<NotFound>()("Workspace.NotFound", { workspaceID: ID }) {}
export interface Interface {
readonly create: (provider: string) => Effect.Effect<Info, WorkspaceDriver.Error | WorkspaceDriver.ProviderNotFound>
readonly connect: (
workspaceID: ID,
) => Effect.Effect<EnvironmentDriver, NotFound | WorkspaceDriver.Error | WorkspaceDriver.ProviderNotFound>
readonly destroy: (
workspaceID: ID,
) => Effect.Effect<void, NotFound | WorkspaceDriver.Error | WorkspaceDriver.ProviderNotFound>
}
export interface Options {
readonly idleThreshold?: Duration.Input
readonly pollInterval?: Duration.Input
}
export class Service extends Context.Service<Service, Interface>()("@opencode/Workspace") {}
interface Connection {
readonly driver: WorkspaceDriver.Interface
readonly environment: EnvironmentDriver
readonly saveBinding: (binding: WorkspaceDriver.Binding) => Effect.Effect<void>
readonly lastActivity: Ref.Ref<number>
readonly active: Ref.Ref<number>
readonly scope: Scope.Closeable
}
export const configured = (options: Options = {}) =>
makeGlobalNode({
service: Service,
layer: layer(options),
deps: [Database.node, WorkspaceDriver.node],
})
const layer = (options: Options) =>
Layer.effect(
Service,
Effect.gen(function* () {
const db = (yield* Database.Service).db
const registry = yield* WorkspaceDriver.RegistryService
const lifetime = yield* Scope.Scope
const connections = new Map<ID, Connection>()
const locks = KeyedMutex.makeUnsafe<ID>()
const idleThreshold = Duration.toMillis(options.idleThreshold ?? Duration.minutes(20))
const load = Effect.fn("Workspace.load")(function* (workspaceID: ID) {
const row = yield* db
.select()
.from(WorkspaceTable)
.where(eq(WorkspaceTable.id, workspaceID))
.get()
.pipe(Effect.orDie)
if (!row) return yield* new NotFound({ workspaceID })
return row
})
const open = Effect.fn("Workspace.open")(function* (workspaceID: ID) {
const existing = connections.get(workspaceID)
if (existing) return existing
const row = yield* load(workspaceID)
const driver = yield* registry.get(row.provider)
const saveBinding = (value: WorkspaceDriver.Binding) =>
db
.update(WorkspaceTable)
.set({ binding: value })
.where(eq(WorkspaceTable.id, workspaceID))
.run()
.pipe(Effect.orDie)
const scope = yield* Scope.fork(lifetime)
const environment = yield* driver.connect({ workspaceID, binding: row.binding, saveBinding }).pipe(
Effect.provideService(Scope.Scope, scope),
Effect.onError((cause) => Scope.close(scope, Exit.failCause(cause))),
)
const now = yield* Clock.currentTimeMillis
const connection: Connection = {
driver,
environment,
saveBinding,
lastActivity: yield* Ref.make(now),
active: yield* Ref.make(0),
scope,
}
connections.set(workspaceID, connection)
yield* db
.update(WorkspaceTable)
.set({ last_used_at: now })
.where(eq(WorkspaceTable.id, workspaceID))
.run()
.pipe(Effect.orDie)
return connection
})
yield* Effect.gen(function* () {
const now = yield* Clock.currentTimeMillis
yield* Effect.forEach(
[...connections.entries()],
([workspaceID, expected]) =>
locks.withLock(workspaceID)(
Effect.gen(function* () {
const connection = connections.get(workspaceID)
if (connection !== expected || (yield* Ref.get(connection.active)) > 0) return
const lastActivity = yield* Ref.get(connection.lastActivity)
if (now - lastActivity < idleThreshold) return
const row = yield* load(workspaceID)
// Deliberate: a racing spawn blocks, then wakes cleanly. Unlocking mid-suspend could reattach a sandbox being terminated.
yield* connection.driver.suspendForIdle({
workspaceID,
binding: row.binding,
saveBinding: connection.saveBinding,
})
yield* db
.update(WorkspaceTable)
.set({ last_used_at: lastActivity })
.where(eq(WorkspaceTable.id, workspaceID))
.run()
.pipe(Effect.orDie)
connections.delete(workspaceID)
yield* Scope.close(connection.scope, Exit.void)
}).pipe(Effect.catchCause((cause) => Effect.logError("workspace idle suspension failed", cause))),
),
{ concurrency: "unbounded", discard: true },
)
}).pipe(Effect.repeat(Schedule.spaced(options.pollInterval ?? Duration.minutes(1))), Effect.forkScoped)
return Service.of({
create: Effect.fn("Workspace.create")(function* (provider) {
const driver = yield* registry.get(provider)
const workspaceID = ID.create()
const result = yield* driver.create({ workspaceID })
const now = yield* Clock.currentTimeMillis
yield* db
.insert(WorkspaceTable)
.values({ id: workspaceID, provider, binding: result.binding, created_at: now, last_used_at: now })
.run()
.pipe(Effect.orDie)
return new Info({ id: workspaceID, provider, binding: result.binding, createdAt: now, lastUsedAt: now })
}),
connect: Effect.fn("Workspace.connect")(function* (workspaceID) {
const spawner = make((command) =>
Effect.acquireRelease(
locks.withLock(workspaceID)(
Effect.gen(function* () {
const connection = yield* open(workspaceID).pipe(
Effect.mapError((cause) =>
systemError({
_tag: "Unknown",
module: "Workspace",
method: "spawn",
description: `Failed to wake workspace ${workspaceID}`,
cause,
}),
),
)
yield* Ref.set(connection.lastActivity, yield* Clock.currentTimeMillis)
yield* Ref.update(connection.active, (active) => active + 1)
return connection
}),
),
(connection) =>
locks.withLock(workspaceID)(
Effect.gen(function* () {
yield* Ref.update(connection.active, (active) => active - 1)
yield* Ref.set(connection.lastActivity, yield* Clock.currentTimeMillis)
}),
),
).pipe(Effect.flatMap((connection) => connection.environment.spawner.spawn(command))),
)
// Overrides are connection-bound; per-spawn routing is required before any driver ships them, so they are deliberately omitted.
return { spawner }
}),
destroy: Effect.fn("Workspace.destroy")(function* (workspaceID) {
yield* locks.withLock(workspaceID)(
Effect.gen(function* () {
const row = yield* load(workspaceID)
const connection = connections.get(workspaceID)
connections.delete(workspaceID)
if (connection) yield* Scope.close(connection.scope, Exit.void)
const driver = yield* registry.get(row.provider)
yield* driver.destroy({ workspaceID, binding: row.binding })
yield* db.delete(WorkspaceTable).where(eq(WorkspaceTable.id, workspaceID)).run().pipe(Effect.orDie)
}),
)
}),
})
}),
)
export const node = configured()
// TODO(workspace-plan): add the boot janitor and ~23h safety snapshot rotation in a later PR.
// TODO(workspace-plan): make cold wake interruptible with a re-pin loop against janitor races.
// TODO(workspace-plan): consider RcMap at end-of-series consolidation; idle suspend and destroy need distinct finalizers.
+70
View File
@@ -0,0 +1,70 @@
export * as WorkspaceDriver from "./driver"
import { Workspace } from "@opencode-ai/schema/workspace"
import { makeGlobalNode } from "@opencode-ai/util/effect/app-node"
import { Context, Effect, Layer, Schema } from "effect"
import type { Scope } from "effect"
import type { Driver as EnvironmentDriver } from "../environment/driver"
/**
* Smallest provider-owned JSON value required to reconnect to the same
* provider resource. Core stores it opaquely and hands it back; only the
* owning driver reads inside.
*/
export const Binding = Schema.Record(Schema.String, Schema.Json)
export type Binding = typeof Binding.Type
export class Error extends Schema.TaggedErrorClass<Error>()("WorkspaceDriver.Error", {
message: Schema.optional(Schema.String),
cause: Schema.optional(Schema.Defect()),
}) {}
export class ProviderNotFound extends Schema.TaggedErrorClass<ProviderNotFound>()("WorkspaceDriver.ProviderNotFound", {
provider: Schema.String,
}) {}
export interface Interface {
readonly create: (input: {
readonly workspaceID: Workspace.ID
}) => Effect.Effect<{ readonly binding: Binding }, Error>
readonly connect: (input: {
readonly workspaceID: Workspace.ID
readonly binding: Binding
readonly saveBinding: (binding: Binding) => Effect.Effect<void>
}) => Effect.Effect<EnvironmentDriver, Error, Scope.Scope>
readonly suspendForIdle: (input: {
readonly workspaceID: Workspace.ID
readonly binding: Binding
readonly saveBinding: (binding: Binding) => Effect.Effect<void>
}) => Effect.Effect<void, Error>
readonly destroy: (input: {
readonly workspaceID: Workspace.ID
readonly binding: Binding
}) => Effect.Effect<void, Error>
}
export const make = (driver: Interface) => driver
export interface Registry {
readonly get: (provider: string) => Effect.Effect<Interface, ProviderNotFound>
}
export class RegistryService extends Context.Service<RegistryService, Registry>()(
"@opencode/WorkspaceDriverRegistry",
) {}
export const registry = (drivers: Readonly<Record<string, Interface>>): Registry => ({
get: (provider) => {
const driver = drivers[provider]
return driver ? Effect.succeed(driver) : Effect.fail(new ProviderNotFound({ provider }))
},
})
export const registryNode = (drivers: Readonly<Record<string, Interface>>) =>
makeGlobalNode({
service: RegistryService,
layer: Layer.succeed(RegistryService, RegistryService.of(registry(drivers))),
deps: [],
})
export const node = registryNode({})
+11
View File
@@ -0,0 +1,11 @@
import { Workspace } from "@opencode-ai/schema/workspace"
import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core"
import type { WorkspaceDriver } from "./driver"
export const WorkspaceTable = sqliteTable("workspace", {
id: text().$type<Workspace.ID>().primaryKey(),
provider: text().notNull(),
binding: text({ mode: "json" }).$type<WorkspaceDriver.Binding>().notNull(),
created_at: integer().notNull(),
last_used_at: integer().notNull(),
})
+9 -41
View File
@@ -307,7 +307,7 @@ describe("Config", () => {
}),
)
it.live("loads and merges authenticated wellknown config before user configuration", () =>
it.live("loads authenticated wellknown config before user configuration", () =>
Effect.acquireUseRelease(
Effect.promise(() => tmpdir()),
(tmp) =>
@@ -322,7 +322,6 @@ describe("Config", () => {
})
const integrationID = Integration.ID.make("https://example.com")
const supplementalID = Integration.ID.make("https://models.example.com")
let key = "secret"
const credentialNode = makeGlobalNode({
service: Credential.Service,
@@ -330,16 +329,13 @@ describe("Config", () => {
Credential.Service,
Credential.Service.of({
all: () => Effect.die("unused Credential.all"),
list: (requested) =>
list: () =>
Effect.succeed([
new Credential.Info({
id: Credential.ID.create(),
integrationID: requested,
integrationID,
label: "default",
value: Credential.Key.make({
type: "key",
key: requested === integrationID ? key : "supplemental",
}),
value: Credential.Key.make({ type: "key", key }),
}),
]),
get: () => Effect.die("unused Credential.get"),
@@ -355,28 +351,17 @@ describe("Config", () => {
integrationID,
manifest: { auth: { command: ["login"], env: "TOKEN" } },
}
const supplemental: WellKnown.Entry = {
origin: "https://models.example.com",
integrationID: supplementalID,
manifest: { auth: { command: ["login"], env: "TOKEN" } },
}
const wellknownNode = makeGlobalNode({
service: WellKnown.Service,
layer: Layer.succeed(
WellKnown.Service,
WellKnown.Service.of({
entries: () => Effect.succeed([entry, supplemental]),
snapshot: () => [entry, supplemental],
entries: () => Effect.succeed([entry]),
snapshot: () => [entry],
refresh: () => Effect.succeed(false),
add: () => Effect.die("unused Wellknown.add"),
remove: () => Effect.die("unused Wellknown.remove"),
resolve: (resolved, variables) =>
Effect.succeed([
{
shell: variables.TOKEN,
enabled_providers: [resolved.integrationID === integrationID ? "primary" : "fable"],
},
]),
resolve: (_entry, variables) => Effect.succeed([{ shell: variables.TOKEN }]),
}),
),
deps: [],
@@ -391,24 +376,7 @@ describe("Config", () => {
initial.flatMap((entry) =>
entry.type === "document" && entry.info.shell ? [entry.info.shell] : [],
),
).toEqual(["secret", "supplemental", "global", "project"])
expect(
initial
.filter((entry): entry is Document => entry.type === "document" && entry.info.shell !== undefined)
.slice(0, 2)
.map((entry) => entry.info.experimental?.policies),
).toEqual([
[
{ action: "provider.use", resource: "*", effect: "deny" },
{ action: "provider.use", resource: "primary", effect: "allow" },
{ action: "provider.use", resource: "fable", effect: "allow" },
],
[
{ action: "provider.use", resource: "*", effect: "deny" },
{ action: "provider.use", resource: "primary", effect: "allow" },
{ action: "provider.use", resource: "fable", effect: "allow" },
],
])
).toEqual(["secret", "global", "project"])
const updated = yield* bus
.subscribe(Event.Updated)
.pipe(Stream.take(1), Stream.runCollect, Effect.forkScoped)
@@ -422,7 +390,7 @@ describe("Config", () => {
refreshed.flatMap((entry) =>
entry.type === "document" && entry.info.shell ? [entry.info.shell] : [],
),
).toEqual(["next", "supplemental", "global", "project"])
).toEqual(["next", "global", "project"])
}).pipe(
Effect.provide(testLayer(project, global, project, undefined, undefined, credentialNode, wellknownNode)),
)
+7 -16
View File
@@ -9,11 +9,12 @@ import { Environment } from "@opencode-ai/core/environment"
import { Location } from "@opencode-ai/core/location"
import { LocationMutation } from "@opencode-ai/core/location-mutation"
import { AbsolutePath } from "@opencode-ai/core/schema"
import { type EnvironmentFilesTransform, transformEnvironmentFiles } from "./fixture/environment"
import { location } from "./fixture/location"
import { tmpdir } from "./fixture/tmpdir"
import { it } from "./lib/effect"
function provide(directory: string, environmentLayer = LayerNode.compile(Environment.node)) {
function provide(directory: string, transformFiles: EnvironmentFilesTransform = () => ({})) {
const activeLocation = Layer.succeed(
Location.Service,
Location.Service.of(location({ directory: AbsolutePath.make(directory) })),
@@ -21,7 +22,7 @@ function provide(directory: string, environmentLayer = LayerNode.compile(Environ
return Effect.provide(
AppNodeBuilder.build(LayerNode.group([LocationMutation.node, FileMutation.node]), [
[Location.node, activeLocation],
[Environment.node, environmentLayer],
[Environment.node, transformEnvironmentFiles(activeLocation, transformFiles)],
]),
)
}
@@ -240,18 +241,8 @@ describe("FileMutation", () => {
)
})
function instrumentWrites(run: <E>(write: Effect.Effect<void, E>, target: string) => Effect.Effect<void, E>) {
return Layer.effect(
Environment.Service,
Effect.gen(function* () {
const environment = yield* Environment.Service
return Environment.Service.of({
...environment,
files: {
...environment.files,
write: (target, content) => run(environment.files.write(target, content), target),
},
})
}),
).pipe(Layer.provide(LayerNode.compile(Environment.node)))
function instrumentWrites(
run: <E>(write: Effect.Effect<void, E>, target: string) => Effect.Effect<void, E>,
): EnvironmentFilesTransform {
return (files) => ({ write: (target, content) => run(files.write(target, content), target) })
}
+22
View File
@@ -0,0 +1,22 @@
import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
import { Environment } from "@opencode-ai/core/environment"
import { Location } from "@opencode-ai/core/location"
import { Effect, Layer } from "effect"
export type EnvironmentFilesTransform = (files: Environment.Files) => Partial<Environment.Files>
export function transformEnvironmentFiles(
location: Layer.Layer<Location.Service>,
transform: EnvironmentFilesTransform = () => ({}),
) {
return Layer.effect(
Environment.Service,
Effect.gen(function* () {
const current = yield* Environment.Service
return Environment.Service.of({
...current,
files: { ...current.files, ...transform(current.files) },
})
}),
).pipe(Layer.provide(AppNodeBuilder.build(Environment.node, [[Location.node, location]])))
}
+7 -2
View File
@@ -3,12 +3,15 @@ import fs from "fs/promises"
import path from "path"
import { Effect } from "effect"
import { LayerNode } from "@opencode-ai/util/effect/layer-node"
import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
import { Location } from "@opencode-ai/core/location"
import { Ripgrep } from "@opencode-ai/core/ripgrep"
import { RelativePath } from "@opencode-ai/core/schema"
import { tmpdir } from "./fixture/tmpdir"
import { testEffect } from "./lib/effect"
import { tempLocationLayer } from "./fixture/location"
const it = testEffect(LayerNode.compile(Ripgrep.node))
const it = testEffect(AppNodeBuilder.build(Ripgrep.node, [[Location.node, tempLocationLayer]]))
describe("Ripgrep", () => {
it.live("globs files as an array", () =>
@@ -129,7 +132,9 @@ describe("Ripgrep", () => {
Effect.promise(() => tmpdir()),
(tmp) =>
Effect.gen(function* () {
yield* Effect.promise(() => fs.writeFile(path.join(tmp.path, "generated.ts"), `Cloudflare${"x".repeat(70 * 1024)}\n`))
yield* Effect.promise(() =>
fs.writeFile(path.join(tmp.path, "generated.ts"), `Cloudflare${"x".repeat(70 * 1024)}\n`),
)
const matches = yield* (yield* Ripgrep.Service).grep({
cwd: tmp.path,
+18 -24
View File
@@ -14,6 +14,7 @@ import { AbsolutePath } from "@opencode-ai/core/schema"
import { Session } from "@opencode-ai/core/session"
import { Tool } from "@opencode-ai/core/tool"
import { EditTool } from "@opencode-ai/core/tool/plugin/edit"
import { transformEnvironmentFiles } from "./fixture/environment"
import { location } from "./fixture/location"
import { tmpdir } from "./fixture/tmpdir"
import { makeLocationNode } from "@opencode-ai/util/effect/app-node"
@@ -80,29 +81,6 @@ const reset = () => {
formatFile = () => Effect.succeed(false)
}
const environment = Layer.effect(
Environment.Service,
Effect.gen(function* () {
const current = yield* Environment.Service
return Environment.Service.of({
...current,
files: {
...current.files,
read: (target, range) =>
current.files
.read(target, range)
.pipe(
Effect.tap((result) =>
Effect.sync(() => reads++).pipe(Effect.andThen(Effect.suspend(() => afterRead(target, result.bytes)))),
),
),
write: (target, content) =>
Effect.sync(() => writes.push(target)).pipe(Effect.andThen(current.files.write(target, content))),
},
})
}),
).pipe(Layer.provide(LayerNode.compile(Environment.node)))
const withTool = <A, E, R>(directory: string, body: (registry: Tool.Interface) => Effect.Effect<A, E, R>) => {
const activeLocation = Layer.succeed(
Location.Service,
@@ -115,7 +93,23 @@ const withTool = <A, E, R>(directory: string, body: (registry: Tool.Interface) =
AppNodeBuilder.build(
LayerNode.group([Tool.node, Tool.node, LocationMutation.node, FileMutation.node, editToolNode]),
[
[Environment.node, environment],
[
Environment.node,
transformEnvironmentFiles(activeLocation, (files) => ({
read: (target, range) =>
files
.read(target, range)
.pipe(
Effect.tap((result) =>
Effect.sync(() => reads++).pipe(
Effect.andThen(Effect.suspend(() => afterRead(target, result.bytes))),
),
),
),
write: (target, content) =>
Effect.sync(() => writes.push(target)).pipe(Effect.andThen(files.write(target, content))),
})),
],
[Location.node, activeLocation],
[Formatter.node, formatter],
[Permission.node, permission],
+47 -31
View File
@@ -14,6 +14,7 @@ import { AbsolutePath } from "@opencode-ai/core/schema"
import { Session } from "@opencode-ai/core/session"
import { Tool } from "@opencode-ai/core/tool"
import { PatchTool } from "@opencode-ai/core/tool/plugin/patch"
import { transformEnvironmentFiles } from "./fixture/environment"
import { location } from "./fixture/location"
import { tmpdir } from "./fixture/tmpdir"
import { makeLocationNode } from "@opencode-ai/util/effect/app-node"
@@ -82,34 +83,6 @@ const reset = () => {
formatFile = () => Effect.succeed(false)
}
const environment = Layer.effect(
Environment.Service,
Effect.gen(function* () {
const current = yield* Environment.Service
return Environment.Service.of({
...current,
files: {
...current.files,
read: (target, range) =>
Effect.sync(() => {
if (!editApproved) readsBeforeEditApproval++
}).pipe(Effect.andThen(current.files.read(target, range))),
remove: (target) => {
if (failRemoveTarget && path.basename(target) === failRemoveTarget) return Effect.die("forced remove failure")
if (failRemoveErrorTarget && path.basename(target) === failRemoveErrorTarget)
return Effect.fail(new Environment.Failed({ path: target, cause: new Error("forced remove failure") }))
return current.files.remove(target)
},
write: (target, content) => {
if (failWriteTarget && path.basename(target) === failWriteTarget)
return Effect.fail(new Environment.Failed({ path: target, cause: new Error("forced write failure") }))
return current.files.write(target, content)
},
},
})
}),
).pipe(Layer.provide(LayerNode.compile(Environment.node)))
const withTool = <A, E, R>(
directory: string,
body: (registry: Tool.Interface) => Effect.Effect<A, E, R>,
@@ -126,7 +99,27 @@ const withTool = <A, E, R>(
}).pipe(
Effect.provide(
AppNodeBuilder.build(LayerNode.group([Tool.node, FileMutation.node, patchToolNode]), [
[Environment.node, environment],
[
Environment.node,
transformEnvironmentFiles(activeLocation, (files) => ({
read: (target, range) =>
Effect.sync(() => {
if (!editApproved) readsBeforeEditApproval++
}).pipe(Effect.andThen(files.read(target, range))),
remove: (target) => {
if (failRemoveTarget && path.basename(target) === failRemoveTarget)
return Effect.die("forced remove failure")
if (failRemoveErrorTarget && path.basename(target) === failRemoveErrorTarget)
return Effect.fail(new Environment.Failed({ path: target, cause: new Error("forced remove failure") }))
return files.remove(target)
},
write: (target, content) => {
if (failWriteTarget && path.basename(target) === failWriteTarget)
return Effect.fail(new Environment.Failed({ path: target, cause: new Error("forced write failure") }))
return files.write(target, content)
},
})),
],
[Location.node, activeLocation],
[Formatter.node, formatter],
[Permission.node, permission],
@@ -215,7 +208,7 @@ describe("PatchTool", () => {
file: "remove.txt",
status: "deleted",
additions: 0,
deletions: 2,
deletions: 1,
patch: expect.stringContaining("-remove"),
},
],
@@ -248,6 +241,29 @@ describe("PatchTool", () => {
),
)
it.live("counts deleted lines with and without a trailing newline", () =>
withTempTool((directory, registry) =>
Effect.gen(function* () {
yield* Effect.promise(() =>
Promise.all([
fs.writeFile(path.join(directory, "trailing.txt"), "remove\n"),
fs.writeFile(path.join(directory, "unterminated.txt"), "remove"),
]),
)
const settled = yield* executeTool(
registry,
call("*** Begin Patch\n*** Delete File: trailing.txt\n*** Delete File: unterminated.txt\n*** End Patch"),
)
expect(settled.status).toBe("completed")
if (settled.status !== "completed") return
expect(settled.output.files).toMatchObject([
{ file: "trailing.txt", additions: 0, deletions: 1 },
{ file: "unterminated.txt", additions: 0, deletions: 1 },
])
}),
),
)
it.live("serializes concurrent patch transactions", () =>
withTempTool((directory, registry) => {
const target = path.join(directory, "concurrent.txt")
@@ -446,7 +462,7 @@ describe("PatchTool", () => {
{
file: "renamed/dir/name.txt",
status: "modified",
patch: expect.stringContaining("-old content\n+new content"),
patch: expect.stringContaining(`Index: ${source}`),
},
],
})
+8 -16
View File
@@ -14,6 +14,7 @@ import { AbsolutePath } from "@opencode-ai/core/schema"
import { Session } from "@opencode-ai/core/session"
import { Tool } from "@opencode-ai/core/tool"
import { WriteTool } from "@opencode-ai/core/tool/plugin/write"
import { transformEnvironmentFiles } from "./fixture/environment"
import { location } from "./fixture/location"
import { tmpdir } from "./fixture/tmpdir"
import { makeLocationNode } from "@opencode-ai/util/effect/app-node"
@@ -68,21 +69,6 @@ const reset = () => {
denyAction = undefined
}
const environment = Layer.effect(
Environment.Service,
Effect.gen(function* () {
const current = yield* Environment.Service
return Environment.Service.of({
...current,
files: {
...current.files,
write: (target, content) =>
Effect.sync(() => writes.push(target)).pipe(Effect.andThen(current.files.write(target, content))),
},
})
}),
).pipe(Layer.provide(LayerNode.compile(Environment.node)))
const withTool = <A, E, R>(directory: string, body: (registry: Tool.Interface) => Effect.Effect<A, E, R>) => {
const activeLocation = Layer.succeed(
Location.Service,
@@ -95,7 +81,13 @@ const withTool = <A, E, R>(directory: string, body: (registry: Tool.Interface) =
AppNodeBuilder.build(
LayerNode.group([Tool.node, Tool.node, LocationMutation.node, FileMutation.node, writeToolNode]),
[
[Environment.node, environment],
[
Environment.node,
transformEnvironmentFiles(activeLocation, (files) => ({
write: (target, content) =>
Effect.sync(() => writes.push(target)).pipe(Effect.andThen(files.write(target, content))),
})),
],
[Location.node, activeLocation],
[Formatter.node, formatter],
[Permission.node, permission],
+106
View File
@@ -0,0 +1,106 @@
import { beforeEach, expect } from "bun:test"
import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
import { Database } from "@opencode-ai/core/database/database"
import { makeMemoryDriver } from "@opencode-ai/core/environment"
import { Workspace } from "@opencode-ai/core/workspace"
import { WorkspaceDriver } from "@opencode-ai/core/workspace/driver"
import { WorkspaceTable } from "@opencode-ai/core/workspace/sql"
import { LayerNode } from "@opencode-ai/util/effect/layer-node"
import { eq } from "drizzle-orm"
import { Effect } from "effect"
import { TestClock } from "effect/testing"
import { ChildProcess } from "effect/unstable/process"
import { testEffect } from "./lib/effect"
const calls: Array<{ readonly operation: string; readonly binding?: WorkspaceDriver.Binding }> = []
const memory = makeMemoryDriver()
let failConnect = false
const driver = WorkspaceDriver.make({
create: ({ workspaceID }) => {
calls.push({ operation: "create" })
return Effect.succeed({ binding: { workspaceID, generation: 0 } })
},
connect: ({ binding }) => {
calls.push({ operation: "connect", binding })
if (failConnect) return Effect.fail(new WorkspaceDriver.Error({ message: "wake failed" }))
return Effect.succeed(memory)
},
suspendForIdle: ({ binding, saveBinding }) => {
calls.push({ operation: "suspendForIdle", binding })
return saveBinding({ ...binding, generation: Number(binding.generation) + 1, suspended: true })
},
destroy: ({ binding }) => {
calls.push({ operation: "destroy", binding })
return Effect.void
},
})
const it = testEffect(
AppNodeBuilder.build(
LayerNode.group([Database.node, Workspace.configured({ idleThreshold: "5 minutes", pollInterval: "1 minute" })]),
[[WorkspaceDriver.node, WorkspaceDriver.registryNode({ fake: driver })]],
),
)
beforeEach(() => {
calls.splice(0)
failConnect = false
})
it.effect("persists the workspace lifecycle and reconnects after idle suspension", () =>
Effect.gen(function* () {
const workspace = yield* Workspace.Service
const created = yield* workspace.create("fake")
expect(created.id.startsWith("wrk_")).toBe(true)
expect(created.binding).toEqual({ workspaceID: created.id, generation: 0 })
const environment = yield* workspace.connect(created.id)
expect(calls.map((call) => call.operation)).toEqual(["create"])
yield* TestClock.adjust("4 minutes")
yield* Effect.scoped(environment.spawner.spawn(ChildProcess.make("activity"))).pipe(Effect.exit)
yield* TestClock.adjust("4 minutes")
expect(calls.map((call) => call.operation)).toEqual(["create", "connect"])
yield* TestClock.adjust("2 minutes")
expect(calls.map((call) => call.operation)).toEqual(["create", "connect", "suspendForIdle"])
const stored = yield* Database.Service.use(({ db }) =>
db.select().from(WorkspaceTable).where(eq(WorkspaceTable.id, created.id)).get(),
).pipe(Effect.orDie)
expect(stored?.binding).toEqual({ workspaceID: created.id, generation: 1, suspended: true })
expect(stored?.last_used_at).toBe(4 * 60 * 1000)
yield* Effect.scoped(environment.spawner.spawn(ChildProcess.make("wake"))).pipe(Effect.exit)
expect(calls.map((call) => call.operation)).toEqual(["create", "connect", "suspendForIdle", "connect"])
expect(calls.at(-1)?.binding).toEqual({ workspaceID: created.id, generation: 1, suspended: true })
yield* workspace.destroy(created.id)
expect(calls.at(-1)?.operation).toBe("destroy")
}),
)
it.effect("surfaces wake failures through the spawn error channel", () =>
Effect.gen(function* () {
const workspace = yield* Workspace.Service
const created = yield* workspace.create("fake")
const environment = yield* workspace.connect(created.id)
yield* Effect.scoped(environment.spawner.spawn(ChildProcess.make("connect"))).pipe(Effect.exit)
yield* TestClock.adjust("6 minutes")
failConnect = true
const error = yield* Effect.scoped(environment.spawner.spawn(ChildProcess.make("wake"))).pipe(Effect.flip)
expect(error).toMatchObject({
_tag: "PlatformError",
reason: {
_tag: "Unknown",
module: "Workspace",
method: "spawn",
description: `Failed to wake workspace ${created.id}`,
},
})
}),
)
+139 -14
View File
@@ -198,13 +198,13 @@ describe("FlowchartDiagram", () => {
B --> A`)
expectDiagram(output).toEqualDiagram(`
╭──────────────
╭───╮ ╭─┴─╮
│ A ├─────────▶│ B │
╰───╯ ╰───╯
╭───────────╮
│ │
│ │
▼ │
╭───╮ ╭─┴─╮
│ A ├──────▶│ B │
╰───╯ ╰───╯
`)
})
@@ -599,7 +599,7 @@ flowchart LR
const output = renderFlowchartDiagram(content)
expect(diagram.edges).toEqual([{ from: "Build", to: "Ship", label: "", style: "thick" }])
expect(output).toContain("━━━━━━━━━▶")
expect(output).toContain("━━━━━━▶")
})
test("parses and renders Mermaid dashed edges", () => {
@@ -611,7 +611,7 @@ flowchart LR
const output = renderFlowchartDiagram(content)
expect(diagram.edges).toEqual([{ from: "Build", to: "Ship", label: "", style: "dashed" }])
expect(output).toContain("─────────▶")
expect(output).toContain("──────▶")
})
test("paints horizontal and vertical dashed routes with solid terminal cells", () => {
@@ -689,11 +689,11 @@ graph LR
`)
expectDiagram(output).toEqualDiagram(`
╭───────╮
╭────────╮ ╭─────╮ ├───────┤
│ Client ├─────────▶│ API ├─────────▶│ Cache │
╰────────╯ ╰─────╯ ├───────┤
╰───────╯
╭───────╮
╭────────╮ ╭─────╮ ├───────┤
│ Client ├──────▶│ API ├──────▶│ Cache │
╰────────╯ ╰─────╯ ├───────┤
╰───────╯
`)
})
@@ -1062,6 +1062,131 @@ flowchart LR
}
})
test.each(["TD", "BT", "LR", "RL"] as const)(
"keeps parallel top-level subgraphs in the same rank for %s diagrams",
(direction) => {
const layout = layoutFlowchartDiagram(`flowchart ${direction}
subgraph source [Source]
A[A]
end
subgraph left [Left]
B[B]
end
subgraph right [Right]
C[C]
end
A --> B
A --> C`)
const source = layout.subgraphBounds.get("source")!
const left = layout.subgraphBounds.get("left")!
const right = layout.subgraphBounds.get("right")!
const leftNode = layout.bounds.get("B")!
const rightNode = layout.bounds.get("C")!
const horizontal = direction === "LR" || direction === "RL"
const reversed = direction === "BT" || direction === "RL"
const start = (bound: typeof source) => {
const value = horizontal ? bound.left : bound.top
const size = horizontal ? bound.width : bound.height
return reversed ? -(value + size) : value
}
const size = (bound: typeof source) => (horizontal ? bound.width : bound.height)
expect(horizontal ? leftNode.centerX : leftNode.centerY).toBe(horizontal ? rightNode.centerX : rightNode.centerY)
expect(Math.min(start(left), start(right))).toBeGreaterThanOrEqual(start(source) + size(source))
},
)
test.each(["TD", "BT", "LR", "RL"] as const)(
"separates oversized parallel subgraph labels across %s diagrams",
(direction) => {
const horizontal = direction === "LR" || direction === "RL"
const label = horizontal ? "one<br/>two<br/>three<br/>four<br/>five" : "A very very wide downstream subgraph title"
const layout = layoutFlowchartDiagram(`flowchart ${direction}
subgraph source [Source]
A[A]
end
subgraph left [${label}]
B[B]
end
subgraph right [Right]
C[C]
end
A --> B
A --> C`)
const left = layout.subgraphBounds.get("left")!
const right = layout.subgraphBounds.get("right")!
const overlap =
left.left < right.left + right.width &&
left.left + left.width > right.left &&
left.top < right.top + right.height &&
left.top + left.height > right.top
expect(overlap).toBe(false)
},
)
test.each(["TD", "BT", "LR", "RL"] as const)(
"ranks nested order-only subgraph dependencies for %s diagrams",
(direction) => {
const layout = layoutFlowchartDiagram(`flowchart ${direction}
subgraph first [First]
subgraph firstInner [First inner]
A[A]
end
end
subgraph second [Second]
subgraph secondInner [Second inner]
B[B]
end
end
firstInner ~~~ secondInner`)
const first = layout.subgraphBounds.get("first")!
const second = layout.subgraphBounds.get("second")!
const horizontal = direction === "LR" || direction === "RL"
const reversed = direction === "BT" || direction === "RL"
const start = (bound: typeof first) => {
const value = horizontal ? bound.left : bound.top
const size = horizontal ? bound.width : bound.height
return reversed ? -(value + size) : value
}
const size = horizontal ? first.width : first.height
expect(start(second)).toBeGreaterThanOrEqual(start(first) + size)
},
)
test.each(["TD", "BT", "LR", "RL"] as const)(
"ranks cyclic top-level subgraphs as one downstream component for %s diagrams",
(direction) => {
const layout = layoutFlowchartDiagram(`flowchart ${direction}
subgraph source [Source]
A[A]
end
subgraph first [First]
B[B]
end
subgraph second [Second]
C[C]
end
A --> B
B --> C
C --> B`)
const source = layout.subgraphBounds.get("source")!
const first = layout.subgraphBounds.get("first")!
const second = layout.subgraphBounds.get("second")!
const horizontal = direction === "LR" || direction === "RL"
const reversed = direction === "BT" || direction === "RL"
const start = (bound: typeof source) => {
const value = horizontal ? bound.left : bound.top
const size = horizontal ? bound.width : bound.height
return reversed ? -(value + size) : value
}
const size = (bound: typeof source) => (horizontal ? bound.width : bound.height)
expect(Math.min(start(first), start(second))).toBeGreaterThanOrEqual(start(source) + size(source))
},
)
test("moves subgraph labels away from crossing routes", () => {
const output = renderFlowchartDiagram(`
flowchart TD
+142 -45
View File
@@ -27,7 +27,7 @@ import type {
export const DEFAULT_MIN_NODE_GAP = 5
export const DEFAULT_MIN_BRANCH_LABEL_GAP = 12
export const DEFAULT_MIN_RANK_GAP = 10
export const DEFAULT_MIN_RANK_GAP = 7
export const DEFAULT_MIN_VERTICAL_RANK_GAP = 4
export const COMPACT_MIN_RANK_GAP = 4
export const COMPACT_MIN_VERTICAL_RANK_GAP = 2
@@ -511,19 +511,79 @@ function collectSubgraphNodeIds(diagram: FlowchartDiagram, subgraphId: string):
return nodeIds
}
function rankGraphComponents(ids: readonly string[], outgoing: ReadonlyMap<string, ReadonlySet<string>>): Map<string, number> {
const reachable = new Map<string, Set<string>>()
for (const id of ids) {
const seen = new Set<string>()
const queue = [id]
for (let index = 0; index < queue.length; index++) {
const current = queue[index]!
if (seen.has(current)) continue
seen.add(current)
queue.push(...(outgoing.get(current) ?? []))
}
reachable.set(id, seen)
}
const componentById = new Map<string, number>()
const components: string[][] = []
for (const id of ids) {
if (componentById.has(id)) continue
const component = ids.filter(
(candidate) => !componentById.has(candidate) && reachable.get(id)!.has(candidate) && reachable.get(candidate)!.has(id),
)
const componentIndex = components.length
components.push(component)
for (const member of component) componentById.set(member, componentIndex)
}
const componentOutgoing = new Map(components.map((_, index) => [index, new Set<number>()]))
const incoming = new Map(components.map((_, index) => [index, 0]))
for (const [from, targets] of outgoing) {
const fromComponent = componentById.get(from)!
for (const to of targets) {
const toComponent = componentById.get(to)!
if (fromComponent === toComponent || componentOutgoing.get(fromComponent)!.has(toComponent)) continue
componentOutgoing.get(fromComponent)!.add(toComponent)
incoming.set(toComponent, incoming.get(toComponent)! + 1)
}
}
const componentRanks = new Map<number, number>()
const queue = components.map((_, index) => index).filter((index) => incoming.get(index) === 0)
for (const component of queue) componentRanks.set(component, 0)
for (let index = 0; index < queue.length; index++) {
const component = queue[index]!
for (const to of componentOutgoing.get(component)!) {
componentRanks.set(to, Math.max(componentRanks.get(to) ?? 0, componentRanks.get(component)! + 1))
incoming.set(to, incoming.get(to)! - 1)
if (incoming.get(to) === 0) queue.push(to)
}
}
return new Map(ids.map((id) => [id, componentRanks.get(componentById.get(id)!) ?? 0]))
}
function separateTopLevelItems(
diagram: FlowchartDiagram,
nodeBounds: Map<string, FlowchartNodeBounds>,
subgraphBounds: ReadonlyMap<string, FlowchartSubgraphBounds>,
gap: number,
): void {
): boolean {
const hasLocalDirection = (diagram.subgraphs ?? []).some(
(subgraph) => subgraph.direction && subgraph.direction !== diagram.direction,
)
const coveredNodeIds = new Set<string>()
const items: { id: string; bounds: FlowchartBounds; nodeIds: Set<string>; rank: number }[] = []
const itemByEndpoint = new Map<string, string>()
for (const subgraph of diagram.subgraphs ?? []) {
const subgraphs = diagram.subgraphs ?? []
const subgraphById = new Map(subgraphs.map((subgraph) => [subgraph.id, subgraph]))
const topLevelSubgraphId = (id: string): string => {
let current = subgraphById.get(id)
while (current?.parentId) current = subgraphById.get(current.parentId)
return current?.id ?? id
}
for (const subgraph of subgraphs) {
if (subgraph.parentId) continue
const bounds = subgraphBounds.get(subgraph.id)
const nodeIds = collectSubgraphNodeIds(diagram, subgraph.id)
@@ -535,6 +595,7 @@ function separateTopLevelItems(
itemByEndpoint.set(nodeId, subgraph.id)
}
}
for (const subgraph of subgraphs) itemByEndpoint.set(subgraph.id, topLevelSubgraphId(subgraph.id))
for (const node of diagram.nodes) {
if (coveredNodeIds.has(node.id)) continue
@@ -543,12 +604,19 @@ function separateTopLevelItems(
items.push({ id: node.id, bounds, nodeIds: new Set([node.id]), rank: 0 })
itemByEndpoint.set(node.id, node.id)
}
if (items.length < 2) return
if (items.length < 2) return false
const horizontal = isHorizontalDirection(diagram.direction)
const moveItem = (item: (typeof items)[number], dx: number, dy: number): void => {
for (const nodeId of item.nodeIds) {
const bounds = nodeBounds.get(nodeId)
if (bounds) translateBounds(bounds, dx, dy)
}
}
if (hasLocalDirection) {
items.sort((a, b) => (horizontal ? a.bounds.left - b.bounds.left : a.bounds.top - b.bounds.top))
let cursor: number | undefined
let moved = false
for (const item of items) {
const start = horizontal ? item.bounds.left : item.bounds.top
const size = horizontal ? item.bounds.width : item.bounds.height
@@ -557,42 +625,31 @@ function separateTopLevelItems(
continue
}
const shift = cursor - start
for (const nodeId of item.nodeIds) {
const bounds = nodeBounds.get(nodeId)
if (bounds) translateBounds(bounds, horizontal ? shift : 0, horizontal ? 0 : shift)
}
moved ||= shift !== 0
moveItem(item, horizontal ? shift : 0, horizontal ? 0 : shift)
cursor = start + shift + size + gap
}
return
return moved
}
const topLevelIds = new Set(
(diagram.subgraphs ?? []).filter((subgraph) => !subgraph.parentId).map((subgraph) => subgraph.id),
)
const topLevelIds = new Set(subgraphs.filter((subgraph) => !subgraph.parentId).map((subgraph) => subgraph.id))
const rankedItems = items.filter((item) => topLevelIds.has(item.id))
if (rankedItems.length < 2) return
if (rankedItems.length < 2) return false
const itemById = new Map(rankedItems.map((item) => [item.id, item]))
const outgoing = new Map(rankedItems.map((item) => [item.id, new Set<string>()]))
const incoming = new Map(rankedItems.map((item) => [item.id, 0]))
for (const edge of diagram.edges) {
const from = itemByEndpoint.get(edge.from)
const to = itemByEndpoint.get(edge.to)
if (!from || !to || from === to || !itemById.has(from) || !itemById.has(to) || outgoing.get(from)!.has(to)) continue
outgoing.get(from)!.add(to)
incoming.set(to, incoming.get(to)! + 1)
}
const queue = rankedItems.filter((item) => incoming.get(item.id) === 0)
for (let index = 0; index < queue.length; index++) {
const item = queue[index]!
for (const to of outgoing.get(item.id)!) {
const downstream = itemById.get(to)!
downstream.rank = Math.max(downstream.rank, item.rank + 1)
incoming.set(to, incoming.get(to)! - 1)
if (incoming.get(to) === 0) queue.push(downstream)
}
}
const ranks = rankGraphComponents(
rankedItems.map((item) => item.id),
outgoing,
)
for (const item of rankedItems) item.rank = ranks.get(item.id)!
const reversed = diagram.direction === "RL" || diagram.direction === "BT"
const primaryStart = (item: (typeof items)[number]): number => {
@@ -600,28 +657,54 @@ function separateTopLevelItems(
const size = horizontal ? item.bounds.width : item.bounds.height
return reversed ? -(start + size) : start
}
rankedItems.sort((a, b) => a.rank - b.rank || primaryStart(a) - primaryStart(b))
const itemsByRank = Map.groupBy(rankedItems, (item) => item.rank)
const rankKeys = [...itemsByRank.keys()].sort((a, b) => a - b)
let cursor: number | undefined
for (const item of rankedItems) {
const start = primaryStart(item)
const size = horizontal ? item.bounds.width : item.bounds.height
let moved = false
for (const rank of rankKeys) {
const rankItems = itemsByRank.get(rank)!
const start = Math.min(...rankItems.map(primaryStart))
const end = Math.max(
...rankItems.map((item) => primaryStart(item) + (horizontal ? item.bounds.width : item.bounds.height)),
)
if (cursor === undefined) {
cursor = start + size + gap
cursor = end + gap
continue
}
const shift = Math.max(0, cursor - start)
if (shift > 0) {
for (const nodeId of item.nodeIds) {
const bounds = nodeBounds.get(nodeId)
if (bounds) {
const offset = reversed ? -shift : shift
translateBounds(bounds, horizontal ? offset : 0, horizontal ? 0 : offset)
}
moved = true
for (const item of rankItems) {
const offset = reversed ? -shift : shift
moveItem(item, horizontal ? offset : 0, horizontal ? 0 : offset)
}
}
cursor = start + shift + size + gap
cursor = end + shift + gap
}
for (const rank of rankKeys) {
const rankItems = itemsByRank
.get(rank)!
.toSorted((a, b) =>
horizontal ? a.bounds.top - b.bounds.top : a.bounds.left - b.bounds.left,
)
let crossCursor: number | undefined
for (const item of rankItems) {
const start = horizontal ? item.bounds.top : item.bounds.left
const size = horizontal ? item.bounds.height : item.bounds.width
if (crossCursor === undefined) {
crossCursor = start + size + gap
continue
}
const shift = Math.max(0, crossCursor - start)
if (shift > 0) {
moved = true
moveItem(item, horizontal ? 0 : shift, horizontal ? shift : 0)
}
crossCursor = start + shift + size + gap
}
}
return moved
}
function layoutSubgraphs(
@@ -676,13 +759,27 @@ function layoutFlowchartWithDirection(
const bounds = layoutRankedNodes(diagram, direction, sizes, minNodeGap, requestedMinRankGap)
layoutLocalSubgraphDirections(diagram, bounds, sizes, minNodeGap, requestedMinRankGap)
let routes = routeFlowchartEdges(diagram, bounds, (edge) => edgeDirection(diagram, edge))
let subgraphBounds = layoutSubgraphs(diagram, bounds, routes)
separateTopLevelItems(diagram, bounds, subgraphBounds, Math.max(1, Math.floor(requestedMinRankGap / 2)))
routes = routeFlowchartEdges(diagram, bounds, (edge) => edgeDirection(diagram, edge))
subgraphBounds = layoutSubgraphs(diagram, bounds, routes)
routes = routeFlowchartEdges(diagram, bounds, (edge) => edgeDirection(diagram, edge), subgraphBounds)
subgraphBounds = layoutSubgraphs(diagram, bounds, routes)
const subgraphs = diagram.subgraphs ?? []
let subgraphBounds = new Map<string, FlowchartSubgraphBounds>()
let routes: FlowchartEdgeRoute[]
if (subgraphs.length === 0) {
routes = routeFlowchartEdges(diagram, bounds, (edge) => edgeDirection(diagram, edge))
} else {
routes = routeFlowchartEdges(diagram, bounds, (edge) => edgeDirection(diagram, edge))
subgraphBounds = layoutSubgraphs(diagram, bounds, routes)
const moved = separateTopLevelItems(
diagram,
bounds,
subgraphBounds,
Math.max(1, Math.floor(requestedMinRankGap / 2)),
)
if (moved) {
routes = routeFlowchartEdges(diagram, bounds, (edge) => edgeDirection(diagram, edge))
subgraphBounds = layoutSubgraphs(diagram, bounds, routes)
}
routes = routeFlowchartEdges(diagram, bounds, (edge) => edgeDirection(diagram, edge), subgraphBounds)
subgraphBounds = layoutSubgraphs(diagram, bounds, routes)
}
const allBounds = [...bounds.values(), ...subgraphBounds.values(), ...routeRenderBounds(routes)]
const dx = Math.max(0, -Math.min(0, ...allBounds.map((bound) => bound.left)))
const dy = Math.max(0, -Math.min(0, ...allBounds.map((bound) => bound.top)))
+51
View File
@@ -0,0 +1,51 @@
import { describe, expect, test } from "bun:test"
import { RGBA } from "@opentui/core"
import { createOpenCodeDiagramPalette } from "./palette.js"
type Rgb = readonly [number, number, number]
const rgb = (value: Rgb) => RGBA.fromInts(...value)
describe("OpenCode diagram palette", () => {
test.each(
[
{
name: "dark theme",
text: [230, 232, 240],
subdued: [114, 120, 138],
secondary: [172, 176, 189],
muted: [149, 154, 169],
},
{
name: "light theme",
text: [32, 35, 43],
subdued: [119, 125, 138],
secondary: [76, 80, 91],
muted: [93, 98, 110],
},
] satisfies ReadonlyArray<{
name: string
text: Rgb
subdued: Rgb
secondary: Rgb
muted: Rgb
}>,
)("derives a controlled neutral ladder for a $name", ({ text, subdued, secondary, muted }) => {
const primary = rgb(text)
const info = RGBA.fromInts(40, 120, 220)
const background = RGBA.fromInts(10, 20, 30)
const palette = createOpenCodeDiagramPalette({
text: primary,
subdued: rgb(subdued),
info,
background,
})
expect(palette.text).toBe(primary)
expect(palette.primary).toBe(primary)
expect(palette.secondary.equals(rgb(secondary))).toBe(true)
expect(palette.muted.equals(rgb(muted))).toBe(true)
expect(palette.warning).toBe(info)
expect(palette.background).toBe(background)
})
})
+20
View File
@@ -0,0 +1,20 @@
import type { RGBA } from "@opentui/core"
import { blendColor } from "./core/color/style.js"
export interface OpenCodeDiagramPaletteInput {
readonly text: RGBA
readonly subdued: RGBA
readonly info: RGBA
readonly background: RGBA
}
export function createOpenCodeDiagramPalette(input: OpenCodeDiagramPaletteInput) {
return {
text: input.text,
primary: input.text,
secondary: blendColor(input.text, input.subdued, 0.5),
muted: blendColor(input.text, input.subdued, 0.7),
warning: input.info,
background: input.background,
}
}
+6 -7
View File
@@ -1,5 +1,6 @@
import { Plugin } from "@opencode-ai/plugin/tui"
import { createMermaidCodeBlockRenderer } from "./markdown.js"
import { createOpenCodeDiagramPalette } from "./palette.js"
export default Plugin.define({
id: "opencode.merman",
@@ -7,14 +8,12 @@ export default Plugin.define({
context.markdown.registerCodeBlockRenderer(
"mermaid",
createMermaidCodeBlockRenderer(context.renderer, () => ({
colors: {
text: context.theme.markdown.text,
primary: context.theme.text.default,
secondary: context.theme.text.subdued,
muted: context.theme.border.default,
warning: context.theme.text.feedback.info.default,
colors: createOpenCodeDiagramPalette({
text: context.theme.text.default,
subdued: context.theme.text.subdued,
info: context.theme.text.feedback.info.default,
background: context.theme.background.default,
},
}),
})),
)
},
+6
View File
@@ -28,6 +28,7 @@ import { SessionRestart } from "@opencode-ai/core/session/execution/restart"
import { PluginRuntime } from "@opencode-ai/core/plugin/runtime"
import { SdkPlugins } from "@opencode-ai/core/plugin/sdk"
import { WellKnown } from "@opencode-ai/core/wellknown"
import { WorkspaceDriver } from "@opencode-ai/core/workspace/driver"
import { Watcher } from "@opencode-ai/core/filesystem/watcher"
import { HttpRouter } from "effect/unstable/http"
import { HttpApiBuilder } from "effect/unstable/httpapi"
@@ -43,6 +44,7 @@ import { formLocationLayer } from "./middleware/form-location"
import { sessionLocationLayer } from "./middleware/session-location"
import { ServerInfo } from "./server-info"
import type { ServerOptions } from "./options"
import { modalWorkspaceDriver, provider as modalProvider } from "./workspace/modal-workspace"
const applicationServices = LayerNode.group([
Database.node,
@@ -115,6 +117,10 @@ function makeRoutes<AuthError, AuthServices>(
],
[PluginRuntime.node, PluginRuntime.layerWithCell(pluginRuntimeCell)],
[PluginRuntime.providerNode, PluginRuntime.providerNodeWithCell(pluginRuntimeCell)],
[
WorkspaceDriver.node,
WorkspaceDriver.registryNode({ [modalProvider]: modalWorkspaceDriver({ app: "opencode-workspaces" }) }),
],
]
const serviceLayer = options.simulation
? Layer.unwrap(
@@ -0,0 +1,129 @@
import { WorkspaceDriver } from "@opencode-ai/core/workspace/driver"
import { Effect, Option, Schema } from "effect"
import type { App, Image, ModalClient, ModalClientParams, Sandbox } from "modal"
import { createModalSandboxWithClient, makeModalDriver, type ModalImageSpec, openModalClient } from "./modal"
export const provider = "modal"
export const ModalBinding = Schema.Struct({
sandboxId: Schema.optional(Schema.String),
snapshotImageId: Schema.optional(Schema.String),
})
export type ModalBinding = typeof ModalBinding.Type
export interface ModalWorkspaceOptions {
readonly app: string
readonly client?: ModalClientParams
readonly image?: ModalImageSpec
}
export const modalWorkspaceDriver = (options: ModalWorkspaceOptions): WorkspaceDriver.Interface => {
const name = (workspaceID: string) => `ws-${workspaceID}`
const decodeBinding = Schema.decodeUnknownOption(ModalBinding)
let clientPromise: Promise<ModalClient> | undefined
let appPromise: Promise<App> | undefined
// The SDK client and app handle are shared for the process lifetime of this driver.
const client = () => (clientPromise ??= openModalClient(options.client))
const app = () =>
(appPromise ??= client().then((value) => value.apps.fromName(options.app, { createIfMissing: true })))
const attempt = <A>(run: () => Promise<A>) =>
Effect.tryPromise({ try: run, catch: (cause) => new WorkspaceDriver.Error({ cause }) })
const binding = (value: WorkspaceDriver.Binding): ModalBinding => Option.getOrElse(decodeBinding(value), () => ({}))
const live = async (lookup: () => Promise<Sandbox>) => {
const { NotFoundError } = await import("modal")
const sandbox = await lookup().catch((error) => {
if (error instanceof NotFoundError) return undefined
throw error
})
if (sandbox && (await sandbox.poll()) === null) return sandbox
}
const findLive = async (modalClient: ModalClient, value: ModalBinding, workspaceID: string) => {
if (value.sandboxId) {
const sandboxID = value.sandboxId
const sandbox = await live(() => modalClient.sandboxes.fromId(sandboxID))
if (sandbox) return sandbox
}
// Name fallback is valid only before the first snapshot; afterward a live named sandbox is stale by design.
if (value.snapshotImageId) return
return live(() => modalClient.sandboxes.fromName(options.app, name(workspaceID)))
}
const createSandbox = async (workspaceID: string, image?: Image) => {
const { AlreadyExistsError } = await import("modal")
const modalClient = await client()
return createModalSandboxWithClient(
modalClient,
await app(),
{
image: options.image,
sandbox: {
name: name(workspaceID),
tags: { workspace: workspaceID },
timeoutMs: 24 * 60 * 60 * 1000,
},
},
image,
).catch((error) => {
if (error instanceof AlreadyExistsError) return modalClient.sandboxes.fromName(options.app, name(workspaceID))
throw error
})
}
const deleteImage = (modalClient: ModalClient, imageID?: string) =>
imageID ? attempt(() => modalClient.images.delete(imageID)).pipe(Effect.ignore) : Effect.void
const terminate = (sandbox?: Sandbox) =>
sandbox ? attempt(() => sandbox.terminate({ wait: true })).pipe(Effect.ignore) : Effect.void
return WorkspaceDriver.make({
create: ({ workspaceID }) =>
attempt(async () => {
const sandbox = await createSandbox(workspaceID)
return { binding: { sandboxId: sandbox.sandboxId } }
}),
connect: ({ workspaceID, binding: value, saveBinding }) =>
Effect.gen(function* () {
const modalBinding = binding(value)
const modalClient = yield* attempt(client)
const sandbox = yield* attempt(async () => {
const existing = await findLive(modalClient, modalBinding, workspaceID)
const image =
existing || !modalBinding.snapshotImageId
? undefined
: await modalClient.images.fromId(modalBinding.snapshotImageId)
return existing ?? createSandbox(workspaceID, image)
})
if (modalBinding.sandboxId !== sandbox.sandboxId) {
yield* saveBinding({ ...modalBinding, sandboxId: sandbox.sandboxId })
}
return makeModalDriver(sandbox)
}),
suspendForIdle: ({ workspaceID, binding: value, saveBinding }) =>
Effect.gen(function* () {
const modalBinding = binding(value)
const modalClient = yield* attempt(client)
const sandbox = yield* attempt(() => findLive(modalClient, modalBinding, workspaceID))
if (!sandbox) return
const snapshot = yield* attempt(() => sandbox.snapshotFilesystem({ ttlMs: null }))
yield* saveBinding({ snapshotImageId: snapshot.imageId })
yield* Effect.all([deleteImage(modalClient, modalBinding.snapshotImageId), terminate(sandbox)], {
concurrency: "unbounded",
discard: true,
})
}),
destroy: ({ workspaceID, binding: value }) =>
Effect.gen(function* () {
const modalBinding = binding(value)
const modalClient = yield* attempt(client)
const sandbox = yield* attempt(() => findLive(modalClient, modalBinding, workspaceID))
yield* Effect.all([terminate(sandbox), deleteImage(modalClient, modalBinding.snapshotImageId)], {
concurrency: "unbounded",
discard: true,
})
}),
})
}
+65 -19
View File
@@ -3,7 +3,7 @@ import { systemError } from "effect/PlatformError"
import type { Command, KillOptions } from "effect/unstable/process/ChildProcess"
import { ExitCode, make, makeHandle, ProcessId } from "effect/unstable/process/ChildProcessSpawner"
import type { Driver } from "@opencode-ai/core/environment"
import type { ModalClientParams, Sandbox, SandboxCreateParams } from "modal"
import type { App, Image, ModalClient, ModalClientParams, Sandbox, SandboxCreateParams } from "modal"
const INNER_WRAPPER = `
pidfile=$1
@@ -13,15 +13,30 @@ trap 'rm -f -- "$pidfile"' EXIT
"$@"
`
// Modal's VM runtime accepts process-group signals without delivering them
// (kill(-pgid) returns 0 and nothing dies; direct-pid signals work), so the
// group is enumerated from /proc and each member is signalled directly. The
// second pass catches children forked between scan and signal.
const KILL = `
pidfile=$1
sig=$2
i=0
while [ ! -s "$1" ] && [ "$i" -lt 250 ]; do sleep 0.02; i=$((i + 1)); done
if [ -s "$1" ]; then
pid=$(cat "$1")
/bin/kill "-$2" "-$pid" 2>/dev/null || true
else
exit 47
fi
while [ ! -s "$pidfile" ] && [ "$i" -lt 250 ]; do sleep 0.02; i=$((i + 1)); done
[ -s "$pidfile" ] || exit 47
target=$(cat "$pidfile")
pass=0
while [ "$pass" -lt 2 ]; do
for stat in /proc/[0-9]*/stat; do
[ -e "$stat" ] || continue
pid=\${stat#/proc/}
pid=\${pid%/stat}
set -- $(sed "s/.*) //" "$stat" 2>/dev/null)
if [ "\${3:-}" = "$target" ]; then
/bin/kill "-$sig" "$pid" 2>/dev/null || true
fi
done
pass=$((pass + 1))
done
`
export interface ModalImageSpec {
@@ -29,13 +44,16 @@ export interface ModalImageSpec {
readonly dockerfileCommands: ReadonlyArray<string>
}
export interface ModalSandboxOptions {
readonly app: string
readonly client?: ModalClientParams
export interface ModalSandboxCreateOptions {
readonly image?: ModalImageSpec
readonly sandbox?: SandboxCreateParams
}
export interface ModalSandboxOptions extends ModalSandboxCreateOptions {
readonly app: string
readonly client?: ModalClientParams
}
/**
* Ubuntu supplies the GNU coreutils and findutils required by the derived Files
* scripts. Busybox images do not satisfy the Environment contract.
@@ -49,12 +67,12 @@ export const ubuntuImage: ModalImageSpec = {
/** Creates a Modal sandbox lazily, keeping the SDK off the server startup path when Modal is unused. */
export const createModalSandbox = async (options: ModalSandboxOptions) => {
const { ModalClient } = await import("modal")
const client = new ModalClient(options.client)
const client = await openModalClient(options.client)
const app = await client.apps.fromName(options.app, { createIfMissing: true })
const imageSpec = options.image ?? ubuntuImage
const image = client.images.fromRegistry(imageSpec.registry).dockerfileCommands([...imageSpec.dockerfileCommands])
const sandbox = await client.sandboxes.create(app, image, options.sandbox)
const sandbox = await createModalSandboxWithClient(client, app, {
image: options.image,
sandbox: options.sandbox,
})
return {
driver: makeModalDriver(sandbox),
sandbox,
@@ -62,14 +80,42 @@ export const createModalSandbox = async (options: ModalSandboxOptions) => {
}
}
export const openModalClient = async (params?: ModalClientParams) => {
const { ModalClient } = await import("modal")
return new ModalClient(params)
}
export const createModalSandboxWithClient = async (
client: ModalClient,
app: App,
options: ModalSandboxCreateOptions,
existingImage?: Image,
) => {
const imageSpec = options.image ?? ubuntuImage
const image =
existingImage ??
client.images.fromRegistry(imageSpec.registry).dockerfileCommands([...imageSpec.dockerfileCommands])
// Always Modal's Full-VM runtime (beta, enabled per account): a real kernel
// with real device nodes, so workspaces can run Docker and other
// kernel-dependent workloads. Costs versus gVisor, measured Aug 2026:
// per-exec floor ~285-535ms versus ~90-165ms, and filesystem snapshots only
// (no memory snapshots — acceptable; fs-snapshot is the persistence design).
return client.sandboxes.create(app, image, {
...options.sandbox,
experimentalOptions: { ...options.sandbox?.experimentalOptions, vm_runtime: true },
})
}
/**
* Adapts Modal exec to the Environment driver. Files intentionally has no native
* overrides: Modal exec and filesystem tools share the same roughly 175ms floor,
* so the derived exec defaults are the simplest implementation with no measured loss.
* overrides: exec latency dominates payload work (VM runtime floor measured
* ~285-535ms per exec, Aug 2026), so the derived exec defaults are the simplest
* implementation with no measured loss.
*
* Modal cannot signal a ContainerProcess. Each command therefore starts a new
* process group and records its leader in a unique pid file; kill runs a second
* sandbox command that signals that group. Pid files are removed best-effort.
* sandbox command that enumerates that group from /proc and signals each member
* directly (see KILL). Pid files are removed best-effort.
*/
export const makeModalDriver = (sandbox: Sandbox): Driver => {
const spawn = Effect.fnUntraced(function* (command: Command) {
@@ -0,0 +1,54 @@
import fs from "node:fs"
import os from "node:os"
import path from "node:path"
import { expect, test } from "bun:test"
import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
import { makeFiles } from "@opencode-ai/core/environment"
import { Workspace } from "@opencode-ai/core/workspace"
import { WorkspaceDriver } from "@opencode-ai/core/workspace/driver"
import { Effect, Layer } from "effect"
import { TestClock } from "effect/testing"
import { modalWorkspaceDriver, provider } from "../src/workspace/modal-workspace"
const enabled =
!!process.env.OPENCODE_TEST_MODAL &&
((!!process.env.MODAL_TOKEN_ID && !!process.env.MODAL_TOKEN_SECRET) ||
fs.existsSync(path.join(os.homedir(), ".modal.toml")))
const testLayer = Layer.provideMerge(
AppNodeBuilder.build(Workspace.configured({ idleThreshold: "1 minute", pollInterval: "1 minute" }), [
[
WorkspaceDriver.node,
WorkspaceDriver.registryNode({ [provider]: modalWorkspaceDriver({ app: "opencode-workspace-tests" }) }),
],
]),
TestClock.layer(),
)
const modalTest = enabled ? test : test.skip
modalTest(
"wakes a workspace from its filesystem snapshot",
() =>
Effect.runPromise(
Effect.gen(function* () {
const workspace = yield* Workspace.Service
yield* Effect.acquireUseRelease(
workspace.create(provider),
(created) =>
Effect.gen(function* () {
const environment = yield* workspace.connect(created.id)
const files = makeFiles(environment)
const file = `/tmp/opencode-workspace-${crypto.randomUUID()}.txt`
yield* files.write(file, new TextEncoder().encode("survived snapshot"))
yield* TestClock.adjust("2 minutes")
const restored = yield* files.read(file)
expect(new TextDecoder().decode(restored.bytes)).toBe("survived snapshot")
}),
(created) => workspace.destroy(created.id).pipe(Effect.ignore),
)
}).pipe(Effect.scoped, Effect.provide(testLayer)),
),
180_000,
)
-9
View File
@@ -1154,15 +1154,6 @@ function App(props: { pair?: DialogPairCredentials }) {
}
})
event.on("session.execution.failed", (evt, { workspace }) => {
if (workspace !== (location.current?.workspaceID ?? data.location.default().workspaceID)) return
toast.show({
variant: "error",
message: evt.data.error.message,
duration: 5000,
})
})
// Suppress the full-screen overlay for transient startup and event-stream retry states.
// Initial connection gets a longer grace period; retries surface more quickly.
const [showReconnecting, setShowReconnecting] = createSignal(false)
+7 -15
View File
@@ -1631,21 +1631,13 @@ function AssistantFooter(props: { message: SessionMessageAssistant }) {
const interrupted = createMemo(() => props.message.error?.message === "Step interrupted")
return (
<>
<Show when={props.message.error && !interrupted()}>
<box
border={["left"]}
paddingTop={1}
paddingBottom={1}
paddingLeft={2}
backgroundColor={theme.background.default}
customBorderChars={SplitBorder.customBorderChars}
borderColor={theme.text.feedback.error.default}
>
<text fg={theme.text.subdued}>{errorMessage(props.message.error)}</text>
<Show when={props.message.error && !interrupted() && !props.message.retry}>
<box paddingLeft={3}>
<text fg={theme.text.feedback.error.default}>Error: {errorMessage(props.message.error)}</text>
</box>
</Show>
<AssistantRetry retry={props.message.retry} />
<box paddingLeft={3} marginTop={props.message.error && !interrupted() ? 1 : 0}>
<box paddingLeft={3} marginTop={props.message.retry || (props.message.error && !interrupted()) ? 1 : 0}>
<text>
<span style={{ fg: props.message.error ? theme.text.subdued : local.agent.color(props.message.agent) }}>
{Locale.titlecase(props.message.agent)}
@@ -2045,9 +2037,9 @@ function AssistantRetry(props: { retry: SessionMessageAssistant["retry"] }) {
return (
<Show when={props.retry}>
{(retry) => (
<box paddingLeft={3} marginTop={1}>
<text fg={theme.text.subdued}>
Retry attempt {retry().attempt} scheduled: {retry().error.message} [{retry().error.type}]
<box paddingLeft={3}>
<text fg={theme.text.feedback.warning.default}>
Retry attempt {retry().attempt} scheduled: {retry().error.message}
</text>
</box>
)}