Repository navigation
fix(kuna): share and bound single-function decompile runs - #4045
Conversation
While the project export builds, every pseudocode request started its own `kuna decompile --addr` process, and each one analyzes the whole image again. A cold batch_decompile therefore launched one process per address, including duplicates, with no bound. Requests for one entry now share one run, which stops when its last waiter is cancelled; that waiter still receives the run's outcome, including unproven cleanup. A provider-wide pool admits at most the host's cores beyond REA_KUNA_JOBS (at least one) at once, and a queued request is answered from the export when it finishes first. Refs #1957 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
[Medium impact] Adds resource pooling for concurrent decompile operations. The PR needs a fix before merging because retained Kuna processes can escape the new concurrency limit. Findings
Fix with agent prompt### Issue 1
src/kuna/KunaSessionClient.ts:806-807
`#singleFunction` releases its slot even when `runKunaCommand` returns `retained`, meaning the process tree may still be running. With one slot, a timeout or cancellation whose cleanup fails immediately admits the next queued run while the old tree remains alive. Repeated failures bypass the new process limit.
Keep the slot with the retained cleanup owner and release it only after cleanup proves the tree stopped.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.SummaryThis PR shares in-flight Kuna decompiles by function address and adds a provider-owned FIFO pool.
Security review found no introduced security issue. Supersession status could not be checked because GitHub access was unauthenticated. No tests were executed during this review. Diagram%%{init: {'theme': 'neutral'}}%%
flowchart TD
Request[Request pseudocode] --> Shared{Run for this address?}
Shared -->|Yes| Wait[Join shared wait]
Shared -->|No| Queue[Wait for slot or export]
Queue -->|Export finishes first| Export[Read project export]
Queue -->|Slot granted| Run[Start Kuna]
Run --> Outcome{Cleanup proven?}
Outcome -->|Yes| Release[Release slot]
Outcome -->|No| Retained[Retain cleanup owner]
Retained --> Early[Slot released too early]
Early --> Next[Next run starts]
Reviews (1) · Last reviewed commit: "fix(kuna): share and bound single-functi..." · Reviewed by Greptile |
| } finally { | ||
| admitted(); |
There was a problem hiding this comment.
Live processes escape the limit
#singleFunction releases its slot even when runKunaCommand returns retained, meaning the process tree may still be running. With one slot, a timeout or cancellation whose cleanup fails immediately admits the next queued run while the old tree remains alive. Repeated failures bypass the new process limit.
Keep the slot with the retained cleanup owner and release it only after cleanup proves the tree stopped.
Knowledge Base Used:
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/kuna/KunaSessionClient.ts
Line: 806-807
Comment:
**Live processes escape the limit**
`#singleFunction` releases its slot even when `runKunaCommand` returns `retained`, meaning the process tree may still be running. With one slot, a timeout or cancellation whose cleanup fails immediately admits the next queued run while the old tree remains alive. Repeated failures bypass the new process limit.
Keep the slot with the retained cleanup owner and release it only after cleanup proves the tree stopped.
**Knowledge Base Used:**
- [Debugger and disassembler integrations](https://app.greptile.com/pre/-/custom-context/knowledge-base/morluto/rea/-/docs/debugger-and-disassembler-integrations.md)
- [Reverse-engineering toolchains](https://app.greptile.com/pre/-/custom-context/knowledge-base/morluto/rea/-/docs/reverse-engineering-toolchains.md)
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.There was a problem hiding this comment.
Valid. #4045 merged before this was addressed, so the fix is in #4052.
Root cause: #singleFunction released its slot in finally whatever the outcome, so slot ownership ended before process ownership did. Structural fix: a retained outcome hands its slot to the retained entry next to its cleanup owner. close() releases it only when the retried stop() is proven; every other outcome releases as before. Validation: a new test with one slot and an unproven stop launched a second kuna decompile on the base revision (2 launches). With the fix it launches none until close proves the stop, and then a new session gets the slot. The Kuna suite (94), check:fast, docs:check and the real verify:kuna lane pass. Remaining: an unproven stop cannot be forced with a real Kuna, so this path is covered with the fake only.
Summary
While a Kuna project export builds, each pseudocode request started its own
kuna decompile --addrprocess, and every one of them analyzes the whole image again. A coldbatch_decompiletherefore started one process per address, duplicates included, with no bound. Refs #1957.Problem and expected behavior
Smallest trigger: cold cache,
open_binarya thin Mach-O withprovider_id: "kuna", then callbatch_decompilewith the same address 12 times. On main this starts 12 concurrentkuna decompiletrees (decomp_dbgchildren at about 360 MiB each). They compete with the background export that would answer all of them. The invariant that breaks: an analysis request should not start an unbounded number of whole-image analyses on the host.Expected: requests for one function share a single run. Concurrent runs are bounded by host capacity. A request still waiting for a slot is answered by the export if the export finishes first.
Change and scope
KunaSessionClientkeeps one shared single-function run per entry address. Each caller's cancellation only ends that caller's wait. The run stops when its last waiter is cancelled, and that waiter gets the run's real outcome, including unproven cleanup (this keepsclose_binary's retained-cleanup reporting intact).KunaRunSlots: one FIFO admission pool perKunaProvider(so per REA process), sizedmax(1, availableParallelism() - REA_KUNA_JOBS). Single-function runs only happen while the export's--jobsworkers are running, so the cores those workers leave free are the real resource limit. Other providers already serialize requests through one engine session; Kuna is the only adapter that starts a process per request, so the bound lives in the Kuna adapter.docs/kuna-provider.mddocuments the sharing, the bound and the hand-off to the export.Not included: no new setting (the bound follows the host and
REA_KUNA_JOBS); single-function results are not memoized after they finish;batch_decompileitself (all providers) still accepts duplicate addresses and returns each one.Scope: an ordinary fix within the existing Kuna provider boundary; no maintainer scope decision applies.
Contract and boundary impact
src/kuna/KunaSessionClient.ts, newsrc/kuna/KunaRunSlots.ts,KunaProvidercomposition).raw_result.sourceshapes.project-exportwith its file span, as any export answer does.close_binary.Expected effects:
(Process launches are the existing
kunaruns; this change starts fewer of them.)Evidence and regression coverage
Tests added or updated:
tests/boundary/providers/kuna/kunaSingleFunctionRuns.test.ts:The fake Kuna now records start times, records exits for
decompile, and supportsdecompileDelayMs.Base reproduction: all 4 new tests fail on the base revision (
git stashof thesrc/kunachanges): 3 assertion failures and one 30 s timeout for the export hand-off.Real Kuna 1.833, macOS arm64 (10 cores), MCP
batch_decompileon a cold cache, targetzipthinned to arm64e (203 KiB, 339 functions). Same script before and after. Kuna family RSS was sampled every 250 ms, so short peaks can be missed:decomp_dbg0x1000012780x100001278Each row is one cold observation on a shared host, not a benchmark. For distinct small functions on this 10-core host the bound (9) barely changes anything. The bound matters on hosts with fewer cores and for larger batches. Response bytes are unchanged (2185399 and 568864); model tokens were not measured.
Real MCP cancellation: three of four concurrent requests for one heavy function were cancelled mid-run. The fourth still got its
single-functionanswer.close_binaryreturnednull, nokunaprocesses were left, andstaging/andtrash/were empty.Remaining proof gaps: no Linux or x86-64 host run of this change; the queued-request hand-off to the export was exercised only with the fake Kuna (one real slot cannot be forced without a setting); multi-session sharing of the pool inside one process is source-derived only.
Observed, derived, and inferred claims remain distinguishable.
Artifact identity, source provenance, and failed attempts remain preserved.
Unsupported, incomplete, unavailable, or uncertain outcomes remain visible.
Validation performed
npm run test:focused -- tests/boundary/providers/kuna/*.test.ts: 93 passed (11 files), three consecutive runs.npm run check:fast: typecheck and lint passed.npm run docs:check: passed.REA_KUNA_COMMAND=… npm run verify:kuna(real Kuna 1.833, darwin/arm64): passed, process lineage verified,cleanup: "complete".Compatibility, safety, and release
runKunaCommandandclose_binary.Author readiness
Human owner:
Before merge
type(scope): outcome.🤖 Generated with Claude Code