-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
758 lines (684 loc) · 26 KB
/
Copy pathdatabase.py
File metadata and controls
758 lines (684 loc) · 26 KB
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
"""Persistent storage layer: SQLite + sqlite-vec for epistemic memory records."""
from __future__ import annotations
import sqlite3
import subprocess
from datetime import datetime, timezone
from typing import Any
import sqlite_vec
from sqlite_vec import serialize_float32
# EmbeddingGemma default (MRL can truncate to 512/256/128).
# This constant is the fallback default; the actual dimension is
# stored in codegrave_meta and controlled by config [storage].embedding_dim.
EMBEDDING_DIM = 768
# A handful of oversized fields would otherwise blow the judge model's context
# window on every future query_memory call — cap per-field text length.
MAX_FIELD_LENGTH = 20_000
def _check_field_lengths(fields: dict[str, str | None]) -> None:
for name, value in fields.items():
if value is not None and len(value) > MAX_FIELD_LENGTH:
raise ValueError(
f"{name} is {len(value)} chars, exceeds the {MAX_FIELD_LENGTH}-char limit"
)
def _resolve_commit_hash(provided: str | None) -> str | None:
"""Validate and resolve a commit hash, with auto-detection of HEAD.
Returns a full 40-char hash, or ``None`` if there's no git repo and no
hash was provided. Raises ``ValueError`` when *provided* is non-empty
but ``git rev-parse --verify`` can't find it in this repository.
"""
def _git(args: list[str]) -> subprocess.CompletedProcess | None:
try:
return subprocess.run(
["git", *args],
capture_output=True,
text=True,
timeout=5,
)
except FileNotFoundError:
return None
if provided:
result = _git(["rev-parse", "--verify", provided])
if result is None:
# git not installed — trust the provided hash as-is.
return provided
if result.returncode != 0:
raise ValueError(
f"Commit hash {provided!r} not found in this repository"
)
return result.stdout.strip()
# No hash provided — try to auto-detect HEAD.
result = _git(["rev-parse", "HEAD"])
if result and result.returncode == 0:
return result.stdout.strip()
return None
ENTRY_TYPES = frozenset(
{
"BUG",
"REGRESSION",
"UPSTREAM",
"PERF",
"DECISION",
"LIMITATION",
"GOTCHA",
"INVARIANT",
}
)
MEMORY_SELECT_COLUMNS = """
memory_records.id,
memory_records.entry_type,
memory_records.title,
memory_records.symptoms,
memory_records.root_cause,
memory_records.wrong_fixes,
memory_records.resolution,
memory_records.lesson,
memory_records.file_path,
memory_records.commit_hash,
memory_records.created_at,
memory_records.status,
memory_records.namespace,
memory_records.approved_by,
memory_records.issue_url,
memory_records.pr_url,
memory_records.validating_test,
memory_records.confidence,
memory_records.derived_from
"""
class DatabaseManager:
"""Handles all SQLite operations with sqlite-vec vector search."""
def __init__(self, db_path: str = ".codegrave.db", embedding_dim: int = 768):
self.db_path = db_path
self.embedding_dim = embedding_dim
self._init_db()
def _get_connection(self) -> sqlite3.Connection:
if self.db_path == ":memory:":
conn = sqlite3.connect(
"file:codegrave_mem?mode=memory&cache=shared", uri=True
)
else:
conn = sqlite3.connect(self.db_path)
conn.enable_load_extension(True)
sqlite_vec.load(conn)
conn.enable_load_extension(False)
conn.row_factory = sqlite3.Row
conn.execute("PRAGMA busy_timeout = 5000")
return conn
def _init_db(self) -> None:
with self._get_connection() as conn:
# ── Metadata table (dimension tracking, future migrations) ──
conn.execute(
"""
CREATE TABLE IF NOT EXISTS codegrave_meta (
key TEXT PRIMARY KEY,
value TEXT NOT NULL
)
"""
)
# Check for dimension mismatch with existing DB.
existing = conn.execute(
"SELECT value FROM codegrave_meta WHERE key = 'embedding_dim'"
).fetchone()
if existing is not None:
stored_dim = int(existing["value"])
if stored_dim != self.embedding_dim:
raise ValueError(
f"Existing database uses {stored_dim}-dimensional embeddings "
f"but the current configuration specifies {self.embedding_dim}. "
f"To migrate: delete the record_embeddings virtual table rows "
f"(or the entire .codegrave.db) and re-record your memories, "
f"or revert embedding_dim to {stored_dim} in your config."
)
else:
conn.execute(
"INSERT INTO codegrave_meta (key, value) VALUES ('embedding_dim', ?)",
(str(self.embedding_dim),),
)
conn.execute(
"""
CREATE TABLE IF NOT EXISTS memory_records (
id INTEGER PRIMARY KEY AUTOINCREMENT,
entry_type TEXT NOT NULL,
title TEXT NOT NULL,
symptoms TEXT NOT NULL,
root_cause TEXT NOT NULL,
wrong_fixes TEXT,
resolution TEXT NOT NULL,
lesson TEXT NOT NULL,
file_path TEXT,
commit_hash TEXT,
created_at TEXT NOT NULL
)
"""
)
# Migrate pre-existing DBs that lack the commit_hash column.
try:
conn.execute(
"ALTER TABLE memory_records ADD COLUMN commit_hash TEXT"
)
except sqlite3.OperationalError:
pass # column already exists
# Migrate pre-existing DBs that lack the namespace column.
try:
conn.execute(
"ALTER TABLE memory_records ADD COLUMN namespace TEXT NOT NULL DEFAULT '.'"
)
except sqlite3.OperationalError:
pass # column already exists
# Migrate pre-existing DBs that lack the status column.
try:
conn.execute(
"ALTER TABLE memory_records ADD COLUMN status TEXT NOT NULL DEFAULT 'approved'"
)
except sqlite3.OperationalError:
pass # column already exists
# Migrate pre-existing DBs that lack provenance columns.
for col in ("approved_by", "issue_url", "pr_url", "validating_test", "confidence"):
try:
conn.execute(f"ALTER TABLE memory_records ADD COLUMN {col} TEXT")
except sqlite3.OperationalError:
pass # column already exists
# Migrate pre-existing DBs that lack the derived_from column
# (comma-separated source record ids, set when a record is the
# canonical result of a consolidate/merge action).
try:
conn.execute("ALTER TABLE memory_records ADD COLUMN derived_from TEXT")
except sqlite3.OperationalError:
pass # column already exists
conn.execute(
f"""
CREATE VIRTUAL TABLE IF NOT EXISTS record_embeddings USING vec0(
embedding float[{self.embedding_dim}]
)
"""
)
# Holding table for consolidation proposals: an agent stages a
# merge here after a human approves the grouping conversationally,
# but nothing in memory_records changes until a human commits (or
# discards) it offline via `codegrave --review`. Source records
# stay untouched and searchable in the meantime.
conn.execute(
"""
CREATE TABLE IF NOT EXISTS consolidated (
id INTEGER PRIMARY KEY AUTOINCREMENT,
source_ids TEXT NOT NULL,
entry_type TEXT NOT NULL,
title TEXT NOT NULL,
symptoms TEXT NOT NULL,
root_cause TEXT NOT NULL,
wrong_fixes TEXT,
resolution TEXT NOT NULL,
lesson TEXT NOT NULL,
rationale TEXT NOT NULL,
created_at TEXT NOT NULL
)
"""
)
# WAL mode allows concurrent readers and writers — essential
# when multiple IDE MCP transports each have their own process
# hitting the same .codegrave.db file.
conn.execute("PRAGMA journal_mode=WAL")
conn.commit()
def insert_record(
self,
entry_type: str,
title: str,
symptoms: str,
root_cause: str,
wrong_fixes: str | None,
resolution: str,
lesson: str,
file_path: str | None,
embedding: list[float],
commit_hash: str | None = None,
status: str = "approved",
namespace: str = ".",
approved_by: str | None = None,
issue_url: str | None = None,
pr_url: str | None = None,
validating_test: str | None = None,
confidence: str | None = None,
derived_from: str | None = None,
) -> int:
entry_type = entry_type.strip().upper()
if entry_type not in ENTRY_TYPES:
raise ValueError(
f"Invalid entry_type {entry_type!r}. "
f"Expected one of: {', '.join(sorted(ENTRY_TYPES))}"
)
if len(embedding) != self.embedding_dim:
raise ValueError(
f"Expected embedding of length {self.embedding_dim}, got {len(embedding)}"
)
_check_field_lengths(
{
"title": title,
"symptoms": symptoms,
"root_cause": root_cause,
"wrong_fixes": wrong_fixes,
"resolution": resolution,
"lesson": lesson,
"file_path": file_path,
}
)
commit_hash = _resolve_commit_hash(commit_hash or None)
created_at = datetime.now(timezone.utc).isoformat()
with self._get_connection() as conn:
cur = conn.execute(
"""
INSERT INTO memory_records (
entry_type, title, symptoms, root_cause, wrong_fixes,
resolution, lesson, file_path, commit_hash, created_at,
status, namespace, approved_by, issue_url, pr_url,
validating_test, confidence, derived_from
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
entry_type,
title,
symptoms,
root_cause,
wrong_fixes,
resolution,
lesson,
file_path,
commit_hash,
created_at,
status,
namespace,
approved_by,
issue_url,
pr_url,
validating_test,
confidence,
derived_from,
),
)
record_id = cur.lastrowid
assert record_id is not None
conn.execute(
"""
INSERT INTO record_embeddings (rowid, embedding)
VALUES (?, ?)
""",
(record_id, serialize_float32(embedding)),
)
conn.commit()
return record_id
def search_similar(
self, query_embedding: list[float], limit: int = 15,
namespace: str | None = None,
) -> list[dict[str, Any]]:
if len(query_embedding) != self.embedding_dim:
raise ValueError(
f"Expected embedding of length {self.embedding_dim}, got {len(query_embedding)}"
)
ns = namespace if namespace is not None else "."
ns_clause, ns_params = _namespace_clause(ns)
with self._get_connection() as conn:
rows = conn.execute(
f"""
SELECT
{MEMORY_SELECT_COLUMNS},
record_embeddings.distance AS distance
FROM record_embeddings
JOIN memory_records
ON memory_records.id = record_embeddings.rowid
WHERE record_embeddings.embedding MATCH ?
AND k = ?
AND memory_records.status = 'approved'
AND {ns_clause}
ORDER BY distance
""",
(serialize_float32(query_embedding), limit, *ns_params),
).fetchall()
return [dict(row) for row in rows]
def get_record(self, record_id: int) -> dict[str, Any] | None:
with self._get_connection() as conn:
row = conn.execute(
"SELECT * FROM memory_records WHERE id = ?", (record_id,)
).fetchone()
return dict(row) if row else None
def update_record(
self,
record_id: int,
*,
entry_type: str | None = None,
title: str | None = None,
symptoms: str | None = None,
root_cause: str | None = None,
wrong_fixes: str | None = None,
resolution: str | None = None,
lesson: str | None = None,
file_path: str | None = None,
embedding: list[float] | None = None,
status: str | None = None,
approved_by: str | None = None,
issue_url: str | None = None,
pr_url: str | None = None,
validating_test: str | None = None,
confidence: str | None = None,
) -> bool:
"""Update the given fields on a record; a None argument leaves that field unchanged.
Returns False if no record with *record_id* exists.
"""
if entry_type is not None:
entry_type = entry_type.strip().upper()
if entry_type not in ENTRY_TYPES:
raise ValueError(
f"Invalid entry_type {entry_type!r}. "
f"Expected one of: {', '.join(sorted(ENTRY_TYPES))}"
)
if embedding is not None and len(embedding) != self.embedding_dim:
raise ValueError(
f"Expected embedding of length {self.embedding_dim}, got {len(embedding)}"
)
_check_field_lengths(
{
"title": title,
"symptoms": symptoms,
"root_cause": root_cause,
"wrong_fixes": wrong_fixes,
"resolution": resolution,
"lesson": lesson,
"file_path": file_path,
}
)
fields = {
"entry_type": entry_type,
"title": title,
"symptoms": symptoms,
"root_cause": root_cause,
"wrong_fixes": wrong_fixes,
"resolution": resolution,
"lesson": lesson,
"file_path": file_path,
"status": status,
"approved_by": approved_by,
"issue_url": issue_url,
"pr_url": pr_url,
"validating_test": validating_test,
"confidence": confidence,
}
fields = {k: v for k, v in fields.items() if v is not None}
with self._get_connection() as conn:
if fields:
set_clause = ", ".join(f"{k} = ?" for k in fields)
cur = conn.execute(
f"UPDATE memory_records SET {set_clause} WHERE id = ?",
(*fields.values(), record_id),
)
if cur.rowcount == 0:
return False
else:
row = conn.execute(
"SELECT 1 FROM memory_records WHERE id = ?", (record_id,)
).fetchone()
if row is None:
return False
if embedding is not None:
conn.execute(
"DELETE FROM record_embeddings WHERE rowid = ?", (record_id,)
)
conn.execute(
"INSERT INTO record_embeddings (rowid, embedding) VALUES (?, ?)",
(record_id, serialize_float32(embedding)),
)
conn.commit()
return True
def delete_record(self, record_id: int) -> bool:
"""Delete a record and its embedding. Returns False if it didn't exist."""
with self._get_connection() as conn:
cur = conn.execute("DELETE FROM memory_records WHERE id = ?", (record_id,))
conn.execute("DELETE FROM record_embeddings WHERE rowid = ?", (record_id,))
conn.commit()
return cur.rowcount > 0
def keyword_search(
self, query: str, limit: int = 15, namespace: str | None = None
) -> list[dict[str, Any]]:
"""Simple keyword (LIKE) search across memory text fields.
Used by the ``agent`` embedding provider when no vector model is
available. Splits the query on whitespace and OR-s each term
across title, symptoms, root_cause, resolution, and lesson.
"""
terms = [t.strip() for t in query.split() if len(t.strip()) >= 2]
ns = namespace if namespace is not None else "."
ns_clause, ns_params = _namespace_clause(ns)
if not terms:
with self._get_connection() as conn:
rows = conn.execute(
f"""
SELECT {MEMORY_SELECT_COLUMNS}, NULL AS distance
FROM memory_records
WHERE status = 'approved' AND {ns_clause}
ORDER BY created_at DESC
LIMIT ?
""",
(*ns_params, limit),
).fetchall()
return [dict(row) for row in rows]
clauses: list[str] = []
params: list[str] = []
for term in terms:
pattern = f"%{term}%"
clauses.append(
"(title LIKE ? OR symptoms LIKE ? OR root_cause LIKE ? "
"OR resolution LIKE ? OR lesson LIKE ?)"
)
params.extend([pattern] * 5)
where = "(" + " OR ".join(clauses) + ")"
with self._get_connection() as conn:
rows = conn.execute(
f"""
SELECT {MEMORY_SELECT_COLUMNS}, NULL AS distance
FROM memory_records
WHERE {where} AND status = 'approved' AND {ns_clause}
ORDER BY created_at DESC
LIMIT ?
""",
(*params, *ns_params, limit),
).fetchall()
return [dict(row) for row in rows]
def get_all_records(self) -> list[dict[str, Any]]:
with self._get_connection() as conn:
rows = conn.execute(
"""
SELECT * FROM memory_records
ORDER BY entry_type ASC, created_at DESC
"""
).fetchall()
return [dict(row) for row in rows]
def get_pending_records(self) -> list[dict[str, Any]]:
"""Return records awaiting human review (status='pending')."""
with self._get_connection() as conn:
rows = conn.execute(
"""
SELECT * FROM memory_records
WHERE status = 'pending'
ORDER BY created_at ASC
"""
).fetchall()
return [dict(row) for row in rows]
def insert_consolidation(
self,
source_ids: str,
entry_type: str,
title: str,
symptoms: str,
root_cause: str,
wrong_fixes: str | None,
resolution: str,
lesson: str,
rationale: str,
) -> int:
"""Stage a proposed merge for offline review. Does not touch memory_records."""
entry_type = entry_type.strip().upper()
if entry_type not in ENTRY_TYPES:
raise ValueError(
f"Invalid entry_type {entry_type!r}. "
f"Expected one of: {', '.join(sorted(ENTRY_TYPES))}"
)
ids = [s.strip() for s in source_ids.split(",") if s.strip()]
if len(ids) < 2:
raise ValueError(
f"source_ids must list at least 2 record ids to merge, got {source_ids!r}"
)
_check_field_lengths(
{
"title": title,
"symptoms": symptoms,
"root_cause": root_cause,
"wrong_fixes": wrong_fixes,
"resolution": resolution,
"lesson": lesson,
"rationale": rationale,
}
)
created_at = datetime.now(timezone.utc).isoformat()
with self._get_connection() as conn:
cur = conn.execute(
"""
INSERT INTO consolidated (
source_ids, entry_type, title, symptoms, root_cause,
wrong_fixes, resolution, lesson, rationale, created_at
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
",".join(ids),
entry_type,
title,
symptoms,
root_cause,
wrong_fixes,
resolution,
lesson,
rationale,
created_at,
),
)
proposal_id = cur.lastrowid
assert proposal_id is not None
conn.commit()
return proposal_id
def get_consolidation_proposals(self) -> list[dict[str, Any]]:
with self._get_connection() as conn:
rows = conn.execute(
"SELECT * FROM consolidated ORDER BY created_at ASC"
).fetchall()
return [dict(row) for row in rows]
def delete_consolidation_proposal(self, proposal_id: int) -> bool:
with self._get_connection() as conn:
cur = conn.execute("DELETE FROM consolidated WHERE id = ?", (proposal_id,))
conn.commit()
return cur.rowcount > 0
def _namespace_clause(namespace: str) -> tuple[str, list[str]]:
"""Build a SQL clause that matches *namespace* and all subdirectories.
``"."`` matches everything (no filter). ``"frontend"`` matches
``"frontend"`` exactly and ``"frontend/%"`` (e.g. ``"frontend/components"``).
"""
if namespace == ".":
return ("1 = 1", [])
return (
"(memory_records.namespace = ? OR memory_records.namespace LIKE ?)",
[namespace, namespace + "/%"],
)
_db: DatabaseManager | None = None
def init_db(db_path: str = ".codegrave.db", embedding_dim: int = EMBEDDING_DIM) -> DatabaseManager:
global _db
_db = DatabaseManager(db_path, embedding_dim=embedding_dim)
return _db
def get_db() -> DatabaseManager:
if _db is None:
return init_db()
return _db
def insert_record(
entry_type: str,
title: str,
symptoms: str,
root_cause: str,
wrong_fixes: str | None,
resolution: str,
lesson: str,
file_path: str | None,
embedding: list[float],
commit_hash: str | None = None,
status: str = "approved",
namespace: str = ".",
approved_by: str | None = None,
issue_url: str | None = None,
pr_url: str | None = None,
validating_test: str | None = None,
confidence: str | None = None,
derived_from: str | None = None,
) -> int:
return get_db().insert_record(
entry_type=entry_type,
title=title,
symptoms=symptoms,
root_cause=root_cause,
wrong_fixes=wrong_fixes,
resolution=resolution,
lesson=lesson,
file_path=file_path,
embedding=embedding,
commit_hash=commit_hash,
status=status,
namespace=namespace,
approved_by=approved_by,
issue_url=issue_url,
pr_url=pr_url,
validating_test=validating_test,
confidence=confidence,
derived_from=derived_from,
)
def search_vectors(query_embedding: list[float], limit: int = 15) -> list[dict[str, Any]]:
return get_db().search_similar(query_embedding, limit=limit)
def get_record(record_id: int) -> dict[str, Any] | None:
return get_db().get_record(record_id)
def get_all_records() -> list[dict[str, Any]]:
return get_db().get_all_records()
def get_pending_records() -> list[dict[str, Any]]:
return get_db().get_pending_records()
def update_record(record_id: int, **fields: Any) -> bool:
return get_db().update_record(record_id, **fields)
def delete_record(record_id: int) -> bool:
return get_db().delete_record(record_id)
def insert_consolidation(
source_ids: str,
entry_type: str,
title: str,
symptoms: str,
root_cause: str,
wrong_fixes: str | None,
resolution: str,
lesson: str,
rationale: str,
) -> int:
return get_db().insert_consolidation(
source_ids=source_ids,
entry_type=entry_type,
title=title,
symptoms=symptoms,
root_cause=root_cause,
wrong_fixes=wrong_fixes,
resolution=resolution,
lesson=lesson,
rationale=rationale,
)
def get_consolidation_proposals() -> list[dict[str, Any]]:
return get_db().get_consolidation_proposals()
def delete_consolidation_proposal(proposal_id: int) -> bool:
return get_db().delete_consolidation_proposal(proposal_id)
def embedding_text(
title: str,
symptoms: str,
root_cause: str,
resolution: str,
lesson: str,
) -> str:
"""Concatenate structured fields used to build the memory embedding."""
return "\n".join(
part.strip()
for part in (title, symptoms, root_cause, resolution, lesson)
if part and part.strip()
)