From 0d20c481ada765f6bbc1b29894e27f1c2c4ea216 Mon Sep 17 00:00:00 2001 From: clover caruso Date: Sun, 25 Oct 2026 04:33:53 -0700 Subject: [PATCH] Prioritize the Knife server over agent workloads Extend the existing process guard to set every Knife server thread to nice -20, keep Claude and Codex harnesses at 0, and lower their commands to 19 on exec. Preserve OOM protection and update the guard without restarting knife-linux. Verified existing threads, new execs, cgroup isolation, and CPU contention with disposable Linux processes; deployment tests pass. Assisted-by: gpt-6.1-sol --- nixos/knife-linux.nix | 2 +- tools/knife-linux-oom.py | 23 +++++++++++++++-------- tools/knife-linux.md | 2 ++ 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/nixos/knife-linux.nix b/nixos/knife-linux.nix index 8fe52be1fed3b258d3ed1f64eb9bf5ed60ce4558..68aefd475d1b931abf4bbd3a8ae696a914c7d9ec 100644 --- a/nixos/knife-linux.nix +++ b/nixos/knife-linux.nix @@ -67,7 +67,7 @@ in }; systemd.services.knife-linux-oom = { - description = "Keep the Knife harnesses out of the OOM killer"; + description = "Protect Knife harnesses and prioritize the server over agent work"; wantedBy = [ "knife-linux.service" ]; bindsTo = [ "knife-linux.service" ]; after = [ "knife-linux.service" ]; diff --git a/tools/knife-linux-oom.py b/tools/knife-linux-oom.py index e6b239fa8d1723d27277247c40abbc8248de8a7c..1ed5b69b711fed314259e5e4abed9dfc0d10324a 100644 --- a/tools/knife-linux-oom.py +++ b/tools/knife-linux-oom.py @@ -1,8 +1,4 @@ -"""Exempts the Knife, Claude, and Codex harnesses in knife-linux from the OOM killer. - -Children inherit oom_score_adj at fork, so each exec outside the harness set gets a -killable score back from the kernel's process-event connector before it can grow. -""" +"""Protects Knife harnesses from OOM and prioritizes the server over their work.""" import errno import os @@ -16,6 +12,7 @@ HARNESS = re.compile( r"/run/podman-init|/home/clo/\.local/share/knife-server/.*" r"|/home/clo/\.knife/tools/(claude/[^/]+/claude|codex/[^/]+/bin/codex)" ) +SERVER = re.compile(r"/home/clo/\.local/share/knife-server/[^/]+/apps/server/dist/bin\.mjs") PROTECTED, WORK = -1000, 800 NETLINK_CONNECTOR, CN_IDX_PROC, PROC_CN_MCAST_LISTEN, PROC_EVENT_EXEC = 11, 1, 1, 2 @@ -28,14 +25,23 @@ def adjust(pid, scope): names = [os.readlink(f"/proc/{pid}/exe"), *(a.decode(errors="replace") for a in argv[1:2])] path = f"/proc/{pid}/oom_score_adj" current = int(open(path).read()) - if any(HARNESS.fullmatch(name) for name in names): + protected = any(HARNESS.fullmatch(name) for name in names) + if protected: wanted = PROTECTED elif current < 0: wanted = WORK else: - return + wanted = current if current != wanted: open(path, "w").write(str(wanted)) + nice = -20 if any(SERVER.fullmatch(name) for name in names) else 0 if protected else 19 + # Niceness belongs to each thread; renicing the leader leaves workers unchanged. + for tid in os.listdir(f"/proc/{pid}/task"): + try: + if os.getpriority(os.PRIO_PROCESS, int(tid)) != nice: + os.setpriority(os.PRIO_PROCESS, int(tid), nice) + except ProcessLookupError: + pass except (FileNotFoundError, ProcessLookupError, PermissionError): pass @@ -77,4 +83,5 @@ def main(): offset += (length + 3) & ~3 or len(data) -main() +if __name__ == "__main__": + main() diff --git a/tools/knife-linux.md b/tools/knife-linux.md index 7c22806f4f24700c5dcb3eb4e71a93a3f312ce3c..00dd4c60904287bd8b18b37639ab980a5bd09278 100644 --- a/tools/knife-linux.md +++ b/tools/knife-linux.md @@ -11,6 +11,8 @@ seccomp filter blocks the `chroot` Chromium-based sandboxes need, so Chromium, Electron, and T3's browser run with their own sandbox off inside the container. [knife-linux-oom.py](knife-linux-oom.py) exempts the Knife server, Claude, and Codex from the OOM killer; whatever they run stays killable at score 800. +It sets every server thread to nice -20, harnesses to 0, and their commands to +19, including newly executed commands. All use normal CPU scheduling. Deploys never restart the container: restarting stops Knife and every running agent. Image and unit changes apply at the next manual -- 2.54.1