Skip to content

fix(api): make the embed token scan linear - #7831

Merged
mmabrouk merged 1 commit into
release/v0.125.2from
fix/api-embed-scan-quadratic
Oct 26, 2026
Merged

mmabrouk merged 1 commit into
release/v0.125.2from
fix/api-embed-scan-quadratic

Conversation

@mmabrouk

Copy link
Copy Markdown
Member

Context

Agent sends waited about 15 s in /api/workflows/revisions/resolve when the agent had large inline skills. While it ran, the API event loop stalled, and Redis 0.5 s socket timeouts fired. On a real revision (670 KB JSON, 7 embedded skills, one skill 1.39 MB), one embed scan took 2.9 s on the revision and 7.8 s on the skills.

The cause is _find_embed_tokens. At every character it ran text[i:].startswith("@ag.embed["). The slice copies the rest of the string each time, so the scan is O(n^2) and holds the event loop.

Changes

_find_embed_tokens now jumps between candidates with text.find("@ag.embed[", i) and does not slice. Matching is unchanged: balanced brackets, incomplete tokens skipped, same order in the returned list. A str.find scan of the same inputs takes under 1 ms.

I checked the rest of embeds/utils.py. _find_snippet_tokens slices only 2 to 3 characters per step, so it is linear. I left it alone.

Tests

  • New TestFindEmbedTokens in api/oss/tests/pytest/unit/embeds/test_utils.py: no token, one token, nested brackets, two tokens, incomplete token at the end, and a 2 MB input that must finish in under 1 s.
  • pytest oss/tests/pytest/unit/embeds: 106 passed.
  • ruff format and ruff check are clean.

@coderabbitai

coderabbitai Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 696f908 and b2ef534.

Files selected for processing: 2
  • api/oss/src/core/embeds/utils.py
  • api/oss/tests/pytest/unit/embeds/test_utils.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 1 remain after this review.

📝 Summary

Summary by CodeRabbit

  • Bug Fixes
    • Embedded content tokens are now detected more reliably, including when they contain nested brackets or appear multiple times in the same text.
    • Incomplete trailing tokens are ignored, and scanning large inputs is more efficient.

The embed-token scanner now searches directly for token prefixes, retains nested-bracket matching, and skips incomplete candidates. Tests cover token detection and scanning a large input.

Changes: Embed Token Scanning

Layer / File(s) Summary
Prefix scanning and validation
api/oss/src/core/embeds/utils.py, api/oss/tests/pytest/unit/embeds/test_utils.py
_find_embed_tokens searches for token prefixes and returns complete tokens with nested brackets. Tests cover absent, multiple, nested, and incomplete tokens, and check runtime on a large input.

Priority: High

Estimated code review effort: 2 (Simple) | ~8 minutes

Merge Risk: Minimal · up to b2ef5

No actionable merge-blocking risk remains.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 60.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 8 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly and concisely states the main change: making the embed token scan linear.
Description check Passed The description directly explains the performance issue, implementation change, preserved behavior, and validation for the embed token scan.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Finishing Touches: 💡 1
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@mmabrouk

Copy link
Copy Markdown
Member Author

🤖 The AI agent says:

I timed the old and new _find_embed_tokens on the real data from the slow agent, on the dev box under load. Both versions return the same tokens.

Input Size Old New
Agent revision JSON 0.67 MB 6,698 ms 0.3 ms
Embedded skills 1.49 MB 32,212 ms 0.6 ms

@mmabrouk mmabrouk added lgtm This PR has been approved by a maintainer merge queue In the release merge queue; an agent is landing it labels Oct 26, 2026
@github-actions

github-actions Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Railway Preview Environment

Item Value
Status stopped
Commit b2ef534b79c7786aff99fde8a85cc74c21e6e702
Manual expiry (UTC) Not granted
CI deadline (UTC) Not granted
Workflow https://github.com/Agenta-AI/agenta/actions/runs/40387078635

PR closed or converted to draft. Environment absence verified; retained storage requires separate provider accounting.

Post /preview as a new comment to request a one-hour preview. Repeating it while active does not extend the hour.
Preview data is disposable. Expiry is enforced by the next successful cleanup sweep, not an exact timer.
Environment deletion stops its compute. Provider-retained storage and permanent services may still be billed.

@mmabrouk
mmabrouk merged commit 13a9d4a into release/v0.125.2 Oct 26, 2026
77 of 78 checks passed
@mmabrouk mmabrouk removed the merge queue In the release merge queue; an agent is landing it label Oct 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm This PR has been approved by a maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant