Skip to content

Feat/kaizen policies - #68

Merged
visahak merged 7 commits into
AgentToolkit:mainfrom
visahak:feat/kaizen-policies
Feb 25, 2026
Merged

Feat/kaizen policies#68
visahak merged 7 commits into
AgentToolkit:mainfrom
visahak:feat/kaizen-policies

Conversation

@visahak

@visahak visahak commented Feb 23, 2026

Copy link
Copy Markdown
Collaborator

Summary by CodeRabbit

  • New Features

    • Added policy support with configurable types, triggers, priorities, and enablement.
    • Added an entity retrieval tool to fetch and filter entities by type; guideline retrieval remains as a backward-compatible alias.
  • Documentation

    • Added policy documentation and updated README to describe entity retrieval, policy usage, and backward-compatibility behavior.
  • Bug Fixes

    • Namespace entity counts now stay in sync after updates.
  • Tests

    • Added unit tests covering policy schema serialization, deserialization, and defaults.

@coderabbitai

coderabbitai Bot commented Feb 23, 2026

Copy link
Copy Markdown

Warning

Rate limit exceeded

@visahak has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 23 minutes and 17 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 4f34b0a and a9f0de6.

📒 Files selected for processing (2)
  • README.md
  • kaizen/frontend/mcp/mcp_server.py
📝 Walkthrough

Walkthrough

Adds a generic MCP tool get_entities() (with backward-compatible get_guidelines()), introduces a Policy Pydantic schema and tests, synchronizes namespace entity counts in filesystem persistence, and adds documentation (POLICIES.md) describing policy usage.

Changes

Cohort / File(s) Summary
MCP tools & server
kaizen/frontend/mcp/mcp_server.py
Added public get_entities(task, entity_type="guideline") and internal get_entities_logic(...); get_guidelines() now delegates to the new logic; response header and type filtering made dynamic to support multiple entity types (e.g., guidelines, policies).
Policy schema & tests
kaizen/schema/policy.py, tests/unit/test_policy_schema.py
New Pydantic models/enums: Policy, PolicyTrigger, PolicyType, TriggerType; unit tests for serialization, deserialization, and trigger defaults.
Backend persistence sync
kaizen/backend/filesystem.py
Update namespace metadata: set data.num_entities = len(data.entities) after entity updates and deletions, then persist.
Documentation
AGENTS.md, README.md, docs/POLICIES.md
Updated public interface docs to include get_entities() and aliasing get_guidelines(); added docs/POLICIES.md describing policy schema, types, triggers, examples, and usage.

Sequence Diagram(s)

sequenceDiagram
    participant Client as Client
    participant MCP as MCP_Server
    participant Search as EntityStore/Search
    participant FS as Filesystem

    Client->>MCP: get_entities(task, entity_type)
    MCP->>Search: search_entities(filters: {type: entity_type}, task)
    Search-->>MCP: list of Entity objects
    MCP->>FS: read/ensure_namespace metadata
    FS-->>MCP: namespace metadata (num_entities, etc.)
    MCP-->>Client: formatted response ("# {EntityType}s for: {task}" + items)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Possibly related PRs

Suggested reviewers

  • vinodmut
  • gaodan-fang

Poem

🐰 I hopped through code and docs tonight,

Found policies tucked snug and right.
Entities called from far and near,
Guidelines kept and new ones clear.
Hop—this patch makes toolsets bright.

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Title check ❓ Inconclusive The title 'Feat/kaizen policies' is related to the changeset, which introduces policy support throughout the codebase, but is vague and uses generic naming conventions without clearly conveying what the feature accomplishes. Consider a more descriptive title such as 'Add policy support with get_entities API and CUGA-compatible policy schema' to better communicate the key changes and intent.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@kaizen/backend/filesystem.py`:
- Around line 222-223: The delete path leaves persisted
FilesystemNamespace.num_entities stale because we only update num_entities when
adding; in delete_entity_by_id update the in-memory namespace data's
num_entities (set data.num_entities = len(data.entities) or equivalent) before
calling self._save_namespace_data so saved metadata stays in sync with the
actual entities; modify delete_entity_by_id to update num_entities and then call
_save_namespace_data.

In `@tests/unit/test_policy_schema.py`:
- Around line 1-8: Add the pytest "unit" marker to the test module so it runs
under -m unit: either set a module-level marker by adding pytestmark =
pytest.mark.unit at the top of tests/unit/test_policy_schema.py (above imports
or right after them) or decorate each test function with `@pytest.mark.unit`;
ensure you import pytest if not already present.

ℹ️ Review info

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cf6171a and 3039262.

📒 Files selected for processing (7)
  • AGENTS.md
  • README.md
  • docs/POLICIES.md
  • kaizen/backend/filesystem.py
  • kaizen/frontend/mcp/mcp_server.py
  • kaizen/schema/policy.py
  • tests/unit/test_policy_schema.py

Comment thread kaizen/backend/filesystem.py
Comment thread tests/unit/test_policy_schema.py Outdated
- Added Policy schema mirroring CUGA models
- Enhanced MCP server with generic get_entities tool
- Restored get_guidelines as a backward-compatible alias
- Fixed num_entities bug in Filesystem backend
- Added dedicated policy documentation and unit tests
@visahak
visahak force-pushed the feat/kaizen-policies branch from 3039262 to ec4c0a9 Compare February 23, 2026 20:46
This commit fixes the stale num_entities when deleting a filesystem entity, removes the missing ensure_namespace function call from the mcp_server after the upstream rebase, and adds a module-level pytest unit mark to the policy schema tests.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🧹 Nitpick comments (2)
kaizen/frontend/mcp/mcp_server.py (1)

78-78: capitalize() produces malformed headers for underscore-separated entity types.

"intent_guard".capitalize()"Intent_guard", giving headers like # Intent_guards for: …. Consider replacing underscores before capitalising.

✨ Suggested improvement
-    header = f"# {entity_type.capitalize()}s for: {task}"
+    header = f"# {entity_type.replace('_', ' ').title()}s for: {task}"
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@kaizen/frontend/mcp/mcp_server.py` at line 78, The header generation uses
entity_type.capitalize() which leaves underscores intact (e.g., "Intent_guard");
update the header creation (the header variable assignment) to replace
underscores and properly title-case the entity type before pluralizing—e.g.,
transform entity_type with entity_type.replace("_", " ").title() (or split and
capitalize each word) and then use that result in f"# {…}s for: {task}" so
headers read "Intent Guards for: …".
kaizen/schema/policy.py (1)

26-26: threshold has no range constraint — invalid values (e.g. 2.5, -0.1) are silently accepted.

🛡️ Suggested validation
-    threshold: float = 0.7  # for natural_language triggers
+    threshold: float = Field(default=0.7, ge=0.0, le=1.0)  # for natural_language triggers
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@kaizen/schema/policy.py` at line 26, The threshold field currently accepts
any float; add validation to enforce 0.0 <= threshold <= 1.0 by adding a check
where the containing class is initialized (e.g., implement a __post_init__ for
dataclasses or a validate_threshold/validator for pydantic models) that raises
ValueError if threshold is out of range, or replace the declaration with a
pydantic Field(threshold: float = Field(0.7, ge=0.0, le=1.0)) to enforce the
bounds automatically; reference the threshold symbol and the class that declares
it when adding this validation.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@docs/POLICIES.md`:
- Around line 17-19: The TriggerType.ALWAYS option is missing from the trigger
list in the docs; update the "Trigger Types" section to include `always` (e.g.,
describe that `always` triggers apply to every input/response such as global
output formatters) so the docs match the policy.py schema and references to
TriggerType.ALWAYS.

In `@kaizen/frontend/mcp/mcp_server.py`:
- Around line 66-84: The call to the undefined function ensure_namespace()
inside get_entities_logic causes a NameError and is redundant because
get_client() already initializes the namespace; remove the ensure_namespace()
call from get_entities_logic (and any other occurrences in this module such as
in get_entities or get_guidelines if present) so the function relies on
get_client()'s internal _namespace_initialized logic and no longer references
the undefined symbol.

In `@kaizen/schema/policy.py`:
- Around line 1-3: Replace deprecated typing generics in the imports and model
annotations: remove List, Dict, Optional from the typing import and use built-in
generics (list, dict) and PEP 604 union syntax (T | None) for optional fields
used in your Pydantic models; update any field annotations that referenced
List[...] to list[...], Dict[...] to dict[..., ...], and Optional[T] to T |
None, keeping Enum, BaseModel, Field, and Any as needed (e.g., adjust model
classes that reference these types in kaizen/schema/policy.py such as any
BaseModel subclasses and their field definitions).

---

Nitpick comments:
In `@kaizen/frontend/mcp/mcp_server.py`:
- Line 78: The header generation uses entity_type.capitalize() which leaves
underscores intact (e.g., "Intent_guard"); update the header creation (the
header variable assignment) to replace underscores and properly title-case the
entity type before pluralizing—e.g., transform entity_type with
entity_type.replace("_", " ").title() (or split and capitalize each word) and
then use that result in f"# {…}s for: {task}" so headers read "Intent Guards
for: …".

In `@kaizen/schema/policy.py`:
- Line 26: The threshold field currently accepts any float; add validation to
enforce 0.0 <= threshold <= 1.0 by adding a check where the containing class is
initialized (e.g., implement a __post_init__ for dataclasses or a
validate_threshold/validator for pydantic models) that raises ValueError if
threshold is out of range, or replace the declaration with a pydantic
Field(threshold: float = Field(0.7, ge=0.0, le=1.0)) to enforce the bounds
automatically; reference the threshold symbol and the class that declares it
when adding this validation.

ℹ️ Review info

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3039262 and ec4c0a9.

📒 Files selected for processing (7)
  • AGENTS.md
  • README.md
  • docs/POLICIES.md
  • kaizen/backend/filesystem.py
  • kaizen/frontend/mcp/mcp_server.py
  • kaizen/schema/policy.py
  • tests/unit/test_policy_schema.py
🚧 Files skipped from review as they are similar to previous changes (2)
  • kaizen/backend/filesystem.py
  • tests/unit/test_policy_schema.py

Comment thread docs/POLICIES.md
Comment thread kaizen/frontend/mcp/mcp_server.py
Comment thread kaizen/schema/policy.py
visahak and others added 3 commits February 23, 2026 16:44
Modernized typing generics in policy schema using built-in list/dict and PEP 604 union syntax. Updated documentation to include the missing "always" trigger type.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1


ℹ️ Review info

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ec4c0a9 and afa450a.

📒 Files selected for processing (3)
  • kaizen/backend/filesystem.py
  • kaizen/frontend/mcp/mcp_server.py
  • tests/unit/test_policy_schema.py

Comment thread kaizen/frontend/mcp/mcp_server.py
@visahak

visahak commented Feb 25, 2026

Copy link
Copy Markdown
Collaborator Author

ready to be reviewed

@gaodan-fang

Copy link
Copy Markdown
Collaborator

@visahak just wanna make sure i understand it correctly, this change is to support create policy manually and store it in kaizen but extract policies from trajectories is not supported yet?

@visahak

visahak commented Feb 25, 2026

Copy link
Copy Markdown
Collaborator Author

@visahak just wanna make sure i understand it correctly, this change is to support create policy manually and store it in kaizen but extract policies from trajectories is not supported yet?

Yes

@gaodan-fang gaodan-fang left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@visahak
visahak merged commit 16aeb78 into AgentToolkit:main Feb 25, 2026
15 checks passed
@visahak
visahak deleted the feat/kaizen-policies branch February 25, 2026 15:58
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.

2 participants