fix concurrent reads to keybindingings config

This commit is contained in:
Julius Marminge authored and Julius Marminge committed 2026-02-26 13:20:36 -08:00
1 parent 07df7fb73b
commit c6d1f92ba9
3 files changed
+85 -32

No files matched your search

+4 -4
View File
@@ -313,8 +313,8 @@ Counts: active `51` (`valid=33`, `partially-valid=18`), closed-invalid `6`
- Threads: PRRT_kwDORLtfbc5w1GPo
- Audit note: Chunk-by-chunk UTF-8 decode can still corrupt split multibyte characters.
- [ ] `C039` The `+` key can be parsed (via trailing `+` handling) but cannot be encoded because `shortcut.key.includes("+")` returns true for the literal `+` key. Consider checking `shortcut.key === "+"` separately and encoding it as `"space"` style (e.g., a special token), or adjusting the condition to allow the single `+` character. <details> <summary>🚀 Reply "<strong>fix it for me</strong>" or copy this <strong>AI Prompt</strong> for your agent:</summary>
- Status: `TODO`
- [x] `C039` The `+` key can be parsed (via trailing `+` handling) but cannot be encoded because `shortcut.key.includes("+")` returns true for the literal `+` key. Consider checking `shortcut.key === "+"` separately and encoding it as `"space"` style (e.g., a special token), or adjusting the condition to allow the single `+` character. <details> <summary>🚀 Reply "<strong>fix it for me</strong>" or copy this <strong>AI Prompt</strong> for your agent:</summary>
- Status: `DONE`
- Verdict: `partially-valid`
- Severity: `Low`
- Area: `Edge-case parsing/platform behavior`
@@ -322,8 +322,8 @@ Counts: active `51` (`valid=33`, `partially-valid=18`), closed-invalid `6`
- Threads: PRRT_kwDORLtfbc5wxvIB
- Audit note: Parser/encoder mismatch remains, but encoder path currently low-use.
- [ ] `C040` `upsertKeybindingRule` has a race condition: concurrent calls read the same file state, then the last write overwrites earlier changes. Consider wrapping the read-modify-write sequence with `Effect.Semaphore` to serialize access. <details> <summary>🚀 Reply "<strong>fix it for me</strong>" or copy this <strong>AI Prompt</strong> for your agent:</summary>
- Status: `TODO`
- [x] `C040` `upsertKeybindingRule` has a race condition: concurrent calls read the same file state, then the last write overwrites earlier changes. Consider wrapping the read-modify-write sequence with `Effect.Semaphore` to serialize access. <details> <summary>🚀 Reply "<strong>fix it for me</strong>" or copy this <strong>AI Prompt</strong> for your agent:</summary>
- Status: `DONE`
- Verdict: `valid`
- Severity: `Low`
- Area: `Edge-case parsing/platform behavior`
+52 -4
View File
@@ -1,4 +1,4 @@
import { KeybindingRule, KeybindingsConfig } from "@t3tools/contracts";
import { KeybindingCommand, KeybindingRule, KeybindingsConfig } from "@t3tools/contracts";
import { NodeServices } from "@effect/platform-node";
import { assert, it } from "@effect/vitest";
import { assertFailure } from "@effect/vitest/utils";
@@ -9,6 +9,7 @@ import {
Keybindings,
KeybindingsConfigError,
KeybindingsLive,
ResolvedKeybindingFromConfig,
compileResolvedKeybindingRule,
parseKeybindingShortcut,
} from "./keybindings";
@@ -39,9 +40,7 @@ const toDetailResult = <A, R>(effect: Effect.Effect<A, KeybindingsConfigError, R
const writeKeybindingsConfig = (configPath: string, rules: readonly KeybindingRule[]) =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const encoded = yield* Schema.encodeEffect(KeybindingsConfigJson)(
rules,
);
const encoded = yield* Schema.encodeEffect(KeybindingsConfigJson)(rules);
yield* fileSystem.writeFileString(configPath, encoded);
});
@@ -104,6 +103,25 @@ it.layer(NodeServices.layer)("keybindings", (it) => {
}),
);
it.effect("encodes resolved plus-key shortcuts", () =>
Effect.gen(function* () {
const encoded = yield* Schema.encodeEffect(ResolvedKeybindingFromConfig)({
command: "terminal.toggle",
shortcut: {
key: "+",
metaKey: false,
ctrlKey: false,
shiftKey: false,
altKey: false,
modKey: true,
},
});
assert.equal(encoded.key, "mod++");
assert.equal(encoded.command, "terminal.toggle");
}),
);
it.effect("rejects invalid rules", () =>
Effect.sync(() => {
assert.isNull(
@@ -299,4 +317,34 @@ it.layer(NodeServices.layer)("keybindings", (it) => {
assert.isTrue(loadedAfterUpsert.some((entry) => entry.command === "terminal.toggle"));
}).pipe(Effect.provide(makeKeybindingsLayer())),
);
it.effect("serializes concurrent upserts to avoid lost updates", () =>
Effect.gen(function* () {
const { keybindingsConfigPath } = yield* ServerConfig;
yield* writeKeybindingsConfig(keybindingsConfigPath, []);
const commands = Array.from(
{ length: 20 },
(_, index): KeybindingCommand => `script.concurrent-${index}.run`,
);
yield* Effect.gen(function* () {
const keybindings = yield* Keybindings;
yield* Effect.all(
commands.map((command, index) =>
keybindings.upsertKeybindingRule({
key: `mod+${String.fromCharCode(97 + index)}`,
command,
}),
),
{ concurrency: "unbounded", discard: true },
);
});
const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
const persistedCommands = new Set(persisted.map((entry) => entry.command));
for (const command of commands) {
assert.isTrue(persistedCommands.has(command), `expected persisted command ${command}`);
}
}).pipe(Effect.provide(makeKeybindingsLayer())),
);
});
+29 -24
View File
@@ -32,6 +32,7 @@ import {
SchemaTransformation,
ServiceMap,
} from "effect";
import * as Semaphore from "effect/Semaphore";
import { ServerConfig } from "./config";
export class KeybindingsConfigError extends Schema.TaggedErrorClass<KeybindingsConfigError>()(
@@ -354,7 +355,8 @@ function encodeShortcut(shortcut: KeybindingShortcut): string | null {
if (shortcut.ctrlKey) modifiers.push("ctrl");
if (shortcut.altKey) modifiers.push("alt");
if (shortcut.shiftKey) modifiers.push("shift");
if (!shortcut.key || shortcut.key.includes("+")) return null;
if (!shortcut.key) return null;
if (shortcut.key !== "+" && shortcut.key.includes("+")) return null;
const key = shortcut.key === " " ? "space" : shortcut.key;
return [...modifiers, key].join("+");
}
@@ -433,6 +435,7 @@ const makeKeybindings = Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const cachedResolvedConfig = yield* Ref.make<ResolvedKeybindingsConfig | null>(null);
const upsertSemaphore = yield* Semaphore.make(1);
const loadCustomKeybindingsConfig = Effect.fn(function* (): Effect.fn.Return<
readonly KeybindingRule[],
@@ -509,29 +512,31 @@ const makeKeybindings = Effect.gen(function* () {
return {
loadResolvedKeybindingsConfig: loadResolvedFromCacheOrDisk,
upsertKeybindingRule: (rule) =>
Effect.gen(function* () {
const customConfig = yield* loadCustomKeybindingsConfig();
const nextConfig = [
...customConfig.filter((entry) => entry.command !== rule.command),
rule,
];
const cappedConfig =
nextConfig.length > MAX_KEYBINDINGS_COUNT
? nextConfig.slice(-MAX_KEYBINDINGS_COUNT)
: nextConfig;
if (nextConfig.length > MAX_KEYBINDINGS_COUNT) {
yield* Effect.logWarning("truncating keybindings config to max entries", {
path: keybindingsConfigPath,
maxEntries: MAX_KEYBINDINGS_COUNT,
});
}
yield* writeConfigAtomically(cappedConfig);
const nextResolved = mergeWithDefaultKeybindings(
compileResolvedKeybindingsConfig(cappedConfig),
);
yield* Ref.set(cachedResolvedConfig, nextResolved);
return nextResolved;
}),
upsertSemaphore.withPermits(1)(
Effect.gen(function* () {
const customConfig = yield* loadCustomKeybindingsConfig();
const nextConfig = [
...customConfig.filter((entry) => entry.command !== rule.command),
rule,
];
const cappedConfig =
nextConfig.length > MAX_KEYBINDINGS_COUNT
? nextConfig.slice(-MAX_KEYBINDINGS_COUNT)
: nextConfig;
if (nextConfig.length > MAX_KEYBINDINGS_COUNT) {
yield* Effect.logWarning("truncating keybindings config to max entries", {
path: keybindingsConfigPath,
maxEntries: MAX_KEYBINDINGS_COUNT,
});
}
yield* writeConfigAtomically(cappedConfig);
const nextResolved = mergeWithDefaultKeybindings(
compileResolvedKeybindingsConfig(cappedConfig),
);
yield* Ref.set(cachedResolvedConfig, nextResolved);
return nextResolved;
}),
),
} satisfies KeybindingsShape;
});