edda-postmortem 0.6.0

L3 post-mortem analysis and learned rules with TTL decay for Edda
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! Post-mortem analysis: produce lessons and rule proposals from session data.
//!
//! The analyzer takes session statistics and trigger reasons, then produces
//! structured findings. The current implementation uses deterministic heuristics;
//! future versions can delegate to LLM (Sonnet) for deeper analysis.
//!
//! Output hierarchy (from GH-157 spec):
//!   - **Rules**: Hook-enforced (block/auto-run), 100% compliance
//!   - **Lessons**: CLAUDE.md auto-maintained paragraph, ~90% compliance
//!   - **Observations**: `edda ask` on-demand, no enforcement

use serde::{Deserialize, Serialize};

use crate::rules::RuleCategory;
use crate::trigger::{PostMortemTrigger, TriggerReason};

/// Severity of a lesson.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum LessonSeverity {
    /// Minor observation, useful context.
    Low,
    /// Actionable insight, should influence future work.
    Medium,
    /// Critical lesson, likely produces a rule proposal.
    High,
}

/// A lesson extracted from post-mortem analysis (descriptive, not prescriptive).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Lesson {
    pub id: String,
    pub text: String,
    pub severity: LessonSeverity,
    pub tags: Vec<String>,
    pub source_trigger: String,
}

/// A rule proposal from post-mortem analysis (prescriptive).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RuleProposal {
    pub trigger: String,
    pub action: String,
    pub anchor_file: Option<String>,
    pub category: RuleCategory,
    pub confidence: f64,
    pub evidence: Vec<String>,
}

/// Complete result of a post-mortem analysis.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PostMortemResult {
    pub session_id: String,
    pub triggers: Vec<TriggerReason>,
    pub lessons: Vec<Lesson>,
    pub rule_proposals: Vec<RuleProposal>,
    pub analyzed_at: String,
}

/// Session data available for analysis.
#[derive(Debug, Clone, Default)]
pub struct AnalysisInput {
    pub session_id: String,
    pub user_prompts: u64,
    pub tool_failures: u64,
    pub failed_commands: Vec<String>,
    pub files_modified: Vec<String>,
    pub file_edit_counts: Vec<(String, u64)>,
    pub commits_made: Vec<String>,
    pub decisions_superseded: u64,
    pub had_conflict: bool,
    pub outcome: String,
    pub duration_minutes: u64,
}

/// Run deterministic post-mortem analysis on a triggered session.
///
/// Produces lessons and rule proposals based on trigger reasons and session data.
/// This is the heuristic analyzer — no LLM calls. Future: `analyze_with_llm()`.
pub fn analyze(trigger: &PostMortemTrigger, input: &AnalysisInput) -> PostMortemResult {
    let mut lessons = Vec::new();
    let mut rule_proposals = Vec::new();

    for reason in &trigger.reasons {
        match reason {
            TriggerReason::SessionFailures => {
                analyze_failures(input, &mut lessons, &mut rule_proposals);
            }
            TriggerReason::AbnormallyLong => {
                analyze_long_session(input, &mut lessons);
            }
            TriggerReason::ExcessiveFileEdits => {
                analyze_file_churn(input, &mut lessons, &mut rule_proposals);
            }
            TriggerReason::DecisionSuperseded => {
                analyze_decision_reversal(input, &mut lessons);
            }
            TriggerReason::MultiAgentConflict => {
                analyze_conflict(input, &mut lessons, &mut rule_proposals);
            }
        }
    }

    PostMortemResult {
        session_id: input.session_id.clone(),
        triggers: trigger.reasons.clone(),
        lessons,
        rule_proposals,
        analyzed_at: now_rfc3339(),
    }
}

// -- Per-trigger analyzers --

fn analyze_failures(
    input: &AnalysisInput,
    lessons: &mut Vec<Lesson>,
    rule_proposals: &mut Vec<RuleProposal>,
) {
    // Lesson: session had failures
    if input.outcome == "error_stuck" {
        lessons.push(Lesson {
            id: new_lesson_id(),
            text: format!(
                "Session got stuck after {} consecutive failures. \
                 Consider breaking the task into smaller steps.",
                input.tool_failures
            ),
            severity: LessonSeverity::High,
            tags: vec!["failure".into(), "stuck".into()],
            source_trigger: "session_failures".into(),
        });
    }

    // Rule proposal: if specific commands keep failing, suggest a pre-check.
    // Filter out compound commands, variable assignments, and shell
    // builtins/keywords/common utilities: their failures are environmental
    // noise, not a missing-tool signal, and such rules previously fired on
    // every Bash call (GH-813).
    for cmd in input
        .failed_commands
        .iter()
        .filter(|cmd| crate::rules::is_trackable_command(cmd))
    {
        let Some(short_cmd) = crate::rules::command_word(cmd) else {
            continue;
        };
        rule_proposals.push(RuleProposal {
            trigger: format!("command_failure:{short_cmd}"),
            action: format!("Verify {short_cmd} is available and configured before running"),
            anchor_file: None,
            category: RuleCategory::Workflow,
            confidence: 0.6,
            evidence: vec![format!("Failed command: {cmd}")],
        });
    }

    if input.tool_failures > 3 {
        lessons.push(Lesson {
            id: new_lesson_id(),
            text: format!(
                "{} tool failures in session. Pattern suggests environment or dependency issue.",
                input.tool_failures
            ),
            severity: LessonSeverity::Medium,
            tags: vec!["failure".into(), "tools".into()],
            source_trigger: "session_failures".into(),
        });
    }
}

fn analyze_long_session(input: &AnalysisInput, lessons: &mut Vec<Lesson>) {
    lessons.push(Lesson {
        id: new_lesson_id(),
        text: format!(
            "Session ran for {} user prompts ({} minutes). \
             Long sessions reduce focus — consider splitting into sub-tasks.",
            input.user_prompts, input.duration_minutes
        ),
        severity: LessonSeverity::Medium,
        tags: vec!["long_session".into(), "productivity".into()],
        source_trigger: "abnormally_long".into(),
    });
}

fn analyze_file_churn(
    input: &AnalysisInput,
    lessons: &mut Vec<Lesson>,
    rule_proposals: &mut Vec<RuleProposal>,
) {
    let churned: Vec<&(String, u64)> = input
        .file_edit_counts
        .iter()
        .filter(|(_, count)| *count >= 3)
        .collect();

    for (file, count) in &churned {
        lessons.push(Lesson {
            id: new_lesson_id(),
            text: format!(
                "File '{file}' was edited {count} times. \
                 Frequent edits to the same file suggest unclear requirements or iterative debugging.",
            ),
            severity: LessonSeverity::Medium,
            tags: vec!["churn".into(), "file_edits".into()],
            source_trigger: "excessive_file_edits".into(),
        });

        // Propose a rule: if a file is frequently edited, add a pre-commit check
        rule_proposals.push(RuleProposal {
            trigger: format!("file_churn:{file}"),
            action: format!("Review '{file}' carefully before committing — historically unstable"),
            anchor_file: Some(file.clone()),
            category: RuleCategory::PreCommit,
            confidence: 0.5,
            evidence: vec![format!(
                "Edited {count} times in session {}",
                input.session_id
            )],
        });
    }
}

fn analyze_decision_reversal(input: &AnalysisInput, lessons: &mut Vec<Lesson>) {
    lessons.push(Lesson {
        id: new_lesson_id(),
        text: format!(
            "{} decision(s) were superseded during this session. \
             Consider spending more time on upfront design before committing to an approach.",
            input.decisions_superseded
        ),
        severity: LessonSeverity::High,
        tags: vec!["decision".into(), "reversal".into()],
        source_trigger: "decision_superseded".into(),
    });
}

fn analyze_conflict(
    input: &AnalysisInput,
    lessons: &mut Vec<Lesson>,
    rule_proposals: &mut Vec<RuleProposal>,
) {
    lessons.push(Lesson {
        id: new_lesson_id(),
        text: "Multi-agent conflict detected. \
               Ensure agents claim non-overlapping scopes before starting work."
            .to_string(),
        severity: LessonSeverity::High,
        tags: vec!["conflict".into(), "multi_agent".into()],
        source_trigger: "multi_agent_conflict".into(),
    });

    rule_proposals.push(RuleProposal {
        trigger: "multi_agent_start".to_string(),
        action: "Run `edda claim` to claim scope before starting multi-agent work".to_string(),
        anchor_file: None,
        category: RuleCategory::Workflow,
        confidence: 0.8,
        evidence: vec![format!("Conflict detected in session {}", input.session_id)],
    });
}

// -- Helpers --

fn new_lesson_id() -> String {
    format!("lesson_{}", ulid::Ulid::new().to_string().to_lowercase())
}

fn now_rfc3339() -> String {
    let now = time::OffsetDateTime::now_utc();
    now.format(&time::format_description::well_known::Rfc3339)
        .expect("RFC3339 formatting should not fail")
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::trigger::PostMortemTrigger;

    fn trigger_with(reasons: Vec<TriggerReason>) -> PostMortemTrigger {
        PostMortemTrigger {
            should_analyze: true,
            reasons,
            session_id: "test-session".to_string(),
        }
    }

    fn base_input() -> AnalysisInput {
        AnalysisInput {
            session_id: "test-session".to_string(),
            outcome: "completed".to_string(),
            ..Default::default()
        }
    }

    #[test]
    fn analyze_session_failures_produces_lessons() {
        let trigger = trigger_with(vec![TriggerReason::SessionFailures]);
        let mut input = base_input();
        input.outcome = "error_stuck".to_string();
        input.tool_failures = 5;
        input.failed_commands = vec!["npm test".to_string()];

        let result = analyze(&trigger, &input);
        assert!(!result.lessons.is_empty());
        assert!(!result.rule_proposals.is_empty());
        assert!(result
            .lessons
            .iter()
            .any(|l| l.tags.contains(&"stuck".to_string())));
    }

    #[test]
    fn analyze_long_session_produces_lesson() {
        let trigger = trigger_with(vec![TriggerReason::AbnormallyLong]);
        let mut input = base_input();
        input.user_prompts = 30;
        input.duration_minutes = 45;

        let result = analyze(&trigger, &input);
        assert_eq!(result.lessons.len(), 1);
        assert!(result.lessons[0].text.contains("30 user prompts"));
    }

    #[test]
    fn analyze_file_churn_produces_rule_proposal() {
        let trigger = trigger_with(vec![TriggerReason::ExcessiveFileEdits]);
        let mut input = base_input();
        input.file_edit_counts = vec![("src/main.rs".to_string(), 5)];

        let result = analyze(&trigger, &input);
        assert!(!result.lessons.is_empty());
        assert!(!result.rule_proposals.is_empty());
        assert!(result.rule_proposals[0].trigger.contains("file_churn"));
    }

    #[test]
    fn analyze_conflict_produces_high_severity() {
        let trigger = trigger_with(vec![TriggerReason::MultiAgentConflict]);
        let input = base_input();

        let result = analyze(&trigger, &input);
        assert!(result
            .lessons
            .iter()
            .any(|l| l.severity == LessonSeverity::High));
        assert!(!result.rule_proposals.is_empty());
    }

    #[test]
    fn failed_command_filters_builtins_compound_and_assignments() {
        let trigger = trigger_with(vec![TriggerReason::SessionFailures]);
        let mut input = base_input();
        input.outcome = "error_stuck".to_string();
        input.tool_failures = 6;
        input.failed_commands = vec![
            "cd /tmp && npm test".to_string(),
            "echo hi; ls -la".to_string(),
            "git status | head".to_string(),
            "FOO=bar npm test".to_string(),
            "cd /tmp".to_string(),
            "echo missing-file".to_string(),
            "grep pattern".to_string(),
            "for f in *; do echo $f; done".to_string(),
            "   ".to_string(),
            "".to_string(),
            "npm test".to_string(),
        ];

        let result = analyze(&trigger, &input);
        // Only the trackable real command survives the filter (GH-813:
        // builtins/keywords/assignments/compound commands produced noise
        // rules that fired on every Bash call).
        assert_eq!(result.rule_proposals.len(), 1);
        assert_eq!(result.rule_proposals[0].trigger, "command_failure:npm");
    }

    #[test]
    fn failed_command_normalizes_quoted_command_triggers() {
        let trigger = trigger_with(vec![TriggerReason::SessionFailures]);
        let mut input = base_input();
        input.failed_commands = vec![r#""python" -V"#.to_string()];

        let result = analyze(&trigger, &input);
        assert_eq!(result.rule_proposals.len(), 1);
        assert_eq!(result.rule_proposals[0].trigger, "command_failure:python");
        assert_eq!(
            result.rule_proposals[0].action,
            "Verify python is available and configured before running"
        );
    }

    #[test]
    fn multiple_triggers_produce_combined_results() {
        let trigger = trigger_with(vec![
            TriggerReason::SessionFailures,
            TriggerReason::AbnormallyLong,
            TriggerReason::ExcessiveFileEdits,
        ]);
        let mut input = base_input();
        input.tool_failures = 5;
        input.user_prompts = 30;
        input.duration_minutes = 45;
        input.file_edit_counts = vec![("a.rs".to_string(), 4)];

        let result = analyze(&trigger, &input);
        // Should have lessons from multiple triggers
        assert!(result.lessons.len() >= 3);
    }
}