fix(acp): replace global approval callback with task-scoped registry#9157
Open
Kewe63 wants to merge 2 commits intoNousResearch:mainfrom
Open
fix(acp): replace global approval callback with task-scoped registry#9157Kewe63 wants to merge 2 commits intoNousResearch:mainfrom
Kewe63 wants to merge 2 commits intoNousResearch:mainfrom
Conversation
Concurrent ACP sessions shared a single process-wide `_approval_callback`
in `terminal_tool.py`. Every new `prompt()` call in `server.py` overwrote
this global with `set_approval_callback()`, so when two sessions ran in
parallel the last writer's callback would handle *all* approval dialogs —
meaning session A could silently receive session B's permission prompt.
Changes:
- Add `register_task_approval_callback(task_id, cb)`,
`unregister_task_approval_callback(task_id)`, and
`get_task_approval_callback(task_id)` to `terminal_tool.py`.
The registry is a `dict` protected by a `threading.Lock`; the global
`_approval_callback` / `set_approval_callback` are kept for CLI use.
- Update `_check_all_guards()` to accept `task_id` and look up the
per-task callback via the new getter, falling back to the global.
- Pass `task_id=effective_task_id` at the single `_check_all_guards()`
call site inside `terminal_tool()`.
- Rewrite the callback wiring in `server.py prompt()`:
- `register_task_approval_callback(session_id, approval_cb)` before
`_run_agent()` (instead of the old `set_approval_callback()`).
- `unregister_task_approval_callback(session_id)` in the `finally`
block of `_run_agent()` so cleanup is guaranteed even on exception.
- Add `tests/acp/test_approval_callback_race.py` with 14 tests covering
the registry API, concurrent isolation, AST checks on `server.py`,
and the `_check_all_guards` routing logic.
…level
The previous lazy initialisation pattern was not thread-safe:
_task_approval_lock = None
def _get_task_approval_lock():
global _task_approval_lock
if _task_approval_lock is None: # two threads can pass here simultaneously
_task_approval_lock = threading.Lock() # each creates a separate Lock object
return _task_approval_lock
Two threads racing through the None-check could each create a distinct
Lock instance and return different objects, making the mutex useless.
Fix: initialise _task_approval_lock directly at module load time so there
is exactly one Lock object from the start. Replace all _get_task_approval_lock()
call sites with direct _task_approval_lock usage. Remove the now-unnecessary
helper function entirely.
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 free
to 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.
Problem
terminal_tool.pyexposed a single process-wide_approval_callbackvariable.server.py'sprompt()method set this global viaset_approval_callback()beforerunning each agent, then restored the previous value afterward.
With up to 4 concurrent ACP sessions sharing the same
ThreadPoolExecutor, thefollowing race was possible:
_approval_callback = cb_A_approval_callback = cb_B_approval_callbackand getscb_B— session B's permission dialoghandles session A's command silently
This means one session could silently grant or deny permissions on behalf of
another, breaking the security boundary between concurrent users.
Root Cause
_approval_callbackinterminal_tool.pyis a module-level global. Any sessionthat calls
set_approval_callback()clobbers every other session's callback forthe duration of its run. The old "save and restore" pattern in
_run_agent()'sfinallyblock was not atomic — another thread could overwrite between the saveand the restore.
Fix
tools/terminal_tool.py_task_approval_callbacks: dictregistry protected by athreading.Lock.register_task_approval_callback(task_id, cb)— store a per-session callbackunregister_task_approval_callback(task_id)— remove it (called infinally)get_task_approval_callback(task_id)— return the task's callback or fall backto the global
_approval_callback(CLI sessions are unaffected)_check_all_guards(command, env_type, task_id="")to accept atask_idparameter and look up the correct callback viaget_task_approval_callback.task_id=effective_task_idat the single_check_all_guards()call siteinside
terminal_tool().acp_adapter/server.pyset_approval_callback(approval_cb)+ save/restore with:register_task_approval_callback(session_id, approval_cb)before_run_agent()unregister_task_approval_callback(session_id)inside thefinallyblock of_run_agent()— guaranteed cleanup even on exception_approval_callbackandset_approval_callback()are untouched; CLIsessions continue to work exactly as before.
Tests
Added
tests/acp/test_approval_callback_race.pywith 14 tests:TestTaskApprovalRegistryTestApprovalCallbackConcurrencyTestServerUsesTaskScopedAPIset_approval_callbacknot called;register/unregistercalled;unregisteris inside afinallyblockTestCheckAllGuardsTaskScoped_check_all_guardsroutes to task cb, falls back correctlyAll 14 tests pass.
Backwards Compatibility
set_approval_callback()/_approval_callbackunchanged — CLI and any othercaller is unaffected.