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
6767 };
6868
6969 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";
7171 wantedBy = [ "knife-linux.service" ];
7272 bindsTo = [ "knife-linux.service" ];
7373 after = [ "knife-linux.service" ];
tools/knife-linux-oom.py+15-8
......@@ -1,8 +1,4 @@
1"""Exempts the Knife, Claude, and Codex harnesses in knife-linux from the OOM killer.
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"""
1"""Protects Knife harnesses from OOM and prioritizes the server over their work."""
62
73import errno
84import os
......@@ -16,6 +12,7 @@ HARNESS = re.compile(
1612 r"/run/podman-init|/home/clo/\.local/share/knife-server/.*"
1713 r"|/home/clo/\.knife/tools/(claude/[^/]+/claude|codex/[^/]+/bin/codex)"
1814)
15SERVER = re.compile(r"/home/clo/\.local/share/knife-server/[^/]+/apps/server/dist/bin\.mjs")
1916PROTECTED, WORK = -1000, 800
2017NETLINK_CONNECTOR, CN_IDX_PROC, PROC_CN_MCAST_LISTEN, PROC_EVENT_EXEC = 11, 1, 1, 2
2118
......@@ -28,14 +25,23 @@ def adjust(pid, scope):
2825 names = [os.readlink(f"/proc/{pid}/exe"), *(a.decode(errors="replace") for a in argv[1:2])]
2926 path = f"/proc/{pid}/oom_score_adj"
3027 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:
3230 wanted = PROTECTED
3331 elif current < 0:
3432 wanted = WORK
3533 else:
36 return
34 wanted = current
3735 if current != wanted:
3836 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
3945 except (FileNotFoundError, ProcessLookupError, PermissionError):
4046 pass
4147
......@@ -77,4 +83,5 @@ def main():
7783 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,
1111Electron, and T3's browser run with their own sandbox off inside the container.
1212[knife-linux-oom.py](knife-linux-oom.py) exempts the Knife server, Claude, and
1313Codex 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
1517Deploys never restart the container: restarting stops Knife and every running
1618agent. Image and unit changes apply at the next manual