feat(memory): support-conserving guideline consolidation#283
Conversation
📝 WalkthroughWalkthroughAdds configurable consolidation modes, support/evidence fields on guidelines, mode-aware clustering with source-index tracking, client wiring for none/lossless/lossy behavior, support metadata at generation sites, and updated tests for support conservation. ChangesGuideline Consolidation with Support/Evidence Tracking
Estimated code review effort: 4 (Complex) | ~60 minutes Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (3)
altk_evolve/llm/guidelines/prompts/combine_guidelines.jinja2 (1)
28-34: 🎯 Functional Correctness | 🔵 Trivial | 💤 Low valueOptional: explicitly forbid repeated indices within one
source_indiceslist.Given the code-level fix needed in
clustering.py's_attribute_supportfor duplicate indices, consider adding an explicit instruction here (e.g. "Each index must appear at most once within a single guideline'ssource_indices") as a lightweight extra guardrail, though the primary fix belongs in the parsing logic.🤖 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 `@altk_evolve/llm/guidelines/prompts/combine_guidelines.jinja2` around lines 28 - 34, The consolidation rules need an explicit guardrail against duplicate indices in a single source_indices list. Update the guidelines in combine_guidelines.jinja2 to state that each consolidated guideline’s source_indices must contain only unique 0-based input indices, so no index can appear more than once within the same list, while still requiring every input index to appear exactly once across all consolidated guidelines.altk_evolve/frontend/client/evolve_client.py (1)
227-228: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winDuplicate support-extraction logic vs.
clustering.py.
int((e.metadata or {}).get("support", 1) or 1)is identical to themember_supportcomputation inaltk_evolve/llm/guidelines/clustering.py(combine_cluster). Extracting this into a small shared helper (e.g.get_entity_support(entity: RecordedEntity) -> int) would prevent the two call sites from silently drifting and corrupting the support-conservation accounting this PR centers on.🤖 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 `@altk_evolve/frontend/client/evolve_client.py` around lines 227 - 228, The support extraction for `support_before` in `evolve_client.py` duplicates the `member_support` logic from `combine_cluster` in `clustering.py`, so move that metadata-to-int conversion into a shared helper such as `get_entity_support(entity)` and use it in both places. Update the `support_before` aggregation to call the helper on each `RecordedEntity`, and keep `combine_cluster` aligned by using the same helper instead of reimplementing the fallback logic inline.tests/unit/test_combine_guidelines.py (1)
49-61: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winConsider adding a regression test for duplicate indices within one
source_indiceslist.Given the
_attribute_supportdouble-counting bug flagged inclustering.py(duplicate index within a single consolidated guideline'ssource_indices, e.g._cg("Merged", "strategy", [0, 0, 1])), a test assertingsupportisn't inflated by such duplicates would guard against regression once that fix is applied.Want me to draft this test case?
Also applies to: 91-141
🤖 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 `@tests/unit/test_combine_guidelines.py` around lines 49 - 61, Add a regression test around combine_guidelines for duplicate entries inside one consolidated guideline’s source_indices. Update the test fixture in test_combine_guidelines.py to include a case like a merged guideline with repeated indices, then assert the resulting support from the combine_guidelines/clustering path is not inflated by duplicates. Use the existing _cg helper and SAMPLE_GUIDELINES setup to locate the test and verify the _attribute_support behavior stays correct.
🤖 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 `@altk_evolve/llm/guidelines/clustering.py`:
- Around line 156-211: The _attribute_support function can double-count support
when a single ConsolidatedGuideline has duplicate entries in source_indices.
Deduplicate the filtered idxs per cg before marking assigned and summing
support, so repeated indices like [0, 0, 1] only contribute once. Keep the same
conservation flow in _attribute_support, including the existing warning if
totals still mismatch, but ensure Guideline.support is computed from unique
member indices only.
In `@altk_evolve/schema/guidelines.py`:
- Around line 90-103: Update the ConsolidationResult docstring in guidelines.py
so it only describes the actual consolidation modes supported by
consolidation_mode in evolve.py; remove the reference to a non-existent
"support" mode and revise the support_before/support_after explanation to match
the current behavior of the consolidation run. Keep the wording aligned with
ConsolidationResult and Consolidation mode handling so the API docs do not imply
a deferred feature is already available.
---
Nitpick comments:
In `@altk_evolve/frontend/client/evolve_client.py`:
- Around line 227-228: The support extraction for `support_before` in
`evolve_client.py` duplicates the `member_support` logic from `combine_cluster`
in `clustering.py`, so move that metadata-to-int conversion into a shared helper
such as `get_entity_support(entity)` and use it in both places. Update the
`support_before` aggregation to call the helper on each `RecordedEntity`, and
keep `combine_cluster` aligned by using the same helper instead of
reimplementing the fallback logic inline.
In `@altk_evolve/llm/guidelines/prompts/combine_guidelines.jinja2`:
- Around line 28-34: The consolidation rules need an explicit guardrail against
duplicate indices in a single source_indices list. Update the guidelines in
combine_guidelines.jinja2 to state that each consolidated guideline’s
source_indices must contain only unique 0-based input indices, so no index can
appear more than once within the same list, while still requiring every input
index to appear exactly once across all consolidated guidelines.
In `@tests/unit/test_combine_guidelines.py`:
- Around line 49-61: Add a regression test around combine_guidelines for
duplicate entries inside one consolidated guideline’s source_indices. Update the
test fixture in test_combine_guidelines.py to include a case like a merged
guideline with repeated indices, then assert the resulting support from the
combine_guidelines/clustering path is not inflated by duplicates. Use the
existing _cg helper and SAMPLE_GUIDELINES setup to locate the test and verify
the _attribute_support behavior stays correct.
🪄 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: defaults
Review profile: CHILL
Plan: Pro
Run ID: e400e1d2-586d-464c-a569-9faf324c470a
📒 Files selected for processing (8)
altk_evolve/config/evolve.pyaltk_evolve/frontend/client/evolve_client.pyaltk_evolve/frontend/mcp/mcp_server.pyaltk_evolve/llm/guidelines/clustering.pyaltk_evolve/llm/guidelines/prompts/combine_guidelines.jinja2altk_evolve/schema/guidelines.pyaltk_evolve/sync/phoenix_sync.pytests/unit/test_combine_guidelines.py
Make guideline consolidation carry a support count and evidence polarity so downstream selection can dose memory by how well-attested each guideline is (the capability-dependent dosage finding from the AppWorld experiments). - schema: add `support` (>=1) and `evidence` (success|failure|both|None) to Guideline; add ConsolidatedGuideline/ConsolidatedGuidelineResponse that carry `source_indices` so consolidation attributes support exactly. - clustering.combine_cluster: sum member support and merge evidence per output guideline via source_indices; carry any uncovered member through unchanged (lossless fail-safe) so total support is conserved and no advice is dropped. Add a `mode` arg (lossless|lossy) driving conservative vs aggressive merging. - combine_guidelines prompt: request `source_indices` (0-based) and lossless framing; add an aggressive variant for lossy mode. - config: add consolidation_mode (none|lossless|lossy) and lossy_target_num_guidelines. Support-threshold *filtering* (sup2/sup3) is left to non-destructive selection in a follow-up, not deletion here. - evolve_client.consolidate_guidelines: pass mode through, short-circuit on none, persist support/evidence, and report support_before/after (ConsolidationResult) so conservation is observable. - phoenix_sync / mcp_server: stamp support=1 on freshly mined guidelines. Tests: support summed, evidence merged (success+failure -> both), uncovered members carried through, none-mode no-op, and support conserved end-to-end. Signed-off-by: JAYARAM RADHAKRISHNAN <jayaramkr@us.ibm.com>
Address CodeRabbit review on the consolidation PR: - _attribute_support: dedupe indices within a single consolidated guideline's source_indices before summing, so a repeated index (e.g. [0, 0, 1]) can't count a member's support twice and inflate the conserved total. Adds a regression test. - ConsolidationResult docstring: drop the reference to a non-existent "support" consolidation mode; describe support-threshold pruning as a future step. Signed-off-by: JAYARAM RADHAKRISHNAN <jayaramkr@us.ibm.com>
c6add1c to
ca1d183
Compare
SummaryThis PR adds support-conserving guideline consolidation modes, including No issues met the confidence threshold for review comments. • ## Testing
|
Resolve conflicts in mcp_server.py and phoenix_sync.py: - Retain consistency-pipeline split (regular/consistency/both) with per-pipeline try/except guards from this branch - Add support=1 field introduced by the consolidation PR (#283) to guideline entity metadata in both files Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
What & why
Consolidation currently merges clustered guidelines but discards how many source guidelines back each one and whether they came from successes or failures. That signal is what's needed to dose memory per model — the capability-dependent dosage finding from AppWorld experiments (DeepSeek-V3.2, gpt-oss-120b): strong models absorb the full playbook, weaker models need a small, selective dose. This PR makes consolidation support-conserving so that signal survives.
This is PR 1 of a staged pair. A follow-up adds dosage-aware retrieval (core + top-k selection) that consumes the
supportsignal introduced here.Changes
schema/guidelines.py): addsupport: int (>=1)andevidence: success|failure|both|NonetoGuideline; addConsolidatedGuideline/ConsolidatedGuidelineResponsecarryingsource_indicesso consolidation attributes support exactly rather than trusting the model to report counts.llm/guidelines/clustering.py):combine_clusternow sums member support and merges evidence (success+failure→both) per output guideline viasource_indices, and carries any uncovered member through unchanged (lossless fail-safe) sosum(support)is conserved and no advice is ever dropped. Newmodearg (losslessdefault /lossy) selects conservative vs aggressive merging.combine_guidelines.jinja2): asks for 0-basedsource_indiceswith lossless framing; adds an aggressive variant forlossy.config/evolve.py):consolidation_mode(none|lossless|lossy) andlossy_target_num_guidelines. Support-threshold filtering (sup2/sup3) is deliberately left to non-destructive selection in the follow-up, not deletion here.consolidate_guidelines): passesmodethrough, short-circuits onnone, persistssupport/evidence, and reportssupport_before/support_afteronConsolidationResultso conservation is observable.phoenix_sync,mcp_server): stampsupport=1on freshly mined guidelines.Tests
Extended
tests/unit/test_combine_guidelines.py: support summed, evidence merged, uncovered members carried through (fail-safe),none-mode no-op, and support conserved throughconsolidate_guidelines. Full unit suite green (395 passed);ruff,ruff format,mypy, and pre-commit all pass. Also verified end-to-end on the filesystem backend (3 originals → 2 consolidated, support 3→3, evidence merged toboth).Compatibility
Backward compatible:
support/evidencedefault to1/None,ConsolidationResultgains fields with defaults, andconsolidate_guidelines(mode=...)is optional (defaults to config). Existing guidelines without the new metadata are treated assupport=1,evidence=None.Summary by CodeRabbit
supportandevidence; consolidation results reportsupport_beforeandsupport_after.