Skip to content

Commit 37de6cb

Browse files
refactor(server): share one keyed lock that releases idle keys (#15577)
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 1beb035 commit 37de6cb

14 files changed

Lines changed: 190 additions & 196 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.

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as KeyedLock from "@t3tools/shared/KeyedLock";
12
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
23
import {
34
ModelSelection,
@@ -36,7 +37,6 @@ import * as ServerSettings from "../serverSettings.ts";
3637
import * as McpSessionRegistry from "../mcp/McpSessionRegistry.ts";
3738
import * as EventSink from "./EventSink.ts";
3839
import * as IdAllocator from "./IdAllocator.ts";
39-
import { makeKeyedSerialExecutor } from "./KeyedSerialExecutor.ts";
4040
import * as ProviderEventIngestor from "./ProviderEventIngestor.ts";
4141
import {
4242
ProviderAdapterEventStreamError,
@@ -386,9 +386,9 @@ export const layerWithOptions = (
386386
// cannot drop cleanup for threads only the earlier session served.
387387
const releaseRecordRetries = yield* FiberSet.make();
388388
const nextSubscriberId = yield* Ref.make(0);
389-
const sessionOpen = yield* makeKeyedSerialExecutor<ProviderSessionId>();
389+
const sessionOpen = yield* KeyedLock.make<ProviderSessionId>();
390390
// Orders a thread's attach against a detach unloading it on the same session.
391-
const threadAttachment = yield* makeKeyedSerialExecutor<string>();
391+
const threadAttachment = yield* KeyedLock.make<string>();
392392
const threadAttachmentKey = (input: {
393393
readonly providerSessionId: ProviderSessionId;
394394
readonly threadId: ThreadId;
@@ -427,7 +427,7 @@ export const layerWithOptions = (
427427
};
428428
const isMcpCredentialReserved = (threadId: ThreadId, mcpCredentialId: string) =>
429429
(mcpCredentialReservations.get(mcpReservationKey(threadId, mcpCredentialId)) ?? 0) > 0;
430-
const mcpPrepareLock = yield* makeKeyedSerialExecutor<ThreadId>();
430+
const mcpPrepareLock = yield* KeyedLock.make<ThreadId>();
431431
/**
432432
* Resolves (or mints) the thread's MCP credential and returns it with a
433433
* reservation held; the caller must drop the reservation exactly once.
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import type { ThreadId } from "@t3tools/contracts";
2+
import * as KeyedLock from "@t3tools/shared/KeyedLock";
23
import * as Context from "effect/Context";
34
import * as Layer from "effect/Layer";
45

5-
import { makeKeyedSerialExecutor, type KeyedSerialExecutor } from "./KeyedSerialExecutor.ts";
6-
76
/** Shared by thread commands and project deletion so both plan against current thread state. */
87
export class ThreadCommandExecutor extends Context.Service<
98
ThreadCommandExecutor,
10-
KeyedSerialExecutor<ThreadId>
9+
KeyedLock.KeyedLock<ThreadId>
1110
>()("t3/orchestration-v2/ThreadCommandExecutor") {}
1211

13-
export const layer = Layer.effect(ThreadCommandExecutor, makeKeyedSerialExecutor<ThreadId>());
12+
export const layer = Layer.effect(ThreadCommandExecutor, KeyedLock.make<ThreadId>());

‎apps/server/src/orchestration-v2/legacy/LegacyV1ThreadImporter.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
ThreadPullRequestLink,
2222
TurnItemId,
2323
} from "@t3tools/contracts";
24+
import * as KeyedLock from "@t3tools/shared/KeyedLock";
2425
import * as Context from "effect/Context";
2526
import * as DateTime from "effect/DateTime";
2627
import * as Effect from "effect/Effect";
@@ -30,7 +31,6 @@ import * as Schema from "effect/Schema";
3031
import * as SqlClient from "effect/unstable/sql/SqlClient";
3132

3233
import * as EventSink from "../EventSink.ts";
33-
import { makeKeyedSerialExecutor } from "../KeyedSerialExecutor.ts";
3434
import { randomUuidV4 } from "../RandomUuid.ts";
3535

3636
const IMPORT_EVENT_PREFIX = "migration:v1";
@@ -347,7 +347,7 @@ function chunks<A>(items: ReadonlyArray<A>, size: number): Array<ReadonlyArray<A
347347
const make = Effect.gen(function* () {
348348
const sql = yield* SqlClient.SqlClient;
349349
const eventSink = yield* EventSink.EventSinkV2;
350-
const transcriptImports = yield* makeKeyedSerialExecutor<ThreadId>();
350+
const transcriptImports = yield* KeyedLock.make<ThreadId>();
351351

352352
const listMessages = (threadId: ThreadId) =>
353353
sql<LegacyMessageRow>`

‎apps/server/src/project/ProjectService.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
type ProjectSnapshot,
99
type ThreadId,
1010
} from "@t3tools/contracts";
11+
import * as KeyedLock from "@t3tools/shared/KeyedLock";
1112
import * as Context from "effect/Context";
1213
import * as DateTime from "effect/DateTime";
1314
import * as Effect from "effect/Effect";
@@ -18,7 +19,6 @@ import * as Schema from "effect/Schema";
1819

1920
import * as EventSink from "../orchestration-v2/EventSink.ts";
2021
import * as IdAllocator from "../orchestration-v2/IdAllocator.ts";
21-
import { makeKeyedSerialExecutor } from "../orchestration-v2/KeyedSerialExecutor.ts";
2222
import * as LegacyV1ThreadImporter from "../orchestration-v2/legacy/LegacyV1ThreadImporter.ts";
2323
import * as ProjectionStore from "../orchestration-v2/ProjectionStore.ts";
2424
import {
@@ -155,8 +155,8 @@ export const make = Effect.gen(function* () {
155155
const threadCommands = yield* ThreadCommandExecutor.ThreadCommandExecutor;
156156
// Commands for one project run in order. Commands that claim a workspace root
157157
// also hold that root, so two projects cannot both claim it.
158-
const projectLocks = yield* makeKeyedSerialExecutor<ProjectId>();
159-
const workspaceLocks = yield* makeKeyedSerialExecutor<string>();
158+
const projectLocks = yield* KeyedLock.make<ProjectId>();
159+
const workspaceLocks = yield* KeyedLock.make<string>();
160160

161161
const toProject = (
162162
row: ProjectStore.ProjectRow,

‎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/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,

‎apps/server/src/vcs/VcsStatusBroadcaster.ts‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import * as PubSub from "effect/PubSub";
1010
import * as Ref from "effect/Ref";
1111
import * as Schedule from "effect/Schedule";
1212
import * as Scope from "effect/Scope";
13-
import * as Semaphore from "effect/Semaphore";
1413
import * as Stream from "effect/Stream";
1514
import * as SynchronizedRef from "effect/SynchronizedRef";
1615
import type {
@@ -22,6 +21,7 @@ import type {
2221
VcsStatusStreamEvent,
2322
} from "@t3tools/contracts";
2423
import { mergeGitStatusParts } from "@t3tools/shared/git";
24+
import * as KeyedLock from "@t3tools/shared/KeyedLock";
2525
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
2626

2727
import * as BackgroundPolicy from "../background/BackgroundPolicy.ts";
@@ -234,15 +234,9 @@ export const make = Effect.gen(function* () {
234234
// One permit per cwd for remote reads that write the cache. Without it a
235235
// periodic poll that started before `gh pr create` can finish after the
236236
// turn-end refresh and overwrite the fresh PR with its stale `pr: null`.
237-
const remoteWriteLocks = new Map<string, Semaphore.Semaphore>();
238-
const withRemoteWriteLock = <A, E, R>(cwd: string, effect: Effect.Effect<A, E, R>) => {
239-
let lock = remoteWriteLocks.get(cwd);
240-
if (lock === undefined) {
241-
lock = Semaphore.makeUnsafe(1);
242-
remoteWriteLocks.set(cwd, lock);
243-
}
244-
return lock.withPermits(1)(effect);
245-
};
237+
const remoteWriteLocks = yield* KeyedLock.make<string>();
238+
const withRemoteWriteLock = <A, E, R>(cwd: string, effect: Effect.Effect<A, E, R>) =>
239+
remoteWriteLocks.withLock(cwd, effect);
246240
const pollersRef = yield* SynchronizedRef.make(new Map<string, ActiveRemotePoller>());
247241

248242
const getCachedStatus = Effect.fn("VcsStatusBroadcaster.getCachedStatus")(function* (

0 commit comments

Comments
 (0)