Skip to content
This repository was archived by the owner on Jul 27, 2026. It is now read-only.

fix: negatively cache failed fetches and cap no_sections content - #43

Open
AnnasMazhar wants to merge 2 commits into
strands-agents:mainfrom
AnnasMazhar:fix/negative-cache-no-sections-cap
Open

AnnasMazhar wants to merge 2 commits into
strands-agents:mainfrom
AnnasMazhar:fix/negative-cache-no-sections-cap

Conversation

@AnnasMazhar

Copy link
Copy Markdown

Two fixes for strands-mcp fetch/cache/hydration path.

Negative caching for failed fetches (#3328):

  • _FAILED sentinel distinguishes not-fetched from failed
  • Short-circuit known failures instead of re-fetching
  • Log exceptions via logger.exception()

Cap no_sections fallback content (#3327):

  • Truncate structureless documents at SMALL_DOC_THRESHOLD (8KB)
  • Append truncation notice
  • Updated test to verify bounded content

All 47 existing tests pass.

Two related fixes for strands-mcp:

Negative caching for failed fetches (#3328):
- Add _FAILED sentinel to distinguish not fetched from failed
- Short-circuit known-failed URLs instead of re-fetching every time
- Log exceptions with logger.exception() instead of silent swallow

Cap no_sections fallback content (#3327):
- Truncate structureless documents at SMALL_DOC_THRESHOLD (8KB)
- Append truncation notice to bounded content
- Update test to verify content is bounded

Fixes #3328, #3327
@AnnasMazhar
AnnasMazhar requested a review from a team as a code owner July 23, 2026 22:24
@mkmeral

mkmeral commented Jul 24, 2026

Copy link
Copy Markdown
Member

@strandly-the-agent review

@strandly-the-agent strandly-the-agent 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.

Verdict: changes requested — negative caching currently crashes search_docs and permanently caches transient failures.

✅ Reviewed exact head d7cfd013 against merge-base bf2ec1f.
✅ pytest tests/ -q: 47 passed · hatch run lint, ruff check ., and git diff --check: passed.
🔴 Two cold repros confirmed both blockers: sentinel dereference and no transient recovery.
⚠️ CI has one successful lint run plus one cancelled duplicate; the rollup reports failure.

Review breakdown
  • Correctness/adversarial: _FAILED leaks through the raw cache and crashes both the first and later search_docs calls after failed hydration. Negative entries also never expire, contrary to the linked issue's short-TTL requirement.
  • Test quality: retained the cross-file failure and TTL-boundary coverage gaps; production truncation passed ASCII-boundary and split-multibyte probes.
  • LLM context: suppressed the imperfect document_small metadata because the content itself carries an explicit truncation notice.
  • Readiness: configured lint and unit tests pass. Direct ruff format --check . would adjust slice spacing at server.py:170; omitted as style.

No additional questions.

except Exception:
logger.exception("Failed to fetch page: %s", url)
_URL_CACHE[url] = _FAILED # negatively cache the failure
return None

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔴 [block] Keep the failure sentinel private from cache consumers.

Storing _FAILED in the dictionary returned by get_url_cache() makes search_docs treat this opaque object as a Page. On the first failed hydration it reaches make_snippet; subsequent searches fail earlier while evaluating cached.content.

Cold repro, repeated twice:

search_call_1_error=AttributeError: 'object' object has no attribute 'content'
search_call_2_error=AttributeError: 'object' object has no attribute 'content'
search_backend_calls=1

Please normalize failed entries to None before exposing them—or otherwise keep the sentinel behind a cache accessor—and use ensure_page()'s return value when building snippets. A cross-file regression should assert that failed hydration still returns the ranked title fallback and performs only one backend call.

cached = _URL_CACHE.get(url)
# Short-circuit on known failures — don't re-fetch
if cached is _FAILED:
return None

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔴 [block] Expire negative-cache entries so transient failures can recover.

This branch has no timestamp, expiry, or invalidation path, so the first timeout permanently disables that URL. In a cold deterministic probe with backend effects [TimeoutError, success], two ensure_page() calls both returned None and the backend call count remained 1; repeating the probe produced the same result.

That conflicts with linked issue harness-sdk#3328, which explicitly requests a short TTL. Please use an expiring negative-cache entry and add a boundary test proving calls are suppressed before expiry and retried at expiry.

Two fixes based on code review by strandly-the-agent:

1. **Sentinel dereference crash** — _FailedEntry in url_cache has no .content
   attribute, causing AttributeError in search_docs hydration loop and
   snippet building. Fix: replace bare sentinel with OOP _FailedEntry class,
   guard call sites with hasattr checks.

2. **No TTL on failed entries** — negative cache entries never expired,
   permanently preventing re-fetch of transient failures. Fix: add
   _FailedEntry with 5-minute TTL (time.monotonic based). Expired entries
   are cleared and retried on the next ensure_page call.

Tests: 47 existing + 2 new (failed entry crash guard, snippet fallback).
All 49 pass.
@AnnasMazhar

Copy link
Copy Markdown
Author

Addressed both review points — fixes pushed to a948d55 on the same branch.

1. Sentinel dereference crash: Replaced bare _FAILED = object() with a proper _FailedEntry class. The hydration loop in search_docs now only calls ensure_page() for None entries (not_yet_fetched), skipping _FailedEntry. The snippet builder guards with hasattr(page, "content"), falling back to display_title for non-Page entries.

2. TTL expiry for negative cache: _FailedEntry records a time.monotonic() timestamp and exposes an expired property. Default TTL is 300 seconds (5 minutes). Expired entries are cleared from the cache and re-fetched on the next ensure_page() call.

Tests: 2 new test methods in TestSearchDocsWithNegativeCache — crash assertion and snippet fallback assertion. All 49 tests pass.

@AnnasMazhar

Copy link
Copy Markdown
Author

Review feedback addressed in commit a948d55:

  1. Sentinel dereference crash — replaced bare sentinel with OOP class, added guards at all call sites.
  2. No TTL on failed entries — carries a 5-minute TTL (time.monotonic based). Expired entries are cleared and retried on the next call.

All 49 tests pass. Could use a re-review when strandly-the-agent gets a chance.

@mkmeral

mkmeral commented Jul 27, 2026

Copy link
Copy Markdown
Member

@AnnasMazhar we have moved the MCP server to our monorepo (strands-agents/harness-sdk), can you also move the PR there? I didn't realize at first that this was the old repo

I will make sure this repo is archived so it does not allow new PRs

This branch has not been deployed

No deployments
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants