LLMAgent's ephemeral ToolAgent dispatch derives both D-9 global-safety conditions from one shared signal, bypassing the AND composition (ADR-2034 D-9) #195

Closed
opened 2026-11-26 11:52:50 +00:00 by CoreRasurae · 0 comments
Member

Metadata

  • Commit Message: fix(agents): derive the ephemeral ToolAgent's own safe_mode and the host's _unsafe_mode independently in LLMAgent tool dispatch
  • Branch: bugfix/m1-llm-tool-dispatch-shared-unsafe-signal

Background and context

PR #193 (issue #189) corrected ToolAgent._is_global_unsafe to require, at once, both (a) the invocation context carrying _unsafe_mode: true and (b) the ToolAgent's own safe_mode field being false (ADR-2034 D-9, docs/index.md §4.4.8: "rather than treating the agent's own config field and host mode as interchangeable"). That fix is correct for a directly constructed ToolAgent -- the only construction path the Behave suite exercises.

However, the dominant real-world path for invoking a skill is a type: llm agent's tool-call loop, which constructs an ephemeral ToolAgent per call via LLMAgent._build_ephemeral_tool_config/_build_tool_context (src/cleveractors/agents/llm.py). Both of those builders are fed from a single boolean, parent_unsafe, returned by LLMAgent._resolve_parent_unsafe:

def _resolve_parent_unsafe(self, context):
    return (self.config.get("safe_mode", True) is False) or bool(
        context and context.get("_unsafe_mode")
    )

_build_ephemeral_tool_config(tool_name, parent_unsafe) sets the ephemeral agent's own safe_mode = not parent_unsafe, and _build_tool_context(parent_unsafe) sets the ephemeral context's _unsafe_mode = parent_unsafe -- the same parent_unsafe value feeds both. This pattern repeats at all three tool-dispatch call sites in _execute_tool_loop (agents/llm.py ~lines 1221-1230, ~1376-1395, ~1544-1553).

This was behavior-neutral before PR #193, when _is_global_unsafe was itself an OR: OR(parent_unsafe, parent_unsafe) == parent_unsafe == OR(own_safe_mode, host_unsafe), so collapsing the two legs into one shared signal reproduced the intended OR exactly. After PR #193 changed _is_global_unsafe to AND, the same collapse now produces AND(parent_unsafe, parent_unsafe) == parent_unsafe == OR(own_safe_mode, host_unsafe) -- i.e. the ephemeral-ToolAgent path still evaluates to the pre-fix OR semantics regardless of the corrected AND composition, because both of the AND's operands are forced identical before _is_global_unsafe ever sees them.

Raised and scoped out (correctly, as pre-existing and outside #189's atomic fix) in reviewer hurui200320's PR #193 reviews; this issue tracks the fix per that review's "Scope notes"/"Scope exclusions." Proven by the companion TDD issue #194.

Current behavior

An LLM agent with skills: [...] and its own safe_mode: false -- with no host ever placed in unsafe mode, and no actor context.global.unsafe declaration in effect -- can still execute a safe_mode: false skill's bundled scripts/ resource or read its non-standard-folder resources through the real tool-call loop. _resolve_parent_unsafe returns True from the agent's own safe_mode: false alone, and that single True is handed to both builders, so the ephemeral ToolAgent ends up with safe_mode: false and _unsafe_mode: true in its context simultaneously -- satisfying _is_global_unsafe's AND even though only one real authority (the agent author) ever opted in. This reproduces, through the real production dispatch path, the exact single-condition vulnerability issue #189 fixed for direct ToolAgent construction.

Expected behavior

The ephemeral ToolAgent's own safe_mode must be derived solely from this LLMAgent's own safe_mode config field, and the ephemeral context's _unsafe_mode must be derived solely from whether the invocation context already carries a genuine host-propagated _unsafe_mode: true (§9.4/§10.3) -- the two conditions resolved independently, exactly as docs/index.md §4.4.8 requires ("rather than treating the agent's own config field and host mode as interchangeable"). An LLM agent with only its own safe_mode: false set, on a host never placed in unsafe mode, must have privileged skill operations refused -- matching the behavior a direct ToolAgent construction already exhibits post-#189. An LLM agent on a genuinely unsafe host, with its own safe_mode still true (default), must likewise have privileged skill operations refused. Only when both are independently true does the gate open -- matching how a real host-level _unsafe_mode signal (propagated via context.global.unsafe, §9.4/§10.3) already reaches a plain type: tool agent unmodified.

Acceptance criteria

  • LLMAgent's tool-call dispatch resolves two independent booleans instead of one shared parent_unsafe: the agent's own unsafe opt-out (from self.config.get("safe_mode", True) is False) and the host-propagated unsafe signal (from context.get("_unsafe_mode"), forwarded verbatim, never OR'd with the agent's own config).
  • _build_ephemeral_tool_config sets the ephemeral ToolAgent's safe_mode from the agent's own unsafe opt-out only.
  • _build_tool_context sets the ephemeral context's _unsafe_mode from the host-propagated signal only.
  • All three _execute_tool_loop dispatch sites (budget-retry, primary, stuck-model-retry mirrors) are updated consistently.
  • The companion TDD scenario from issue #194 now passes without @tdd_expected_fail (tag removed).
  • Every existing Behave scenario exercising the LLM-agent tool-call/skill-dispatch path that previously relied on the collapsed signal (e.g. a host-unsafe actor automatically granting itself own-safe_mode: false, or vice versa) is reviewed and updated to configure both conditions explicitly where a genuinely open gate is intended.
  • No regression to ToolAgent._is_global_unsafe itself or to direct ToolAgent construction (already correct per #189).

Supporting information

  • Spec: docs/index.md §4.4.8 ("The double-safety gate", "Global safety").
  • docs/adr/ADR-2034-skill-package-support.md (D-9, corrected 2026-11-25).
  • Code: src/cleveractors/agents/llm.py (_resolve_parent_unsafe, _build_ephemeral_tool_config, _build_tool_context, and their three call sites in _execute_tool_loop).
  • Prior history: issue #115/#116 (introduced the single shared parent_unsafe signal, correct under the then-OR'd D-9; not revisited when D-9 was corrected to AND by issue #189/PR #193).
  • Raised during PR #193 review by hurui200320, explicitly scoped out of that PR as a separate, pre-existing architectural question.

Subtasks

  • Replace _resolve_parent_unsafe with two independently-resolved signals (or two focused helper methods) for the agent's own unsafe opt-out and the host-propagated unsafe signal.
  • Update _build_ephemeral_tool_config and _build_tool_context call sites (and their docstrings) to each take only the signal they actually need.
  • Update all three _execute_tool_loop dispatch sites to resolve and thread both signals independently.
  • Remove @tdd_expected_fail from the issue #194 regression scenario now that it passes for the right reason.
  • Audit and update existing features/llm_agent_tool_loop.feature/agent-skill-dispatch scenarios whose current assertions depend on the collapsed signal.
  • Tests (Behave): scenarios proving the two conditions are independent on the real LLM-agent tool-dispatch path (own-safe_mode-only refused, host-unsafe-only refused, both-true opens the gate), mirroring the #189 regression shape.
  • Verify coverage >=97% via nox -s coverage_report.
  • Run nox (all default sessions), fix any errors.

Definition of Done

This issue is complete when:

  • All subtasks above are completed and checked off.
  • A Git commit is created where the first line matches the Commit Message in Metadata exactly.
  • The commit is pushed to the branch matching the Branch in Metadata exactly.
  • The commit is submitted as a PR to develop, reviewed, and merged.
## Metadata - **Commit Message:** fix(agents): derive the ephemeral ToolAgent's own safe_mode and the host's _unsafe_mode independently in LLMAgent tool dispatch - **Branch:** bugfix/m1-llm-tool-dispatch-shared-unsafe-signal ## Background and context PR #193 (issue #189) corrected `ToolAgent._is_global_unsafe` to require, at once, both (a) the invocation context carrying `_unsafe_mode: true` and (b) the `ToolAgent`'s own `safe_mode` field being `false` (ADR-2034 D-9, `docs/index.md` §4.4.8: "rather than treating the agent's own config field and host mode as interchangeable"). That fix is correct for a directly constructed `ToolAgent` -- the only construction path the Behave suite exercises. However, the dominant real-world path for invoking a skill is a `type: llm` agent's tool-call loop, which constructs an *ephemeral* `ToolAgent` per call via `LLMAgent._build_ephemeral_tool_config`/`_build_tool_context` (`src/cleveractors/agents/llm.py`). Both of those builders are fed from a single boolean, `parent_unsafe`, returned by `LLMAgent._resolve_parent_unsafe`: ```python def _resolve_parent_unsafe(self, context): return (self.config.get("safe_mode", True) is False) or bool( context and context.get("_unsafe_mode") ) ``` `_build_ephemeral_tool_config(tool_name, parent_unsafe)` sets the ephemeral agent's own `safe_mode = not parent_unsafe`, and `_build_tool_context(parent_unsafe)` sets the ephemeral context's `_unsafe_mode = parent_unsafe` -- the *same* `parent_unsafe` value feeds both. This pattern repeats at all three tool-dispatch call sites in `_execute_tool_loop` (`agents/llm.py` ~lines 1221-1230, ~1376-1395, ~1544-1553). This was behavior-neutral before PR #193, when `_is_global_unsafe` was itself an OR: `OR(parent_unsafe, parent_unsafe) == parent_unsafe == OR(own_safe_mode, host_unsafe)`, so collapsing the two legs into one shared signal reproduced the intended OR exactly. After PR #193 changed `_is_global_unsafe` to AND, the same collapse now produces `AND(parent_unsafe, parent_unsafe) == parent_unsafe == OR(own_safe_mode, host_unsafe)` -- i.e. the ephemeral-ToolAgent path still evaluates to the pre-fix OR semantics regardless of the corrected AND composition, because both of the AND's operands are forced identical before `_is_global_unsafe` ever sees them. Raised and scoped out (correctly, as pre-existing and outside #189's atomic fix) in reviewer hurui200320's PR #193 reviews; this issue tracks the fix per that review's "Scope notes"/"Scope exclusions." Proven by the companion TDD issue #194. ## Current behavior An LLM agent with `skills: [...]` and its own `safe_mode: false` -- with **no** host ever placed in unsafe mode, and no actor `context.global.unsafe` declaration in effect -- can still execute a `safe_mode: false` skill's bundled `scripts/` resource or read its non-standard-folder resources through the real tool-call loop. `_resolve_parent_unsafe` returns `True` from the agent's own `safe_mode: false` alone, and that single `True` is handed to *both* builders, so the ephemeral `ToolAgent` ends up with `safe_mode: false` **and** `_unsafe_mode: true` in its context simultaneously -- satisfying `_is_global_unsafe`'s AND even though only one real authority (the agent author) ever opted in. This reproduces, through the real production dispatch path, the exact single-condition vulnerability issue #189 fixed for direct `ToolAgent` construction. ## Expected behavior The ephemeral `ToolAgent`'s own `safe_mode` must be derived solely from this `LLMAgent`'s own `safe_mode` config field, and the ephemeral context's `_unsafe_mode` must be derived solely from whether the *invocation* context already carries a genuine host-propagated `_unsafe_mode: true` (§9.4/§10.3) -- the two conditions resolved independently, exactly as `docs/index.md` §4.4.8 requires ("rather than treating the agent's own config field and host mode as interchangeable"). An LLM agent with only its own `safe_mode: false` set, on a host never placed in unsafe mode, must have privileged skill operations refused -- matching the behavior a direct `ToolAgent` construction already exhibits post-#189. An LLM agent on a genuinely unsafe host, with its own `safe_mode` still `true` (default), must likewise have privileged skill operations refused. Only when both are independently true does the gate open -- matching how a real host-level `_unsafe_mode` signal (propagated via `context.global.unsafe`, §9.4/§10.3) already reaches a plain `type: tool` agent unmodified. ## Acceptance criteria - [ ] `LLMAgent`'s tool-call dispatch resolves two independent booleans instead of one shared `parent_unsafe`: the agent's own unsafe opt-out (from `self.config.get("safe_mode", True) is False`) and the host-propagated unsafe signal (from `context.get("_unsafe_mode")`, forwarded verbatim, never OR'd with the agent's own config). - [ ] `_build_ephemeral_tool_config` sets the ephemeral `ToolAgent`'s `safe_mode` from the agent's own unsafe opt-out only. - [ ] `_build_tool_context` sets the ephemeral context's `_unsafe_mode` from the host-propagated signal only. - [ ] All three `_execute_tool_loop` dispatch sites (budget-retry, primary, stuck-model-retry mirrors) are updated consistently. - [ ] The companion TDD scenario from issue #194 now passes without `@tdd_expected_fail` (tag removed). - [ ] Every existing Behave scenario exercising the LLM-agent tool-call/skill-dispatch path that previously relied on the collapsed signal (e.g. a host-unsafe actor automatically granting itself own-`safe_mode: false`, or vice versa) is reviewed and updated to configure both conditions explicitly where a genuinely open gate is intended. - [ ] No regression to `ToolAgent._is_global_unsafe` itself or to direct `ToolAgent` construction (already correct per #189). ## Supporting information - Spec: `docs/index.md` §4.4.8 ("The double-safety gate", "Global safety"). - `docs/adr/ADR-2034-skill-package-support.md` (D-9, corrected 2026-11-25). - Code: `src/cleveractors/agents/llm.py` (`_resolve_parent_unsafe`, `_build_ephemeral_tool_config`, `_build_tool_context`, and their three call sites in `_execute_tool_loop`). - Prior history: issue #115/#116 (introduced the single shared `parent_unsafe` signal, correct under the then-OR'd D-9; not revisited when D-9 was corrected to AND by issue #189/PR #193). - Raised during PR #193 review by hurui200320, explicitly scoped out of that PR as a separate, pre-existing architectural question. ## Subtasks - [ ] Replace `_resolve_parent_unsafe` with two independently-resolved signals (or two focused helper methods) for the agent's own unsafe opt-out and the host-propagated unsafe signal. - [ ] Update `_build_ephemeral_tool_config` and `_build_tool_context` call sites (and their docstrings) to each take only the signal they actually need. - [ ] Update all three `_execute_tool_loop` dispatch sites to resolve and thread both signals independently. - [ ] Remove `@tdd_expected_fail` from the issue #194 regression scenario now that it passes for the right reason. - [ ] Audit and update existing `features/llm_agent_tool_loop.feature`/agent-skill-dispatch scenarios whose current assertions depend on the collapsed signal. - [ ] Tests (Behave): scenarios proving the two conditions are independent on the real LLM-agent tool-dispatch path (own-safe_mode-only refused, host-unsafe-only refused, both-true opens the gate), mirroring the #189 regression shape. - [ ] Verify coverage >=97% via `nox -s coverage_report`. - [ ] Run `nox` (all default sessions), fix any errors. ## Definition of Done This issue is complete when: - All subtasks above are completed and checked off. - A Git commit is created where the first line matches the Commit Message in Metadata exactly. - The commit is pushed to the branch matching the Branch in Metadata exactly. - The commit is submitted as a PR to develop, reviewed, and merged.
CoreRasurae added this to the v2.1.0 milestone 2026-11-26 11:52:50 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
cleveragents/cleveractors-core#195
No description provided.