fix: Windows e2e stability (CrossSpawnSpawner, snapshot isolation, session race guards) (#19163)

This commit is contained in:
Kit Langton
2026-03-25 19:49:14 -04:00
committed by GitHub
parent 5179b87aef
commit 8864fdce2f
5 changed files with 246 additions and 172 deletions
+2 -1
View File
@@ -6,7 +6,8 @@ const serverHost = process.env.PLAYWRIGHT_SERVER_HOST ?? "127.0.0.1"
const serverPort = process.env.PLAYWRIGHT_SERVER_PORT ?? "4096" const serverPort = process.env.PLAYWRIGHT_SERVER_PORT ?? "4096"
const command = `bun run dev -- --host 0.0.0.0 --port ${port}` const command = `bun run dev -- --host 0.0.0.0 --port ${port}`
const reuse = !process.env.CI const reuse = !process.env.CI
const workers = Number(process.env.PLAYWRIGHT_WORKERS ?? (process.env.CI ? 5 : 0)) || undefined const workers =
Number(process.env.PLAYWRIGHT_WORKERS ?? (process.env.CI ? (process.platform === "win32" ? 2 : 5) : 0)) || undefined
export default defineConfig({ export default defineConfig({
testDir: "./e2e", testDir: "./e2e",
+3 -2
View File
@@ -1,4 +1,5 @@
import { NodeChildProcessSpawner, NodeFileSystem, NodePath } from "@effect/platform-node" import { NodeFileSystem, NodePath } from "@effect/platform-node"
import * as CrossSpawnSpawner from "@/effect/cross-spawn-spawner"
import { Effect, Layer, ServiceMap, Stream } from "effect" import { Effect, Layer, ServiceMap, Stream } from "effect"
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process" import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"
import { makeRunPromise } from "@/effect/run-service" import { makeRunPromise } from "@/effect/run-service"
@@ -258,7 +259,7 @@ export namespace Git {
) )
export const defaultLayer = layer.pipe( export const defaultLayer = layer.pipe(
Layer.provide(NodeChildProcessSpawner.layer), Layer.provide(CrossSpawnSpawner.layer),
Layer.provide(NodeFileSystem.layer), Layer.provide(NodeFileSystem.layer),
Layer.provide(NodePath.layer), Layer.provide(NodePath.layer),
) )
+6 -1
View File
@@ -13,6 +13,7 @@ import { fn } from "@/util/fn"
import { Agent } from "@/agent/agent" import { Agent } from "@/agent/agent"
import { Plugin } from "@/plugin" import { Plugin } from "@/plugin"
import { Config } from "@/config/config" import { Config } from "@/config/config"
import { NotFoundError } from "@/storage/db"
import { ProviderTransform } from "@/provider/transform" import { ProviderTransform } from "@/provider/transform"
import { ModelID, ProviderID } from "@/provider/schema" import { ModelID, ProviderID } from "@/provider/schema"
@@ -60,7 +61,11 @@ export namespace SessionCompaction {
const config = await Config.get() const config = await Config.get()
if (config.compaction?.prune === false) return if (config.compaction?.prune === false) return
log.info("pruning") log.info("pruning")
const msgs = await Session.messages({ sessionID: input.sessionID }) const msgs = await Session.messages({ sessionID: input.sessionID }).catch((err) => {
if (NotFoundError.isInstance(err)) return undefined
throw err
})
if (!msgs) return
let total = 0 let total = 0
let pruned = 0 let pruned = 0
const toPrune = [] const toPrune = []
@@ -4,6 +4,15 @@ import { Session } from "./index"
import { MessageV2 } from "./message-v2" import { MessageV2 } from "./message-v2"
import { SessionTable, MessageTable, PartTable } from "./session.sql" import { SessionTable, MessageTable, PartTable } from "./session.sql"
import { ProjectTable } from "../project/project.sql" import { ProjectTable } from "../project/project.sql"
import { Log } from "../util/log"
const log = Log.create({ service: "session.projector" })
function foreign(err: unknown) {
if (typeof err !== "object" || err === null) return false
if ("code" in err && err.code === "SQLITE_CONSTRAINT_FOREIGNKEY") return true
return "message" in err && typeof err.message === "string" && err.message.includes("FOREIGN KEY constraint failed")
}
export type DeepPartial<T> = T extends object ? { [K in keyof T]?: DeepPartial<T[K]> | null } : T export type DeepPartial<T> = T extends object ? { [K in keyof T]?: DeepPartial<T[K]> | null } : T
@@ -76,6 +85,7 @@ export default [
const time_created = data.info.time.created const time_created = data.info.time.created
const { id, sessionID, ...rest } = data.info const { id, sessionID, ...rest } = data.info
try {
db.insert(MessageTable) db.insert(MessageTable)
.values({ .values({
id, id,
@@ -85,6 +95,10 @@ export default [
}) })
.onConflictDoUpdate({ target: MessageTable.id, set: { data: rest } }) .onConflictDoUpdate({ target: MessageTable.id, set: { data: rest } })
.run() .run()
} catch (err) {
if (!foreign(err)) throw err
log.warn("ignored late message update", { messageID: id, sessionID })
}
}), }),
SyncEvent.project(MessageV2.Event.Removed, (db, data) => { SyncEvent.project(MessageV2.Event.Removed, (db, data) => {
@@ -102,6 +116,7 @@ export default [
SyncEvent.project(MessageV2.Event.PartUpdated, (db, data) => { SyncEvent.project(MessageV2.Event.PartUpdated, (db, data) => {
const { id, messageID, sessionID, ...rest } = data.part const { id, messageID, sessionID, ...rest } = data.part
try {
db.insert(PartTable) db.insert(PartTable)
.values({ .values({
id, id,
@@ -112,5 +127,9 @@ export default [
}) })
.onConflictDoUpdate({ target: PartTable.id, set: { data: rest } }) .onConflictDoUpdate({ target: PartTable.id, set: { data: rest } })
.run() .run()
} catch (err) {
if (!foreign(err)) throw err
log.warn("ignored late part update", { partID: id, messageID, sessionID })
}
}), }),
] ]
+55 -7
View File
@@ -1,5 +1,5 @@
import { NodeFileSystem, NodePath } from "@effect/platform-node" import { NodeFileSystem, NodePath } from "@effect/platform-node"
import { Cause, Duration, Effect, Layer, Schedule, ServiceMap, Stream } from "effect" import { Cause, Duration, Effect, Layer, Schedule, Semaphore, ServiceMap, Stream } from "effect"
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process" import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"
import path from "path" import path from "path"
import z from "zod" import z from "zod"
@@ -7,6 +7,7 @@ import * as CrossSpawnSpawner from "@/effect/cross-spawn-spawner"
import { InstanceState } from "@/effect/instance-state" import { InstanceState } from "@/effect/instance-state"
import { makeRunPromise } from "@/effect/run-service" import { makeRunPromise } from "@/effect/run-service"
import { AppFileSystem } from "@/filesystem" import { AppFileSystem } from "@/filesystem"
import { Hash } from "@/util/hash"
import { Config } from "../config/config" import { Config } from "../config/config"
import { Global } from "../global" import { Global } from "../global"
import { Log } from "../util/log" import { Log } from "../util/log"
@@ -38,7 +39,6 @@ export namespace Snapshot {
const core = ["-c", "core.longpaths=true", "-c", "core.symlinks=true"] const core = ["-c", "core.longpaths=true", "-c", "core.symlinks=true"]
const cfg = ["-c", "core.autocrlf=false", ...core] const cfg = ["-c", "core.autocrlf=false", ...core]
const quote = [...cfg, "-c", "core.quotepath=false"] const quote = [...cfg, "-c", "core.quotepath=false"]
interface GitResult { interface GitResult {
readonly code: ChildProcessSpawner.ExitCode readonly code: ChildProcessSpawner.ExitCode
readonly text: string readonly text: string
@@ -66,12 +66,23 @@ export namespace Snapshot {
Effect.gen(function* () { Effect.gen(function* () {
const fs = yield* AppFileSystem.Service const fs = yield* AppFileSystem.Service
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner const spawner = yield* ChildProcessSpawner.ChildProcessSpawner
const locks = new Map<string, Semaphore.Semaphore>()
const lock = (key: string) => {
const hit = locks.get(key)
if (hit) return hit
const next = Semaphore.makeUnsafe(1)
locks.set(key, next)
return next
}
const state = yield* InstanceState.make<State>( const state = yield* InstanceState.make<State>(
Effect.fn("Snapshot.state")(function* (ctx) { Effect.fn("Snapshot.state")(function* (ctx) {
const state = { const state = {
directory: ctx.directory, directory: ctx.directory,
worktree: ctx.worktree, worktree: ctx.worktree,
gitdir: path.join(Global.Path.data, "snapshot", ctx.project.id), gitdir: path.join(Global.Path.data, "snapshot", ctx.project.id, Hash.fast(ctx.worktree)),
vcs: ctx.project.vcs, vcs: ctx.project.vcs,
} }
@@ -108,6 +119,7 @@ export namespace Snapshot {
const exists = (file: string) => fs.exists(file).pipe(Effect.orDie) const exists = (file: string) => fs.exists(file).pipe(Effect.orDie)
const read = (file: string) => fs.readFileString(file).pipe(Effect.catch(() => Effect.succeed(""))) const read = (file: string) => fs.readFileString(file).pipe(Effect.catch(() => Effect.succeed("")))
const remove = (file: string) => fs.remove(file).pipe(Effect.catch(() => Effect.void)) const remove = (file: string) => fs.remove(file).pipe(Effect.catch(() => Effect.void))
const locked = <A, E, R>(fx: Effect.Effect<A, E, R>) => lock(state.gitdir).withPermits(1)(fx)
const enabled = Effect.fnUntraced(function* () { const enabled = Effect.fnUntraced(function* () {
if (state.vcs !== "git") return false if (state.vcs !== "git") return false
@@ -190,6 +202,8 @@ export namespace Snapshot {
}) })
const cleanup = Effect.fnUntraced(function* () { const cleanup = Effect.fnUntraced(function* () {
return yield* locked(
Effect.gen(function* () {
if (!(yield* enabled())) return if (!(yield* enabled())) return
if (!(yield* exists(state.gitdir))) return if (!(yield* exists(state.gitdir))) return
const result = yield* git(args(["gc", `--prune=${prune}`]), { cwd: state.directory }) const result = yield* git(args(["gc", `--prune=${prune}`]), { cwd: state.directory })
@@ -201,9 +215,13 @@ export namespace Snapshot {
return return
} }
log.info("cleanup", { prune }) log.info("cleanup", { prune })
}),
)
}) })
const track = Effect.fnUntraced(function* () { const track = Effect.fnUntraced(function* () {
return yield* locked(
Effect.gen(function* () {
if (!(yield* enabled())) return if (!(yield* enabled())) return
const existed = yield* exists(state.gitdir) const existed = yield* exists(state.gitdir)
yield* fs.ensureDir(state.gitdir).pipe(Effect.orDie) yield* fs.ensureDir(state.gitdir).pipe(Effect.orDie)
@@ -222,9 +240,13 @@ export namespace Snapshot {
const hash = result.text.trim() const hash = result.text.trim()
log.info("tracking", { hash, cwd: state.directory, git: state.gitdir }) log.info("tracking", { hash, cwd: state.directory, git: state.gitdir })
return hash return hash
}),
)
}) })
const patch = Effect.fnUntraced(function* (hash: string) { const patch = Effect.fnUntraced(function* (hash: string) {
return yield* locked(
Effect.gen(function* () {
yield* add() yield* add()
const result = yield* git( const result = yield* git(
[...quote, ...args(["diff", "--cached", "--no-ext-diff", "--name-only", hash, "--", "."])], [...quote, ...args(["diff", "--cached", "--no-ext-diff", "--name-only", hash, "--", "."])],
@@ -245,13 +267,19 @@ export namespace Snapshot {
.filter(Boolean) .filter(Boolean)
.map((x) => path.join(state.worktree, x).replaceAll("\\", "/")), .map((x) => path.join(state.worktree, x).replaceAll("\\", "/")),
} }
}),
)
}) })
const restore = Effect.fnUntraced(function* (snapshot: string) { const restore = Effect.fnUntraced(function* (snapshot: string) {
return yield* locked(
Effect.gen(function* () {
log.info("restore", { commit: snapshot }) log.info("restore", { commit: snapshot })
const result = yield* git([...core, ...args(["read-tree", snapshot])], { cwd: state.worktree }) const result = yield* git([...core, ...args(["read-tree", snapshot])], { cwd: state.worktree })
if (result.code === 0) { if (result.code === 0) {
const checkout = yield* git([...core, ...args(["checkout-index", "-a", "-f"])], { cwd: state.worktree }) const checkout = yield* git([...core, ...args(["checkout-index", "-a", "-f"])], {
cwd: state.worktree,
})
if (checkout.code === 0) return if (checkout.code === 0) return
log.error("failed to restore snapshot", { log.error("failed to restore snapshot", {
snapshot, snapshot,
@@ -265,9 +293,13 @@ export namespace Snapshot {
exitCode: result.code, exitCode: result.code,
stderr: result.stderr, stderr: result.stderr,
}) })
}),
)
}) })
const revert = Effect.fnUntraced(function* (patches: Snapshot.Patch[]) { const revert = Effect.fnUntraced(function* (patches: Snapshot.Patch[]) {
return yield* locked(
Effect.gen(function* () {
const seen = new Set<string>() const seen = new Set<string>()
for (const item of patches) { for (const item of patches) {
for (const file of item.files) { for (const file of item.files) {
@@ -291,13 +323,20 @@ export namespace Snapshot {
} }
} }
} }
}),
)
}) })
const diff = Effect.fnUntraced(function* (hash: string) { const diff = Effect.fnUntraced(function* (hash: string) {
return yield* locked(
Effect.gen(function* () {
yield* add() yield* add()
const result = yield* git([...quote, ...args(["diff", "--cached", "--no-ext-diff", hash, "--", "."])], { const result = yield* git(
[...quote, ...args(["diff", "--cached", "--no-ext-diff", hash, "--", "."])],
{
cwd: state.worktree, cwd: state.worktree,
}) },
)
if (result.code !== 0) { if (result.code !== 0) {
log.warn("failed to get diff", { log.warn("failed to get diff", {
hash, hash,
@@ -307,14 +346,21 @@ export namespace Snapshot {
return "" return ""
} }
return result.text.trim() return result.text.trim()
}),
)
}) })
const diffFull = Effect.fnUntraced(function* (from: string, to: string) { const diffFull = Effect.fnUntraced(function* (from: string, to: string) {
return yield* locked(
Effect.gen(function* () {
const result: Snapshot.FileDiff[] = [] const result: Snapshot.FileDiff[] = []
const status = new Map<string, "added" | "deleted" | "modified">() const status = new Map<string, "added" | "deleted" | "modified">()
const statuses = yield* git( const statuses = yield* git(
[...quote, ...args(["diff", "--no-ext-diff", "--name-status", "--no-renames", from, to, "--", "."])], [
...quote,
...args(["diff", "--no-ext-diff", "--name-status", "--no-renames", from, to, "--", "."]),
],
{ cwd: state.directory }, { cwd: state.directory },
) )
@@ -359,6 +405,8 @@ export namespace Snapshot {
} }
return result return result
}),
)
}) })
yield* cleanup().pipe( yield* cleanup().pipe(