feat(v0.7.4): delta-only rule injection — opt-in, ~65% token reduction#177
Conversation
The pyproject.toml force-includes assets/demo_brains/sdr/system.db but .gitignore had `*.db` excluding it, breaking the editable install on CI. Override with explicit `!src/gradata/assets/demo_brains/sdr/*.db`.
…sion - New BrainConfig.injection_mode: 'full' (default) | 'delta' (opt-in) - Delta mode: first session emits full rule set, subsequent emit only diff - Migration 005: injection_log tracks (brain_id, agent, session_id, fingerprint) so cross-session deltas are computable per agent - Hooks (inject_brain_rules.py) check log, emit either full or 'rules unchanged since last session' header + delta block - 7 new tests in test_delta_injection.py + integration verified through test_cluster_injection (21 passed scoped) - Token cost: 47-rule brain steady state ~688 tokens/session full → ~12 tokens/session delta Council Option A complete. Council Option B (weekly gradata tune APO cron) already scheduled. Council Option C (lift-report polish + /gradata) shipped via PRs #19, #2.
There was a problem hiding this comment.
Your free trial has ended. If you'd like to continue receiving code reviews, you can add a payment method here.
|
Caution Review failedFailed to post review comments 📝 Walkthrough
WalkthroughThis PR introduces delta-injection for brain rules: a feature that selectively injects only newly graduated rules into subsequent sessions by computing rule-set deltas. The implementation spans configuration, database schema, core logic, and comprehensive tests. ChangesDelta-Injection Feature
Sequence DiagramsequenceDiagram
participant Session as Session Start
participant Config as BrainConfig
participant Brain as Injection Logic
participant DB as Injection Log DB
participant Rules as Rule Set
Session->>Config: load(brain_dir)
Config-->>Session: delta_injection flag
Session->>Brain: invoke inject_brain_rules()
Brain->>DB: _ensure_injection_log() schema exists
DB-->>Brain: ready
Brain->>Brain: _session_id() / _agent_type() from context
Brain->>Rules: get full_rule_ids (all available rules)
Rules-->>Brain: rule list
alt delta_injection enabled
Brain->>DB: query previous rule-set hash for session
DB-->>Brain: prior hash & mtime
Brain->>Brain: _rule_set_hash(current rules, mtime)
Brain->>Brain: compare hashes, compute delta via _delta_rule_ids()
Brain->>Brain: _changed_ratio() to check threshold
Brain->>Brain: select only new/changed rule IDs
Brain->>Rules: filter to delta subset
else delta_injection disabled
Brain->>Rules: use full rule set
end
Rules-->>Brain: selected rules + delta header
Brain->>DB: log injection_log row (rule-set hash, session, agent)
DB-->>Brain: logged
Brain-->>Session: synthesized rules block with delta metadata
Estimated Code Review Effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly Related PRs
Suggested Labels
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 OpenGrep (1.20.0)OpenGrep fatal error (exit code 2): �[32m✔�[39m �[1mOpengrep OSS�[0m �[1m Loading rules from local config...�[0m Comment |
v0.7.4 — delta-only rule injection (token reduction, opt-in)
Council Option A: ship delta-only injection. ~65% token reduction per session for steady-state brains.
What changed
BrainConfig.injection_mode:'full'(default, preserves v0.7.3 behavior) |'delta'(opt-in)(brain_id, agent, session_id, fingerprint)so delta is computed per-agent-per-sessionhooks/inject_brain_rules.py: whendeltamode, checks log → emits either full rule set (first session) or# rules unchanged since last session+ diff blockToken cost simulation
47 active rules, steady state, 14 tokens/rule + 30 token wrapper, 12-token delta header.
~65% lower. Marginal session 1 overhead (12 tokens) for ~676 token savings on every subsequent session.
Tests
tests/test_delta_injection.pyRisk
injection_mode='full'is the default, no behavior change.Council context
gradata tuneAPO cron — scheduled (cron job_id6963170b02e6, Sundays 4am)Stacked on
main, NOT on v0.7.3-prove-holdout, so it can land independently.