|
| 1 | +"""Backlog logic — pure functional core, no I/O. |
| 2 | +
|
| 3 | +Decides how to format, summarise, and persist study backlog items. |
| 4 | +The imperative shells (_topics.py, _study.py) handle all side effects. |
| 5 | +
|
| 6 | +See docs/mentoring/functional-core-imperative-shell.md for the pattern. |
| 7 | +See docs/architecture/study-backlog-phase1.md for the full design. |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +from collections import defaultdict |
| 13 | +from dataclasses import dataclass, field |
| 14 | +from typing import TYPE_CHECKING |
| 15 | + |
| 16 | +if TYPE_CHECKING: |
| 17 | + from studyctl.session_state import TopicEntry |
| 18 | + |
| 19 | + |
| 20 | +@dataclass |
| 21 | +class BacklogItem: |
| 22 | + """A single backlog entry — pre-fetched from DB.""" |
| 23 | + |
| 24 | + id: int |
| 25 | + question: str |
| 26 | + topic_tag: str | None |
| 27 | + tech_area: str | None |
| 28 | + source: str # parked | struggled | manual |
| 29 | + context: str | None |
| 30 | + parked_at: str |
| 31 | + session_topic: str | None # from study_sessions.topic via join |
| 32 | + |
| 33 | + |
| 34 | +@dataclass |
| 35 | +class FormattedBacklog: |
| 36 | + """Result of format_backlog_list() — ready for display.""" |
| 37 | + |
| 38 | + items: list[BacklogItem] = field(default_factory=list) |
| 39 | + total: int = 0 |
| 40 | + by_tech: dict[str, list[BacklogItem]] = field(default_factory=dict) |
| 41 | + by_source: dict[str, int] = field(default_factory=dict) |
| 42 | + |
| 43 | + |
| 44 | +@dataclass |
| 45 | +class PersistAction: |
| 46 | + """A struggled topic to persist to parked_topics.""" |
| 47 | + |
| 48 | + question: str |
| 49 | + topic_tag: str | None |
| 50 | + context: str | None |
| 51 | + study_session_id: str |
| 52 | + source: str = "struggled" |
| 53 | + |
| 54 | + |
| 55 | +def format_backlog_list( |
| 56 | + items: list[BacklogItem], |
| 57 | + *, |
| 58 | + tech_filter: str | None = None, |
| 59 | + source_filter: str | None = None, |
| 60 | +) -> FormattedBacklog: |
| 61 | + """Filter and group backlog items for display. Pure logic.""" |
| 62 | + filtered = items |
| 63 | + if tech_filter: |
| 64 | + filtered = [i for i in filtered if i.tech_area == tech_filter] |
| 65 | + if source_filter: |
| 66 | + filtered = [i for i in filtered if i.source == source_filter] |
| 67 | + |
| 68 | + by_tech: dict[str, list[BacklogItem]] = defaultdict(list) |
| 69 | + by_source: dict[str, int] = defaultdict(int) |
| 70 | + for item in filtered: |
| 71 | + tech_key = item.tech_area or "Uncategorized" |
| 72 | + by_tech[tech_key].append(item) |
| 73 | + by_source[item.source] += 1 |
| 74 | + |
| 75 | + return FormattedBacklog( |
| 76 | + items=filtered, |
| 77 | + total=len(filtered), |
| 78 | + by_tech=dict(by_tech), |
| 79 | + by_source=dict(by_source), |
| 80 | + ) |
| 81 | + |
| 82 | + |
| 83 | +def build_backlog_summary( |
| 84 | + pending_items: list[BacklogItem], |
| 85 | + current_topic: str, |
| 86 | +) -> str | None: |
| 87 | + """Build markdown snippet for agent persona injection. |
| 88 | +
|
| 89 | + Returns None if no pending items. Prioritises items matching |
| 90 | + the current topic's tech area (simple substring match). |
| 91 | + """ |
| 92 | + if not pending_items: |
| 93 | + return None |
| 94 | + |
| 95 | + # Sort: items matching current topic first, then the rest |
| 96 | + topic_lower = current_topic.lower() |
| 97 | + |
| 98 | + def relevance(item: BacklogItem) -> int: |
| 99 | + if item.tech_area and item.tech_area.lower() in topic_lower: |
| 100 | + return 0 |
| 101 | + if item.topic_tag and item.topic_tag.lower() in topic_lower: |
| 102 | + return 0 |
| 103 | + return 1 |
| 104 | + |
| 105 | + sorted_items = sorted(pending_items, key=relevance) |
| 106 | + |
| 107 | + lines = [f"You have {len(sorted_items)} outstanding study topics:\n"] |
| 108 | + for item in sorted_items: |
| 109 | + tech = f" [{item.tech_area}]" if item.tech_area else "" |
| 110 | + source = f" ({item.source})" if item.source != "parked" else "" |
| 111 | + lines.append(f"- {item.question}{tech}{source}") |
| 112 | + |
| 113 | + return "\n".join(lines) |
| 114 | + |
| 115 | + |
| 116 | +def plan_auto_persist( |
| 117 | + topic_entries: list[TopicEntry], |
| 118 | + existing_questions: set[str], |
| 119 | + study_session_id: str, |
| 120 | +) -> list[PersistAction]: |
| 121 | + """Decide which struggled topics to persist to parked_topics. |
| 122 | +
|
| 123 | + Filters for status='struggling', deduplicates against |
| 124 | + existing_questions (already in parked_topics for this session). |
| 125 | + """ |
| 126 | + actions: list[PersistAction] = [] |
| 127 | + for entry in topic_entries: |
| 128 | + if entry.status != "struggling": |
| 129 | + continue |
| 130 | + if entry.topic in existing_questions: |
| 131 | + continue |
| 132 | + actions.append( |
| 133 | + PersistAction( |
| 134 | + question=entry.topic, |
| 135 | + topic_tag=entry.topic, |
| 136 | + context=entry.note or None, |
| 137 | + study_session_id=study_session_id, |
| 138 | + ) |
| 139 | + ) |
| 140 | + return actions |
0 commit comments