feat(guardrails): add before_final_action hook for workflow and chat final commits#534
Open
nitinawari wants to merge 1 commit into
Open
Conversation
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.
Summary
Adds the third guardrail hook point for FinBot Labs —
before_final_action— so defenders can inspect and score agent outcomes before they are committed. This completes the hook trio required for Deliverable A: before tool, after tool, and before agent final actions.The hook fires when:
Guardrails remain passive: a block verdict is logged and scorable via GuardrailPreventionDetector, but does not stop execution.What changed
Guardrail core
finbot/guardrails/schemas.py— AddedHookKind.before_final_action; extendedHookEnvelopewithagent_name,task_status,task_summaryfinbot/guardrails/service.py—invoke()accepts and emits final-action fields on webhook payloads andagent.guardrail.*eventsAgent integration
finbot/agents/base.py_invoke_before_final_action_guardrail()helper forcomplete_taskcomplete_task→before_final_action(notbefore_tool)after_toolfires oncomplete_taskbeforelog_task_completioncomplete_taskfinbot/agents/chat.py_invoke_before_final_action_guardrail()for final chat replies (tool_name: chat_response)_save_message("assistant", ...)Labs configuration & UI
finbot/core/data/models.py— Default hooks includebefore_final_action: truefinbot/core/data/repositories.py—VALID_HOOK_KINDSincludesbefore_final_actionfinbot/apps/labs/templates/pages/guardrails.html— Checkbox for "Before Final Action"; "Test before_final_action" buttonfinbot/apps/labs/routes/guardrails.py—POST /api/v1/guardrails/test/before-final-actionCTF detection
finbot/ctf/detectors/implementations/guardrail_prevention.pyrequired_hook_kind: before_final_actionrequired_task_statusfilteragent_name,task_status,task_summaryTests
tests/unit/labs/test_guardrail_final_action.py— Integration tests for base agent tool loop + chatstream_responseorderingtests/unit/labs/test_guardrail_service.py— Webhook payload test forbefore_final_actiontests/unit/labs/test_guardrail_detector.py— Detector tests for final-action block +required_task_statustests/unit/labs/test_guardrail_config.py— Default hooks assertion updatedTest plan
uv run pytest tests/unit/labs/test_guardrail_final_action.py -v(7 tests)uv run pytest tests/unit/labs/ -vcomplete_task→ Guardrail Activity showshook_kind: before_final_actionbefore_final_actionwithtool_name: chat_responsebefore_tool) still work unchangedNotes
blockverdict) is out of scope for this PR; discuss with mentor separatelyhooks_jsonwithoutbefore_final_actionwill have the hook disabled until users re-save config in Labs UIGSoC mapping
Week 1-2 (phase 1)