Skip to content

feat(rules): slot-grouped brain injection + graduation-time classifier#138

Merged
Gradata merged 2 commits intomainfrom
feat/synthesizer-wire
Apr 22, 2026
Merged

feat(rules): slot-grouped brain injection + graduation-time classifier#138
Gradata merged 2 commits intomainfrom
feat/synthesizer-wire

Conversation

@Gradata
Copy link
Copy Markdown
Owner

@Gradata Gradata commented Apr 22, 2026

Summary

Lays the groundwork for wiring prompt_synthesizer into the session-start injection path (the legacy inject_brain_rules.py N-rules block will be replaced in a follow-up PR).

  • synthesize_brain_injection(lessons, *, budget_tokens, max_per_slot, persona_baseline) — new slot-grouped entry point that orders rules by Preston Rhodes' 6-step checklist (task → context → examples → persona → format → tone), enforces a 400-token default budget, and preserves inline r:xxxx anchors for capture_learning.py attribution.
  • classify_slot(item) — resolves explicit slot → example pair → category lookup → context fallback. Exposed at import time so graduation can tag lessons once, at promotion time.
  • Lesson.slot — new optional dataclass field (default ""). Format/parse round-trip through lessons.md as Slot: <value>.
  • Graduation: _ensure_slot(lesson) runs once per lesson at INSTINCT→PATTERN and PATTERN→RULE promotion. Idempotent; classifier errors are swallowed (slot is optional metadata).

Legacy synthesize_rules_prompt is untouched for back-compat. All existing tests remain green.

Test plan

  • tests/test_prompt_synthesizer.py — 29 tests: classify_slot resolution order, Preston-Rhodes slot ordering, persona baseline (str vs Path), budget-drops-lowest-priority-first, examples-slot promotion on example_draft, Lesson-like object support.
  • tests/test_slot_graduation.py — 7 tests: default slot, format omits empty, format+parse round-trip, INSTINCT→PATTERN assigns slot, existing slot preserved, no-promotion leaves slot unset.
  • Full suite: 3931 pass, 2 skip (pytest tests/).

Generated with Gradata

Gradata and others added 2 commits April 21, 2026 21:13
… baseline

Adds synthesize_brain_injection() — the canonical Preston-Rhodes 6-step
output (task → context → examples → persona → format → tone). Groups
rules by inferred slot, enforces a token budget (GRADATA_SYNTH_BUDGET,
default 400), and seeds the persona slot from a soul.md-style baseline
with graduated persona-slot rules layered as overrides.

classify_slot() maps Lesson.category → slot; example_draft/corrected
pairs route into the examples slot regardless of category.

Legacy synthesize_rules_prompt() untouched for back-compat.

Co-Authored-By: Gradata <noreply@gradata.ai>
Adds a Preston-Rhodes slot (task/context/examples/persona/format/tone) to
every graduating lesson. The slot is assigned once when a lesson is first
promoted (INSTINCT->PATTERN or PATTERN->RULE) by calling
prompt_synthesizer.classify_slot, then round-trips through
format_lessons / parse_lessons as a "Slot: <value>" meta line.

Downstream, prompt_synthesizer.synthesize_brain_injection uses this slot
to bucket lessons into the 6-step prompt ordering (task first, tone last).
Legacy lessons without a slot fall back to category inference at render
time.
Copy link
Copy Markdown

@greptile-apps greptile-apps Bot left a comment

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.

@Gradata Gradata merged commit e636bb4 into main Apr 22, 2026
7 of 9 checks passed
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 22, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 08b5c29b-58fb-498d-8cb5-9dd163e6c14c

📥 Commits

Reviewing files that changed from the base of the PR and between ff8c053 and c1575b5.

📒 Files selected for processing (6)
  • Gradata/src/gradata/_types.py
  • Gradata/src/gradata/enhancements/prompt_synthesizer.py
  • Gradata/src/gradata/enhancements/self_improvement/_confidence.py
  • Gradata/src/gradata/enhancements/self_improvement/_graduation.py
  • Gradata/tests/test_prompt_synthesizer.py
  • Gradata/tests/test_slot_graduation.py

📝 Walkthrough

Summary

  • New Lesson.slot field: Added optional string field (default "") to store Preston-Rhodes synthesis slot identifier; serialized/parsed through lessons.md as Slot: <value> metadata
  • New public API synthesize_brain_injection(): Slot-grouped brain-injection entry point implementing Preston-Rhodes 6-step ordering (task → context → examples → persona → format → tone) with configurable token budget (default 400, env var GRADATA_SYNTH_BUDGET) and per-slot caps
  • New public API classify_slot(): Slot classifier using explicit slot → example pair → category mapping → context fallback; exposed at import time for graduation-time assignment
  • Graduation-time slot assignment: _ensure_slot() idempotently assigns slots during INSTINCT→PATTERN and PATTERN→RULE promotions; swallows classifier errors (slot is optional metadata)
  • Persona baseline support: synthesize_brain_injection() accepts optional persona baseline (raw string or Markdown file path) with "Overrides" clause support
  • Backward compatibility preserved: Legacy synthesize_rules_prompt() unchanged; all existing tests green (3931 pass, 2 skip)
  • Exports added: DEFAULT_BUDGET_TOKENS, SLOT_ORDER, classify_slot, synthesize_brain_injection
  • Markdown round-trip: parse_lessons() and format_lessons() extended to handle optional Slot: metadata lines

Walkthrough

The PR introduces Preston-Rhodes slot-based lesson synthesis and classification. A new slot field is added to lessons, slot classification logic is implemented with fallback precedence rules, a slot-ordered prompt synthesizer with token budgeting is created, the graduation pipeline is extended to auto-assign slots via classification, and markdown parsing/formatting are updated to handle slot metadata.

Changes

Cohort / File(s) Summary
Core Type Definition
Gradata/src/gradata/_types.py
Added slot: str = "" field to Lesson class to store Preston-Rhodes synthesis slot identifier, intended for assignment at graduation.
Slot Synthesis & Classification
Gradata/src/gradata/enhancements/prompt_synthesizer.py
Introduced classify_slot() for 6-step Preston Rhodes slot inference (explicit slot → examples → category mapping → context fallback) and new public synthesize_brain_injection() function that renders slot-grouped prompts with token budgeting, persona baseline support, and per-slot caps. Module documentation updated to identify brain-injection as canonical session-start generator.
Markdown Serialization
Gradata/src/gradata/enhancements/self_improvement/_confidence.py
Extended parse_lessons() to parse case-insensitive Slot: metadata lines and format_lessons() to emit Slot: lines when slot is non-empty, enabling round-trip preservation of slot metadata in markdown.
Graduation Integration
Gradata/src/gradata/enhancements/self_improvement/_graduation.py
Added _ensure_slot() to attempt best-effort slot assignment via classify_slot() during lesson promotion, hooked after rule refinement (ToT) and before state transitions, with silent failure on classification errors.
Test Coverage
Gradata/tests/test_prompt_synthesizer.py, Gradata/tests/test_slot_graduation.py
Added tests validating slot classification precedence, brain-injection synthesis ordering/budgeting/persona handling, empty input handling, per-slot caps, and round-trip markdown serialization. New test module verifies slot assignment during graduation transitions and preservation of existing slots.

Sequence Diagram(s)

sequenceDiagram
    participant User as Caller
    participant Synth as synthesize_brain_injection()
    participant Classify as classify_slot()
    participant Render as Slot Renderer
    participant Budget as Token Budgeter

    User->>Synth: lessons[], budget_tokens
    loop For each lesson
        Synth->>Classify: lesson
        Classify-->>Synth: slot (explicit→examples→category→context)
    end
    Synth->>Render: slot→sentence groups (SLOT_ORDER)
    Render-->>Synth: formatted sentences
    Synth->>Budget: sentences, budget_tokens
    Budget-->>Synth: truncated sentences (drop lowest-priority)
    Synth-->>User: SynthesizedPrompt (text, anchors_used)
Loading
sequenceDiagram
    participant Grad as graduate()
    participant Lesson as Lesson (INSTINCT→PATTERN)
    participant Ensure as _ensure_slot()
    participant Classify as classify_slot()

    Grad->>Lesson: refine rule wording (ToT)
    Grad->>Ensure: lesson
    alt slot empty?
        Ensure->>Classify: lesson
        Classify-->>Ensure: inferred slot
        Ensure->>Lesson: assign slot
    else slot already set
        Ensure-->>Grad: (no-op)
    end
    note over Ensure: failures swallowed
    Grad->>Lesson: promote INSTINCT→PATTERN
    Grad-->>Grad: complete
Loading

Estimated Code Review Effort

🎯 4 (Complex) | ⏱️ ~75 minutes

Possibly Related PRs

Suggested Labels

feature

✨ 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 feat/synthesizer-wire

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

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