feat: lesson extractor for F5.3 cross-session learning bullet 1 #42
Notifications
Due Date
No due date set.
Blocks
#37 feat: rolegraph session-frequency edge weighting from concept enrichment (f5.3 bullet 3)
terraphim/agent-tasks
#40 feat: auto-suggest correction from KG thesaurus during capture
terraphim/agent-tasks
Reference: terraphim/agent-tasks#42
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Problem Statement
Spec section F5.3 Cross-Session Learning in
docs/specifications/terraphim-agent-session-search-spec.mdlists three bullets:CapturedLearningcorrection so the next time the failing command appears, the agent already knows the answer./sessions recommend).Bullets 2 and 3 have issues. Bullet 1 has none. No code path scans imported sessions for paired failure-then-success commands and emits
CapturedLearningrecords with corrections pre-filled. Today, learnings are only captured live via thePostToolUsehook (terraphim-agent learn hook) -- past sessions imported viasessions importcontain rich fail→fix history that goes unmined.Verification:
Spec text (verbatim, F5.3):
This is the highest-leverage F5.3 bullet because it bootstraps the entire learning store from existing session history rather than requiring users to re-encounter every failure live.
Acceptance Criteria
crates/terraphim_agent/src/learnings/lesson_extractor.rsexposing:BashToolinvocation with non-zero exit code (or stderr containingerror:/Error:/fatal:) followed within the same session by a successfulBashToolinvocation whose command shares a normalised prefix (first token, e.g.npm/bun/cargo) is a candidate pair.1.0if the second command runs within 5 minutes of the first,0.7if 5-30 min,0.4if 30+ min,0.0discarded if a different command-class succeeded between them./sessions extract-lessons [--session ID] [--min-confidence 0.5] [--dry-run]:--dry-run, callsLearningStore::add_correction()for each extracted lesson with confidence ≥ threshold.--dry-run, prints proposed lessons as a table.terraphim-agent learn extract-lessons [--session ID] [--min-confidence 0.5] [--dry-run].CapturedLearning.notesfield getsextracted-from-session:<session-id>prefix so a reviewer can trace provenance.(failed_command, session_id)already present in the store before inserting)..jsonlsession undercrates/terraphim_sessions/fixtures/, run extractor, assert expected lessons.cargo test -p terraphim_agent learnings::lesson_extractorpasses.cargo clippy -p terraphim_agent -- -D warningspasses.Proposed Approach
Crates affected:
crates/terraphim_agent(new module + REPL/CLI wiring), read-only access tocrates/terraphim_sessions.Files to modify / add:
crates/terraphim_agent/src/learnings/lesson_extractor.rs(new, ~150 LoC)crates/terraphim_agent/src/learnings/mod.rs(addpub mod lesson_extractor)crates/terraphim_agent/src/repl/commands.rs(addExtractLessonstoSessionsSubcommandenum and dispatch arm)crates/terraphim_agent/src/main.rs(addExtractLessonstoLearnSubenum and dispatch arm)crates/terraphim_agent/tests/lesson_extractor_test.rs(new integration test, ~80 LoC)crates/terraphim_sessions/fixtures/fail_then_fix_npm_to_bun.jsonl(new test fixture, ~20 lines)Why this approach: Pure-read pass over already-imported sessions, deterministic confidence scoring, no LLM calls, no new deps. Reuses existing
LearningStore::add_correction()so corrections appear inlearn list/learn queryimmediately.Dependencies
/sessions recommend) and #37 (rolegraph session weighting) to complete F5.3 (3/3 bullets covered).Verification
Scope
~150 LoC core + ~80 LoC tests + ~30 LoC REPL/CLI wiring + ~20 lines fixture = under 300 lines total. Single PR, well under the 500-line cap.
Upstream: This issue tracks a feature for terraphim/terraphim-ai (mirror in
terraphim/agent-tasksbecause the canonical repo currently 404s).