-
Notifications
You must be signed in to change notification settings - Fork 3.6k
fix(web): retry JS-shell 200s past every cache and name the escalation #5936
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+651
−66
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,7 +14,8 @@ use super::spec::{ | |
| }; | ||
| use super::web::extract::{DocumentKind, ExtractedDocument, decode_response_body}; | ||
| use super::web::fetch::{ | ||
| DEFAULT_MAX_BYTES, DEFAULT_TIMEOUT, FetchOptions, HARD_MAX_BYTES, HARD_MAX_TIMEOUT, fetch, | ||
| DEFAULT_MAX_BYTES, DEFAULT_TIMEOUT, FetchAttempt, FetchOptions, HARD_MAX_BYTES, | ||
| HARD_MAX_TIMEOUT, fetch_readable, | ||
| }; | ||
| use super::web::overflow::bound_text as bound_web_text; | ||
| #[cfg(test)] | ||
|
|
@@ -73,6 +74,10 @@ struct FetchReceipt { | |
| cache_hit: bool, | ||
| retries: usize, | ||
| redirects: usize, | ||
| /// Every request this fetch made, in order: session `cache_hit`, whether | ||
| /// the attempt bypassed caches, which one produced content, and the | ||
| /// response headers that explain the cache state (#5904). | ||
| attempts: Vec<FetchAttempt>, | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
|
|
@@ -165,38 +170,56 @@ impl ToolSpec for FetchUrlTool { | |
| let timeout_ms = optional_u64(&input, "timeout_ms", DEFAULT_TIMEOUT.as_millis() as u64)? | ||
| .clamp(1, HARD_MAX_TIMEOUT.as_millis() as u64); | ||
| let requested_fields = parse_fields(&input)?; | ||
| let fetched = fetch( | ||
| // Bound as a reference so the `Fn` extractor can run twice without | ||
| // moving the field list into its first future. | ||
| let requested_fields = &requested_fields; | ||
| // A 2xx that extracts to nothing is re-fetched once past every cache | ||
| // before it becomes an error; the receipt keeps both attempts (#5904). | ||
| let readable = fetch_readable( | ||
| &url, | ||
| &FetchOptions::new(Duration::from_millis(timeout_ms), max_bytes, FETCH_ACCEPT), | ||
| context, | ||
| "fetch_url", | ||
| |fetched: super::web::fetch::FetchedPayload| { | ||
| Box::pin(async move { | ||
| let is_success = (200..300).contains(&fetched.status); | ||
| let body_text = if requested_fields.is_empty() { | ||
| None | ||
| } else { | ||
| // JSON is never allowed to discover an encoding from body markup. | ||
| Some(decode_response_body( | ||
| &fetched.bytes, | ||
| Some(&fetched.content_type), | ||
| false, | ||
| )?) | ||
| }; | ||
| let fields = match body_text.as_deref() { | ||
| Some(body) => { | ||
| project_json_fields(body, &fetched.content_type, requested_fields)? | ||
| } | ||
| None => None, | ||
| }; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Run extraction before field projection so JS-shell detection always wins, or ensure project_json_fields cannot return an error for HTML shells and add a regression test with non-empty requested_fields. |
||
| let extracted = extract_fetched_document( | ||
| format, | ||
| &fetched.url, | ||
| &fetched.content_type, | ||
| &fetched.bytes, | ||
| is_success, | ||
| body_text.as_deref(), | ||
| PdfTextCommand::system(context.cancel_token.as_ref()), | ||
| ) | ||
| .await?; | ||
| Ok((extracted, fields)) | ||
| }) | ||
| }, | ||
| ) | ||
| .await?; | ||
| let super::web::fetch::ReadableFetch { | ||
| payload: fetched, | ||
| document: (extracted, fields), | ||
| attempts, | ||
| } = readable; | ||
| let is_success = (200..300).contains(&fetched.status); | ||
| let body_text = if requested_fields.is_empty() { | ||
| None | ||
| } else { | ||
| // JSON is never allowed to discover an encoding from body markup. | ||
| Some(decode_response_body( | ||
| &fetched.bytes, | ||
| Some(&fetched.content_type), | ||
| false, | ||
| )?) | ||
| }; | ||
| let fields = match body_text.as_deref() { | ||
| Some(body) => project_json_fields(body, &fetched.content_type, &requested_fields)?, | ||
| None => None, | ||
| }; | ||
| let extracted = extract_fetched_document( | ||
| format, | ||
| &fetched.url, | ||
| &fetched.content_type, | ||
| &fetched.bytes, | ||
| is_success, | ||
| body_text.as_deref(), | ||
| PdfTextCommand::system(context.cancel_token.as_ref()), | ||
| ) | ||
| .await?; | ||
|
|
||
| let citation_title = extracted.title.clone(); | ||
| let (processed, artifact_write) = render_extracted( | ||
|
|
@@ -229,6 +252,7 @@ impl ToolSpec for FetchUrlTool { | |
| cache_hit: fetched.cache_hit, | ||
| retries: fetched.retries, | ||
| redirects: fetched.redirects, | ||
| attempts, | ||
| }, | ||
| artifact, | ||
| fields, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[WARNING] Field projection may mask the JS-shell marker and bypass the retry
In the fetch_url extraction closure, project_json_fields is called before extract_fetched_document. If a non-empty requested_fields input causes project_json_fields to fail on a text/html shell body, fetch_readable sees a non-JS-shell error, is_js_shell_error returns false, and the cache-busting retry never runs. The new tests only exercise the no-fields path, so this interaction is unverified and likely still broken for field requests.