mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 08:39:02 +03:00
refactor(core): tighten worktree strategy and pty schemas and decode overlay once (#57950)
This commit is contained in:
committed by
GitHub
Unverified
parent
1b1ad488f8
commit
eeb9124b4e
@@ -4,6 +4,7 @@ import net from "node:net"
|
||||
import path from "node:path"
|
||||
import { Cause, Data, Duration, Effect, Schema, Semaphore } from "effect"
|
||||
import type { Handoff } from "@opencode/schema/persistent-pty"
|
||||
import { Session } from "@opencode/schema/session"
|
||||
|
||||
const ProtocolVersion = 7
|
||||
const MaxFrameBytes = 8 * 1024 * 1024
|
||||
@@ -19,7 +20,7 @@ export const WireTerminal = Schema.Struct({
|
||||
pid: Schema.NullOr(Schema.Number),
|
||||
title: Schema.String,
|
||||
foreground_process: Schema.NullOr(Schema.String),
|
||||
group_id: Schema.String,
|
||||
group_id: Session.ID,
|
||||
command: Schema.Array(Schema.String),
|
||||
cwd: Schema.String,
|
||||
cols: Schema.Number,
|
||||
@@ -681,8 +682,10 @@ async function* decoder(socket: net.Socket) {
|
||||
if (pending.length !== 0) throw new Error("opencode-pty truncated frame")
|
||||
}
|
||||
|
||||
const decodeWireResponse = Schema.decodeUnknownSync(Schema.fromJsonString(WireResponse))
|
||||
|
||||
function decode(payload: Uint8Array) {
|
||||
return Schema.decodeUnknownSync(WireResponse)(JSON.parse(Buffer.from(payload).toString("utf8")))
|
||||
return decodeWireResponse(Buffer.from(payload).toString("utf8"))
|
||||
}
|
||||
|
||||
function failure(kind: DaemonError["kind"], cause: unknown) {
|
||||
|
||||
@@ -337,7 +337,7 @@ const makeLayer = (options: Options = {}) =>
|
||||
},
|
||||
})
|
||||
.pipe(Effect.mapError(unavailable))
|
||||
if (attachment.role === "controller") current.set(Session.ID.make(attachment.terminal.group_id), id)
|
||||
if (attachment.role === "controller") current.set(attachment.terminal.group_id, id)
|
||||
return {
|
||||
info: toInfo(attachment.terminal),
|
||||
role: attachment.role,
|
||||
@@ -400,7 +400,7 @@ function toInfo(value: WireTerminal): Info {
|
||||
pid: value.pid ?? 0,
|
||||
...(status === "exited" ? { exitCode: value.lifecycle.exit_code ?? undefined } : {}),
|
||||
}),
|
||||
sessionID: Session.ID.make(value.group_id),
|
||||
sessionID: value.group_id,
|
||||
foregroundProcess: value.foreground_process,
|
||||
size: { cols: value.cols, rows: value.rows },
|
||||
output: { head: value.output_head, tail: value.output_tail },
|
||||
|
||||
@@ -145,14 +145,14 @@ const layer = Layer.effect(
|
||||
const changed = replaced ?? unarchived
|
||||
if (changed) yield* bus.publish(ProjectSchema.Event.Updated, fromRow(changed))
|
||||
if (!project.vcs) return project
|
||||
const directories: Array<{ projectID: ID; directory: AbsolutePath; strategy?: string }> = [
|
||||
const directories: Array<{ projectID: ID; directory: AbsolutePath; strategy?: Worktree.StrategyID }> = [
|
||||
{ projectID: project.id, directory: project.canonical },
|
||||
]
|
||||
if (project.directory !== project.canonical)
|
||||
directories.push({
|
||||
projectID: project.id,
|
||||
directory: project.directory,
|
||||
strategy: project.vcs.type === "git" ? "git" : undefined,
|
||||
strategy: project.vcs.type === "git" ? Worktree.StrategyID.make("git") : undefined,
|
||||
})
|
||||
// A missing directory row means this directory's resolution is a new durable
|
||||
// fact. The row insert commits atomically with the event, so a crash between
|
||||
|
||||
@@ -234,16 +234,21 @@ export function mergeOverlay(
|
||||
): Record<string, Json> | undefined {
|
||||
if (base === undefined) return overlay && decodeJsonRecord({ ...overlay })
|
||||
if (overlay === undefined) return decodeJsonRecord({ ...base })
|
||||
return decodeJsonRecord(
|
||||
Object.fromEntries(
|
||||
new Set([...Object.keys(base), ...Object.keys(overlay)]).values().map((key): [string, unknown] => {
|
||||
const left = base[key]
|
||||
const right = overlay[key]
|
||||
if (right === undefined) return [key, left]
|
||||
if (isRecord(left) && isRecord(right)) return [key, mergeOverlay(left, right) ?? {}]
|
||||
return [key, right]
|
||||
}),
|
||||
),
|
||||
return decodeJsonRecord(mergeRecord(base, overlay))
|
||||
}
|
||||
|
||||
function mergeRecord(
|
||||
base: Readonly<Record<string, unknown>>,
|
||||
overlay: Readonly<Record<string, unknown>>,
|
||||
): Record<string, unknown> {
|
||||
return Object.fromEntries(
|
||||
new Set([...Object.keys(base), ...Object.keys(overlay)]).values().map((key): [string, unknown] => {
|
||||
const left = base[key]
|
||||
const right = overlay[key]
|
||||
if (right === undefined) return [key, left]
|
||||
if (isRecord(left) && isRecord(right)) return [key, mergeRecord(left, right)]
|
||||
return [key, right]
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -83,7 +83,7 @@ export const Event = Worktree.Event
|
||||
|
||||
interface StoredInput {
|
||||
readonly directory: AbsolutePath
|
||||
readonly strategy?: string
|
||||
readonly strategy?: StrategyID
|
||||
readonly replace?: boolean
|
||||
}
|
||||
|
||||
@@ -260,7 +260,7 @@ const layer = Layer.effect(
|
||||
const stored = yield* ops.find(input.projectID, worktreeDirectory)
|
||||
if (!stored?.strategy) return yield* new InvalidDirectoryError({ directory: worktreeDirectory })
|
||||
const settings = yield* load(row.worktree, current)
|
||||
const strategy = yield* getStrategy(StrategyID.make(stored.strategy), settings.strategies)
|
||||
const strategy = yield* getStrategy(stored.strategy, settings.strategies)
|
||||
yield* strategy
|
||||
.remove({
|
||||
directory: worktreeDirectory,
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { integer, primaryKey, sqliteTable, text } from "drizzle-orm/sqlite-core"
|
||||
import type { Worktree } from "@opencode/schema/worktree"
|
||||
import { absoluteColumn } from "../database/path.js"
|
||||
import { ProjectSchema } from "../project/schema.js"
|
||||
import { ProjectTable } from "../project/sql.js"
|
||||
@@ -11,7 +12,7 @@ export const WorktreeTable = sqliteTable(
|
||||
.notNull()
|
||||
.references(() => ProjectTable.id, { onDelete: "cascade" }),
|
||||
directory: absoluteColumn().notNull(),
|
||||
strategy: text(),
|
||||
strategy: text().$type<Worktree.StrategyID>(),
|
||||
time_created: integer()
|
||||
.notNull()
|
||||
.$default(() => Date.now()),
|
||||
|
||||
@@ -345,7 +345,7 @@ function terminal(id: number) {
|
||||
pid: null,
|
||||
title: "test",
|
||||
foreground_process: null,
|
||||
group_id: "test",
|
||||
group_id: "ses_test",
|
||||
command: ["test"],
|
||||
cwd: "/tmp",
|
||||
cols: 80,
|
||||
|
||||
@@ -136,7 +136,9 @@ function stored(projectID: Project.ID) {
|
||||
.all()
|
||||
.pipe(
|
||||
Effect.orDie,
|
||||
Effect.map((rows) => rows.toSorted((a, b) => a.directory.localeCompare(b.directory))),
|
||||
Effect.map((rows): Array<{ directory: AbsolutePath; strategy: string | null }> =>
|
||||
rows.toSorted((a, b) => a.directory.localeCompare(b.directory)),
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
@@ -504,7 +506,11 @@ describe("Worktree", () => {
|
||||
yield* Effect.addFinalizer(() => Effect.promise(() => fs.rm(unavailable, { recursive: true, force: true })))
|
||||
yield* input.db
|
||||
.insert(WorktreeTable)
|
||||
.values({ project_id: input.projectID, directory: unavailable, strategy: "acme/missing" })
|
||||
.values({
|
||||
project_id: input.projectID,
|
||||
directory: unavailable,
|
||||
strategy: Worktree.StrategyID.make("acme/missing"),
|
||||
})
|
||||
.run()
|
||||
.pipe(Effect.orDie)
|
||||
|
||||
@@ -612,7 +618,7 @@ describe("Worktree", () => {
|
||||
.insert(WorktreeTable)
|
||||
.values([
|
||||
{ project_id: input.projectID, directory: target },
|
||||
{ project_id: input.projectID, directory: unchanged, strategy: "git" },
|
||||
{ project_id: input.projectID, directory: unchanged, strategy: Worktree.StrategyID.make("git") },
|
||||
])
|
||||
.run()
|
||||
.pipe(Effect.orDie)
|
||||
|
||||
Reference in New Issue
Block a user