Skip to content

fix(agents): persist pending_action for gated tool calls#337

Merged
w7-mgfcode merged 1 commit into
devfrom
fix/agents-persist-pending-action
May 31, 2026
Merged

fix(agents): persist pending_action for gated tool calls#337
w7-mgfcode merged 1 commit into
devfrom
fix/agents-persist-pending-action

Conversation

@w7-mgfcode

Copy link
Copy Markdown
Owner

Root cause

The experiment agent can call an approval-gated mutation tool (save_scenario, create_alias, archive_run). Those tools returned {"status":"approval_required", ...} to the model, but never wrote a machine-readable pending_action to session/deps state. service.chat() / stream_chat() decide approval by inspecting final_result.pending_action / final_result.approval_required — fields that the agent's structured output (ExperimentReport) does not define. So a gated call left the session active, persisted no pending_action, and emitted no approval_required event — the Chat UI received only the assistant's prose and showed no Approve/Reject card. (Investigation: session e4fe2f76… logged tool_save_scenario requires_approval=true, yet agent_session.pending_action stayed null and status stayed active.)

Fix

Deterministic, deps-based propagation (does not depend on the model echoing the request into its output):

  • AgentDeps gains a pending_action: dict | None slot + set_pending_action(action_type, arguments, description).
  • The three gated tools call ctx.deps.set_pending_action(...) when approval is required, before returning the approval dict. The save_scenario arguments are exactly what _execute_pending_action replays on approval.
  • service.chat() and service.stream_chat() check deps.pending_action first (new shared _record_pending_action helper), persisting session.pending_action, flipping the session to awaiting_approval, and emitting the approval_required event. The legacy final_result.pending_action / approval_required checks remain as fallbacks.
  • ExperimentReport is intentionally left unchanged (the model-dependent alternative was the fragile path).

Tests

Three new regression tests in app/features/agents/tests/test_service.py:

  • test_set_pending_action_records_requestAgentDeps.set_pending_action records the request.
  • test_chat_persists_pending_action_from_depschat() persists pending_action + sets awaiting_approval when the output has no approval field.
  • test_stream_chat_emits_approval_required_from_depsstream_chat() emits the approval_required event from deps.pending_action.

Covers the full chain: gated tool → deps.pending_actionawaiting_approvalapproval_required.

Validation

  • ruff check
  • ruff format --check
  • mypy app/ ✅ — only pre-existing xgboost optional-dep import-not-found errors in untouched files (forecasting/, registry/)
  • pyright app/features/agents/ ✅ 0 errors
  • pytest -m "not integration" ✅ 1650 passed, 12 skipped (agents slice 139, scenarios 53)

Notes

Closes #336

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sorry @w7-mgfcode, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@coderabbitai

coderabbitai Bot commented May 31, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: b9b1b621-9be2-4acf-9180-3a17742c33b8

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/agents-persist-pending-action

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@w7-mgfcode w7-mgfcode merged commit e896fc6 into dev May 31, 2026
8 checks passed
@w7-mgfcode w7-mgfcode deleted the fix/agents-persist-pending-action branch May 31, 2026 22:56
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.

1 participant