Re-trace only flows that touch *changed_files*. Much faster than full trace. 1. Find flow IDs whose memberships reference nodes in *changed_files*. 2. Collect the entry-point node IDs of those flows before deleting them. 3. Delete only the affected flows and their memberships. 4. R
(
store: GraphStore,
changed_files: list[str],
max_depth: int = 15,
)
| 440 | |
| 441 | |
| 442 | def incremental_trace_flows( |
| 443 | store: GraphStore, |
| 444 | changed_files: list[str], |
| 445 | max_depth: int = 15, |
| 446 | ) -> int: |
| 447 | """Re-trace only flows that touch *changed_files*. Much faster than full trace. |
| 448 | |
| 449 | 1. Find flow IDs whose memberships reference nodes in *changed_files*. |
| 450 | 2. Collect the entry-point node IDs of those flows before deleting them. |
| 451 | 3. Delete only the affected flows and their memberships. |
| 452 | 4. Re-detect entry points, keeping those in *changed_files* **or** whose |
| 453 | node ID was an entry point of a deleted flow. |
| 454 | 5. BFS-trace each relevant entry point via :func:`_trace_single_flow`. |
| 455 | 6. INSERT the new flows (without clearing unrelated flows). |
| 456 | |
| 457 | Returns the number of re-traced flows that were stored. |
| 458 | """ |
| 459 | if not changed_files: |
| 460 | return 0 |
| 461 | |
| 462 | conn = store._conn |
| 463 | changed_file_set = set(changed_files) |
| 464 | |
| 465 | # ------------------------------------------------------------------ |
| 466 | # 1. Find affected flow IDs |
| 467 | # ------------------------------------------------------------------ |
| 468 | placeholders = ",".join("?" * len(changed_files)) |
| 469 | affected_rows = conn.execute( |
| 470 | f"SELECT DISTINCT fm.flow_id FROM flow_memberships fm " # nosec B608 |
| 471 | f"JOIN nodes n ON n.id = fm.node_id " |
| 472 | f"WHERE n.file_path IN ({placeholders})", |
| 473 | changed_files, |
| 474 | ).fetchall() |
| 475 | affected_ids = [r[0] for r in affected_rows] |
| 476 | |
| 477 | # ------------------------------------------------------------------ |
| 478 | # 2. Collect old entry-point node IDs before deletion |
| 479 | # ------------------------------------------------------------------ |
| 480 | entry_point_ids: set[int] = set() |
| 481 | if affected_ids: |
| 482 | ep_placeholders = ",".join("?" * len(affected_ids)) |
| 483 | ep_rows = conn.execute( |
| 484 | f"SELECT entry_point_id FROM flows " # nosec B608 |
| 485 | f"WHERE id IN ({ep_placeholders})", |
| 486 | affected_ids, |
| 487 | ).fetchall() |
| 488 | entry_point_ids = {r[0] for r in ep_rows} |
| 489 | |
| 490 | # ------------------------------------------------------------------ |
| 491 | # 3. Delete affected flows and their memberships |
| 492 | # ------------------------------------------------------------------ |
| 493 | # Wrap in an explicit transaction so a crash mid-loop cannot leave |
| 494 | # orphaned flow_memberships rows pointing at deleted flows. See #258. |
| 495 | if affected_ids: |
| 496 | if conn.in_transaction: |
| 497 | conn.commit() |
| 498 | conn.execute("BEGIN IMMEDIATE") |
| 499 | try: |