Skip to content

feat(v0.7.4): delta-only rule injection — opt-in, ~65% token reduction#177

Merged
Gradata merged 2 commits into
mainfrom
v0.7.4-delta-injection
May 6, 2026
Merged

feat(v0.7.4): delta-only rule injection — opt-in, ~65% token reduction#177
Gradata merged 2 commits into
mainfrom
v0.7.4-delta-injection

Conversation

@Gradata

@Gradata Gradata commented May 6, 2026

Copy link
Copy Markdown
Owner

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)
  • Migration 005_injection_log.py: tracks (brain_id, agent, session_id, fingerprint) so delta is computed per-agent-per-session
  • hooks/inject_brain_rules.py: when delta mode, checks log → emits either full rule set (first session) or # rules unchanged since last session + diff block

Token cost simulation

47 active rules, steady state, 14 tokens/rule + 30 token wrapper, 12-token delta header.

mode session 1 session 2 session 5 avg
full ~688 ~688 ~688 ~688
delta ~700 ~12 ~12 ~241

~65% lower. Marginal session 1 overhead (12 tokens) for ~676 token savings on every subsequent session.

Tests

  • 7 new tests in tests/test_delta_injection.py
  • 21 passed scoped (delta + cluster injection)
  • 4196 passed full non-integration suite (verified locally on the merge commit before push)

Risk

  • None for default usersinjection_mode='full' is the default, no behavior change.
  • Opt-in users get a 4th column added to a new SQLite table, append-only.
  • Rollback: drop migration 005 + flip config back. No data loss.

Council context

  • Option A: ship delta-only injection (this PR) ✓
  • Option B: weekly gradata tune APO cron — scheduled (cron job_id 6963170b02e6, Sundays 4am)
  • Option C: lift-report polish + /gradata index — shipped via gradata-cloud#19 + gradata-plugin#2
  • Option D: deferred

Stacked on main, NOT on v0.7.3-prove-holdout, so it can land independently.

Oliver Le added 2 commits May 6, 2026 09:59
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.

@greptile-apps greptile-apps 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.

Your free trial has ended. If you'd like to continue receiving code reviews, you can add a payment method here.

@coderabbitai

coderabbitai Bot commented May 6, 2026

Copy link
Copy Markdown

Caution

Review failed

Failed to post review comments

📝 Walkthrough
  • New opt-in delta injection mode (injection_mode: 'delta') reduces token usage ~65% after the first session by emitting only rule changes instead of full rule sets
  • New public API: BrainConfig.load(brain_dir) classmethod for loading brain configuration
  • New public field: BrainConfig.delta_injection: bool = False to control injection behavior per-brain
  • Migration 005_injection_log.py creates an append-only SQLite table tracking injection history (brain_id, agent, session_id, fingerprint) for delta computation
  • Enhanced inject_brain_rules.py hook implements per-session and per-agent delta logic, emitting abbreviated headers and rule diffs when delta mode is enabled
  • 7 new test cases in test_delta_injection.py verify full injection, delta headers, new rule handling, and per-agent tracking
  • Backward compatible: default behavior unchanged; opt-in feature with no breaking changes
  • Demo brain assets tracked in git by overriding .gitignore negation rules for SQLite database files under src/gradata/assets/demo_brains/sdr/

Walkthrough

This 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.

Changes

Delta-Injection Feature

Layer / File(s) Summary
Configuration & Assets
.gitignore, src/gradata/_config.py
.gitignore force-includes demo brain assets. BrainConfig gains delta_injection: bool = False field and a public load() classmethod to read per-brain settings from brain-config.json.
Database Schema
src/gradata/_migrations/005_injection_log.py
New migration module creates injection_log table with rule-set hash, agent/session tracking, and indexed queries to support per-session delta computation and audit trails.
Delta-Injection Core Logic
src/gradata/hooks/inject_brain_rules.py
Adds hashing utilities (_rule_set_hash, _split_rule_set_hash), session/agent identifiers (_session_id, _agent_type), delta computation (_delta_rule_ids), and injection-log initialization (_ensure_injection_log). Main session-start flow integrates delta_injection flag to compute and apply rule-set deltas when enabled, filtering injected rules and prefixing with delta metadata headers.
Tests
tests/test_delta_injection.py
Helper utilities for lesson/config/log manipulation. Test suite validates full injection when disabled, delta-only injection when enabled, header correctness, multi-session coherence, new-rule handling, re-anchoring logic, per-agent tracking, and injection logging.

Sequence Diagram

sequenceDiagram
    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
Loading

Estimated Code Review Effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly Related PRs

  • Gradata/gradata#140: Both PRs modify inject_brain_rules.py within the session-start injection pipeline; #140 changes rule rendering to synthesized prose while this PR adds delta-based rule selection and headers.
  • Gradata/gradata#45: Both extend inject_brain_rules.py to control what gets injected at session start; one adds delta filtering while the other integrates meta-rule injection.

Suggested Labels

feature

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 7.14% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main feature: delta-only rule injection with an opt-in model and measurable token reduction (~65%).
Description check ✅ Passed The description is comprehensive and directly related to the changeset, documenting the new injection_mode config, migration, hook changes, test coverage, token savings analysis, and rollback strategy.
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.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch v0.7.4-delta-injection

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):
┌──────────────┐
│ Opengrep CLI │
└──────────────┘

�[32m✔�[39m �[1mOpengrep OSS�[0m
�[32m✔�[39m Basic security coverage for first-party code vulnerabilities.

�[1m Loading rules from local config...�[0m
[00.16][ERROR]: Error: exception Glob.Lexer.Syntax_error("malformed glob pattern: missing ']'")
Raised at Glob__Lexer.syntax_error in file "libs/glob/Lexer.mll", line 8, characters 2-26
Called from Glob__Lexer.__ocaml_lex_token_rec in file "libs/glob/Lexer.mll", line 29, characters 26-53
Cal


Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot added the feature label May 6, 2026
@Gradata Gradata merged commit 2b639dd into main May 6, 2026
9 checks passed
@Gradata Gradata deleted the v0.7.4-delta-injection branch May 6, 2026 17:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant