Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

fix(tui): fix keybindings watcher dropping rename events on Linux

kdgmpw693081bbbcc5
diff --git a/jiuwenswarm/channels/tui/frontend/src/core/keybindings/resolver.ts b/jiuwenswarm/channels/tui/frontend/src/core/keybindings/resolver.ts
index 296bf815d1..a9e27589c8 100644
--- a/jiuwenswarm/channels/tui/frontend/src/core/keybindings/resolver.ts
+++ b/jiuwenswarm/channels/tui/frontend/src/core/keybindings/resolver.ts
@@ -1,17 +1,21 @@
import { type KeyId, matchesKey } from "@mariozechner/pi-tui";
import type { KeybindingAction, KeybindingContextName } from "./actions.js";
-import { loadKeybindings, startKeybindingsWatcher } from "./store.js";
+import { getKeybindingsMtimeMs, loadKeybindings, startKeybindingsWatcher } from "./store.js";
import type { KeybindingWarning, ResolvedBindings } from "./types.js";
// Initialized at module load from defaults + user file. /keybindings triggers
// reloadResolver() after the user edits the file.
let current: ResolvedBindings = loadKeybindings().resolved;
+let lastMtimeMs: number | undefined = getKeybindingsMtimeMs();
/** Reload bindings from disk. Returns any validation warnings. */
export function reloadResolver(): KeybindingWarning[] {
+ const mtimeMs = getKeybindingsMtimeMs();
+ if (mtimeMs === lastMtimeMs) return [];
const result = loadKeybindings();
current = result.resolved;
+ lastMtimeMs = mtimeMs;
return result.warnings;
}
diff --git a/jiuwenswarm/channels/tui/frontend/src/core/keybindings/store.ts b/jiuwenswarm/channels/tui/frontend/src/core/keybindings/store.ts
index e56d9b038b..d2c68e037c 100644
--- a/jiuwenswarm/channels/tui/frontend/src/core/keybindings/store.ts
+++ b/jiuwenswarm/channels/tui/frontend/src/core/keybindings/store.ts
@@ -1,4 +1,4 @@
-import { existsSync, readFileSync } from "node:fs";
+import { existsSync, readFileSync, statSync } from "node:fs";
import { watch } from "node:fs";
import { homedir } from "node:os";
import { dirname, join } from "node:path";
@@ -29,6 +29,14 @@ export function keybindingsFileExists(): boolean {
return existsSync(KEYBINDINGS_FILE);
}
+export function getKeybindingsMtimeMs(): number | undefined {
+ try {
+ return statSync(KEYBINDINGS_FILE).mtimeMs;
+ } catch {
+ return undefined;
+ }
+}
+
/** Build a resolved map from a list of blocks (no validation). */
function buildResolved(blocks: KeybindingBlock[]): ResolvedBindings {
const resolved: ResolvedBindings = new Map();
@@ -263,8 +271,12 @@ export function startKeybindingsWatcher(onReload: () => void): void {
try {
_watcher = watch(dirPath, { persistent: false }, (_eventType, filename) => {
- // Ignore unrelated files in the same directory.
- if (filename !== fileName) return;
+ // Node 在 Linux 下对 rename 事件回调的 filename 可能为 null/undefined
+ //(vi/nano 的「写临时文件→rename 覆盖」保存方式正会触发此语义)。
+ // 该目录可能还有其它配置文件,null filename 也会触发同目录其它文件的
+ // rename 事件,此处一并放行;reloadResolver 内部用 mtime 比对兜底,
+ // keybindings.json 未变时直接跳过,不会真正读盘。
+ if (filename != null && filename !== fileName) return;
onReload();
});
} catch {