Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
19659b5
feat(plugins): a plugin command's `layer.addUrl` with "bbox" goes to …
elpidiofilho Nov 6, 2026
2715012
test(plugins): a plugin command runs again and answers the window add…
elpidiofilho Nov 8, 2026
d4494f0
Merge main into feat/1330-plugin-window
elpidiofilho Nov 8, 2026
dd95812
fix(plugins): a window asked for the view keeps the view it was asked…
elpidiofilho Nov 9, 2026
c01bb07
fix(plugins): keep the view's box beside a window request, not in it …
elpidiofilho Nov 9, 2026
5d77e0c
fix(plugins): a window of the view joins only one asked in the same v…
elpidiofilho Nov 9, 2026
159e418
fix(plugins): view boxes travel with the waits; join check inside the…
elpidiofilho Nov 9, 2026
8b86400
fix(plugins): keep the view boxes of live waits when no command waits…
elpidiofilho Nov 9, 2026
5df8b26
docs(plugins): a script or model the app runs leaves a window to it t…
elpidiofilho Nov 9, 2026
76b3198
fix(plugins): a command run again gets each view's own window (#1330)
elpidiofilho Nov 9, 2026
6b82009
docs(plugins): UrlWaits keeps its doc comment, above VIEW_ASKED (#1330)
elpidiofilho Nov 9, 2026
11ebd32
Merge remote-tracking branch 'origin/main' into pr-1581
giswqs Nov 9, 2026
86b9113
docs(data-formats): wrap the window paragraph
giswqs Nov 9, 2026
d5ad0f0
Merge main (6cae5e90, #1501) into feat/1330-plugin-window
elpidiofilho Nov 10, 2026
c06e1d0
fix(plugins): a caller's own "viewAsked" is left out of what it asked…
elpidiofilho Nov 10, 2026
00bc1ae
test(plugins): a window of the view being read joins only the same vi…
elpidiofilho Nov 10, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 61 additions & 21 deletions crates/engine/src/cmd/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3361,6 +3361,15 @@ fn add_url_as_given(s: &mut Session, p: &Value) -> CmdResult {
// adding it answered, as often as the run asks for it (the app downloaded and added it once).
// Answering it changes nothing (the layer is its own undo step).
let wanted = s.asked_for(p);
// A window of the view is answered by the one asked in the view the command sees now.
#[cfg(not(target_arch = "wasm32"))]
let wanted = match p.get("bbox") {
Some(b) if s.leave_downloads && is_view(Some(b)) => {
let at = ps(p, "url").map(str::trim).unwrap_or_default();
Session::with_view(&wanted, Some(window_settled(s, p, at, None)?.bbox))
}
_ => wanted,
};
if s.leave_downloads
&& let Some((answer, added)) = s.rerun_answer(&wanted)
{
Expand Down Expand Up @@ -3411,13 +3420,20 @@ fn add_url_as_given(s: &mut Session, p: &Value) -> CmdResult {
}
if window {
window_checks(p, url, scan)?;
// A plugin's command run by the app leaves what would wait to the app, off the frame; a
// window waits for its header and tiles, which the app doesn't take over yet.
// A plugin's command run by the app leaves what would wait to the app, off the frame
// ([`read_cog_window`]); a box that can't be read is refused now, while the command runs.
#[cfg(not(target_arch = "wasm32"))]
if s.leave_downloads {
Comment thread
elpidioassinaturas marked this conversation as resolved.
Resolved
return Err(format!(
"{url}: a window (\"bbox\") waits for its tiles, which a plugin's command can't do in the app yet: run layer.addUrl from the command line or a script, or leave out \"bbox\" to draw the file in place"
));
let settled = window_settled(s, p, url, None)?;
let mut asked = p.clone();
asked["url"] = json!(url);
// `"view"` is the view now: the app reads the window on a later frame, and a command
// run again after a download sees the view it first ran with only while it runs. The
// request keeps the box beside what was asked, by which a run again is answered.
s.window_view = is_view(p.get("bbox")).then_some(settled.bbox);
Comment thread
elpidioassinaturas marked this conversation as resolved.
Resolved

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Low confidence (about 40%): a recorded step for a plugin's "view" window replays the view at replay time.

The box kept here is only used for the read and the rerun match. When the window lands, the completion goes through layer.addDownload with window: {"bbox": "view"}, and the step recorder (crates/ui-egui/src/lib.rs:2507) turns that into layer.addUrl with "bbox": "view". A replay then reads whatever view is current at that moment, not the box the command asked for. The same thing already happens for a ui.addUrl window from #1512. This PR extends it to plugin commands, scripts and models, so it may be worth a note in the recording path or the docs. I haven't checked whether plugin-run steps are recorded at all, so treat this as a question rather than a defect.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Checked: plugin-run deliveries are recorded, as the host command and not as the plugin's (the recording check in permission_policy_plugin_deferred_probe_refuses_before_fallback, pluginpanel.rs: a plugin's layer.addUrl lands as one layer.addUrl step, without id or request). So a plugin's "view" window is recorded as layer.addUrl with "bbox": "view", and a replay reads the view current then.

That is by design and the same as every other step that reads the view: a recording doesn't keep the view ("Panes, dialogs and moving the view aren't recorded", Workspace › Record Task), so a step that asked for the view reads the one the replay is on, as a WCS coverage or a ui.addUrl window from #1512 does. A command that asked for a box ([w, s, e, n]) is recorded with that box and replays it. Keeping the resolved box instead would change what a "view" step means for one command only, so I've left it as is.

🤖 Written with Claude Code

let queued = s.request_url(&asked);
s.window_view = None;
return queued;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
return add_cog_window(s, p, url);
}
Expand Down Expand Up @@ -4137,6 +4153,13 @@ pub struct WindowAsked {
name: Option<String>,
}

#[cfg(all(test, not(target_arch = "wasm32")))]
impl WindowAsked {
pub(crate) fn bbox(&self) -> [f64; 4] {
self.bbox
}
}

/// A window read by [`read_cog_window`], waiting to be added to the project: its raster made and
/// its GeoTIFF written, so that adding it holds no frame for long.
#[cfg(not(target_arch = "wasm32"))]
Expand All @@ -4153,9 +4176,9 @@ pub struct CogWindow {

/// `p`'s box and cell size, read on the frame, which holds the view.
#[cfg(not(target_arch = "wasm32"))]
fn window_settled(s: &Session, p: &Value, url: &str) -> Result<WindowAsked, String> {
fn window_settled(s: &Session, p: &Value, url: &str, seen: Option<[f64; 4]>) -> Result<WindowAsked, String> {
let named = |e: String| if e.starts_with(url) { e } else { format!("{url}: {e}") };
let bbox = window_box(s, p).map_err(named)?;
let bbox = window_box(s, p, seen).map_err(named)?;
let cell = match p.get("cellSize").filter(|v| !v.is_null()) {
None => None,
Some(v) => Some(v.as_f64().filter(|c| c.is_finite() && *c > 0.0).ok_or_else(|| named("cellSize is a size above 0".into()))?),
Expand All @@ -4168,18 +4191,25 @@ fn window_settled(s: &Session, p: &Value, url: &str) -> Result<WindowAsked, Stri
/// to its login, checked as the command checks it. Errors quote the address without its keys.
#[cfg(not(target_arch = "wasm32"))]
pub fn window_asked(s: &Session, p: &Value) -> Result<WindowAsked, String> {
window_asked_in(s, p, None)
}

/// [`window_asked`], `"bbox": "view"` being `seen` when given: the box the view was when a request
/// was queued ([`crate::UrlRequest::view_asked`]).
#[cfg(not(target_arch = "wasm32"))]
pub fn window_asked_in(s: &Session, p: &Value, seen: Option<[f64; 4]>) -> Result<WindowAsked, String> {
let url = ps(p, "url").map(str::trim).filter(|u| !u.is_empty()).ok_or("url is required")?;
let checked = web_address(url)
.and_then(|_| super::raster::as_scan(p))
.and_then(|scan| window_checks(p, url, scan))
.and_then(|()| window_settled(s, p, url));
.and_then(|()| window_settled(s, p, url, seen));
checked.map_err(|e| crate::sources::message_with_address(url, &e))
}

/// `layer.addUrl` with `"bbox"`, for those that wait: read here and added.
#[cfg(not(target_arch = "wasm32"))]
fn add_cog_window(s: &mut Session, p: &Value, url: &str) -> CmdResult {
let read = read_cog_window(&window_settled(s, p, url)?)?;
let read = read_cog_window(&window_settled(s, p, url, None)?)?;
add_window(s, p, read)
}

Expand Down Expand Up @@ -4512,21 +4542,25 @@ pub(crate) fn box_in_file_longitudes(x0: f64, x1: f64, fx0: f64, fx1: f64) -> Re

/// A window's box: `[west, south, east, north]` in degrees, or `"view"`, the map's extent.
#[cfg(not(target_arch = "wasm32"))]
fn window_box(s: &Session, p: &Value) -> Result<[f64; 4], String> {
fn window_box(s: &Session, p: &Value, seen: Option<[f64; 4]>) -> Result<[f64; 4], String> {
const NEED: &str = "bbox is [west, south, east, north] in degrees, or \"view\"";
let [w, so, e, n] = match p.get("bbox") {
Some(Value::String(v)) if v.trim().eq_ignore_ascii_case("view") => {
let e = super::workspace::view_extent(s);
if e.is_empty() {
return Err("the map's extent isn't known; give bbox".into());
}
// A view past 180° (across the antimeridian, or the world more than once) isn't cut
// to fit: what it shows past there would be left out.
if e.min_x < -180.0 || e.max_x > 180.0 {
return Err("the view reaches past 180° of longitude; give bbox".into());
Some(v) if is_view(Some(v)) => match seen {
// The view a request was asked in ([`crate::UrlRequest::view_asked`]).
Some(b) => b,
None => {
let e = super::workspace::view_extent(s);
if e.is_empty() {
return Err("the map's extent isn't known; give bbox".into());
}
// A view past 180° (across the antimeridian, or the world more than once) isn't
// cut to fit: what it shows past there would be left out.
if e.min_x < -180.0 || e.max_x > 180.0 {
return Err("the view reaches past 180° of longitude; give bbox".into());
}
[e.min_x, e.min_y.max(-90.0), e.max_x, e.max_y.min(90.0)]
}
[e.min_x, e.min_y.max(-90.0), e.max_x, e.max_y.min(90.0)]
}
},
Some(Value::Array(a)) => {
let v: Option<Vec<f64>> = a.iter().map(|x| x.as_f64().filter(|x| x.is_finite())).collect();
match v.as_deref() {
Expand All @@ -4540,6 +4574,12 @@ fn window_box(s: &Session, p: &Value) -> Result<[f64; 4], String> {
if w < e && so < n && lon(w) && lon(e) && lat(so) && lat(n) { Ok([w, so, e, n]) } else { Err(NEED.into()) }
}

/// `"bbox": "view"`, in any case or spacing.
#[cfg(not(target_arch = "wasm32"))]
fn is_view(bbox: Option<&Value>) -> bool {
Comment thread
elpidioassinaturas marked this conversation as resolved.
Resolved
bbox.and_then(Value::as_str).is_some_and(|v| v.trim().eq_ignore_ascii_case("view"))
}

/// A GeoTIFF's name from its address: the file name without its extension, or for a dated
/// address (`…/landsat/{date:YYYY}.tif`) what is left of it once the dates are taken out, else its
/// folder's name.
Expand Down
47 changes: 37 additions & 10 deletions crates/engine/src/cmd/plugin_fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,19 @@ pub(crate) struct Retry {
tries: u32,
}

/// The key a window of the view is told apart by when a command run again is answered
/// ([`Session::with_view`]): matched only, never read as a parameter.
const VIEW_ASKED: &str = "viewAsked";

/// The downloads plugin commands wait for (#1202): those the app is making, with what was
/// asked and since when, and those added, with what adding them answered, until the command
/// waiting for them runs again.
#[derive(Clone, Default)]
pub(crate) struct UrlWaits {
/// The view's boxes of the window requests asked with `"bbox": "view"`, by their numbers: a
/// request for the view joins one being made only when it was asked in the same view. Kept
/// here, they go aside with a project and back on a rollback as the waits do.
pub(crate) views: std::collections::BTreeMap<u64, [f64; 4]>,
owners: std::collections::BTreeMap<u64, crate::policy::Continuation>,
in_flight: Vec<(u64, Value, Instant)>,
landed: Vec<(u64, Value, CmdResult, Added)>,
Expand Down Expand Up @@ -616,11 +624,18 @@ impl UrlWaits {
}
}

/// Keeps the views of the requests still being made or landed only.
fn prune_views(&mut self) {
let live: Vec<u64> = self.in_flight.iter().map(|(n, ..)| *n).chain(self.landed.iter().map(|(n, ..)| *n)).collect();
self.views.retain(|n, _| live.contains(n));
}

/// Only what `waiting` still waits for is kept.
fn keep_only(&mut self, waiting: &[Retry]) {
let wanted = |n: &u64| waiting.iter().any(|r| r.requests.contains(n));
self.in_flight.retain(|(n, ..)| wanted(n));
self.landed.retain(|(n, ..)| wanted(n));
self.views.retain(|n, _| wanted(n));
let retained: Vec<u64> = self
.in_flight
.iter()
Expand Down Expand Up @@ -667,10 +682,13 @@ impl Session {
/// eventual `layer.addDownload`; a command run again asks for the same.
pub(crate) fn asked_for(&self, p: &Value) -> Value {
let mut asked = without_request(p);
if self.web_logins.is_anonymous()
&& let Some(o) = asked.as_object_mut()
{
o.insert("anonymous".into(), json!(true));
if let Some(o) = asked.as_object_mut() {
// Only the session tells windows of the view apart ([`Session::with_view`]): a caller's
// own `viewAsked` is left out.
o.remove(VIEW_ASKED);
if self.web_logins.is_anonymous() {
o.insert("anonymous".into(), json!(true));
}
}
asked
}
Expand Down Expand Up @@ -733,6 +751,17 @@ impl Session {
Ok(None)
}

/// What a window of the view was asked as, for a command run again to be answered by: `asked`
/// with the box the view was then (`UrlWaits::views`), so two windows of the view asked in
/// two views are told apart. Others are `asked` as it is.
pub(crate) fn with_view(asked: &Value, view: Option<[f64; 4]>) -> Value {
let mut asked = asked.clone();
if let (Some(v), Some(o)) = (view, asked.as_object_mut()) {
o.insert(VIEW_ASKED.into(), json!(v));
}
asked
}

/// Whether a command run again has an answer for `wanted`.
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn rerun_has(&self, wanted: &Value) -> bool {
Expand Down Expand Up @@ -886,6 +915,7 @@ impl Session {
fn poll_plugin_retries_at(&mut self, now: Instant) -> Vec<(String, CmdResult)> {
self.url_waits.documents_in();
if self.plugin_retries.is_empty() {
self.url_waits.prune_views();
return Vec::new();
}
// Said here too: the command may go on without it, and what it answers goes nowhere.
Expand Down Expand Up @@ -917,12 +947,9 @@ impl Session {
// runs, and the ones it waited for. These stay in `landed` until no command waits
// for them (`keep_only`, below), so another command waiting for the same download
// gets the answer too.
let mine = self
.url_waits
.landed
.iter()
.filter(|(n, ..)| r.requests.contains(n))
.map(|(_, asked, answer, added)| (asked.clone(), answer.clone(), added.clone()));
let mine = self.url_waits.landed.iter().filter(|(n, ..)| r.requests.contains(n)).map(|(n, asked, answer, added)| {
(Self::with_view(asked, self.url_waits.views.get(n).copied()), answer.clone(), added.clone())
});
self.rerun_landed = r.answered.iter().cloned().chain(mine).collect();
self.rerun_said = r.said;
self.retry_tries = r.tries;
Expand Down
10 changes: 10 additions & 0 deletions crates/engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,18 @@ pub struct UrlRequest {
key: u64,
params: Value,
authority: std::sync::Arc<policy::Continuation>,
/// For a window asked with `"bbox": "view"`: the box the view was then, kept apart from its
/// parameters, which a caller gives and a command run again is matched by.
view: Option<[f64; 4]>,
}
impl UrlRequest {
pub fn params(&self) -> &Value {
&self.params
}
/// The box `"bbox": "view"` was when the command asked (`cmd::layer::window_asked_in`).
pub fn view_asked(&self) -> Option<[f64; 4]> {
self.view
}
}

pub type CmdResult = Result<Value, String>;
Expand Down Expand Up @@ -838,6 +845,8 @@ pub struct Session {
/// Downloads a command asked for where nothing may wait (the browser): the parameters of
/// `layer.addUrl` calls, for the front end to fetch and add (see [`Session::take_url_requests`]).
pub(crate) url_requests: Vec<UrlRequest>,
/// The view's box a window request being queued keeps ([`UrlRequest::view_asked`]).
pub(crate) window_view: Option<[f64; 4]>,
/// Set by [`Session::request_url`] for [`Session::guarded`]: the command running changed
/// nothing, it only left a request (a plugin's own answer can't claim that).
request_only: bool,
Expand Down Expand Up @@ -1176,6 +1185,7 @@ impl Session {
#[cfg(not(target_arch = "wasm32"))]
landed_windows: Vec::new(),
url_requests: Vec::new(),
window_view: None,
file_outputs: Vec::new(),
#[cfg(test)]
queue_gp_files: false,
Expand Down
55 changes: 51 additions & 4 deletions crates/engine/src/policy/downloads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ use std::{
},
};

/// What a window the app read off the frame says it read (`"window"`, #1330) is what its request
/// asked for: a box, and its cell size and `"cog"` if the request gave them, nothing else.
fn asked_window(asked: &Value, window: &Value) -> bool {
const KEYS: [&str; 3] = ["bbox", "cellSize", "cog"];
let given = |k: &str| asked.get(k).filter(|v| !v.is_null());
given("bbox").is_some()
&& window.as_object().is_some_and(|w| w.keys().all(|k| KEYS.contains(&k.as_str())) && KEYS.iter().all(|k| given(k) == w.get(*k)))
}

pub(super) struct Claim {
pub c: Continuation,
pub params: Value,
Expand Down Expand Up @@ -53,7 +62,7 @@ impl Session {
requests: self
.url_requests
.iter()
.map(|q| UrlRequest { key: q.key, params: q.params.clone(), authority: q.authority.clone() })
.map(|q| UrlRequest { key: q.key, params: q.params.clone(), authority: q.authority.clone(), view: q.view })
.collect(),
queued: self.policy.work.queued.clone(),
}
Expand Down Expand Up @@ -234,6 +243,7 @@ impl Session {
.iter()
.find(|q| {
crate::cmd::plugins::fetch::same_request(&q.params, &asked)
&& q.view == self.window_view
Comment thread
elpidioassinaturas marked this conversation as resolved.
Resolved
&& self.same_url_caller(&q.authority, &c)
&& self.check_continuation(&q.authority).is_ok()
})
Expand All @@ -251,8 +261,13 @@ impl Session {
.call_requests
.as_ref()
.and_then(|_| {
// A window of the view is the one asked in the same view: another is read again.
self.url_waits.in_flight(&asked, |n| {
self.url_waits.owner(n).is_some_and(|owner| self.same_url_caller(owner, &c) && self.check_continuation(owner).is_ok())
self.url_waits.views.get(&n).copied() == self.window_view
&& self
.url_waits
.owner(n)
.is_some_and(|owner| self.same_url_caller(owner, &c) && self.check_continuation(owner).is_ok())
})
})
.filter(|n| !self.url_waits.taken_away(*n, &self.project));
Expand All @@ -279,7 +294,11 @@ impl Session {
self.next_pending = self.next_pending.wrapping_add(1);
let key = self.next_pending;
self.policy.work.queued.insert(key, Queued { c: Arc::downgrade(&c), import, consumed: Arc::new(AtomicBool::new(false)) });
self.url_requests.push(UrlRequest { key, params, authority: c });
let view = self.window_view.take();
if let Some(v) = view {
self.url_waits.views.insert(number, v);
}
self.url_requests.push(UrlRequest { key, params, authority: c, view });
Comment thread
elpidioassinaturas marked this conversation as resolved.
Resolved
}
if !import
&& let Some(asked_now) = self.call_requests.as_mut()
Expand Down Expand Up @@ -358,6 +377,11 @@ impl Session {
}
let c = (*c).clone();
let id = self.download_started();
// The view a window of the view was asked in, by its number, now that the request is made
// (the command's own waits may have been restored since it was queued).
if let (Some(v), Some(n)) = (request.view, request.params.get("request").and_then(Value::as_u64)) {
self.url_waits.views.insert(n, v);
}
self.policy.work.claims.insert(id, Claim { c, params: request.params.clone(), live: Arc::new(AtomicBool::new(true)), import });
Ok(id)
}
Expand Down Expand Up @@ -446,7 +470,8 @@ impl Session {
|| (key == "whole" && value.as_bool() == Some(true))
|| (key == "zoom"
&& crate::cmd::bool_value(value) == Some(false)
&& claim.params.get(key).and_then(crate::cmd::bool_value) == Some(true));
&& claim.params.get(key).and_then(crate::cmd::bool_value) == Some(true))
|| (key == "window" && asked_window(&claim.params, value));
if !refinement && claim.params.get(key) != Some(value) {
return Err("the download's address or parameters changed since it was requested".into());
}
Expand Down Expand Up @@ -505,3 +530,25 @@ impl Session {
self.policy.work.claims.remove(&id);
}
}

#[cfg(test)]
mod tests {
use super::asked_window;
use serde_json::json;

/// A window the app read completes its request only as what was asked (#1330): the same box,
/// cell size and `"cog"`, and nothing more; a request with no box takes no window.
#[test]
fn a_landed_window_completes_only_what_was_asked() {
let asked = json!({"url": "https://x.org/a.tif", "bbox": [10.25, 45.5, 10.5, 45.75], "cellSize": 30, "name": "W"});
assert!(asked_window(&asked, &json!({"bbox": [10.25, 45.5, 10.5, 45.75], "cellSize": 30})));
assert!(!asked_window(&asked, &json!({"bbox": [0, 0, 1, 1], "cellSize": 30})), "another box");
assert!(!asked_window(&asked, &json!({"bbox": [10.25, 45.5, 10.5, 45.75]})), "its cell size left out");
assert!(!asked_window(&asked, &json!({"bbox": [10.25, 45.5, 10.5, 45.75], "cellSize": 30, "cog": true})), "cog not asked");
assert!(!asked_window(&asked, &json!({"bbox": [10.25, 45.5, 10.5, 45.75], "cellSize": 30, "url": "https://y.org"})), "another key");
assert!(!asked_window(&asked, &json!("view")), "not an object");
let view = json!({"url": "https://x.org/a.tif", "bbox": "view", "cellSize": null});
assert!(asked_window(&view, &json!({"bbox": "view"})), "a null cell size is none given");
assert!(!asked_window(&json!({"url": "https://x.org/a.tif"}), &json!({})), "no box asked");
}
}
Loading
Loading