Skip to content

perf(ai): high-performance AST context packer with binary search truncation #118

Closed

Description

@dan-petty

Problem Statement

DevSecOps review highlighted an O(N^2) algorithm in _prune_tree_to_budget (devops_cli.ai.context_packer) which iteratively executes ast.unparse(tree) and count_tokens inside a while-loop for every statement popped, incurring excessive CPU and latency overhead when pruning large modules.

Proposed Changes

  • Replace linear re-unparsing with binary-search truncation index discovery.
  • Memoize per-statement token weight estimates.
  • Preserve AST semantic integrity while reducing context packing overhead from seconds to <10ms.

Acceptance Criteria

  • Benchmark execution runtime under 10ms for 1,000-line AST trees.
  • Cyclomatic complexity <= 10.
  • Zero syntax truncation errors in tests/test_context_packer.py.

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority/p1-highscope/aitype/refactor

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions