fix(keymap): prevent Niri and Hyprland CPU-budget failures (1.3.4) (#135)

* fix(keymap): reduce Niri parser CPU usage

* test(keymap): cover Niri parser scan budget

* fix(keymap): keep Hyprland refreshes within CPU budget

* chore(keymap): bump release to 1.3.4
This commit is contained in:
blacku
2026-07-28 21:11:34 -04:00
committed by GitHub
parent c04fe989e4
commit 9c60991b0d
9 changed files with 449 additions and 102 deletions
+7 -4
View File
@@ -59,10 +59,13 @@ assert all(";" not in entry["template"] and "{" not in re.sub(r"\{\{[^}]+\}\}",
assert all("#" not in entry["template"]
for entry in entries if entry["source"] == "mangowc")
# The retained UI deliberately renders only a small result window and removes
# callbacks that are no longer part of the current render.
# The retained UI deliberately renders only a small result window and passes
# closures directly instead of registering callback names in the script global
# environment. Keep this assertion aligned with the plugin API 9 callback form.
assert "local visibleCount = math.min(#matches, 6)" in panel
assert "finishDynamicCallbackRender()" in panel
assert 'registerDynamicCallback(callbackName' in panel
assert "local selectedEntry = entry" in panel
assert "onClick = useCallback" in panel
assert "finishDynamicCallbackRender()" not in panel
assert "registerDynamicCallback(" not in panel
print(f"command library tests: ok ({len(entries)} entries: {dict(counts)})")
+135
View File
@@ -0,0 +1,135 @@
local rootPath = "/fixture/hypr/hyprland.lua"
local files = {}
local rootLines = {
"-- Hyprland configuration",
'require("keybind")',
'require("colors")',
'require("noctalia").apply_theme()',
'require("keymap")',
}
for index = 1, 120 do
rootLines[#rootLines + 1] = string.format("hl.config({ value_%d = %d })", index, index)
end
files[rootPath] = table.concat(rootLines, "\n")
local bindLines, bindDescriptions = {}, {}
for index = 1, 60 do
if index % 15 == 1 then
bindLines[#bindLines + 1] = "-- " .. tostring(math.floor(index / 15) + 1) .. ". Group"
end
local description = "Action " .. tostring(index)
bindDescriptions[#bindDescriptions + 1] = description
bindLines[#bindLines + 1] = string.format(
'hl.bind("SUPER + Key%d", hl.dsp.exec_cmd("command-%d"), { description = "%s" })',
index, index, description
)
end
files["/fixture/hypr/keybind.lua"] = table.concat(bindLines, "\n")
local unrelated = {}
for index = 1, 100 do
unrelated[#unrelated + 1] = string.format("local value_%d = %d", index, index)
end
files["/fixture/hypr/colors.lua"] = table.concat(unrelated, "\n")
files["/fixture/hypr/noctalia.lua"] = table.concat(unrelated, "\n")
files["/fixture/hypr/keymap.lua"] = table.concat({
"-- Managed by Noctalia Keymap.",
"-- 5. Managed",
'hl.bind("CTRL + grave", hl.dsp.exec_cmd("managed-command"), { description = "Managed action" })',
}, "\n")
bindDescriptions[#bindDescriptions + 1] = "Managed action"
local textRecords = {}
for index, description in ipairs(bindDescriptions) do
textRecords[#textRecords + 1] = table.concat({
"bindd",
"\tmodmask: 64",
"\tsubmap: ",
"\tkey: Key" .. tostring(index),
"\tkeycode: 0",
"\tcatchall: false",
"\tdescription: " .. description,
"\tdispatcher: __lua",
"\targ: " .. tostring(index),
"",
}, "\n")
end
local textOutput = table.concat(textRecords, "\n")
local values, watchers = {}, {}
local instructionBlocks, firstAsyncInstructionBlocks = 0, nil
noctalia = {
getConfig = function(key)
return ({
compositor = "hyprland",
hyprland_config = rootPath,
merge_sequential = false,
show_undescribed = true,
})[key]
end,
getenv = function(key) return key == "HYPRLAND_INSTANCE_SIGNATURE" and "fixture" or "" end,
expandPath = function(path) return path end,
fileExists = function(path) return files[path] ~= nil end,
listDir = function() return nil end,
readFile = function(path) return files[path] end,
commandExists = function(command) return command == "hyprctl" end,
json = { decode = function() error("Hyprland emitted invalid JSON") end },
runAsync = function(command, callback)
if firstAsyncInstructionBlocks == nil then firstAsyncInstructionBlocks = instructionBlocks end
callback({
exitCode = 0,
timedOut = false,
stdout = command == "hyprctl binds -j" and "{invalid-json" or textOutput,
})
return true
end,
tr = function(key)
if key == "category.other" then return "Other" end
if key == "category.undescribed" then return "Without description" end
return key
end,
state = {
get = function(key) return values[key] end,
set = function(key, value)
values[key] = value
if watchers[key] ~= nil then watchers[key](value) end
end,
watch = function(key, callback) watchers[key] = callback end,
},
}
debug.sethook(function() instructionBlocks = instructionBlocks + 1 end, "", 1000)
assert(loadfile("service.luau"))()
debug.sethook()
local initialInstructionBlocks = instructionBlocks
instructionBlocks, firstAsyncInstructionBlocks = 0, nil
debug.sethook(function() instructionBlocks = instructionBlocks + 1 end, "", 1000)
watchers["keymap.refresh_request"](1)
debug.sethook()
local refreshInstructionBlocks = instructionBlocks
local refreshScanInstructionBlocks = firstAsyncInstructionBlocks
local snapshot = values["keymap.snapshot"]
assert(snapshot.status == "ready", "split Hyprland fallback did not publish a ready snapshot")
assert(snapshot.total == #bindDescriptions, "split Hyprland fallback lost binds")
local editable = 0
for _, category in ipairs(snapshot.categories or {}) do
for _, bind in ipairs(category.binds or {}) do
if bind.capabilities ~= nil and bind.capabilities.combo == true
and bind.capabilities.description == true and bind.capabilities.command == true
and bind.source ~= nil and bind.raw_snippet ~= nil and bind.fingerprint == "exact-v1" then
editable = editable + 1
end
end
end
assert(editable == #bindDescriptions, "literal Hyprland binds did not retain editable source provenance")
assert(refreshScanInstructionBlocks < 50, "Hyprland source scan exceeded its callback instruction budget")
assert(refreshInstructionBlocks < 150, "Hyprland refresh exceeded its regression instruction budget")
assert(refreshScanInstructionBlocks ~= nil, "Hyprland parser did not start the live bind request")
print(string.format(
"hypr CPU-budget regression tests: ok (%d initial / %d refresh scan / %d refresh total blocks, %d editable binds)",
initialInstructionBlocks, refreshScanInstructionBlocks, refreshInstructionBlocks, editable
))
+100
View File
@@ -0,0 +1,100 @@
local rootLines = {}
for index = 1, 420 do
rootLines[#rootLines + 1] = "// Stock Niri configuration documentation line " .. tostring(index)
end
rootLines[#rootLines + 1] = "binds {"
for index = 1, 114 do
rootLines[#rootLines + 1] = string.format(
" Mod+Key%d repeat=false { focus-workspace %d; }", index, index
)
end
rootLines[#rootLines + 1] = "}"
rootLines[#rootLines + 1] = 'include "mine/binds.kdl"'
rootLines[#rootLines + 1] = 'include optional=true "mine/debug.kdl"'
rootLines[#rootLines + 1] = 'include "mine/theme.kdl"'
local includeLines = { "binds {" }
for index = 115, 126 do
includeLines[#includeLines + 1] = string.format(
' Mod+Key%d hotkey-overlay-title="Included %d" { focus-workspace %d; }',
index, index, index
)
end
includeLines[#includeLines + 1] = "}"
local sources = {
["/fixture/config.kdl"] = table.concat(rootLines, "\n"),
["/fixture/mine/binds.kdl"] = table.concat(includeLines, "\n"),
["/fixture/mine/debug.kdl"] = "binds {\n Mod+Key127 { toggle-debug-tint; }\n}",
["/fixture/mine/theme.kdl"] = string.rep("// unrelated theme setting\n", 500),
}
local values, watchers = {}, {}
local reads = {}
local xorCalls = 0
local stringSubCalls = 0
local originalBit32 = bit32
local originalStringSub = string.sub
string.sub = function(...)
stringSubCalls = stringSubCalls + 1
return originalStringSub(...)
end
bit32 = {
bxor = function(left, right)
xorCalls = xorCalls + 1
local result, place = 0, 1
for _ = 1, 8 do
if left % 2 ~= right % 2 then result = result + place end
left = math.floor(left / 2)
right = math.floor(right / 2)
place = place * 2
end
return result
end,
}
noctalia = {
state = {
get = function(key) return values[key] end,
set = function(key, value)
values[key] = value
if watchers[key] ~= nil then watchers[key](value) end
end,
watch = function(key, callback) watchers[key] = callback end,
},
getConfig = function(key)
return ({
compositor = "niri", niri_config = "/fixture/config.kdl", merge_sequential = false,
})[key]
end,
getenv = function(key) return key == "NIRI_SOCKET" and "test" or "" end,
fileExists = function(path) return path == "/fixture/config.kdl" end,
listDir = function() return nil end,
readFile = function(path)
reads[path] = (reads[path] or 0) + 1
return sources[path]
end,
tr = function(key, args)
if args ~= nil and args.action ~= nil then return args.action end
return key == "category.other" and "Other" or key
end,
}
assert(loadfile("niri_service.luau"))()
bit32 = originalBit32
string.sub = originalStringSub
local snapshot = values["keymap.snapshot"]
assert(snapshot.status == "ready", "large, split Niri fixture did not parse")
assert(snapshot.total == 127, "large, split Niri fixture lost binds")
assert(reads["/fixture/config.kdl"] == 1, "Niri root config should only be read once")
assert(reads["/fixture/mine/binds.kdl"] == 1, "Niri bind include should only be read once")
assert(reads["/fixture/mine/debug.kdl"] == 1, "Niri optional include should only be read once")
assert(reads["/fixture/mine/theme.kdl"] == 1, "Niri non-bind include should only be read once")
assert(xorCalls > 0, "Niri parser did not use the native-xor fingerprint path")
assert(
stringSubCalls < 45000,
"Niri parser scanned an unrelated include character by character: " .. tostring(stringSubCalls)
)
print("niri CPU-budget regression tests: ok")
+3
View File
@@ -111,6 +111,9 @@ local function runCase(case)
case.id, source, case.snippet, case.capabilities,
case.startLine, case.endLine
)
-- Active Hyprland source entries use exact byte comparison to keep hashing
-- out of the service's tightly budgeted refresh callback.
if case.compositor == "Hyprland" then bind.fingerprint = "exact-v1" end
stateValues["keymap.snapshot"] = {
status = "ready", compositor = case.compositor, source = root,
categories = { { name = case.category, binds = { bind } } },