perf: tail-scan discord stop hook + single-scan supervisor reconcile - #3
Open
Castrozan wants to merge 3 commits into
Open
perf: tail-scan discord stop hook + single-scan supervisor reconcile#3Castrozan wants to merge 3 commits into
Castrozan wants to merge 3 commits into
Conversation
…er turn The Stop hook ran on every turn-end and json.loads-ed the entire transcript jsonl. With the 1M context window live transcripts reach 25-150 MB, so each turn-end paid an O(transcript) parse. It only needs the suffix from the newest discord-envelope user turn onward, so reverse-read the file newest-line-first and stop at that turn. Benchmark (47 MB / 8000-entry transcript, best of 5): discord turn is last entry: 217.8ms -> 0.8ms (272x) discord turn 50 entries from end: 217.2ms -> 36.0ms (6x) Decision semantics are identical: the suffix always contains the newest discord turn and everything after it, which is all chat_id_needing_reply inspects.
The reconcile loop runs every 10s. find_session_agent_wrapper_processes ran a global pgrep then forked one `ps -ww -p PID -o command=` per matched wrapper to recover its command line. Live there are 2 sessions x 4 wrappers, so each cycle paid 2 x (1 pgrep + 4 ps) = 10 forks just to discover processes, growing with the wrapper count and re-scanned per session. Read every process command line once with `ps -axww -o pid=,command=` and filter in Python, so discovery is a single fork regardless of wrapper count. Subprocess forks for discovery drop from 1+N to 1 per session call (10 -> 2 per live cycle, ~48k fewer forks/day). Matching is identical: the same full-command-line pattern the old pgrep -f used.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for freeto join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Focused performance pass on the clawde agent loop. A 35-agent adversarial workflow profiled every subsystem (heartbeat gate/probe, supervisor reconcile, wrapper/watchdog, discord hooks, a2a adapter, identity/redeploy) and confirmed 11 real hot-path bottlenecks against source. The two that are both high-impact and live are fixed and measured here; the rest are dormant or LOW with fixes the verifiers rated unsafe, documented with reasons in
PERF_TRACKER.md.Fixes
Discord Stop hook: tail-scan instead of full transcript parse (every turn-end).
The hook
json.loads-ed the entire transcript jsonl on every turn-end; with the 1M context window live transcripts reach tens of MB, so each turn paid an O(transcript) parse. It only needs the suffix from the newest discord-envelope user turn onward, so it now reverse-reads the file newest-line-first and stops at that turn. Decision semantics are identical.Benchmark (47 MB / 8000-entry transcript, best of 5): common case 217.8ms → 0.8ms (272x); discord turn 50 entries deep 217.2ms → 36.0ms.
Supervisor reconcile: one
psscan instead ofpgrep+ N×ps(every 10s).find_session_agent_wrapper_processesran a global pgrep then forked oneps -ww -p PIDper matched wrapper. It now reads every command line once withps -axww -o pid=,command=and filters in Python, so discovery is a single fork regardless of wrapper count. Live (2 sessions × 4 wrappers): discovery forks drop ~10 → 2 per cycle, roughly 48k fewer forks/day. Matching is identical to the oldpgrep -fpattern.How verified
nix flake checkgreen (unit-tests, formatting, lint).PERF_TRACKER.md.Not merged
Base
main, for review only.