@devries-cameron20 / t3code Mirror · read-only / commits / 03b2105281
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 23, 15:30 UTC

fix(server): prevent busy terminals from starving history persistence (#17181)

Wout Stiens committed Oct 10, 2026 at 13:48 UTC 03b210528160e68c8dbfc27f11f693085f92ab09
2 files changed +105 -29
packages/shared/src/KeyedCoalescingWorker.test.ts
+94 -1
@@ -6,6 +6,98 @@ import * as Effect from "effect/Effect";
6 import { makeKeyedCoalescingWorker } from "./KeyedCoalescingWorker.ts";
7
8 describe("makeKeyedCoalescingWorker", () => {
9 + it.live("lets a queued key drain before a continuously updated key finishes", () =>
10 + Effect.scoped(
11 + Effect.gen(function* () {
12 + const processed: string[] = [];
13 + const busyStarted = yield* Deferred.make<void>();
14 + const releaseBusy = yield* Deferred.make<void>();
15 +
16 + const worker = yield* makeKeyedCoalescingWorker<string, number, never, never>({
17 + merge: (_current, next) => next,
18 + process: (key, value): Effect.Effect<void> =>
19 + Effect.gen(function* () {
20 + processed.push(`${key}:${value}`);
21 + if (key !== "busy") return;
22 +
23 + if (value === 1) {
24 + yield* Deferred.succeed(busyStarted, undefined);
25 + yield* Deferred.await(releaseBusy);
26 + }
27 + if (value < 4) yield* worker.enqueue(key, value + 1);
28 + }),
29 + });
30 +
31 + yield* worker.enqueue("busy", 1);
32 + yield* Deferred.await(busyStarted);
33 + yield* worker.enqueue("quiet", 1);
34 + yield* Deferred.succeed(releaseBusy, undefined);
35 + yield* worker.drainKey("quiet");
36 + yield* worker.drainKey("busy");
37 +
38 + expect(processed).toEqual(["busy:1", "quiet:1", "busy:2", "busy:3", "busy:4"]);
39 + }),
40 + ),
41 + );
42 +
43 + it.live("merges updates while waiting behind another key and drains the merged batch", () =>
44 + Effect.scoped(
45 + Effect.gen(function* () {
46 + const processed: string[] = [];
47 + const busyStarted = yield* Deferred.make<void>();
48 + const releaseBusy = yield* Deferred.make<void>();
49 + const quietStarted = yield* Deferred.make<void>();
50 + const releaseQuiet = yield* Deferred.make<void>();
51 + const mergedStarted = yield* Deferred.make<void>();
52 + const releaseMerged = yield* Deferred.make<void>();
53 +
54 + const worker = yield* makeKeyedCoalescingWorker<string, string, never, never>({
55 + merge: (current, next) => `${current}+${next}`,
56 + process: (key, value) =>
57 + Effect.gen(function* () {
58 + processed.push(`${key}:${value}`);
59 + if (value === "first") {
60 + yield* Deferred.succeed(busyStarted, undefined);
61 + yield* Deferred.await(releaseBusy);
62 + } else if (key === "quiet") {
63 + yield* Deferred.succeed(quietStarted, undefined);
64 + yield* Deferred.await(releaseQuiet);
65 + } else if (value === "a+b+c") {
66 + yield* Deferred.succeed(mergedStarted, undefined);
67 + yield* Deferred.await(releaseMerged);
68 + }
69 + }),
70 + });
71 +
72 + yield* worker.enqueue("busy", "first");
73 + yield* Deferred.await(busyStarted);
74 + yield* worker.enqueue("quiet", "quiet");
75 + yield* worker.enqueue("busy", "a");
76 + yield* Deferred.succeed(releaseBusy, undefined);
77 + yield* Deferred.await(quietStarted);
78 + expect(processed).toEqual(["busy:first", "quiet:quiet"]);
79 + yield* worker.enqueue("busy", "b");
80 + yield* worker.enqueue("busy", "c");
81 +
82 + const drained = yield* Deferred.make<void>();
83 + yield* Effect.forkChild(
84 + worker.drainKey("busy").pipe(Effect.andThen(Deferred.succeed(drained, undefined))),
85 + { startImmediately: true },
86 + );
87 + expect(yield* Deferred.isDone(drained)).toBe(false);
88 +
89 + yield* Deferred.succeed(releaseQuiet, undefined);
90 + yield* Deferred.await(mergedStarted);
91 + expect(yield* Deferred.isDone(drained)).toBe(false);
92 + yield* Deferred.succeed(releaseMerged, undefined);
93 + yield* Deferred.await(drained);
94 + yield* worker.drainKey("quiet");
95 +
96 + expect(processed).toEqual(["busy:first", "quiet:quiet", "busy:a+b+c"]);
97 + }),
98 + ),
99 + );
100 +
101 it.live("waits for latest work enqueued during active processing before draining the key", () =>
102 Effect.scoped(
103 Effect.gen(function* () {
@@ -85,12 +177,13 @@ describe("makeKeyedCoalescingWorker", () => {
177
178 yield* worker.enqueue("terminal-1", "first");
179 yield* Deferred.await(firstStarted);
180 + yield* worker.enqueue("terminal-2", "quiet");
181 yield* worker.enqueue("terminal-1", "second");
182 yield* Deferred.succeed(releaseFailure, undefined);
183 yield* Deferred.await(secondProcessed);
184 yield* worker.drainKey("terminal-1");
185
93 - expect(processed).toEqual(["terminal-1:first", "terminal-1:second"]);
186 + expect(processed).toEqual(["terminal-1:first", "terminal-2:quiet", "terminal-1:second"]);
187 }),
188 ),
189 );
packages/shared/src/KeyedCoalescingWorker.ts
+11 -28
@@ -2,8 +2,9 @@
2 * KeyedCoalescingWorker - A keyed worker that keeps only the latest value per key.
3 *
4 * Enqueues for an active or already-queued key are merged atomically instead of
5 - * creating duplicate queued items. `drainKey()` resolves only when that key has
6 - * no queued, pending, or active work left.
5 + * creating duplicate queued items. Each batch yields to other queued keys before
6 + * processing newer updates. `drainKey()` resolves only when that key has no
7 + * queued, pending, or active work left.
8 *
9 * @module KeyedCoalescingWorker
10 */
@@ -35,28 +36,9 @@ export const makeKeyedCoalescingWorker = <K, V, E, R>(options: {
36 activeKeys: new Set(),
37 });
38
38 - const processKey = (key: K, value: V): Effect.Effect<void, E, R> =>
39 - options.process(key, value).pipe(
40 - Effect.flatMap(() =>
41 - TxRef.modify(stateRef, (state) => {
42 - const nextValue = state.latestByKey.get(key);
43 - if (nextValue === undefined) {
44 - const activeKeys = new Set(state.activeKeys);
45 - activeKeys.delete(key);
46 - return [null, { ...state, activeKeys }] as const;
47 - }
48 -
49 - const latestByKey = new Map(state.latestByKey);
50 - latestByKey.delete(key);
51 - return [nextValue, { ...state, latestByKey }] as const;
52 - }).pipe(Effect.tx),
53 - ),
54 - Effect.flatMap((nextValue) =>
55 - nextValue === null ? Effect.void : processKey(key, nextValue),
56 - ),
57 - );
58 -
59 - const cleanupFailedKey = (key: K): Effect.Effect<void> =>
39 + // Return dirty keys to the tail on success or failure. Keep pending values
40 + // available for merging until the next batch starts.
41 + const completeKey = (key: K): Effect.Effect<void> =>
42 TxRef.modify(stateRef, (state) => {
43 const activeKeys = new Set(state.activeKeys);
44 activeKeys.delete(key);
@@ -69,10 +51,11 @@ export const makeKeyedCoalescingWorker = <K, V, E, R>(options: {
51
52 return [false, { ...state, activeKeys }] as const;
53 }).pipe(
72 - Effect.tx,
54 Effect.flatMap((shouldRequeue) =>
55 shouldRequeue ? TxQueue.offer(queue, key) : Effect.void,
56 ),
57 + Effect.tx,
58 + Effect.asVoid,
59 );
60
61 yield* TxQueue.take(queue).pipe(
@@ -100,9 +83,9 @@ export const makeKeyedCoalescingWorker = <K, V, E, R>(options: {
83 Effect.flatMap((item) =>
84 item === null
85 ? Effect.void
103 - : processKey(item.key, item.value).pipe(
104 - Effect.catchCause(() => cleanupFailedKey(item.key)),
105 - ),
86 + : options
87 + .process(item.key, item.value)
88 + .pipe(Effect.ignoreCause, Effect.ensuring(completeKey(item.key))),
89 ),
90 Effect.forever,
91 Effect.forkScoped,