authoravatarclover caruso<git@paperclover.net>2026-10-25 04:33:53-07:00
committeravatarclover caruso<git@paperclover.net>2026-11-12 16:08:55-08:00
log0d20c481ada765f6bbc1b29894e27f1c2c4ea216
tree937425f803bbcc66b7140ccf780d5ac425bba090
parent2b3ff5841b47959fe8b22c78617bd88558c0ad1a
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

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

3 files changed, 18 insertions(+), 9 deletions(-)

nixos/knife-linux.nix+1-1
...@@ -67,7 +67,7 @@ in...@@ -67,7 +67,7 @@ in
67 };67 };
6868
69 systemd.services.knife-linux-oom = {69 systemd.services.knife-linux-oom = {
70 description = "Keep the Knife harnesses out of the OOM killer";70 description = "Protect Knife harnesses and prioritize the server over agent work";
71 wantedBy = [ "knife-linux.service" ];71 wantedBy = [ "knife-linux.service" ];
72 bindsTo = [ "knife-linux.service" ];72 bindsTo = [ "knife-linux.service" ];
73 after = [ "knife-linux.service" ];73 after = [ "knife-linux.service" ];
tools/knife-linux-oom.py+15-8
...@@ -1,8 +1,4 @@...@@ -1,8 +1,4 @@
1"""Exempts the Knife, Claude, and Codex harnesses in knife-linux from the OOM killer.1"""Protects Knife harnesses from OOM and prioritizes the server over their work."""
2
3Children inherit oom_score_adj at fork, so each exec outside the harness set gets a
4killable score back from the kernel's process-event connector before it can grow.
5"""
62
7import errno3import errno
8import os4import os
...@@ -16,6 +12,7 @@ HARNESS = re.compile(...@@ -16,6 +12,7 @@ HARNESS = re.compile(
16 r"/run/podman-init|/home/clo/\.local/share/knife-server/.*"12 r"/run/podman-init|/home/clo/\.local/share/knife-server/.*"
17 r"|/home/clo/\.knife/tools/(claude/[^/]+/claude|codex/[^/]+/bin/codex)"13 r"|/home/clo/\.knife/tools/(claude/[^/]+/claude|codex/[^/]+/bin/codex)"
18)14)
15SERVER = re.compile(r"/home/clo/\.local/share/knife-server/[^/]+/apps/server/dist/bin\.mjs")
19PROTECTED, WORK = -1000, 80016PROTECTED, WORK = -1000, 800
20NETLINK_CONNECTOR, CN_IDX_PROC, PROC_CN_MCAST_LISTEN, PROC_EVENT_EXEC = 11, 1, 1, 217NETLINK_CONNECTOR, CN_IDX_PROC, PROC_CN_MCAST_LISTEN, PROC_EVENT_EXEC = 11, 1, 1, 2
2118
...@@ -28,14 +25,23 @@ def adjust(pid, scope):...@@ -28,14 +25,23 @@ def adjust(pid, scope):
28 names = [os.readlink(f"/proc/{pid}/exe"), *(a.decode(errors="replace") for a in argv[1:2])]25 names = [os.readlink(f"/proc/{pid}/exe"), *(a.decode(errors="replace") for a in argv[1:2])]
29 path = f"/proc/{pid}/oom_score_adj"26 path = f"/proc/{pid}/oom_score_adj"
30 current = int(open(path).read())27 current = int(open(path).read())
31 if any(HARNESS.fullmatch(name) for name in names):28 protected = any(HARNESS.fullmatch(name) for name in names)
29 if protected:
32 wanted = PROTECTED30 wanted = PROTECTED
33 elif current < 0:31 elif current < 0:
34 wanted = WORK32 wanted = WORK
35 else:33 else:
36 return34 wanted = current
37 if current != wanted:35 if current != wanted:
38 open(path, "w").write(str(wanted))36 open(path, "w").write(str(wanted))
37 nice = -20 if any(SERVER.fullmatch(name) for name in names) else 0 if protected else 19
38 # Niceness belongs to each thread; renicing the leader leaves workers unchanged.
39 for tid in os.listdir(f"/proc/{pid}/task"):
40 try:
41 if os.getpriority(os.PRIO_PROCESS, int(tid)) != nice:
42 os.setpriority(os.PRIO_PROCESS, int(tid), nice)
43 except ProcessLookupError:
44 pass
39 except (FileNotFoundError, ProcessLookupError, PermissionError):45 except (FileNotFoundError, ProcessLookupError, PermissionError):
40 pass46 pass
4147
...@@ -77,4 +83,5 @@ def main():...@@ -77,4 +83,5 @@ def main():
77 offset += (length + 3) & ~3 or len(data)83 offset += (length + 3) & ~3 or len(data)
7884
7985
80main()86if __name__ == "__main__":
87 main()
tools/knife-linux.md+2
...@@ -11,6 +11,8 @@ seccomp filter blocks the `chroot` Chromium-based sandboxes need, so Chromium,...@@ -11,6 +11,8 @@ seccomp filter blocks the `chroot` Chromium-based sandboxes need, so Chromium,
11Electron, and T3's browser run with their own sandbox off inside the container.11Electron, and T3's browser run with their own sandbox off inside the container.
12[knife-linux-oom.py](knife-linux-oom.py) exempts the Knife server, Claude, and12[knife-linux-oom.py](knife-linux-oom.py) exempts the Knife server, Claude, and
13Codex from the OOM killer; whatever they run stays killable at score 800.13Codex from the OOM killer; whatever they run stays killable at score 800.
14It sets every server thread to nice -20, harnesses to 0, and their commands to
1519, including newly executed commands. All use normal CPU scheduling.
1416
15Deploys never restart the container: restarting stops Knife and every running17Deploys never restart the container: restarting stops Knife and every running
16agent. Image and unit changes apply at the next manual18agent. Image and unit changes apply at the next manual