Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(mcp-host): treat a writer lease in transition as transient contention

Alfredo Lopez0e2c5ffde09a
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index 642e04a30..0ad0982c9 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.505",
+ "version": "0.1.506",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.505",
+ "version": "0.1.506",
"license": "SEE LICENSE IN LICENSE",
"dependencies": {
"@anthropic-ai/sdk": "^0.32.0",
diff --git a/mcp-host/package.json b/mcp-host/package.json
index 2ba1fba6a..3cb8a689f 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.505",
+ "version": "0.1.506",
"description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude",
"main": "dist/main.js",
"engines": {
diff --git a/mcp-host/src/internalTools/gfsContentDownload.test.ts b/mcp-host/src/internalTools/gfsContentDownload.test.ts
index d66f0ae3b..592ac99d0 100644
--- a/mcp-host/src/internalTools/gfsContentDownload.test.ts
+++ b/mcp-host/src/internalTools/gfsContentDownload.test.ts
@@ -108,7 +108,8 @@ describe('governed GFS content download', () => {
expect((await fs.stat(path.dirname(path.join(callerRoot, result.path)))).mode & 0o777).toBe(
0o700
)
- }, 10_000)
+ // The durable 3.8 MB publication measured 7.7 s on a loaded host.
+ }, 30_000)
it('waits for each partial write before pulling more content', async () => {
let finish!: () => void
diff --git a/mcp-host/src/internalTools/gfsDownloadStoreFutureLeaseCapacity.test.ts b/mcp-host/src/internalTools/gfsDownloadStoreFutureLeaseCapacity.test.ts
index 3fb92e75e..6e31a9c15 100644
--- a/mcp-host/src/internalTools/gfsDownloadStoreFutureLeaseCapacity.test.ts
+++ b/mcp-host/src/internalTools/gfsDownloadStoreFutureLeaseCapacity.test.ts
@@ -308,7 +308,8 @@ describe('future execution protection and whole admission capacity', () => {
for (const receipt of receipts) expect(store.debugRecord(receipt.id)).toBeDefined()
for (const identity of ['caller-c', 'caller-d', 'caller-e', 'caller-f', 'caller-g'])
await store.releaseReceiptOwner(`${identity}-owner`, identity)
- })
+ // 10 durable publications measured 3.3 s on a loaded host, inside Vitest's 5 s default.
+ }, 30_000)
it('keeps all default-cap small copies when current physical capacity is zero', async () => {
const { root, store } = await setup({
diff --git a/mcp-host/src/internalTools/gfsStoreWriterLease.ts b/mcp-host/src/internalTools/gfsStoreWriterLease.ts
index 9c8c24d41..7165b5715 100644
--- a/mcp-host/src/internalTools/gfsStoreWriterLease.ts
+++ b/mcp-host/src/internalTools/gfsStoreWriterLease.ts
@@ -36,6 +36,9 @@ export class GfsStoreWriterLease {
private markerHandle?: fs.FileHandle
private rootHandle?: fs.FileHandle
private localOwner = false
+ // True while this lease is acquiring or releasing. Either ends on its own,
+ // and the next attempt judges the store from scratch.
+ private inTransition = false
private verifiedMarker?: string
private readonly databasePath: string
private readonly markerPath: string
@@ -60,20 +63,23 @@ export class GfsStoreWriterLease {
private async acquireInternal(transition?: OperatorOwnershipTransition): Promise<void> {
const priorOwner = GfsStoreWriterLease.localOwners.get(this.root)
if (priorOwner) {
- let transient = false
+ let transient = priorOwner.inTransition
try {
- if (priorOwner.verifiedMarker) {
+ if (!transient && priorOwner.verifiedMarker) {
await priorOwner.verifyHeld()
await priorOwner.verifyOwnershipPaths(priorOwner.verifiedMarker)
transient = true
}
} catch {
- /* Ambiguous or changed ownership is never a retry signal. */
+ // Ambiguous or changed ownership is never a retry signal, unless the
+ // owner started releasing while it was being checked.
+ transient = priorOwner.inTransition
}
throw new GfsStoreWriterOwnershipError('Writer already active', 'writer_locked', transient)
}
GfsStoreWriterLease.localOwners.set(this.root, this)
this.localOwner = true
+ this.inTransition = true
let contentionMarker: string | undefined
try {
await transition?.assertPhysicalFenceHeld()
@@ -206,6 +212,7 @@ export class GfsStoreWriterLease {
await this.verifyHeld()
await transition?.assertPhysicalFenceHeld()
await this.rootHandle.sync()
+ this.inTransition = false
} catch (error) {
let transient = false
if ((error as { code?: string }).code === 'SQLITE_BUSY' && contentionMarker) {
@@ -267,6 +274,7 @@ export class GfsStoreWriterLease {
async release(): Promise<void> {
// Closing the connection rolls back its lock-only transaction and releases
// kernel ownership. No on-disk ownership inode is removed.
+ this.inTransition = true
this.database?.close()
this.database = undefined
this.verifiedMarker = undefined
diff --git a/mcp-host/src/internalTools/gfsStoreWriterLeaseTransition.test.ts b/mcp-host/src/internalTools/gfsStoreWriterLeaseTransition.test.ts
new file mode 100644
index 000000000..622b798d8
--- /dev/null
+++ b/mcp-host/src/internalTools/gfsStoreWriterLeaseTransition.test.ts
@@ -0,0 +1,106 @@
+import { afterEach, describe, expect, it } from 'vitest'
+import * as fs from 'node:fs/promises'
+import { tmpdir } from 'node:os'
+import { join } from 'node:path'
+import { GfsDownloadStore } from './gfsDownloadStore'
+import { GfsStoreWriterLease } from './gfsStoreWriterLease'
+
+/**
+ * A same-process writer that is still acquiring, or already releasing, is a
+ * transition that ends by itself: the next attempt re-judges the store from
+ * scratch. Only a held writer whose fence or inodes changed is permanent.
+ * `acquire()` reads the in-process owner synchronously, so starting it right
+ * after another lease's `acquire()`/`release()` lands inside that transition.
+ */
+const roots: string[] = []
+const leases: GfsStoreWriterLease[] = []
+
+async function v2StoreRoot(): Promise<string> {
+ const value = await fs.mkdtemp(join(tmpdir(), 'gfs-lease-transition-'))
+ roots.push(value)
+ const store = new GfsDownloadStore(value)
+ await store.initialize()
+ await store.close()
+ return join(await fs.realpath(value), '.gfs-download-store')
+}
+
+function lease(storeRoot: string): GfsStoreWriterLease {
+ const value = new GfsStoreWriterLease(storeRoot, false)
+ leases.push(value)
+ return value
+}
+
+afterEach(async () => {
+ for (const value of leases.splice(0)) await value.release()
+ for (const value of roots.splice(0)) await fs.rm(value, { recursive: true, force: true })
+})
+
+describe('GFS writer lease during a same-process ownership transition', () => {
+ it('reports a writer that is releasing as transient contention', async () => {
+ const storeRoot = await v2StoreRoot()
+ const first = lease(storeRoot)
+ await first.acquire()
+ first.assertHeld()
+
+ const releasing = first.release()
+ await expect(lease(storeRoot).acquire()).rejects.toMatchObject({
+ reason: 'writer_locked',
+ transientWriterContention: true,
+ })
+ await releasing
+
+ // Witness: the transition really ended and the next attempt owns the store.
+ const next = lease(storeRoot)
+ await next.acquire()
+ next.assertHeld()
+ })
+
+ it('reports a writer that starts releasing during the contention check as transient', async () => {
+ const storeRoot = await v2StoreRoot()
+ const first = lease(storeRoot)
+ await first.acquire()
+
+ const attempt = lease(storeRoot).acquire()
+ const releasing = first.release()
+ await expect(attempt).rejects.toMatchObject({
+ reason: 'writer_locked',
+ transientWriterContention: true,
+ })
+ await releasing
+ expect(() => first.assertHeld()).toThrow('Writer ownership lost')
+
+ const next = lease(storeRoot)
+ await next.acquire()
+ next.assertHeld()
+ })
+
+ it('reports a writer that is still acquiring as transient contention', async () => {
+ const storeRoot = await v2StoreRoot()
+ const first = lease(storeRoot)
+
+ const acquiring = first.acquire()
+ await expect(lease(storeRoot).acquire()).rejects.toMatchObject({
+ reason: 'writer_locked',
+ transientWriterContention: true,
+ })
+ await acquiring
+ // Witness: the in-flight acquisition completed and holds the lock.
+ first.assertHeld()
+ })
+
+ it('keeps a held writer whose fence changed as permanent recovery state', async () => {
+ const storeRoot = await v2StoreRoot()
+ const first = lease(storeRoot)
+ await first.acquire()
+ // Witness: before the change the same contention is transient.
+ await expect(lease(storeRoot).acquire()).rejects.toMatchObject({
+ transientWriterContention: true,
+ })
+
+ await fs.writeFile(join(storeRoot, 'writer.lock'), '{bad')
+ await expect(lease(storeRoot).acquire()).rejects.toMatchObject({
+ reason: 'writer_locked',
+ transientWriterContention: false,
+ })
+ })
+})