Skip to content

Parse Markdown sections without repeated suffix list copies #1890

Closed

Description

@phernandez

Problem

Follow-up from the reviewed CodeRabbit deep scan (K2). This ticket tracks one distinct fix; duplicate scan rows are consolidated here.

Reviewed evidence and scope

  • K2 (scan ID f1130f1a-fdd1-47fe-9b41-0cdb1ab1fd96, review A, Medium): headings[position+1:] copies suffix lists even when lookahead immediately breaks. Linear-time comment is incorrect; use indices/stack.
    Source at current baseline: src/basic_memory/markdown/sections.py:190.

Fix and validation

  • Parse Markdown sections without repeated suffix list copies.
  • Recheck current callers and supported behavior before choosing the smallest complete fix.
  • Add a bounded regression at the affected public or service boundary; demonstrate it fails without the fix.
  • Preserve legitimate inputs and tenant/project boundaries. Run focused tests plus repository static/type checks.

The scan/review establishes the code-level concern described above; it does not establish production exploitation. Conditional branches and local/tooling-only exposure must retain that scope.

Activity

  1. added
    security
    on Oct 28, 2026
  2. claude commented on Oct 28, 2026

    @claude
    Contributor

    Claude finished @phernandez's task in 22s —— View job


    Triage

    Type: enhancement (performance/robustness hardening; the issue carries the security label, but the scan only establishes a code-level concern, not exploitation)
    Priority: Medium
    Component: Markdown parsing (markdown/sections.py), which also feeds the note-read API's section= slicing. No Cloud-specific behavior is involved.
    Complexity: Simple

    Findings

    • Confirmed at src/basic_memory/markdown/sections.py:190. For each heading, headings[position + 1 :] copies the suffix list before the loop breaks. That makes the cost O(n²) in the heading count for a large note, even when the very next heading ends the section.
    • The comment at lines 187-188 claims the look-ahead is linear because of the level cap of 6. That claim is wrong, because the slice copy is not bounded by the cap.
    • A small fix looks sufficient. Iterate with itertools.islice or an index, or use the stack that already exists to close open sections when a heading of the same or higher level arrives. The stack approach gives an O(n) single pass. Update the incorrect comment as part of the change.
    • A regression test with many headings should assert the output stays identical and the work stays bounded. For example, count slice or iteration steps, or use a generous time bound on a large input. Existing section tests should cover behavior preservation.

    I found no duplicate issues. I didn't make any code changes, because this run was triage only.

  3. added
    enhancement
    on Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementsecurity

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions