Skip to content

Commit 4a5e393

Browse files
authored
fix(core): serialize local artifact directory swaps per artifact (#68114)
1 parent c8dd995 commit 4a5e393

2 files changed

Lines changed: 55 additions & 6 deletions

File tree

‎packages/core/src/artifact.ts‎

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
import type { SessionID } from "@opencode/schema/session-id"
1919
import { Global } from "@opencode/util/global"
2020
import { makeLocationNode } from "@opencode/util/effect/app-node"
21-
import { Clock, Context, Effect, Layer, Option, Schema } from "effect"
21+
import { Clock, Context, Effect, Layer, Option, Schema, Semaphore } from "effect"
2222
import { Bus } from "./bus.js"
2323
import { Flag } from "./flag.js"
2424
import { KV } from "./kv.js"
@@ -200,6 +200,18 @@ export const locate = (kv: KV.Interface, id: ID) =>
200200
}),
201201
)
202202

203+
// The artifact service is location-scoped while artifact directories are global, so one lock per artifact
204+
// is shared by every location in this process. It serializes directory swaps so two writes cannot move the
205+
// same directory out from under each other.
206+
const writeLocks = new Map<ID, Semaphore.Semaphore>()
207+
const lockFor = (id: ID) => {
208+
const current = writeLocks.get(id)
209+
if (current) return current
210+
const created = Semaphore.makeUnsafe(1)
211+
writeLocks.set(id, created)
212+
return created
213+
}
214+
203215
const layer = Layer.effect(
204216
Service,
205217
Effect.gen(function* () {
@@ -226,10 +238,16 @@ const layer = Layer.effect(
226238
prompt:
227239
"Artifacts are served as HTML by this OpenCode server. Only people who can reach this server can open them.",
228240
create: (input) =>
229-
replaceDirectoryFiles(itemDir(input.id), input.files).pipe(Effect.as({ url: viewURL(input.id) })),
230-
read: (id) => readDirectoryFiles(itemDir(id)),
231-
update: (id, input) => replaceDirectoryFiles(itemDir(id), input.files).pipe(Effect.as({ url: viewURL(id) })),
232-
delete: (id) => Effect.promise(() => fs.rm(itemDir(id), { recursive: true, force: true })),
241+
lockFor(input.id).withPermit(
242+
replaceDirectoryFiles(itemDir(input.id), input.files).pipe(Effect.as({ url: viewURL(input.id) })),
243+
),
244+
read: (id) => lockFor(id).withPermit(readDirectoryFiles(itemDir(id))),
245+
update: (id, input) =>
246+
lockFor(id).withPermit(
247+
replaceDirectoryFiles(itemDir(id), input.files).pipe(Effect.as({ url: viewURL(id) })),
248+
),
249+
delete: (id) =>
250+
lockFor(id).withPermit(Effect.promise(() => fs.rm(itemDir(id), { recursive: true, force: true }))),
233251
}
234252

235253
const snapshotProviders = (data: Data) =>

‎packages/core/test/artifact.test.ts‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from "node:path"
22
import { describe, expect } from "bun:test"
3-
import { Effect, Layer, Stream } from "effect"
3+
import { Cause, Effect, Exit, Layer, Stream } from "effect"
44
import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder"
55
import { Artifact } from "@opencode/core/artifact"
66
import { Config } from "@opencode/core/config"
@@ -213,4 +213,35 @@ describe("experimental.artifacts service and tools", () => {
213213
}).pipe(Effect.provide(buildLayer(true, dir)))
214214
}),
215215
)
216+
217+
it.live("applies concurrent updates of the same artifact without racing on rename", () =>
218+
Effect.gen(function* () {
219+
const dir = (yield* tmpdirScoped("opencode-artifact-race-")).path
220+
yield* Effect.gen(function* () {
221+
const artifacts = yield* Artifact.Service
222+
const created = yield* artifacts.create({
223+
title: "Race",
224+
files: { "index.html": "<h1>v0</h1>", "assets/base.txt": "base" },
225+
})
226+
// Every writer stages and swaps the same directory, so overlapping renames must not
227+
// pull the directory out from under another writer.
228+
const exits = yield* Effect.forEach(
229+
Array.from({ length: 16 }, (_, index) => index),
230+
(index) =>
231+
artifacts
232+
.update(created.id, {
233+
title: `Revision ${index}`,
234+
files: {
235+
"index.html": `<h1>v${index}</h1>`,
236+
[`assets/${index}.txt`]: "x".repeat(256 * 1024),
237+
},
238+
})
239+
.pipe(Effect.exit),
240+
{ concurrency: "unbounded" },
241+
)
242+
const failures = exits.flatMap((exit) => (Exit.isFailure(exit) ? [Cause.pretty(exit.cause)] : []))
243+
expect(failures).toEqual([])
244+
}).pipe(Effect.provide(buildLayer(true, dir)))
245+
}),
246+
)
216247
})

0 commit comments

Comments
 (0)