agent-shell icon indicating copy to clipboard operation
agent-shell copied to clipboard

Emacs starts to seize up while agent is streaming.

Open claytharrison opened this issue 4 months ago • 10 comments

Checklist

  • [x] I agree to communicate with the author myself (not AI-generated).
  • [x] I've read the README's Filing issues section.
  • [x] I'm running the latest versions (fill in below).
    • agent-shell version: 20260805.1036 — commit fa20e538
    • acp.el version: 20260803.2034 — commit 7d5c16eb
    • ACP package (e.g. claude-agent-acp) version: pi-acp 0.0.32
    • Agent CLI (e.g. claude, gemini) version: Pi 0.80.10 (with --no-extensions) ; also checked with Opencode 1.18.14
  • [N/A] For requesting new agent support, I'm including a link to the ACP-capable agent or related ACP package.
  • [x] For issues, I'm including ACP traffic (as per README).

Report

I've been experiencing crippling slowdowns and freezes in Emacs when using agent-shell (on any of my devices; linux laptop, desktop, native Android app) ever since the new markdown renderer was merged in (regression occurred in 6f2e6dc). It really makes agent-shell unusable for me for anything more than trivial tasks accomplished with minimal tokens. To submit this report I tried to isolate the cause using the procedure below by turning off Pi extensions, comparing using both Pi and Opencode, forbidding tool calls, and running with emacs -Q to isolate anything weird from my personal emacs setup. For any commit after the merge of the new markdown renderer, this procedure yields slowdowns and freezes on both Pi and Opencode.

(sorry the title doesn't super well reflect the actual bug, I didn't quite have the words to describe the problem when I started writing this issue, did a lot more investigation, then forgot to update the title before submitting)

Steps to reproduce

(this part is AI-written, sorry; but human edited)

Measured on a headless emacs -Q --daemon with agent-shell installed via package.el/MELPA. Exact sequence:

  1. Configure the daemon: agent-shell-session-strategy = new; opencode model pinned via agent-shell-opencode-default-model-id = "opencode-go/deepseek-v4-flash"; pi pointed at an agent dir (PI_CODING_AGENT_DIR) whose settings.json has defaultThinkingLevel: high.
  2. Start a shell (agent-shell--dwim with the pi or opencode config), wait for the session id, confirm agent-shell--current-thought-level-id = high.
  3. Instrumentation (for measurement only): (agent-shell-toggle-logging) to enable ACP traffic capture, and an advice on acp-traffic-log-traffic that disables its hardcoded 1000-line truncation so the full stream is logged.
  4. Submit a pure-generation prompt with no tool calls via the standard interface (not viewport - didn't test that at all) — e.g. a long creative-writing task ("write a 3-part short story + philosophical essay on the Ship of Theseus… think deeply before each part"), with an explicit "do not use any tools, respond with pure text".
  5. While the agent streams: every ~9s, time an emacsclient -e '(buffer-size)' round-trip (bash probe, setsid-detached so it survives).
  6. Observe the monotonic degradation: at ~250KB / ~8.5k agent_thought_chunk notifications, evals take >1s; at ~2.4–4.2MB / ~9–13k chunks, a trivial eval takes 6–8s+ (total freeze — typing and redisplay become unusable). M-x profiler-start + report at the end shows server-process-filter 41–61%, timer-event-handler 31–46%, redisplay only 1–4%.
  7. Killing the agent process (not emacs) lets the daemon drain and recover.

Artifacts

Profiler and traffic for the run using Pi with no extensions.

profiler-memory.elprofile.txt profiler-cpu.elprofile.txt traffic.traffic.txt

claytharrison avatar Aug 06 '26 13:08 claytharrison

Added some improvements in https://github.com/xenodium/agent-shell/commit/c0d94fb6902233f7b298a8f0b82743e42602ec36. Can you try that please? Should be in v0.70.3

xenodium avatar Aug 09 '26 09:08 xenodium

I had similar issues and looked into this a bit.

ever since the new markdown renderer was merged in

Markdown rendering isn't the bottleneck in this trace. In the reported emacs -Q configuration, thoughts default to collapsed, that setting is passed to the streamed thought fragment, and collapsed bodies skip Markdown rendering. The attached CPU profile shows its hot notification stacks in fragment updates, text-property searches, jit-lock-after-change, and redundantly reapplying the already-collapsed parent-group state.

The general problem seems to be that, presuming roughly fixed-size chunks, several scans on update i traverse Θ(i) accumulated text-property intervals. Summed over N updates, this path is Θ(N²).

The changes in c0d94fb only partially address this:

Work performed on update i Status after c0d94fb
field=output is reapplied to the complete fragment, producing an after-change span over the accumulated output. The modification is limited to the untagged tail, although finding that tail still searches from the start of the range.
The block and body ranges are derived, followed by another block lookup and three section lookups. A marker computes the block end once and reuses it, and bounded searches start at the block beginning, but obtaining the body range still traverses its accumulated intervals.
Each append applies a fresh body-property list after inserting the chunk, leaving Θ(N) text-property intervals after N fixed-size chunks. Unchanged.
The collapsed parent group is looked up and collapsed again after every update; collapsing it rediscovers and modifies the complete child region. Unchanged.

Ideally, the streaming update path would perform O(L) total work for L output characters — equivalently O(N) for fixed-size nonempty chunks — rather than Θ(N²).

Scott-Guest avatar Aug 09 '26 23:08 Scott-Guest

@Scott-Guest thanks for this!

Did some work on row 4 in https://github.com/xenodium/agent-shell/commit/a1f9466c4c6ae3633d6e1c3eff6ba34961943b47 and also pushed markdown improvements to make whitespace normalisation cheaper in https://github.com/xenodium/agent-shell/commit/0c490307d2a216b494f5682bd4f5d7f7fc6da368

xenodium avatar Aug 10 '26 13:08 xenodium

Unfortunately still having issues but it's improving. Got some fresh slop eval below, but I believe it. I saw in another issue that you haven't seen this and use mostly Claude Code. I wonder if you don't have this problem because Claude sends back pre-summarized thinking traces (or none at all)?


Benchmark results -- every ~9 seconds measuring the wall-clock latency of one emacsclient round-trip eval (a trivial (buffer-size)-ish read of agent-shell state), with a timeout 8. If the eval doesn't complete within 8s, the sample is recorded as FAIL.

version avg max FAIL (8s) onset
0fec197 (pre-regression) 156 ms 1.2 s 0/20 never
fa20e53 (pre-fix) 2950 ms 7.2 s 13/25 (52%) ~2.5 min
c0d94fb 1623 ms 7.1 s 11/32 (34%) ~3.5 min
4aa030e (v0.70.4) 1988 ms 7.2 s 10/32 (31%) ~3.9 min
1721178 (v0.71.1) 1940 ms 6.7 s 9/30 (30%) ~3.3 min

So the first fix improved things a bit, but freeze rate is still pretty high.

The function-level profile of v0.71.1 still shows the same #1 bottleneck from the original 6f2e6dc regression:

  agent-shell--on-notification          333K
  agent-shell--update-fragment          327K
  agent-shell-ui-update-fragment        305K
  agent-shell-ui--nearest-range-matching-property   184K   ← still #1
  text-property-search-forward / -backward           158K / 83K
  agent-shell-ui--block-range                         47K

None of the three fix commits touched agent-shell-ui--nearest-range-matching-property — the per-chunk O(buffer) text-property scan that locates the block/body range. That's the real quadratic core. Every fix so far (tag-untagged-output, group-children-end, whitespace watermark) has trimmed costs around it.

claytharrison avatar Aug 10 '26 15:08 claytharrison

Did more work for Rows 2 and 4 https://github.com/xenodium/agent-shell/commit/ca58d2ad3fedd438e37d272c199a3272853273a7, https://github.com/xenodium/agent-shell/commit/074bde1f207f97d0530b188da762937e54c48c15, https://github.com/xenodium/agent-shell/commit/024e3487a949e145688f3cdce18948c6d3367425 and https://github.com/xenodium/agent-shell/commit/719260394a3de4689a088a7284cff5e6af3a629d

None of the three fix commits touched agent-shell-ui--nearest-range-matching-property

I was hoping I could get acceptable performance without introducing further complexity. Its getting hairy.

Should be in the latest (you'll need at least v0.71.3).

xenodium avatar Aug 11 '26 11:08 xenodium

As mentioned elsewhere (https://github.com/xenodium/agent-shell/issues/764#issuecomment-5255787605) my benchmark shows this resolves the issues, won't get much of a chance to actually try it til tomorrow though

claytharrison avatar Aug 11 '26 16:08 claytharrison

Been running ab620f3 for a couple days and things are very much improved for day-to-day use, but sessions that run quite long still end up causing hangs, though not quite as severely as before. I'll pull the latest commit and see if anything changes.

claytharrison avatar Aug 14 '26 07:08 claytharrison

Been running https://github.com/xenodium/agent-shell/commit/ab620f37dd88089afa559de2b0f189dc8c9566e5 for a couple days and things are very much improved for day-to-day use

Good to hear.

but sessions that run quite long still end up causing hangs

There's been more changes landed since. Maybe try latest?

Also, have you tried tweaking agent-shell-header-style?

xenodium avatar Aug 14 '26 09:08 xenodium

Also, have you tried tweaking agent-shell-header-style?

Yes the whole time it's been set to text, it was the first thing I tried before I started profiling.

Maybe try latest?

Yesterday I pulled to latest (7166967) and was able to work for a good while without encountering any freezes, even momentarily. I think this is largely solved, or at least mitigated enough to handle even longer session lengths. I'll drive it a bit more today and come back if I hit it again.

claytharrison avatar Aug 18 '26 06:08 claytharrison

Yesterday I pulled to latest (https://github.com/xenodium/agent-shell/commit/71669673d769f6bf37cb6d911e29b76f7cf995b5) and was able to work for a good while without encountering any freezes, even momentarily. I think this is largely solved

Great to hear. Going to close issue. Happy to reopen if needed.

If you are getting value out of agent-shell, consider sponsoring the project so I can continue supporting like in this instane. I for one, do not use OpenCode, so the effort was primarily driven to cater for these users.

xenodium avatar Aug 18 '26 13:08 xenodium