@devries-cameron20 / t3code-1 / commits / 3ea253f80f
Fork 0

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

fix(server): suppress redundant keybindings change emission on unchanged revalidation (#23775)

Signed-off-by: Shaswat Raj <shaswat340@gmail.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Shaswat Raj committed Nov 14, 2026 at 09:18 UTC 3ea253f80f20b764bbdc2bb9322d38371f2cb622
2 files changed +76 -7
apps/server/src/keybindings.test.ts
+40
@@ -6,8 +6,10 @@ import * as Effect from "effect/Effect";
6 import * as FileSystem from "effect/FileSystem";
7 import * as Layer from "effect/Layer";
8 import * as Logger from "effect/Logger";
9 +import * as Option from "effect/Option";
10 import * as Path from "effect/Path";
11 import * as Schema from "effect/Schema";
12 +import * as Stream from "effect/Stream";
13 import * as ServerConfig from "./config.ts";
14 import * as Keybindings from "./keybindings.ts";
15 import * as SharedKeybindings from "@t3tools/shared/keybindings";
@@ -616,4 +618,42 @@ it.layer(NodeServices.layer)("keybindings", (it) => {
618 }
619 }).pipe(Effect.provide(layerKeybindings())),
620 );
621 +
622 + it.effect(
623 + "suppresses change emission when revalidating unchanged keybindings configuration",
624 + () =>
625 + Effect.gen(function* () {
626 + const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
627 + yield* writeKeybindingsConfig(keybindingsConfigPath, [
628 + { key: "mod+j", command: "terminal.toggle" },
629 + ]);
630 +
631 + const keybindings = yield* Keybindings.Keybindings;
632 + yield* keybindings.start;
633 +
634 + const changes = yield* keybindings.subscribeChanges;
635 +
636 + // Revalidate when config on disk has not changed (e.g. atime bump on Windows).
637 + // `revalidate` publishes before it returns, so a wrongly emitted event would be
638 + // buffered in the subscription ahead of the real change observed below.
639 + yield* keybindings.revalidate;
640 +
641 + yield* writeKeybindingsConfig(keybindingsConfigPath, [
642 + { key: "mod+j", command: "terminal.toggle" },
643 + { key: "mod+k", command: "threadPanel.toggle" },
644 + ]);
645 + yield* keybindings.revalidate;
646 +
647 + // The first buffered event must be the real change, not the unchanged state.
648 + const change = Option.getOrThrow(yield* Stream.runHead(changes));
649 + assert.isTrue(
650 + change.keybindings.some(
651 + (rule) =>
652 + rule.command === "threadPanel.toggle" &&
653 + rule.shortcut.key === "k" &&
654 + rule.shortcut.modKey,
655 + ),
656 + );
657 + }).pipe(Effect.scoped, Effect.provide(layerKeybindings())),
658 + );
659 });
apps/server/src/keybindings.ts
+36 -7
@@ -8,7 +8,7 @@ import {
8 ResolvedKeybindingsConfig,
9 type ServerRemoveKeybindingInput,
10 type ServerUpsertKeybindingInput,
11 - type ServerConfigIssue,
11 + ServerConfigIssue,
12 } from "@t3tools/contracts";
13 import * as Array from "effect/Array";
14 import * as Cache from "effect/Cache";
@@ -180,6 +180,13 @@ export interface KeybindingsConfigState {
180 readonly issues: readonly ServerConfigIssue[];
181 }
182
183 +const isSameKeybindingsConfigState = Schema.toEquivalence(
184 + Schema.Struct({
185 + keybindings: ResolvedKeybindingsConfig,
186 + issues: Schema.Array(ServerConfigIssue),
187 + }),
188 +);
189 +
190 export interface KeybindingsChangeEvent {
191 readonly keybindings: ResolvedKeybindingsConfig;
192 readonly issues: readonly ServerConfigIssue[];
@@ -220,6 +227,8 @@ export class Keybindings extends Context.Service<
227
228 readonly streamChanges: Stream.Stream<KeybindingsChangeEvent>;
229
230 + readonly revalidate: Effect.Effect<void, KeybindingsConfigError>;
231 +
232 /** Acquire before reading a snapshot to buffer changes until the stream starts. */
233 readonly subscribeChanges: Effect.Effect<
234 Stream.Stream<KeybindingsChangeEvent>,
@@ -248,12 +257,17 @@ const make = Effect.gen(function* () {
257 const upsertSemaphore = yield* Semaphore.make(1);
258 const resolvedConfigCacheKey = "resolved" as const;
259 const changesPubSub = yield* PubSub.unbounded<KeybindingsChangeEvent>();
260 + const lastEmittedStateRef = yield* Ref.make<KeybindingsConfigState | null>(null);
261 const startedRef = yield* Ref.make(false);
262 const startedDeferred = yield* Deferred.make<void, KeybindingsConfigError>();
263 const watcherScope = yield* Scope.make("sequential");
264 yield* Effect.addFinalizer(() => Scope.close(watcherScope, Exit.void));
255 - const emitChange = (configState: KeybindingsConfigState) =>
256 - PubSub.publish(changesPubSub, configState).pipe(Effect.asVoid);
265 + const emitChange = Effect.fn("keybindings.emitChange")(function* (
266 + configState: KeybindingsConfigState,
267 + ) {
268 + yield* Ref.set(lastEmittedStateRef, configState);
269 + yield* PubSub.publish(changesPubSub, configState);
270 + });
271
272 const readConfigExists = fs.exists(keybindingsConfigPath).pipe(
273 Effect.mapError(
@@ -432,14 +446,27 @@ const make = Effect.gen(function* () {
446
447 const loadConfigStateFromCacheOrDisk = Cache.get(resolvedConfigCache, resolvedConfigCacheKey);
448
435 - const reloadAndEmit = Effect.gen(function* () {
449 + const reloadConfigState = Effect.gen(function* () {
450 yield* Cache.invalidate(resolvedConfigCache, resolvedConfigCacheKey);
437 - const configState = yield* loadConfigStateFromCacheOrDisk;
451 + return yield* loadConfigStateFromCacheOrDisk;
452 + });
453 +
454 + const reloadAndEmit = Effect.gen(function* () {
455 + const configState = yield* reloadConfigState;
456 yield* emitChange(configState);
457 return configState;
458 });
459
442 - const revalidateAndEmit = upsertSemaphore.withPermits(1)(reloadAndEmit);
460 + const revalidateAndEmit = upsertSemaphore.withPermits(1)(
461 + Effect.gen(function* () {
462 + const previousState = yield* Ref.get(lastEmittedStateRef);
463 + const configState = yield* reloadConfigState;
464 + if (!(previousState && isSameKeybindingsConfigState(previousState, configState))) {
465 + yield* emitChange(configState);
466 + }
467 + return configState;
468 + }),
469 + );
470
471 const syncDefaultKeybindingsOnStartup = upsertSemaphore.withPermits(1)(
472 Effect.gen(function* () {
@@ -606,7 +633,8 @@ const make = Effect.gen(function* () {
633 yield* startWatcher;
634 yield* syncDefaultKeybindingsOnStartup;
635 yield* Cache.invalidate(resolvedConfigCache, resolvedConfigCacheKey);
609 - yield* loadConfigStateFromCacheOrDisk;
636 + const initialState = yield* loadConfigStateFromCacheOrDisk;
637 + yield* Ref.set(lastEmittedStateRef, initialState);
638 });
639
640 const startupExit = yield* Effect.exit(startup);
@@ -624,6 +652,7 @@ const make = Effect.gen(function* () {
652 syncDefaultKeybindingsOnStartup,
653 loadConfigState: loadConfigStateFromCacheOrDisk,
654 getSnapshot: loadConfigStateFromCacheOrDisk,
655 + revalidate: revalidateAndEmit,
656 get streamChanges() {
657 return Stream.fromPubSub(changesPubSub);
658 },