-
Notifications
You must be signed in to change notification settings - Fork 0
cleanup(enhancements): move retrieval_fusion, flip Beta-LB gate, add invariant + obfuscation tests #163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
cleanup(enhancements): move retrieval_fusion, flip Beta-LB gate, add invariant + obfuscation tests #163
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from gradata._types import Lesson, LessonState | ||
| from gradata.enhancements.self_improvement import graduate | ||
| from gradata.enhancements.self_improvement._confidence import ( | ||
| INITIAL_CONFIDENCE, | ||
| MIN_APPLICATIONS_FOR_PATTERN, | ||
| PATTERN_THRESHOLD, | ||
| ) | ||
|
|
||
|
|
||
| def _lesson(confidence: float, fire_count: int) -> Lesson: | ||
| return Lesson( | ||
| date="2026-05-02", | ||
| state=LessonState.INSTINCT, | ||
| confidence=confidence, | ||
| category="PROCESS", | ||
| description="Follow the existing process", | ||
| fire_count=fire_count, | ||
| ) | ||
|
|
||
|
|
||
| def test_fresh_lesson_starts_as_instinct() -> None: | ||
| lesson = _lesson(INITIAL_CONFIDENCE, 0) | ||
|
|
||
| assert lesson.state is LessonState.INSTINCT | ||
|
|
||
|
|
||
| def test_pattern_threshold_tie_does_not_promote() -> None: | ||
| lesson = _lesson(PATTERN_THRESHOLD, MIN_APPLICATIONS_FOR_PATTERN) | ||
|
|
||
| active, graduated = graduate([lesson]) | ||
|
|
||
| assert lesson.state is LessonState.INSTINCT | ||
| assert active == [lesson] | ||
| assert graduated == [] | ||
|
|
||
|
|
||
| def test_above_pattern_threshold_with_enough_fires_promotes() -> None: | ||
| lesson = _lesson(PATTERN_THRESHOLD + 0.01, MIN_APPLICATIONS_FOR_PATTERN) | ||
|
|
||
| graduate([lesson]) | ||
|
|
||
| assert lesson.state is LessonState.PATTERN |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import re | ||
|
|
||
| from gradata import Brain | ||
| from gradata.middleware._core import RuleSource, build_brain_rules_block | ||
|
|
||
| _RAW_CONFIDENCE_FLOAT = re.compile(r"(?<![\w.])(?:0(?:\.\d+)?|1(?:\.0+)?)(?![\w.])") | ||
|
|
||
|
|
||
| def _assert_no_raw_confidence_float(prompt: str) -> None: | ||
| leaks = _RAW_CONFIDENCE_FLOAT.findall(prompt) | ||
| assert not leaks, f"raw confidence float leaked into prompt-bound text: {prompt}" | ||
|
Comment on lines
+8
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
set -euo pipefail
python - <<'PY'
import re
current = re.compile(r"(?<![\w.])(?:0(?:\.\d+)?|1(?:\.0+)?)(?![\w.])")
proposed = re.compile(r"(?<![\w.])(?:0\.\d+|1\.0+)(?![\w.])")
samples = [
"<brain-rules>\n[RULE] Use 1 sentence.\n</brain-rules>",
"<brain-rules>\n[RULE] Keep 0 padding.\n</brain-rules>",
"<brain-rules>\n[RULE:0.95] Prefer concrete dates.\n</brain-rules>",
"<brain-rules>\n[RULE:1.00] Prefer concrete dates.\n</brain-rules>",
]
for s in samples:
print("TEXT:", s.replace("\n", "\\n"))
print(" current:", current.findall(s))
print("proposed:", proposed.findall(s))
print("-" * 60)
PYRepository: Gradata/gradata Length of output: 701 Regex matches plain integers and will cause false positives. The optional decimal portion in the current pattern ( Proposed fix-_RAW_CONFIDENCE_FLOAT = re.compile(r"(?<![\w.])(?:0(?:\.\d+)?|1(?:\.0+)?)(?![\w.])")
+_RAW_CONFIDENCE_FLOAT = re.compile(r"(?<![\w.])(?:0\.\d+|1\.0+)(?![\w.])")The proposed regex requires an explicit decimal point and digits, matching only actual floats like 🤖 Prompt for AI Agents |
||
|
|
||
|
|
||
| def test_apply_brain_rules_prompt_does_not_leak_raw_confidence(tmp_path) -> None: | ||
| brain = Brain.init( | ||
| tmp_path / "brain", | ||
| name="ObfuscationGate", | ||
| domain="Testing", | ||
| embedding="local", | ||
| interactive=False, | ||
| ) | ||
|
Comment on lines
+16
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
set -euo pipefail
echo "Looking for test conftest and isolation hooks..."
fd -i 'conftest.py' tests
echo
echo "Searching for BRAIN_DIR and _paths cache refresh logic in test infra..."
rg -n -C3 'BRAIN_DIR|_paths|Brain\.init|reload|refresh' tests
echo
echo "Reference call site in this test file:"
rg -n -C2 'test_apply_brain_rules_prompt_does_not_leak_raw_confidence|Brain\.init\(' Gradata/tests/test_score_obfuscation_gate.pyRepository: Gradata/gradata Length of output: 219 🏁 Script executed: #!/bin/bash
set -euo pipefail
# Find all conftest.py files in the repository
echo "=== Locating conftest.py files ==="
git ls-files | grep -i conftest.py
echo
echo "=== Checking Gradata/tests directory structure ==="
git ls-files | grep "^Gradata/tests/" | head -20
echo
echo "=== Looking for the target test file ==="
git ls-files | grep "test_score_obfuscation_gate.py"Repository: Gradata/gradata Length of output: 953 🏁 Script executed: #!/bin/bash
set -euo pipefail
echo "=== Reading conftest.py for test isolation setup ==="
cat -n Gradata/tests/conftest.py
echo
echo "=== Checking for test_score_obfuscation_gate.py ==="
git ls-files | grep "test_score_obfuscation_gate.py"
echo
echo "=== Reading test_score_obfuscation_gate.py around lines 16-23 ==="
head -30 Gradata/tests/test_score_obfuscation_gate.pyRepository: Gradata/gradata Length of output: 9867 Use the This test calls 🤖 Prompt for AI Agents |
||
| result = brain.add_rule( | ||
| "Prefer concrete dates over relative dates", | ||
| "PROCESS", | ||
| state="RULE", | ||
| confidence=0.95, | ||
| ) | ||
| assert result["added"] is True | ||
|
|
||
| prompt = brain.apply_brain_rules("write a status update", max_rules=5) | ||
|
|
||
| assert "<brain-rules>" in prompt | ||
| _assert_no_raw_confidence_float(prompt) | ||
|
|
||
|
|
||
| def test_middleware_brain_rules_block_does_not_leak_raw_confidence() -> None: | ||
| source = RuleSource( | ||
| lessons=[ | ||
| { | ||
| "state": "RULE", | ||
| "confidence": 0.95, | ||
| "category": "PROCESS", | ||
| "description": "Prefer concrete dates over relative dates", | ||
| }, | ||
| { | ||
| "state": "PATTERN", | ||
| "confidence": 0.72, | ||
| "category": "STYLE", | ||
| "description": "Keep summaries short", | ||
| }, | ||
| ] | ||
| ) | ||
|
|
||
| prompt = build_brain_rules_block(source) | ||
|
|
||
| assert "<brain-rules>" in prompt | ||
| _assert_no_raw_confidence_float(prompt) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Default-on Beta-LB now hard-blocks lessons that lack posterior state.
Enabling the gate by default turns any
Lessonwithout persistedalpha/beta_paraminto an automatic PATTERN→RULE deny, because_passes_beta_lb_gate()falls back toBeta(1,1), whose 5th-percentile lower bound is far below the default0.85. That means legacy or handcrafted PATTERN lessons will stop graduating entirely unless the env override is set, which is a much stronger behavior change than “tighter calibration.”Possible compatibility guard
def _passes_beta_lb_gate( lesson: Lesson, config: tuple[bool, float, int] | None = None, ) -> bool: @@ - alpha = getattr(lesson, "alpha", 1.0) - beta_param = getattr(lesson, "beta_param", 1.0) + alpha = getattr(lesson, "alpha", None) + beta_param = getattr(lesson, "beta_param", None) + if alpha is None or beta_param is None: + return True # keep legacy promotion behavior until posterior state is backfilled + from gradata.rules.rule_engine import _beta_ppf_05 return _beta_ppf_05(alpha, beta_param) >= thresholdAlso applies to: 151-155
🤖 Prompt for AI Agents