Skip to content

perf(web/context): concurrent @-reference expansion + web_extract truncate-store robustness - #55207

Merged
teknium1 merged 2 commits into
NousResearch:mainfrom
kshitijk4poor:feat/web-extract-and-context-perf
Jun 30, 2026
Merged

perf(web/context): concurrent @-reference expansion + web_extract truncate-store robustness#55207
teknium1 merged 2 commits into
NousResearch:mainfrom
kshitijk4poor:feat/web-extract-and-context-perf

Conversation

@kshitijk4poor

Copy link
Copy Markdown
Collaborator

Summary

Two independent, provider-agnostic web/context optimizations + robustness fixes,
both verified with tests. They sit at the core agent layer (not in any
provider plugin), so they help every web backend (exa/tavily/firecrawl/parallel)
equally.


Part 1 — Concurrent @-reference expansion (agent/context_references.py)

A message with multiple @-references (especially several @url: refs, each a
full web_extract round-trip) expanded them in a serial for ref in refs: await
loop — N independent fetches paid back-to-back.

Switched to asyncio.gather over the independent _expand_reference calls,
reassembling warnings/blocks in original positional order so output is
byte-identical to the serial path. The token-budget check is unchanged (it runs
once, after all refs expand).

  • Generic + provider-agnostic (above the provider dispatch layer).
  • RED/GREEN: 3 @url: refs @ 0.2s each = 0.60s serial → ~0.20s concurrent.

Part 2 — web_extract truncate-store robustness (tools/web_tools.py)

Two gaps in the truncate-store path (from #54843):

  1. Unbounded stored file. _store_full_text wrote the full clean page to
    cache/web via path.write_text(content) with no upper bound — a multi-MB
    page meant unbounded per-extract disk writes. Now capped at
    MAX_STORED_TEXT_CHARS (2MB, the pre-truncate-store refusal ceiling), with a
    marker appended when capped. The model only ever sees char_limit regardless.

  2. Dead offset=<line> placeholder. The truncation footer told the model
    read_file path="…" offset=<line> — a literal placeholder it had to guess.
    Now computes the real starting line of the omitted middle (head line count

      1. so the first read_file lands in the gap.

Why one PR

Both are small, core-layer web/context perf+robustness changes discovered in the
same pass; kept together for review convenience. They touch disjoint files
(context_references.py vs web_tools.py) and can be reverted independently by
commit.

Verification

  • tests/agent/test_context_refs_concurrent.py (new) + test_context_references.py
  • tests/tools/test_web_extract_robustness.py (new) + test_web_tools_truncate.py + test_web_tools.py
  • 32 passed, no regressions. Diff: +149/−7 across 4 files (2 source, 2 new test files).

Multiple @-references in one message (esp. @url: refs, each a full
web_extract round-trip) were expanded in a serial `for ref in refs: await`
loop. Switch to asyncio.gather over the independent _expand_reference calls,
reassembling warnings/blocks in original positional order so output is
byte-identical to the serial path; the token-budget check is unchanged.

Generic + provider-agnostic: helps every web backend equally (exa/tavily/
firecrawl/parallel) since it's above the provider layer. RED/GREEN test:
3 url refs @ 0.2s each = 0.60s serial -> ~0.20s concurrent.
…le offset

Two robustness gaps from the NousResearch#54843 truncate-store path:

- _store_full_text wrote the full clean page to cache/web with no upper
  bound (path.write_text(content)); a multi-MB page → unbounded per-extract
  disk write. Cap at MAX_STORED_TEXT_CHARS (2MB, the pre-truncate-store
  refusal ceiling) with a marker when capped.
- The truncation footer told the model 'read_file ... offset=<line>' — a
  literal placeholder it had to guess. Compute the real starting line of the
  omitted middle (head line count + 1) so the first read_file lands in the gap.
@alt-glitch alt-glitch added type/perf Performance improvement or optimization P3 Low — cosmetic, nice to have comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint tool/web Web search and extraction labels Jun 29, 2026

@tonydwb tonydwb left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Clean performance improvement for concurrent @-reference expansion and web_extract truncation. 4 files, well-scoped optimization with proper test coverage.

@teknium1
teknium1 merged commit c9269fb into NousResearch:main Jun 30, 2026
29 of 30 checks passed
@kshitijk4poor
kshitijk4poor deleted the feat/web-extract-and-context-perf branch August 5, 2026 07:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint P3 Low — cosmetic, nice to have tool/web Web search and extraction type/perf Performance improvement or optimization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants