Skip to content

fix(plugins): trim or drop request results too large for a plugin's heap - #398

Merged
fcoury merged 2 commits into
mainfrom
fix/plugin-result-budget
Oct 15, 2026
Merged

fcoury merged 2 commits into
mainfrom
fix/plugin-result-budget

Conversation

@fcoury

@fcoury fcoury commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

A request result too large for the receiving plugin's 64 MiB Husk heap failed while Red was binding the callback argument, before any plugin code ran, and the plugin was quarantined. #393 fixed the second, accounting-only copy behind #388, but a large TypeScript project's empty workspace-symbol query, about 60,000 symbols or roughly 130 MB once decoded, still stopped lsp_symbols this way.

Fixes #394, which also completes the remaining part of #388.

What changes

  • Results are checked against the plugin's free heap before its callback runs. The check uses the runtime's own accounting, which husk-runtime now exposes as heap_cell_size, heap_value_size and Vm::available_heap_bytes. Results other than symbol and reference lists are delivered unchanged whenever they fit, so they behave as today.
  • Oversized symbol and reference results are trimmed. For DocumentSymbols, WorkspaceSymbols and References, a list larger than half the free heap keeps only the leading entries that fit in that half. The plugin needs the other half to work with the list: 28,000 symbols (62 MB) fit the 64 MiB heap, yet lsp_symbols failed building picker items from them. Lists between half and all of the free heap were delivered whole before; they now arrive trimmed, with total giving the real count.
  • Every other oversized result is dropped instead of quarantining the plugin. Its callback isn't called, and Red shows a warning attributed to the plugin, for example reader: dropped a GetBufferText result too large for its memory (70.0 MiB needed, 64.0 MiB free). It also logs a WARN line. I chose dropping over delivering { ok: false } because most of these callbacks read fields that only exist on success, and reading a missing field fails, which would quarantine the plugin anyway.
  • Successful symbol and reference results now include total, the entry count before any trimming. lsp_symbols uses it, so its status reads "first 4096 of 60000" from the real count.
  • Host API 0.20.0 is open, because 0.19.0 shipped with v0.9.0:
    • RED_HOST_API_VERSION is 0.20.0, and 0.19.0 stays in the supported list, so 0.19.0 packages keep loading.
    • The schema version is bumped, and docs/plugin_api_changes.json records the new total fields and the oversized-result behavior.
    • docs/PLUGIN_API.md has a new "Oversized request results" section.
    • Manifests, protocol docs and examples stay at the earliest version their features need. The JSON-RPC handshake example now shows the host's 0.20.0.

How to Test

  1. Save this plugin as reader.hk. It reads the whole buffer:

    pub fn activate() {
        red::add_command("ReadAll", read_all);
        red::add_command("Ping", ping);
    }
    
    fn read_all() {
        red::request("GetBufferText", loaded);
    }
    
    fn loaded(payload: Json) {
        red::execute("Print", "read the whole buffer");
    }
    
    fn ping() {
        red::execute("Print", "reader is still running");
    }
  2. Add reader = "/absolute/path/to/reader.hk" to the [plugins] table of your config.toml.

  3. Create a file larger than the plugin heap, such as python3 -c "open('big.txt','w').write(('x'*1023+'\n')*71680)", which writes 70 MiB. Open it with red big.txt.

  4. Run :ReadAll. The bottom line shows ! reader: dropped a GetBufferText result too large for its memory (70.0 MiB needed, 64.0 MiB free), and red.log in the config directory has a [WARN] Dropped GetBufferTextresult for pluginreader`` line. Before this change, the same request quarantined reader.

  5. Run :Ping, then open :messages. reader is still running is listed, so the plugin is still active.

  6. Symbol trimming, optional: in a TypeScript project with tens of thousands of symbols, using typescript-language-server, press Space w. The picker should fill with a results truncated: first 4096 of N; type to narrow status instead of stopping lsp_symbols. workspace_symbols_larger_than_the_plugin_heap_are_trimmed_before_delivery covers this with 60,000 symbols, and fails on main with Husk heap byte limit exceeded (requested 132867124 bytes …).

Other new tests:

  • workspace_symbols_that_barely_fit_the_plugin_heap_are_trimmed_before_delivery: 28,000 symbols, which fit the heap but quarantined lsp_symbols without the half-heap limit
  • result_larger_than_the_plugin_heap_is_dropped_without_failing_the_plugin
  • trimmed_result_keeps_the_longest_prefix_that_fits
  • available_heap_bytes_predicts_whether_an_argument_fits, in husk-runtime
  • dropped_plugin_result_is_reported_as_a_plugin_warning

The existing 20,000-symbol test (44 MB) is now trimmed before delivery and still shows first 4096 of 20000.

A request result that did not fit in the receiving plugin's 64 MiB Husk
heap failed while binding the callback argument, before any plugin code
ran, and the registry quarantined the plugin. A large TypeScript
project's empty workspace-symbol query (about 60,000 symbols, roughly
130 MB once decoded) still stopped lsp_symbols this way after #393.

The runtime now compares each result with the plugin's free heap before
calling back. Results that fit are delivered unchanged. Oversized
DocumentSymbols, WorkspaceSymbols and References results keep the
leading entries that fit in half of the free heap, leaving room for one
more binding of the list, which the runtime charges again. Any other
oversized result is dropped: the plugin stays active and Red shows a
warning attributed to it.

Successful results of those three requests now carry `total`, the entry
count before trimming, which lsp_symbols uses for its truncation status.
That field opens host API 0.20.0, recorded in the change manifest and
documented in PLUGIN_API.md; 0.19.0 packages keep loading.

Fixes #394
@fcoury-oai

Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-15T17:41:07.157445Z 2bb8e60 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2bb8e60dd5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/plugin/runtime.rs Resolved
A list that fits the heap can still leave the plugin no room to build
picker items from it, which quarantined lsp_symbols at 28,000 symbols.
@fcoury
fcoury merged commit b08fdb6 into main Oct 15, 2026
18 checks passed
@fcoury
fcoury deleted the fix/plugin-result-budget branch October 15, 2026 18:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Plugin request results have no size limit, so one oversized response can quarantine a plugin

2 participants