Skip to content

Commit 075fcc4

Browse files
refactor(server): share one keyed lock that releases idle keys
Several services kept a hand-rolled Map<key, Semaphore> for per-key mutual exclusion and never removed entries, so the maps grew with every thread, cwd, ssh target, session, and update lock key ever seen. Add KeyedLock in packages/shared, an RcMap of Semaphore(1) whose entry lives only while someone holds or waits on its key. KeyedSerialExecutor now uses it, and the terminal Manager, VcsStatusBroadcaster, ssh tunnel manager, provider maintenance coordinator, CheckpointService, and OpenCode2 session gates move onto it, keeping one FIFO permit per key. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent eac52f0 commit 075fcc4

12 files changed

Lines changed: 208 additions & 226 deletions

‎apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.ts‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ import * as McpProviderSession from "../../mcp/McpProviderSession.ts";
7878
import { buildRuntimeInstructions } from "../../provider/RuntimeInstructions.ts";
7979
import { t3OrchestrationSystemPrompt } from "../../provider/T3OrchestrationInstructions.ts";
8080
import { SKILL_MENTION_PATTERN } from "@t3tools/shared/composerInlineTokens";
81+
import * as KeyedLock from "@t3tools/shared/KeyedLock";
8182
import { getModelSelectionStringOptionValue, modelSelectionsEqual } from "@t3tools/shared/model";
8283
import { causeErrorTag } from "@t3tools/shared/observability";
8384

@@ -887,18 +888,13 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
887888
const stagedReverts = new Set<string>();
888889
// Starting a turn and cutting the history take turns on a session: each
889890
// checks that the other is not running before its own requests yield.
890-
const sessionGates = new Map<string, Semaphore.Semaphore>();
891+
const sessionGates = yield* KeyedLock.make<string>();
891892
const exclusive =
892893
(providerThread: OrchestrationV2ProviderThread) =>
893894
<A, E, R>(effect: Effect.Effect<A, E, R>) => {
894895
const sessionId = providerThread.nativeThreadRef?.nativeId;
895896
if (sessionId == null) return effect;
896-
let gate = sessionGates.get(sessionId);
897-
if (gate === undefined) {
898-
gate = Semaphore.makeUnsafe(1);
899-
sessionGates.set(sessionId, gate);
900-
}
901-
return gate.withPermit(effect);
897+
return sessionGates.withLock(sessionId, effect);
902898
};
903899
const emit = (event: ProviderAdapter.ProviderAdapterV2Event) =>
904900
Queue.offer(events, event).pipe(Effect.asVoid);

‎apps/server/src/orchestration-v2/CheckpointService.ts‎

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ import * as Context from "effect/Context";
1414
import * as DateTime from "effect/DateTime";
1515
import * as Effect from "effect/Effect";
1616
import * as Encoding from "effect/Encoding";
17+
import * as KeyedLock from "@t3tools/shared/KeyedLock";
1718
import * as Layer from "effect/Layer";
18-
import * as Ref from "effect/Ref";
1919
import * as Schema from "effect/Schema";
20-
import * as Semaphore from "effect/Semaphore";
2120

2221
import { parseTurnDiffFilesFromNumstat } from "../checkpointing/Diffs.ts";
2322
import * as CheckpointStore from "../checkpointing/CheckpointStore.ts";
@@ -249,29 +248,9 @@ export const layer: Layer.Layer<
249248
Effect.gen(function* () {
250249
const checkpointStore = yield* CheckpointStore.CheckpointStore;
251250
const idAllocator = yield* IdAllocator.IdAllocatorV2;
252-
const workspaceSemaphores = yield* Ref.make(new Map<string, Semaphore.Semaphore>());
253-
254-
const getWorkspaceSemaphore = (cwd: string) =>
255-
Effect.gen(function* () {
256-
const existing = (yield* Ref.get(workspaceSemaphores)).get(cwd);
257-
if (existing !== undefined) {
258-
return existing;
259-
}
260-
261-
const created = yield* Semaphore.make(1);
262-
return yield* Ref.modify(workspaceSemaphores, (current) => {
263-
const concurrent = current.get(cwd);
264-
if (concurrent !== undefined) {
265-
return [concurrent, current];
266-
}
267-
const updated = new Map(current);
268-
updated.set(cwd, created);
269-
return [created, updated];
270-
});
271-
});
272-
251+
const workspaceLocks = yield* KeyedLock.make<string>();
273252
const withWorkspaceLock = <A, E, R>(cwd: string, effect: Effect.Effect<A, E, R>) =>
274-
Effect.flatMap(getWorkspaceSemaphore(cwd), (semaphore) => semaphore.withPermits(1)(effect));
253+
workspaceLocks.withLock(cwd, effect);
275254

276255
const isGitCheckpointable = (cwd: string) =>
277256
checkpointStore.isGitRepository(cwd).pipe(Effect.orElseSucceed(() => false));

‎apps/server/src/orchestration-v2/KeyedSerialExecutor.test.ts‎

Lines changed: 0 additions & 67 deletions
This file was deleted.
Lines changed: 8 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
import * as Effect from "effect/Effect";
2-
import * as Ref from "effect/Ref";
3-
import * as Semaphore from "effect/Semaphore";
4-
5-
interface LockEntry {
6-
readonly semaphore: Semaphore.Semaphore;
7-
readonly users: number;
8-
}
1+
import * as KeyedLock from "@t3tools/shared/KeyedLock";
2+
import type * as Effect from "effect/Effect";
3+
import type * as Scope from "effect/Scope";
94

105
export interface KeyedSerialExecutor<Key> {
116
readonly withLock: <A, E, R>(key: Key, effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
@@ -15,41 +10,8 @@ export interface KeyedSerialExecutor<Key> {
1510
* Serializes work that targets the same domain identity without coupling
1611
* unrelated identities to a process-wide mutex.
1712
*/
18-
export const makeKeyedSerialExecutor = <Key>(): Effect.Effect<KeyedSerialExecutor<Key>> =>
19-
Effect.gen(function* () {
20-
const locks = yield* Ref.make(new Map<Key, LockEntry>());
21-
22-
const acquireLock = (key: Key) =>
23-
Effect.gen(function* () {
24-
const candidate = yield* Semaphore.make(1);
25-
return yield* Ref.modify(locks, (current) => {
26-
const existing = current.get(key);
27-
const semaphore = existing?.semaphore ?? candidate;
28-
const next = new Map(current);
29-
next.set(key, { semaphore, users: (existing?.users ?? 0) + 1 });
30-
return [semaphore, next] as const;
31-
});
32-
});
33-
34-
const releaseLock = (key: Key) =>
35-
Ref.update(locks, (current) => {
36-
const existing = current.get(key);
37-
if (existing === undefined) return current;
38-
const next = new Map(current);
39-
if (existing.users === 1) {
40-
next.delete(key);
41-
} else {
42-
next.set(key, { ...existing, users: existing.users - 1 });
43-
}
44-
return next;
45-
});
46-
47-
return {
48-
withLock: (key, effect) =>
49-
Effect.acquireUseRelease(
50-
acquireLock(key),
51-
(semaphore) => semaphore.withPermit(effect),
52-
() => releaseLock(key),
53-
),
54-
} satisfies KeyedSerialExecutor<Key>;
55-
});
13+
export const makeKeyedSerialExecutor = <Key>(): Effect.Effect<
14+
KeyedSerialExecutor<Key>,
15+
never,
16+
Scope.Scope
17+
> => KeyedLock.make<Key>();

‎apps/server/src/provider/providerMaintenanceCommandCoordinator.ts‎

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import * as KeyedLock from "@t3tools/shared/KeyedLock";
12
import * as Effect from "effect/Effect";
23
import * as Ref from "effect/Ref";
3-
import * as Semaphore from "effect/Semaphore";
44

55
export interface ProviderMaintenanceCommandCoordinatorShape<E> {
66
readonly withCommandLock: <A, R>(input: {
@@ -15,7 +15,7 @@ export const makeProviderMaintenanceCommandCoordinator = Effect.fn(
1515
"makeProviderMaintenanceCommandCoordinator",
1616
)(function* <E>(input: { readonly makeAlreadyRunningError: (targetKey: string) => E }) {
1717
const runningTargetsRef = yield* Ref.make<ReadonlySet<string>>(new Set());
18-
const locksRef = yield* Ref.make<ReadonlyMap<string, Semaphore.Semaphore>>(new Map());
18+
const locks = yield* KeyedLock.make<string>();
1919

2020
const acquireTarget = Effect.fn("acquireTarget")(function* (targetKey: string) {
2121
return yield* Ref.modify(runningTargetsRef, (runningTargets) => {
@@ -35,24 +35,6 @@ export const makeProviderMaintenanceCommandCoordinator = Effect.fn(
3535
return next;
3636
});
3737

38-
const getLock = Effect.fn("getProviderMaintenanceCommandLock")(function* (lockKey: string) {
39-
const existing = (yield* Ref.get(locksRef)).get(lockKey);
40-
if (existing) {
41-
return existing;
42-
}
43-
44-
const lock = yield* Semaphore.make(1);
45-
return yield* Ref.modify(locksRef, (locks) => {
46-
const current = locks.get(lockKey);
47-
if (current) {
48-
return [current, locks] as const;
49-
}
50-
const next = new Map(locks);
51-
next.set(lockKey, lock);
52-
return [lock, next] as const;
53-
});
54-
});
55-
5638
const withCommandLock: ProviderMaintenanceCommandCoordinatorShape<E>["withCommandLock"] = ({
5739
targetKey,
5840
lockKey,
@@ -65,13 +47,10 @@ export const makeProviderMaintenanceCommandCoordinator = Effect.fn(
6547
return yield* Effect.fail(input.makeAlreadyRunningError(targetKey));
6648
}
6749

68-
return yield* Effect.gen(function* () {
69-
const lock = yield* getLock(lockKey);
70-
if (onQueued) {
71-
yield* onQueued;
72-
}
73-
return yield* lock.withPermits(1)(run);
74-
}).pipe(Effect.ensuring(releaseTarget(targetKey)));
50+
return yield* (onQueued ?? Effect.void).pipe(
51+
Effect.andThen(locks.withLock(lockKey, run)),
52+
Effect.ensuring(releaseTarget(targetKey)),
53+
);
7554
});
7655

7756
return {

‎apps/server/src/provider/providerMaintenanceRunner.test.ts‎

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from "@t3tools/contracts";
88
import { ServerProviderUpdateError } from "@t3tools/contracts";
99
import * as Cause from "effect/Cause";
10+
import * as Context from "effect/Context";
1011
import * as Effect from "effect/Effect";
1112
import * as Exit from "effect/Exit";
1213
import * as Fiber from "effect/Fiber";
@@ -223,24 +224,27 @@ const makeTestRunner = (
223224
})),
224225
},
225226
) =>
226-
Effect.service(ProviderMaintenanceRunner.ProviderMaintenanceRunner).pipe(
227-
Effect.provide(
228-
ProviderMaintenanceRunner.layer.pipe(
229-
Layer.provide(
230-
Layer.mergeAll(
231-
Layer.succeed(ProviderRegistry.ProviderRegistry, registry),
232-
Layer.succeed(ModelManifest.ModelManifest, {
233-
current: Effect.succeed(manifest),
234-
refresh: Effect.succeed(manifest),
235-
forceRefresh: Effect.succeed(manifest),
236-
refreshInBackground: Effect.void,
237-
}),
238-
// Fresh per runner so a version cached by one test cannot leak into another.
239-
Layer.sync(ProviderVersionCache, () => new Map()),
240-
),
227+
// Built in the test's scope: the runner must stay open while the test uses it.
228+
Layer.build(
229+
ProviderMaintenanceRunner.layer.pipe(
230+
Layer.provide(
231+
Layer.mergeAll(
232+
Layer.succeed(ProviderRegistry.ProviderRegistry, registry),
233+
Layer.succeed(ModelManifest.ModelManifest, {
234+
current: Effect.succeed(manifest),
235+
refresh: Effect.succeed(manifest),
236+
forceRefresh: Effect.succeed(manifest),
237+
refreshInBackground: Effect.void,
238+
}),
239+
// Fresh per runner so a version cached by one test cannot leak into another.
240+
Layer.sync(ProviderVersionCache, () => new Map()),
241241
),
242242
),
243243
),
244+
).pipe(
245+
Effect.map((context) =>
246+
Context.get(context, ProviderMaintenanceRunner.ProviderMaintenanceRunner),
247+
),
244248
);
245249

246250
describe("providerMaintenanceRunner", () => {

‎apps/server/src/terminal/Manager.ts‎

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
ProviderInstanceId,
4141
} from "@t3tools/contracts";
4242
import { makeKeyedCoalescingWorker } from "@t3tools/shared/KeyedCoalescingWorker";
43+
import * as KeyedLock from "@t3tools/shared/KeyedLock";
4344
import { HostProcessArchitecture, HostProcessPlatform } from "@t3tools/shared/hostProcess";
4445
import { mergePathEntries } from "@t3tools/shared/shell";
4546

@@ -58,7 +59,6 @@ import * as Option from "effect/Option";
5859
import * as Path from "effect/Path";
5960
import * as Schema from "effect/Schema";
6061
import * as Scope from "effect/Scope";
61-
import * as Semaphore from "effect/Semaphore";
6262
import * as SynchronizedRef from "effect/SynchronizedRef";
6363

6464
import * as ServerConfig from "../config.ts";
@@ -1569,7 +1569,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
15691569
sessions: new Map(),
15701570
killFibers: new Map(),
15711571
});
1572-
const threadLocksRef = yield* SynchronizedRef.make(new Map<string, Semaphore.Semaphore>());
1572+
const threadLocks = yield* KeyedLock.make<string>();
15731573
const terminalEventListeners = new Set<(event: TerminalEvent) => Effect.Effect<void>>();
15741574
const workerScope = yield* Scope.make("sequential");
15751575
yield* Effect.addFinalizer(() => Scope.close(workerScope, Exit.void));
@@ -1598,29 +1598,10 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
15981598
f: (state: TerminalManagerState) => readonly [A, TerminalManagerState],
15991599
) => SynchronizedRef.modify(managerStateRef, f);
16001600

1601-
const getThreadSemaphore = (threadId: string) =>
1602-
SynchronizedRef.modifyEffect(threadLocksRef, (current) => {
1603-
const existing: Option.Option<Semaphore.Semaphore> = Option.fromNullishOr(
1604-
current.get(threadId),
1605-
);
1606-
return Option.match(existing, {
1607-
onNone: () =>
1608-
Semaphore.make(1).pipe(
1609-
Effect.map((semaphore) => {
1610-
const next = new Map(current);
1611-
next.set(threadId, semaphore);
1612-
return [semaphore, next] as const;
1613-
}),
1614-
),
1615-
onSome: (semaphore) => Effect.succeed([semaphore, current] as const),
1616-
});
1617-
});
1618-
16191601
const withThreadLock = <A, E, R>(
16201602
threadId: string,
16211603
effect: Effect.Effect<A, E, R>,
1622-
): Effect.Effect<A, E, R> =>
1623-
Effect.flatMap(getThreadSemaphore(threadId), (semaphore) => semaphore.withPermit(effect));
1604+
): Effect.Effect<A, E, R> => threadLocks.withLock(threadId, effect);
16241605

16251606
const clearKillFiber = Effect.fn("terminal.clearKillFiber")(function* (
16261607
process: PtyAdapter.PtyProcess | null,

0 commit comments

Comments
 (0)