fix(opencode): serialize mcp auth mutations (#29852)
This commit is contained in:
83
packages/opencode/test/mcp/auth.test.ts
Normal file
83
packages/opencode/test/mcp/auth.test.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
import { expect, test } from "bun:test"
|
||||
import { setTimeout as sleep } from "node:timers/promises"
|
||||
import { Effect, Layer } from "effect"
|
||||
import { AppFileSystem } from "@opencode-ai/core/filesystem"
|
||||
import { EffectFlock } from "@opencode-ai/core/util/effect-flock"
|
||||
import { McpAuth } from "../../src/mcp/auth"
|
||||
|
||||
function authFile() {
|
||||
let raw = ""
|
||||
let activeWrites = 0
|
||||
let sawOverlap = false
|
||||
|
||||
const layer = Layer.effect(
|
||||
AppFileSystem.Service,
|
||||
Effect.gen(function* () {
|
||||
const fs = yield* AppFileSystem.Service
|
||||
|
||||
return AppFileSystem.Service.of({
|
||||
...fs,
|
||||
readJson: (file) =>
|
||||
file.endsWith("mcp-auth.json")
|
||||
? Effect.try({
|
||||
try: () => {
|
||||
if (!raw) throw new Error("mcp-auth.json missing")
|
||||
return JSON.parse(raw)
|
||||
},
|
||||
catch: (cause) => new AppFileSystem.FileSystemError({ method: "readJson", cause }),
|
||||
})
|
||||
: fs.readJson(file),
|
||||
writeJson: (file, value, mode) =>
|
||||
file.endsWith("mcp-auth.json")
|
||||
? Effect.promise(async () => {
|
||||
activeWrites++
|
||||
sawOverlap = sawOverlap || activeWrites > 1
|
||||
raw = ""
|
||||
await sleep(10)
|
||||
const next = JSON.stringify(value, null, 2)
|
||||
raw = sawOverlap ? `${next}\n}` : next
|
||||
activeWrites--
|
||||
})
|
||||
: fs.writeJson(file, value, mode),
|
||||
})
|
||||
}),
|
||||
).pipe(Layer.provide(AppFileSystem.defaultLayer))
|
||||
|
||||
return { layer, raw: () => raw }
|
||||
}
|
||||
|
||||
function authService(layer: Layer.Layer<AppFileSystem.Service>) {
|
||||
return McpAuth.Service.use((auth) => Effect.succeed(auth)).pipe(
|
||||
Effect.provide(
|
||||
McpAuth.layer.pipe(
|
||||
Layer.provide(EffectFlock.defaultLayer),
|
||||
Layer.provide(layer),
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
test("serializes concurrent auth file updates across service instances", async () => {
|
||||
const file = authFile()
|
||||
|
||||
await Effect.runPromise(
|
||||
Effect.gen(function* () {
|
||||
const first = yield* authService(file.layer)
|
||||
const second = yield* authService(file.layer)
|
||||
|
||||
yield* Effect.all(
|
||||
[
|
||||
first.updateTokens("posthog", { accessToken: "access-token" }, "https://mcp.posthog.com/mcp"),
|
||||
second.updateClientInfo("posthog", { clientId: "client-id" }, "https://mcp.posthog.com/mcp"),
|
||||
],
|
||||
{ concurrency: "unbounded" },
|
||||
)
|
||||
|
||||
const entry = yield* first.get("posthog")
|
||||
expect(entry?.tokens?.accessToken).toBe("access-token")
|
||||
expect(entry?.clientInfo?.clientId).toBe("client-id")
|
||||
expect(entry?.serverUrl).toBe("https://mcp.posthog.com/mcp")
|
||||
expect(() => JSON.parse(file.raw())).not.toThrow()
|
||||
}),
|
||||
)
|
||||
})
|
||||
Reference in New Issue
Block a user