Skip to content

fix(policies): preserve generated ToolGuard code - #14378

Merged
erichare merged 2 commits into
release-1.11.2from
fix/le-2094-preserve-toolguard-code
Aug 3, 2026
Merged

fix(policies): preserve generated ToolGuard code#14378
erichare merged 2 commits into
release-1.11.2from
fix/le-2094-preserve-toolguard-code

Conversation

@erichare

@erichare erichare commented Aug 3, 2026

Copy link
Copy Markdown
Member

Summary

  • preserve ToolGuard-generated CodeInput fields when the local Step_2 directory is missing or invalid
  • retain only ToolGuard-tagged generated fields when a Breaking component update rebuilds the Policies template
  • regenerate the component index and add regression coverage for both loss paths

Root cause

Canvas refresh treated an unavailable scratch directory as an authoritative empty directory and removed every generated field from the posted build config. Separately, component updates rebuilt Policies from its static template and copied values only for fields already present in that new schema, so dynamic guard fields disappeared before autosave.

Impact

Opening a Policies flow without its local scratch directory no longer deletes the guard code stored in the flow. Accepting a component update also retains the complete generated field dictionaries, without resurrecting unrelated retired inputs or requiring the optional ToolGuard runtime during template replacement.

When a real Step_2 directory exists, the existing synchronization behavior remains unchanged: files on disk refresh/add fields and absent files are pruned as stale.

Validation

  • uv run pytest src/backend/tests/unit/components/models_and_agents/policies/test_guard_sync_utils.py src/backend/tests/unit/components/models_and_agents/policies/test_policies_component.py -q — 25 passed
  • targeted ruff check and ruff format
  • LFX_DEV=1 uv run python scripts/build_component_index.py
  • dynamic Policies component recompilation from generated source
  • full pre-commit hook suite

Addresses LE-2094 (BUG-01 and BUG-02).

Related to #14050.

Summary by CodeRabbit

  • Bug Fixes

    • Preserved generated guard-code fields when refreshing policy nodes.
    • Prevented generated fields from being removed when the code directory is missing or invalid.
    • Ensured obsolete and user-defined dynamic fields are cleaned up while existing generated fields remain intact.
  • Tests

    • Added coverage for guard-code synchronization, ignored files, missing directories, and frontend-node updates.

@coderabbitai

coderabbitai Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Important

Review skipped

Auto incremental reviews are disabled on this repository.

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: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 408c7bd5-9398-4b00-815d-9d187cb35e7d

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

Walkthrough

The PR preserves generated ToolGuard fields when generated-code sources are unavailable or when Policies frontend nodes are refreshed. It adds synchronization and frontend-node regression tests. Generated component metadata and its checksum are updated.

Changes

Generated guard preservation

Layer / File(s) Summary
Guard field synchronization
src/lfx/src/lfx/components/models_and_agents/policies/guard_sync_utils.py, src/backend/tests/unit/components/models_and_agents/policies/test_guard_sync_utils.py
Synchronization preserves generated fields when the Step 2 path is missing or is not a directory. Valid Step 2 files replace stale generated fields while unrelated configuration remains unchanged.
Frontend node guard preservation
src/lfx/src/lfx/components/models_and_agents/policies_component.py, src/lfx/src/lfx/_assets/component_index.json, src/backend/tests/unit/components/models_and_agents/policies/test_policies_component.py
update_frontend_node restores generated ToolGuard fields after template updates. Tests cover preserved, removed, default, and non-generated dynamic fields. The component index and checksum are regenerated.

Estimated code review effort: 3 (Moderate) | ~20 minutes

🚥 Pre-merge checks | ✅ 8 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 53.85% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (8 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: preserving generated ToolGuard code in the Policies component.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Test Coverage For New Implementations ✅ Passed Adds 3 regression tests for Step_2 synchronization and 1 frontend-node preservation test; both are substantive backend test_*.py files with direct assertions.
Test Quality And Coverage ✅ Passed Tests cover missing and invalid Step_2 preservation, valid-directory refresh/add/prune/filter behavior, and async component updates that preserve only ToolGuard fields.
Test File Naming And Structure ✅ Passed Both added files use test_*.py under backend unit tests, with pytest fixtures/async markers, descriptive names, and positive plus negative edge-case coverage; no frontend or integration tests were...
Excessive Mock Usage Warning ✅ Passed The added sync tests use real tmp_path files and no mocks. The component test uses one patch only to verify ToolGuard is not imported; core update behavior remains real.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/le-2094-preserve-toolguard-code

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.

@github-actions github-actions Bot added the bug Something isn't working label Aug 3, 2026
@erichare
erichare marked this pull request as ready for review August 3, 2026 17:24
@github-actions

github-actions Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

✅ Test Coverage Advisor

No source changes detected without accompanying tests. Thanks for keeping coverage up! 🎉

Advisory check only — never blocks merge.

@github-actions github-actions Bot added bug Something isn't working and removed bug Something isn't working labels Aug 3, 2026

@coderabbitai coderabbitai 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.

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/backend/tests/unit/components/models_and_agents/policies/test_policies_component.py (1)

74-74: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Remove the redundant async marker.

pytest-asyncio already auto-detects this async test with asyncio_mode = "auto" set in pyproject.toml.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@src/backend/tests/unit/components/models_and_agents/policies/test_policies_component.py`
at line 74, Remove the redundant `@pytest.mark.asyncio` decorator from the async
test in the policies component test module, relying on the existing asyncio_mode
= "auto" configuration for detection.

Source: Learnings

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@src/backend/tests/unit/components/models_and_agents/policies/test_policies_component.py`:
- Around line 75-113: Update
test_component_update_preserves_only_generated_guard_fields to use
ComponentTestBaseWithoutClient instead of directly relying on the mock_component
fixture. Add the required component_class, default_kwargs, and
file_names_mapping fixtures for PoliciesComponent, preserving the existing
assertions and ImportError behavior.

---

Nitpick comments:
In
`@src/backend/tests/unit/components/models_and_agents/policies/test_policies_component.py`:
- Line 74: Remove the redundant `@pytest.mark.asyncio` decorator from the async
test in the policies component test module, relying on the existing asyncio_mode
= "auto" configuration for detection.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: b41f3c9c-e122-4d4a-bf69-987be99674b0

📥 Commits

Reviewing files that changed from the base of the PR and between 0f34c27 and c694de5.

📒 Files selected for processing (5)
  • src/backend/tests/unit/components/models_and_agents/policies/test_guard_sync_utils.py
  • src/backend/tests/unit/components/models_and_agents/policies/test_policies_component.py
  • src/lfx/src/lfx/_assets/component_index.json
  • src/lfx/src/lfx/components/models_and_agents/policies/guard_sync_utils.py
  • src/lfx/src/lfx/components/models_and_agents/policies_component.py

@github-actions github-actions Bot added bug Something isn't working and removed bug Something isn't working labels Aug 3, 2026
@erichare
erichare merged commit 94025ba into release-1.11.2 Aug 3, 2026
93 of 160 checks passed
@erichare
erichare deleted the fix/le-2094-preserve-toolguard-code branch August 3, 2026 17:56
@codecov

codecov Bot commented Aug 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 58.42%. Comparing base (d3d7007) to head (2856d9a).
⚠️ Report is 4 commits behind head on release-1.11.2.

Additional details and impacted files

Impacted file tree graph

@@                Coverage Diff                 @@
##           release-1.11.2   #14378      +/-   ##
==================================================
- Coverage           61.44%   58.42%   -3.03%     
==================================================
  Files                2452     2391      -61     
  Lines              239862   237872    -1990     
  Branches            36330    35594     -736     
==================================================
- Hits               147395   138986    -8409     
- Misses              90661    97080    +6419     
  Partials             1806     1806              
Flag Coverage Δ
backend 67.20% <ø> (+0.03%) ⬆️
frontend 55.38% <ø> (-4.71%) ⬇️
lfx 60.75% <ø> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.
see 715 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions

github-actions Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Frontend Unit Test Coverage Report

Coverage Summary

Lines Statements Branches Functions
Coverage: 47%
47.33% (67565/142734) 70.29% (9507/13525) 45.8% (1555/3395)

Unit Test Results

Tests Skipped Failures Errors Time
5401 0 💤 0 ❌ 0 🔥 21m 7s ⏱️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant