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 22, 07:00 UTC
fix(server): saving a shortcut no longer deletes rules it can't read (#24494)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: maria-rcks <254055478+maria-rcks@users.noreply.github.com>
Victor Vogel committed
Nov 1, 2026 at 16:00 UTC
a0d08d7550d54c394780c6f49e41b0cf210fd47e
6 files changed
+147
-84
apps/server/src/keybindings.test.ts
+47
@@ -444,6 +444,53 @@ it.layer(NodeServices.layer)("keybindings", (it) => {
444
}).pipe(Effect.provide(layerKeybindings())),
445
);
446
447
+ it.effect("keeps entries it cannot decode when editing other keybindings", () =>
448
+ Effect.gen(function* () {
449
+ const fs = yield* FileSystem.FileSystem;
450
+ const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
451
+ const futureCommand = { key: "mod+shift+k", command: "command.from-a-newer-build" };
452
+ const badShortcut = { key: "mod+shift+d+o", command: "terminal.new" };
453
+ yield* fs.writeFileString(
454
+ keybindingsConfigPath,
455
+ JSON.stringify([
456
+ { key: "mod+r", command: "script.run-tests.run" },
457
+ futureCommand,
458
+ badShortcut,
459
+ ]),
460
+ );
461
+ const keybindings = yield* Keybindings.Keybindings;
462
+ const persisted = Effect.map(fs.readFileString(keybindingsConfigPath), JSON.parse);
463
+
464
+ const saved = yield* keybindings.upsertKeybindingRule({
465
+ key: "mod+shift+r",
466
+ command: "script.run-tests.run",
467
+ });
468
+ assert.equal(saved.issues.length, 2);
469
+ assert.deepEqual(yield* persisted, [
470
+ { key: "mod+r", command: "script.run-tests.run" },
471
+ futureCommand,
472
+ badShortcut,
473
+ { key: "mod+shift+r", command: "script.run-tests.run" },
474
+ ]);
475
+
476
+ const removed = yield* keybindings.removeKeybindingRule({
477
+ key: "mod+r",
478
+ command: "script.run-tests.run",
479
+ });
480
+ assert.deepEqual(yield* persisted, [
481
+ futureCommand,
482
+ badShortcut,
483
+ { key: "mod+shift+r", command: "script.run-tests.run" },
484
+ ]);
485
+ const { issues } = yield* keybindings.loadConfigState;
486
+ assert.deepEqual(removed.issues, issues);
487
+ assert.deepEqual(
488
+ issues.map((issue) => issue.kind === "keybindings.invalid-entry" && issue.index),
489
+ [0, 1],
490
+ );
491
+ }).pipe(Effect.provide(layerKeybindings())),
492
+ );
493
+
494
it.effect("removes a hand-written keybinding targeted by its settings spelling", () =>
495
Effect.gen(function* () {
496
const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
apps/server/src/keybindings.ts
+36
-66
@@ -1,6 +1,5 @@
1
import {
2
KeybindingRule,
3
- KeybindingsConfig,
3
KeybindingsConfigError,
4
KeybindingShortcut,
5
KeybindingWhenNode,
@@ -170,11 +169,11 @@ function encodeWhenAst(node: KeybindingWhenNode): string {
169
}
170
171
const RawKeybindingsEntries = fromLenientJson(Schema.Array(Schema.Unknown));
173
-const KeybindingsConfigPrettyJson = fromJsonStringPretty(KeybindingsConfig);
172
+const RawKeybindingsEntriesPrettyJson = fromJsonStringPretty(Schema.Array(Schema.Unknown));
173
const decodeKeybindingRuleExit = Schema.decodeUnknownExit(KeybindingRule);
174
const decodeResolvedKeybindingFromConfigExit = Schema.decodeExit(ResolvedKeybindingFromConfig);
175
const decodeRawKeybindingsEntriesExit = Schema.decodeUnknownExit(RawKeybindingsEntries);
177
-const encodeKeybindingsConfigPrettyJson = Schema.encodeEffect(KeybindingsConfigPrettyJson);
176
+const encodeRawKeybindingsEntriesPrettyJson = Schema.encodeEffect(RawKeybindingsEntriesPrettyJson);
177
178
export interface KeybindingsConfigState {
179
readonly keybindings: ResolvedKeybindingsConfig;
@@ -234,11 +233,11 @@ export class Keybindings extends Context.Service<
233
*/
234
readonly upsertKeybindingRule: (
235
input: ServerUpsertKeybindingInput,
237
- ) => Effect.Effect<ResolvedKeybindingsConfig, KeybindingsConfigError>;
236
+ ) => Effect.Effect<KeybindingsConfigState, KeybindingsConfigError>;
237
238
readonly removeKeybindingRule: (
239
input: ServerRemoveKeybindingInput,
241
- ) => Effect.Effect<ResolvedKeybindingsConfig, KeybindingsConfigError>;
240
+ ) => Effect.Effect<KeybindingsConfigState, KeybindingsConfigError>;
241
}
242
>()("t3/keybindings") {}
243
@@ -278,8 +277,10 @@ const make = Effect.gen(function* () {
277
),
278
);
279
280
+ // Entries this build cannot decode, such as a typo or a command from a newer
281
+ // build, are written back unchanged so editing another shortcut keeps them.
282
const loadWritableCustomKeybindingsConfig = Effect.fn(function* (): Effect.fn.Return<
282
- readonly KeybindingRule[],
283
+ ReadonlyArray<{ readonly value: unknown; readonly rule: KeybindingRule | null }>,
284
KeybindingsConfigError
285
> {
286
if (!(yield* readConfigExists)) {
@@ -298,29 +299,16 @@ const make = Effect.gen(function* () {
299
),
300
);
301
301
- return yield* Effect.forEach(rawConfig, (entry) =>
302
- Effect.gen(function* () {
303
- const decodedRule = decodeKeybindingRuleExit(entry);
304
- if (decodedRule._tag === "Failure") {
305
- yield* Effect.logWarning("ignoring invalid keybinding entry", {
306
- path: keybindingsConfigPath,
307
- entry,
308
- error: Cause.pretty(decodedRule.cause),
309
- });
310
- return null;
311
- }
312
- const resolved = decodeResolvedKeybindingFromConfigExit(decodedRule.value);
313
- if (resolved._tag === "Failure") {
314
- yield* Effect.logWarning("ignoring invalid keybinding entry", {
315
- path: keybindingsConfigPath,
316
- entry,
317
- error: Cause.pretty(resolved.cause),
318
- });
319
- return null;
320
- }
321
- return decodedRule.value;
322
- }),
323
- ).pipe(Effect.map(Array.filter(Predicate.isNotNull)));
302
+ return rawConfig.map((entry) => {
303
+ const decodedRule = decodeKeybindingRuleExit(entry);
304
+ if (
305
+ decodedRule._tag === "Success" &&
306
+ decodeResolvedKeybindingFromConfigExit(decodedRule.value)._tag === "Success"
307
+ ) {
308
+ return { value: decodedRule.value, rule: decodedRule.value };
309
+ }
310
+ return { value: entry, rule: null };
311
+ });
312
});
313
314
const loadRuntimeCustomKeybindingsConfig = Effect.fn(function* (): Effect.fn.Return<
@@ -403,8 +391,8 @@ const make = Effect.gen(function* () {
391
),
392
);
393
406
- const writeConfigAtomically = (rules: readonly KeybindingRule[]) => {
407
- return encodeKeybindingsConfigPrettyJson(rules).pipe(
394
+ const writeConfigAtomically = (entries: ReadonlyArray<unknown>) => {
395
+ return encodeRawKeybindingsEntriesPrettyJson(entries).pipe(
396
Effect.map((encoded) => `${encoded}\n`),
397
Effect.flatMap((encoded) =>
398
writeFileStringAtomically({
@@ -444,13 +432,14 @@ const make = Effect.gen(function* () {
432
433
const loadConfigStateFromCacheOrDisk = Cache.get(resolvedConfigCache, resolvedConfigCacheKey);
434
447
- const revalidateAndEmit = upsertSemaphore.withPermits(1)(
448
- Effect.gen(function* () {
449
- yield* Cache.invalidate(resolvedConfigCache, resolvedConfigCacheKey);
450
- const configState = yield* loadConfigStateFromCacheOrDisk;
451
- yield* emitChange(configState);
452
- }),
453
- );
435
+ const reloadAndEmit = Effect.gen(function* () {
436
+ yield* Cache.invalidate(resolvedConfigCache, resolvedConfigCacheKey);
437
+ const configState = yield* loadConfigStateFromCacheOrDisk;
438
+ yield* emitChange(configState);
439
+ return configState;
440
+ });
441
+
442
+ const revalidateAndEmit = upsertSemaphore.withPermits(1)(reloadAndEmit);
443
444
const syncDefaultKeybindingsOnStartup = upsertSemaphore.withPermits(1)(
445
Effect.gen(function* () {
@@ -648,7 +637,8 @@ const make = Effect.gen(function* () {
637
const rule = keybindingRuleFromInput(input);
638
const replaceTarget = replaceTargetFromUpsertInput(input);
639
const nextConfig = [
651
- ...customConfig.filter((entry) => {
640
+ ...customConfig.filter(({ rule: entry }) => {
641
+ if (!entry) return true;
642
if (replaceTarget) {
643
return (
644
!isSameKeybindingRule(entry, replaceTarget) && !isSameKeybindingRule(entry, rule)
@@ -656,7 +646,7 @@ const make = Effect.gen(function* () {
646
}
647
return !isSameKeybindingRule(entry, rule);
648
}),
659
- rule,
649
+ { value: rule, rule },
650
];
651
const cappedConfig =
652
nextConfig.length > MAX_KEYBINDINGS_COUNT
@@ -668,19 +658,8 @@ const make = Effect.gen(function* () {
658
maxEntries: MAX_KEYBINDINGS_COUNT,
659
});
660
}
671
- yield* writeConfigAtomically(cappedConfig);
672
- const nextResolved = mergeWithDefaultKeybindings(
673
- compileResolvedKeybindingsConfig(cappedConfig),
674
- );
675
- yield* Cache.set(resolvedConfigCache, resolvedConfigCacheKey, {
676
- keybindings: nextResolved,
677
- issues: [],
678
- });
679
- yield* emitChange({
680
- keybindings: nextResolved,
681
- issues: [],
682
- });
683
- return nextResolved;
661
+ yield* writeConfigAtomically(cappedConfig.map(({ value }) => value));
662
+ return yield* reloadAndEmit;
663
}),
664
),
665
removeKeybindingRule: (input) =>
@@ -688,20 +667,11 @@ const make = Effect.gen(function* () {
667
Effect.gen(function* () {
668
const customConfig = yield* loadWritableCustomKeybindingsConfig();
669
const target = keybindingRuleFromInput(input);
691
- const nextConfig = customConfig.filter((entry) => !isSameKeybindingRule(entry, target));
692
- yield* writeConfigAtomically(nextConfig);
693
- const nextResolved = mergeWithDefaultKeybindings(
694
- compileResolvedKeybindingsConfig(nextConfig),
670
+ const nextConfig = customConfig.filter(
671
+ ({ rule }) => !rule || !isSameKeybindingRule(rule, target),
672
);
696
- yield* Cache.set(resolvedConfigCache, resolvedConfigCacheKey, {
697
- keybindings: nextResolved,
698
- issues: [],
699
- });
700
- yield* emitChange({
701
- keybindings: nextResolved,
702
- issues: [],
703
- });
704
- return nextResolved;
673
+ yield* writeConfigAtomically(nextConfig.map(({ value }) => value));
674
+ return yield* reloadAndEmit;
675
}),
676
),
677
} satisfies Keybindings["Service"];
apps/server/src/ws.test.ts
+2
-2
@@ -762,7 +762,7 @@ it.effect.each(["pending", "cached"] as const)(
762
const expectedSettings = [duringDiscovery, latestSettings].map(
763
ServerSettings.redactServerSettingsForClient,
764
);
765
- const expectedKeybindings = [duringAck, latestKeybindings];
765
+ const expectedKeybindings = [duringAck.keybindings, latestKeybindings.keybindings];
766
// The two sources can interleave, but each must retain its own publication order.
767
for (let i = 0; i < 4; i++) {
768
const event = yield* Queue.take(responses);
@@ -790,7 +790,7 @@ it.effect.each(["pending", "cached"] as const)(
790
resolved.config.settings,
791
ServerSettings.redactServerSettingsForClient(latestSettings),
792
);
793
- assert.deepEqual(resolved.config.keybindings, latestKeybindings);
793
+ assert.deepEqual(resolved.config.keybindings, latestKeybindings.keybindings);
794
yield* acknowledge;
795
}
796
yield* server.write(0, { _tag: "Interrupt", requestId, interruptors: [] });
apps/server/src/ws.ts
+2
-4
@@ -2538,14 +2538,12 @@ export const layerWsRpc = (
2538
[WS_METHODS.serverUpsertKeybinding]:
2539
/* Request instrumentation already owns the span; keep this helper in that context. */
2540
Effect.fnUntraced(function* (rule) {
2541
- const keybindingsConfig = yield* keybindings.upsertKeybindingRule(rule);
2542
- return { keybindings: keybindingsConfig, issues: [] };
2541
+ return yield* keybindings.upsertKeybindingRule(rule);
2542
}),
2543
[WS_METHODS.serverRemoveKeybinding]:
2544
/* Request instrumentation already owns the span; keep this helper in that context. */
2545
Effect.fnUntraced(function* (rule) {
2547
- const keybindingsConfig = yield* keybindings.removeKeybindingRule(rule);
2548
- return { keybindings: keybindingsConfig, issues: [] };
2546
+ return yield* keybindings.removeKeybindingRule(rule);
2547
}),
2548
[WS_METHODS.serverRunStorageCleanup]: () => storageCleanup.runNow,
2549
[WS_METHODS.serverGetStorageCleanupReport]: () => storageCleanup.reports,
apps/web/src/components/KeybindingsUpdateToast.logic.test.ts
new
+47
@@ -0,0 +1,47 @@
1
+import { describe, expect, it } from "vite-plus/test";
2
+import type { ServerConfigStreamEvent } from "@t3tools/contracts";
3
+
4
+import { createKeybindingsUpdateToastController } from "./KeybindingsUpdateToast.logic";
5
+
6
+function keybindingsUpdated(issueMessages: ReadonlyArray<string>): ServerConfigStreamEvent {
7
+ return {
8
+ version: 1,
9
+ type: "keybindingsUpdated",
10
+ payload: {
11
+ keybindings: [],
12
+ issues: issueMessages.map((message, index) => ({
13
+ kind: "keybindings.invalid-entry" as const,
14
+ message,
15
+ index,
16
+ })),
17
+ },
18
+ };
19
+}
20
+
21
+describe("keybindings update toast", () => {
22
+ it("shows one warning when a save and the file watcher report the same issue", () => {
23
+ let currentTime = 0;
24
+ const controller = createKeybindingsUpdateToastController({ now: () => currentTime });
25
+ const invalid = { _tag: "InvalidConfiguration", message: "unknown command" };
26
+
27
+ expect(controller.handle(keybindingsUpdated(["unknown command"]))).toEqual(invalid);
28
+ currentTime = 100;
29
+ expect(controller.handle(keybindingsUpdated(["unknown command"]))).toBeNull();
30
+
31
+ currentTime = 200;
32
+ expect(controller.handle(keybindingsUpdated(["bad shortcut"]))).toEqual({
33
+ _tag: "InvalidConfiguration",
34
+ message: "bad shortcut",
35
+ });
36
+
37
+ currentTime = 5_000;
38
+ expect(controller.handle(keybindingsUpdated(["bad shortcut"]))).toEqual({
39
+ _tag: "InvalidConfiguration",
40
+ message: "bad shortcut",
41
+ });
42
+ currentTime = 5_100;
43
+ expect(controller.handle(keybindingsUpdated([]))).toEqual({ _tag: "Success" });
44
+ currentTime = 5_200;
45
+ expect(controller.handle(keybindingsUpdated([]))).toBeNull();
46
+ });
47
+});
apps/web/src/components/KeybindingsUpdateToast.logic.ts
+13
-12
@@ -1,6 +1,6 @@
1
import type { ServerConfigStreamEvent } from "@t3tools/contracts";
2
3
-const KEYBINDINGS_SUCCESS_TOAST_COOLDOWN_MS = 2_000;
3
+const KEYBINDINGS_TOAST_COOLDOWN_MS = 2_000;
4
5
export type KeybindingsUpdateToastDecision =
6
| { readonly _tag: "Success" }
@@ -14,7 +14,7 @@ export function createKeybindingsUpdateToastController(input: {
14
readonly now?: () => number;
15
}): KeybindingsUpdateToastController {
16
const now = input.now ?? Date.now;
17
- let lastSuccessToastAt: number | null = null;
17
+ let lastToast: { readonly key: string; readonly at: number } | null = null;
18
19
return {
20
handle: (event) => {
@@ -23,23 +23,24 @@ export function createKeybindingsUpdateToastController(input: {
23
}
24
25
const issue = event.payload.issues.find((entry) => entry.kind.startsWith("keybindings."));
26
- if (issue) {
27
- return {
28
- _tag: "InvalidConfiguration",
29
- message: issue.message,
30
- };
31
- }
26
+ const decision: KeybindingsUpdateToastDecision = issue
27
+ ? { _tag: "InvalidConfiguration", message: issue.message }
28
+ : { _tag: "Success" };
29
30
+ // A save and the server's file watcher each publish the same snapshot,
31
+ // so an identical toast inside the cooldown is the same change twice.
32
+ const key = issue ? `invalid:${issue.message}` : "success";
33
const currentTime = now();
34
if (
35
- lastSuccessToastAt !== null &&
36
- currentTime - lastSuccessToastAt < KEYBINDINGS_SUCCESS_TOAST_COOLDOWN_MS
35
+ lastToast !== null &&
36
+ lastToast.key === key &&
37
+ currentTime - lastToast.at < KEYBINDINGS_TOAST_COOLDOWN_MS
38
) {
39
return null;
40
}
41
41
- lastSuccessToastAt = currentTime;
42
- return { _tag: "Success" };
42
+ lastToast = { key, at: currentTime };
43
+ return decision;
44
},
45
};
46
}