From 97ed5822dfccf4bde3a50904ee46caca213c6238 Mon Sep 17 00:00:00 2001 From: Shaswat Raj Date: Fri, 23 Oct 2026 00:10:01 +0530 Subject: [PATCH 1/4] fix(server): suppress redundant keybindings change emission on unchanged revalidation Fixes #23734 When `fs.watch` detects changes to the keybindings configuration directory (such as NTFS last-access time updates on Windows during file reads), `revalidateAndEmit` previously invalidated the cache and emitted `changesPubSub` unconditionally. This caused spurious 'Keybindings updated' toast notifications on the web/desktop client. This change stores the last emitted keybindings configuration state and checks whether the revalidated state has actually changed before publishing to `changesPubSub`. If the reloaded keybindings and issues are identical, change emission is suppressed. Signed-off-by: Shaswat Raj --- apps/server/src/keybindings.test.ts | 38 ++++++++++++++ apps/server/src/keybindings.ts | 77 +++++++++++++++++++++++++++-- 2 files changed, 112 insertions(+), 3 deletions(-) diff --git a/apps/server/src/keybindings.test.ts b/apps/server/src/keybindings.test.ts index 80d4a52625ed7..43c520d88a847 100644 --- a/apps/server/src/keybindings.test.ts +++ b/apps/server/src/keybindings.test.ts @@ -8,6 +8,7 @@ import * as Layer from "effect/Layer"; import * as Logger from "effect/Logger"; import * as Path from "effect/Path"; import * as Schema from "effect/Schema"; +import * as Stream from "effect/Stream"; import * as ServerConfig from "./config.ts"; import * as Keybindings from "./keybindings.ts"; import { KeybindingsConfigError, MAX_KEYBINDINGS_COUNT } from "@t3tools/contracts"; @@ -513,4 +514,41 @@ it.layer(NodeServices.layer)("keybindings", (it) => { } }).pipe(Effect.provide(layerKeybindings())), ); + + it.effect( + "suppresses change emission when revalidating unchanged keybindings configuration", + () => + Effect.gen(function* () { + const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig; + yield* writeKeybindingsConfig(keybindingsConfigPath, [ + { key: "mod+j", command: "terminal.toggle" }, + ]); + + const keybindings = yield* Keybindings.Keybindings; + yield* keybindings.start; + + const emitted: Keybindings.KeybindingsChangeEvent[] = []; + const subscription = yield* keybindings.subscribeChanges; + yield* Stream.runForEach(subscription, (event) => + Effect.sync(() => { + emitted.push(event); + }), + ).pipe(Effect.forkScoped); + + // Revalidate when config on disk has not changed (e.g. atime bump on Windows) + yield* keybindings.revalidate; + assert.equal(emitted.length, 0); + + // Update the file content on disk and revalidate + yield* writeKeybindingsConfig(keybindingsConfigPath, [ + { key: "mod+j", command: "terminal.toggle" }, + { key: "mod+k", command: "threadPanel.toggle" }, + ]); + yield* keybindings.revalidate; + assert.equal(emitted.length, 1); + assert.isTrue( + emitted[0]?.keybindings.some((rule) => rule.command === "threadPanel.toggle"), + ); + }).pipe(Effect.scoped, Effect.provide(layerKeybindings())), + ); }); diff --git a/apps/server/src/keybindings.ts b/apps/server/src/keybindings.ts index 7fbfdbe9e651c..de5d629afc433 100644 --- a/apps/server/src/keybindings.ts +++ b/apps/server/src/keybindings.ts @@ -187,6 +187,64 @@ export interface KeybindingsConfigState { readonly issues: readonly ServerConfigIssue[]; } +function isSameConfigIssue(left: ServerConfigIssue, right: ServerConfigIssue): boolean { + if (left.kind !== right.kind || left.message !== right.message) { + return false; + } + if (left.kind === "keybindings.invalid-entry" && right.kind === "keybindings.invalid-entry") { + return left.index === right.index; + } + return true; +} + +function isSameResolvedShortcut(left: KeybindingShortcut, right: KeybindingShortcut): boolean { + return ( + left.key === right.key && + left.metaKey === right.metaKey && + left.ctrlKey === right.ctrlKey && + left.shiftKey === right.shiftKey && + left.altKey === right.altKey && + left.modKey === right.modKey + ); +} + +function isSameWhenAst( + left: KeybindingWhenNode | null | undefined, + right: KeybindingWhenNode | null | undefined, +): boolean { + if (left === right) return true; + if (!left || !right) return false; + return encodeWhenAst(left) === encodeWhenAst(right); +} + +function isSameResolvedRule(left: ResolvedKeybindingRule, right: ResolvedKeybindingRule): boolean { + return ( + left.command === right.command && + isSameResolvedShortcut(left.shortcut, right.shortcut) && + isSameWhenAst(left.whenAst, right.whenAst) + ); +} + +function isSameKeybindingsConfigState( + left: KeybindingsConfigState, + right: KeybindingsConfigState, +): boolean { + if (left === right) return true; + if (left.keybindings.length !== right.keybindings.length) return false; + if (left.issues.length !== right.issues.length) return false; + for (let i = 0; i < left.keybindings.length; i++) { + if (!isSameResolvedRule(left.keybindings[i]!, right.keybindings[i]!)) { + return false; + } + } + for (let i = 0; i < left.issues.length; i++) { + if (!isSameConfigIssue(left.issues[i]!, right.issues[i]!)) { + return false; + } + } + return true; +} + export interface KeybindingsChangeEvent { readonly keybindings: ResolvedKeybindingsConfig; readonly issues: readonly ServerConfigIssue[]; @@ -227,6 +285,8 @@ export class Keybindings extends Context.Service< readonly streamChanges: Stream.Stream; + readonly revalidate: Effect.Effect; + /** Acquire before reading a snapshot to buffer changes until the stream starts. */ readonly subscribeChanges: Effect.Effect< Stream.Stream, @@ -255,12 +315,17 @@ const make = Effect.gen(function* () { const upsertSemaphore = yield* Semaphore.make(1); const resolvedConfigCacheKey = "resolved" as const; const changesPubSub = yield* PubSub.unbounded(); + const lastEmittedStateRef = yield* Ref.make(null); const startedRef = yield* Ref.make(false); const startedDeferred = yield* Deferred.make(); const watcherScope = yield* Scope.make("sequential"); yield* Effect.addFinalizer(() => Scope.close(watcherScope, Exit.void)); - const emitChange = (configState: KeybindingsConfigState) => - PubSub.publish(changesPubSub, configState).pipe(Effect.asVoid); + const emitChange = Effect.fn("keybindings.emitChange")(function* ( + configState: KeybindingsConfigState, + ) { + yield* Ref.set(lastEmittedStateRef, configState); + yield* PubSub.publish(changesPubSub, configState); + }); const readConfigExists = fs.exists(keybindingsConfigPath).pipe( Effect.mapError( @@ -452,8 +517,12 @@ const make = Effect.gen(function* () { const revalidateAndEmit = upsertSemaphore.withPermits(1)( Effect.gen(function* () { + const previousState = yield* Ref.get(lastEmittedStateRef); yield* Cache.invalidate(resolvedConfigCache, resolvedConfigCacheKey); const configState = yield* loadConfigStateFromCacheOrDisk; + if (previousState && isSameKeybindingsConfigState(previousState, configState)) { + return; + } yield* emitChange(configState); }), ); @@ -625,7 +694,8 @@ const make = Effect.gen(function* () { yield* startWatcher; yield* syncDefaultKeybindingsOnStartup; yield* Cache.invalidate(resolvedConfigCache, resolvedConfigCacheKey); - yield* loadConfigStateFromCacheOrDisk; + const initialState = yield* loadConfigStateFromCacheOrDisk; + yield* Ref.set(lastEmittedStateRef, initialState); }); const startupExit = yield* Effect.exit(startup); @@ -643,6 +713,7 @@ const make = Effect.gen(function* () { syncDefaultKeybindingsOnStartup, loadConfigState: loadConfigStateFromCacheOrDisk, getSnapshot: loadConfigStateFromCacheOrDisk, + revalidate: revalidateAndEmit, get streamChanges() { return Stream.fromPubSub(changesPubSub); }, From 938349493499470d52a7371bbe246734e07d298a Mon Sep 17 00:00:00 2001 From: Shaswat Raj Date: Sun, 1 Nov 2026 16:46:00 +0530 Subject: [PATCH 2/4] test(server): wait for keybindings change events deterministically The regression test consumed the change subscription in a scoped fork and asserted on an array right after `revalidate` returned, so the assertions raced the consumer fiber. Read the subscription with an awaited `Stream.runHead` instead. `revalidate` publishes before it returns, so a wrongly emitted event for the unchanged config would sit ahead of the real change in the subscription; asserting that the first buffered event carries the new `mod+k` rule observes both cases without a timing window. A wall-clock timeout would not fire under the test clock, so none is used. Co-Authored-By: Claude Fable 5.1 --- apps/server/src/keybindings.test.ts | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/apps/server/src/keybindings.test.ts b/apps/server/src/keybindings.test.ts index 43c520d88a847..1e2d4c87e8632 100644 --- a/apps/server/src/keybindings.test.ts +++ b/apps/server/src/keybindings.test.ts @@ -6,6 +6,7 @@ import * as Effect from "effect/Effect"; import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Logger from "effect/Logger"; +import * as Option from "effect/Option"; import * as Path from "effect/Path"; import * as Schema from "effect/Schema"; import * as Stream from "effect/Stream"; @@ -527,17 +528,12 @@ it.layer(NodeServices.layer)("keybindings", (it) => { const keybindings = yield* Keybindings.Keybindings; yield* keybindings.start; - const emitted: Keybindings.KeybindingsChangeEvent[] = []; - const subscription = yield* keybindings.subscribeChanges; - yield* Stream.runForEach(subscription, (event) => - Effect.sync(() => { - emitted.push(event); - }), - ).pipe(Effect.forkScoped); + const changes = yield* keybindings.subscribeChanges; - // Revalidate when config on disk has not changed (e.g. atime bump on Windows) + // Revalidate when config on disk has not changed (e.g. atime bump on Windows). + // `revalidate` publishes before it returns, so a wrongly emitted event would be + // buffered in the subscription ahead of the real change observed below. yield* keybindings.revalidate; - assert.equal(emitted.length, 0); // Update the file content on disk and revalidate yield* writeKeybindingsConfig(keybindingsConfigPath, [ @@ -545,9 +541,16 @@ it.layer(NodeServices.layer)("keybindings", (it) => { { key: "mod+k", command: "threadPanel.toggle" }, ]); yield* keybindings.revalidate; - assert.equal(emitted.length, 1); + + // The first buffered event must be the real change, not the unchanged state. + const change = Option.getOrThrow(yield* Stream.runHead(changes)); assert.isTrue( - emitted[0]?.keybindings.some((rule) => rule.command === "threadPanel.toggle"), + change.keybindings.some( + (rule) => + rule.command === "threadPanel.toggle" && + rule.shortcut.key === "k" && + rule.shortcut.modKey, + ), ); }).pipe(Effect.scoped, Effect.provide(layerKeybindings())), ); From 30642f2b0138d01a87e859a4c88c53658b1043ff Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 12 Nov 2026 21:54:24 +0000 Subject: [PATCH 3/4] refactor(server): compare keybindings state with schema equivalence AI-assisted; verified with the tests and tools listed above. --- apps/server/src/keybindings.ts | 65 ++++------------------------------ 1 file changed, 7 insertions(+), 58 deletions(-) diff --git a/apps/server/src/keybindings.ts b/apps/server/src/keybindings.ts index 17f58906795ea..49a7d85d4fa36 100644 --- a/apps/server/src/keybindings.ts +++ b/apps/server/src/keybindings.ts @@ -8,7 +8,7 @@ import { ResolvedKeybindingsConfig, type ServerRemoveKeybindingInput, type ServerUpsertKeybindingInput, - type ServerConfigIssue, + ServerConfigIssue, } from "@t3tools/contracts"; import * as Array from "effect/Array"; import * as Cache from "effect/Cache"; @@ -180,63 +180,12 @@ export interface KeybindingsConfigState { readonly issues: readonly ServerConfigIssue[]; } -function isSameConfigIssue(left: ServerConfigIssue, right: ServerConfigIssue): boolean { - if (left.kind !== right.kind || left.message !== right.message) { - return false; - } - if (left.kind === "keybindings.invalid-entry" && right.kind === "keybindings.invalid-entry") { - return left.index === right.index; - } - return true; -} - -function isSameResolvedShortcut(left: KeybindingShortcut, right: KeybindingShortcut): boolean { - return ( - left.key === right.key && - left.metaKey === right.metaKey && - left.ctrlKey === right.ctrlKey && - left.shiftKey === right.shiftKey && - left.altKey === right.altKey && - left.modKey === right.modKey - ); -} - -function isSameWhenAst( - left: KeybindingWhenNode | null | undefined, - right: KeybindingWhenNode | null | undefined, -): boolean { - if (left === right) return true; - if (!left || !right) return false; - return encodeWhenAst(left) === encodeWhenAst(right); -} - -function isSameResolvedRule(left: ResolvedKeybindingRule, right: ResolvedKeybindingRule): boolean { - return ( - left.command === right.command && - isSameResolvedShortcut(left.shortcut, right.shortcut) && - isSameWhenAst(left.whenAst, right.whenAst) - ); -} - -function isSameKeybindingsConfigState( - left: KeybindingsConfigState, - right: KeybindingsConfigState, -): boolean { - if (left === right) return true; - if (left.keybindings.length !== right.keybindings.length) return false; - if (left.issues.length !== right.issues.length) return false; - for (let i = 0; i < left.keybindings.length; i++) { - if (!isSameResolvedRule(left.keybindings[i]!, right.keybindings[i]!)) { - return false; - } - } - for (let i = 0; i < left.issues.length; i++) { - if (!isSameConfigIssue(left.issues[i]!, right.issues[i]!)) { - return false; - } - } - return true; -} +const isSameKeybindingsConfigState = Schema.toEquivalence( + Schema.Struct({ + keybindings: ResolvedKeybindingsConfig, + issues: Schema.Array(ServerConfigIssue), + }), +); export interface KeybindingsChangeEvent { readonly keybindings: ResolvedKeybindingsConfig; From 28bab7681792c32ef3c6a486484548963d7aa082 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sat, 14 Nov 2026 03:10:30 +0000 Subject: [PATCH 4/4] test(server): drop narrating comment in keybindings revalidation test Co-Authored-By: Claude Opus 5.5 --- apps/server/src/keybindings.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/server/src/keybindings.test.ts b/apps/server/src/keybindings.test.ts index 6d56405c60015..7bff1a6823df1 100644 --- a/apps/server/src/keybindings.test.ts +++ b/apps/server/src/keybindings.test.ts @@ -638,7 +638,6 @@ it.layer(NodeServices.layer)("keybindings", (it) => { // buffered in the subscription ahead of the real change observed below. yield* keybindings.revalidate; - // Update the file content on disk and revalidate yield* writeKeybindingsConfig(keybindingsConfigPath, [ { key: "mod+j", command: "terminal.toggle" }, { key: "mod+k", command: "threadPanel.toggle" },