mirror of
https://github.com/storytold/wordcraft.git
synced 2026-11-11 16:31:54 -06:00
Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cb178bb18d | ||
|
|
77780d19db | ||
|
|
92e0e67f3a | ||
|
|
a34c94e4c1 | ||
|
|
a42c9eaab3 |
File diff suppressed because one or more lines are too long
@@ -108,8 +108,8 @@ pub fn formats() -> Vec<String> {
|
||||
|
||||
/// The picture on the clipboard: copied files, else image pixels, else (Wayland) an encoded
|
||||
/// picture in a format arboard doesn't decode. A picture that is there but can't be read says
|
||||
/// why (#833). The UI checks the sizes before inserting anything; on Windows they are checked
|
||||
/// from the header before anything is decoded (#3623).
|
||||
/// why (#833). The UI checks the sizes before inserting anything; on Windows and Wayland they are
|
||||
/// checked from the header before anything is decoded (#3623).
|
||||
pub fn picture() -> Option<ClipboardPicture> {
|
||||
let mut clipboard = match arboard::Clipboard::new() {
|
||||
Ok(c) => c,
|
||||
@@ -136,6 +136,10 @@ fn decoded_picture(_: &mut arboard::Clipboard) -> Option<ClipboardPicture> {
|
||||
/// The picture arboard decodes from the clipboard, else an encoded one read past it.
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
fn decoded_picture(clipboard: &mut arboard::Clipboard) -> Option<ClipboardPicture> {
|
||||
#[cfg(all(unix, not(any(target_os = "macos", target_os = "android", target_os = "emscripten"))))]
|
||||
if let Some(picture) = platform::picture() {
|
||||
return Some(picture);
|
||||
}
|
||||
match clipboard.get_image() {
|
||||
Ok(image) => Some(ClipboardPicture::Pixels { width: image.width, height: image.height, rgba: image.bytes.into_owned() }),
|
||||
Err(arboard::Error::ContentNotAvailable) => platform::encoded_picture().map(ClipboardPicture::Encoded),
|
||||
@@ -160,6 +164,7 @@ mod platform {
|
||||
use std::io::Read;
|
||||
|
||||
use wl_clipboard_rs::paste::{ClipboardType, MimeType, Seat, get_contents, get_mime_types_ordered};
|
||||
use wordcraft_ui_egui::paste_picture::{self, ClipboardPicture};
|
||||
|
||||
fn wayland() -> bool {
|
||||
std::env::var_os("WAYLAND_DISPLAY").is_some()
|
||||
@@ -364,6 +369,22 @@ mod platform {
|
||||
assert!(complete(0, 0, 0));
|
||||
}
|
||||
}
|
||||
|
||||
/// The Wayland clipboard's picture, read encoded under the paste's byte cap and refused from
|
||||
/// its header when a paste can't take its size, before anything decodes it. arboard decodes
|
||||
/// and converts the PNG to RGBA with no size limits. `None` when Wayland offers no picture
|
||||
/// (or isn't reachable): arboard is asked then, which also covers X11.
|
||||
pub fn picture() -> Option<ClipboardPicture> {
|
||||
if !wayland() {
|
||||
return None;
|
||||
}
|
||||
let offered = formats();
|
||||
let mime = super::PICTURE_MIMES.into_iter().find(|m| offered.iter().any(|o| o == m))?;
|
||||
Some(match read(mime, wordcraft_ui_egui::paste_picture::MAX_FILE_BYTES) {
|
||||
Some(bytes) => wordcraft_ui_egui::paste_picture::encoded_picture(bytes),
|
||||
None => ClipboardPicture::Unreadable("the clipboard picture is larger than 200 MB, or couldn't be read".into()),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
|
||||
@@ -136,7 +136,34 @@ impl Server {
|
||||
if batch.is_empty() {
|
||||
Some(error(Value::Null, INVALID_REQUEST, "empty batch"))
|
||||
} else {
|
||||
let replies: Vec<Value> = batch.into_iter().filter_map(|m| self.handle(m)).collect();
|
||||
// Held to the same output budget as the `batch` tool: the rest of the batch is
|
||||
// refused once the replies pass it.
|
||||
let mut replies: Vec<Value> = Vec::new();
|
||||
let mut size = 0usize;
|
||||
for m in batch {
|
||||
if size > crate::tools::MAX_BATCH_OUTPUT {
|
||||
// Notifications get no reply, run or not.
|
||||
if let Some(id) = m.get("id").cloned() {
|
||||
replies.push(error(id, INTERNAL_ERROR, "the batch's replies passed the output limit; not run"));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let id = m.get("id").cloned();
|
||||
if let Some(r) = self.handle(m) {
|
||||
size = size.saturating_add(crate::tools::json_len(&r));
|
||||
if size > crate::tools::MAX_BATCH_OUTPUT {
|
||||
// This reply is the one that crosses the limit: it is not kept.
|
||||
let id = id.unwrap_or(Value::Null);
|
||||
replies.push(error(
|
||||
id,
|
||||
INTERNAL_ERROR,
|
||||
"the batch's replies passed the output limit; this reply and the rest are left out",
|
||||
));
|
||||
} else {
|
||||
replies.push(r);
|
||||
}
|
||||
}
|
||||
}
|
||||
(!replies.is_empty()).then_some(Value::Array(replies))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -452,3 +452,32 @@ fn invalid_utf8_does_not_change_arguments_or_stop_later_requests() {
|
||||
assert_eq!(replies[1]["result"]["isError"], false);
|
||||
assert_eq!(tool(&mut s, "get_text", json!({}))["text"], "good", "malformed text must never reach the backend");
|
||||
}
|
||||
|
||||
/// A backend whose every result is 1 MiB of text.
|
||||
struct Big;
|
||||
|
||||
impl crate::Backend for Big {
|
||||
fn call(&mut self, _: &str, _: Value) -> Result<Value, String> {
|
||||
Ok(json!("x".repeat(1 << 20)))
|
||||
}
|
||||
fn has_ui(&self) -> bool {
|
||||
false
|
||||
}
|
||||
fn describe(&self) -> String {
|
||||
"big".into()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_legacy_batch_does_not_keep_the_reply_that_crosses_the_output_budget() {
|
||||
let n = (crate::tools::MAX_BATCH_OUTPUT >> 20) + 5;
|
||||
let one =
|
||||
|i: usize| json!({"jsonrpc": "2.0", "id": i, "method": "tools/call", "params": {"name": "execute", "arguments": {"command": "edit.copy"}}});
|
||||
let line = Value::Array((0..n).map(one).collect()).to_string();
|
||||
let mut s = Server::new(Box::new(Big));
|
||||
let out = s.handle_line(&line).expect("reply");
|
||||
assert!(out.len() <= crate::tools::MAX_BATCH_OUTPUT + (1 << 16), "{}", out.len());
|
||||
let replies: Vec<Value> = serde_json::from_str(&out).unwrap();
|
||||
assert_eq!(replies.len(), n);
|
||||
assert!(replies.iter().any(|r| r["error"]["message"].as_str().is_some_and(|m| m.contains("left out"))));
|
||||
}
|
||||
|
||||
+98
-4
@@ -216,6 +216,26 @@ fn with_active_id(b: &mut dyn Backend, r: Result<Value, String>) -> Result<Value
|
||||
Ok(v)
|
||||
}
|
||||
|
||||
/// Most bytes of results one `batch` (or one legacy JSON-RPC batch) keeps for its reply.
|
||||
pub const MAX_BATCH_OUTPUT: usize = 64 << 20;
|
||||
|
||||
/// Length of `v` written as JSON, counted without keeping the text.
|
||||
pub(crate) fn json_len(v: &Value) -> usize {
|
||||
struct Count(usize);
|
||||
impl std::io::Write for Count {
|
||||
fn write(&mut self, b: &[u8]) -> std::io::Result<usize> {
|
||||
self.0 = self.0.saturating_add(b.len());
|
||||
Ok(b.len())
|
||||
}
|
||||
fn flush(&mut self) -> std::io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
let mut n = Count(0);
|
||||
let _ = serde_json::to_writer(&mut n, v);
|
||||
n.0
|
||||
}
|
||||
|
||||
fn wrap(r: Result<Value, String>) -> ToolResult {
|
||||
match r {
|
||||
Ok(v) => ToolResult::json(&v),
|
||||
@@ -267,15 +287,27 @@ pub fn call_tool(b: &mut dyn Backend, name: &str, a: &Value) -> ToolResult {
|
||||
"batch" => {
|
||||
let keep = a.get("keepGoing").and_then(Value::as_bool).unwrap_or(false);
|
||||
let mut out = Vec::new();
|
||||
// What the results hold so far: past MAX_BATCH_OUTPUT the batch stops, rather than
|
||||
// keeping copies of a document command after command until the reply is written.
|
||||
let mut size = 0usize;
|
||||
for c in a.get("commands").and_then(Value::as_array).cloned().unwrap_or_default() {
|
||||
let id = c.get("command").and_then(Value::as_str).unwrap_or("");
|
||||
let r = exec(b, id, c.get("params").cloned().unwrap_or(json!({})), doc);
|
||||
let failed = r.is_err();
|
||||
out.push(match r {
|
||||
let mut failed = r.is_err();
|
||||
let entry = match r {
|
||||
Ok(v) => json!({"command": id, "ok": true, "result": v}),
|
||||
Err(e) => json!({"command": id, "ok": false, "error": e}),
|
||||
});
|
||||
if failed && !keep {
|
||||
};
|
||||
// Counted as the reply stores it: the array is text content, so its quotes and
|
||||
// backslashes are escaped again.
|
||||
size = size.saturating_add(json_len(&Value::String(entry.to_string())));
|
||||
if size > MAX_BATCH_OUTPUT {
|
||||
out.push(json!({"command": id, "ok": false, "error": format!("the batch's results passed {MAX_BATCH_OUTPUT} bytes; this command's result and the commands after it are left out")}));
|
||||
failed = true;
|
||||
} else {
|
||||
out.push(entry);
|
||||
}
|
||||
if failed && (!keep || size > MAX_BATCH_OUTPUT) {
|
||||
return ToolResult { content: vec![json!({"type": "text", "text": Value::Array(out).to_string()})], is_error: true };
|
||||
}
|
||||
}
|
||||
@@ -410,6 +442,68 @@ mod tests {
|
||||
assert!(b.seen.take().expect("called").1.get("doc").is_none());
|
||||
}
|
||||
|
||||
/// A backend whose every result is 1 MiB of text (a whole document, say).
|
||||
struct Big {
|
||||
calls: usize,
|
||||
}
|
||||
|
||||
impl Backend for Big {
|
||||
fn call(&mut self, _: &str, _: Value) -> Result<Value, String> {
|
||||
self.calls += 1;
|
||||
Ok(json!("x".repeat(1 << 20)))
|
||||
}
|
||||
fn has_ui(&self) -> bool {
|
||||
false
|
||||
}
|
||||
fn describe(&self) -> String {
|
||||
"big".into()
|
||||
}
|
||||
}
|
||||
|
||||
/// A backend whose every result is 1 MiB of quotes, which the reply escapes twice.
|
||||
struct Quoted {
|
||||
calls: usize,
|
||||
}
|
||||
|
||||
impl Backend for Quoted {
|
||||
fn call(&mut self, _: &str, _: Value) -> Result<Value, String> {
|
||||
self.calls += 1;
|
||||
Ok(json!("\"".repeat(1 << 20)))
|
||||
}
|
||||
fn has_ui(&self) -> bool {
|
||||
false
|
||||
}
|
||||
fn describe(&self) -> String {
|
||||
"quoted".into()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_batch_budgets_the_escaped_text_it_returns() {
|
||||
// Each result is 2 MiB as JSON and 4 MiB once the array is stored as text.
|
||||
let commands: Vec<Value> = (0..40).map(|_| json!({"command": "edit.copy"})).collect();
|
||||
let mut b = Quoted { calls: 0 };
|
||||
let r = call_tool(&mut b, "batch", &json!({"commands": commands, "keepGoing": true}));
|
||||
assert!(r.is_error);
|
||||
assert!(b.calls <= (MAX_BATCH_OUTPUT >> 22) + 1, "{} commands ran", b.calls);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_batch_stops_once_its_results_pass_the_output_budget() {
|
||||
// 200 commands of 1 MiB results each kept 200 MiB until the reply; it stops at 64 MiB.
|
||||
let commands: Vec<Value> = (0..200).map(|_| json!({"command": "edit.copy"})).collect();
|
||||
let mut b = Big { calls: 0 };
|
||||
let r = call_tool(&mut b, "batch", &json!({"commands": commands, "keepGoing": true}));
|
||||
assert!(r.is_error);
|
||||
assert!(b.calls <= (MAX_BATCH_OUTPUT >> 20) + 1, "{} commands ran", b.calls);
|
||||
let text = r.content[0]["text"].as_str().unwrap_or_default();
|
||||
assert!(text.len() <= MAX_BATCH_OUTPUT + 1024, "{}", text.len());
|
||||
assert!(text.contains("left out"));
|
||||
// A small batch is untouched.
|
||||
let r = call_tool(&mut b, "batch", &json!({"commands": [{"command": "edit.copy"}]}));
|
||||
assert!(!r.is_error);
|
||||
}
|
||||
|
||||
/// The password has to be in the schema, or a client has no way to know it can be sent.
|
||||
#[test]
|
||||
fn the_schema_advertises_the_password_as_optional() {
|
||||
|
||||
@@ -67,11 +67,16 @@ fn too_large_to_paste(width: u32, height: u32) -> ClipboardPicture {
|
||||
ClipboardPicture::Unreadable(format!("the clipboard picture is too large to paste ({width} × {height} pixels)"))
|
||||
}
|
||||
|
||||
/// Whether the renderer can decode `bytes` within its budget (a 16-bit picture takes more than its pixel count says).
|
||||
fn probe_fits(bytes: &[u8]) -> bool {
|
||||
wordcraft_geom::picture::probe(bytes).is_ok_and(|p| p.fits(wordcraft_geom::MAX_DECODED_BYTES))
|
||||
}
|
||||
|
||||
/// An encoded picture the host read from the clipboard (the Windows "PNG" format), refused from
|
||||
/// its header when a paste can't take its size, before anything decodes it (#3623).
|
||||
pub fn encoded_picture(bytes: Vec<u8>) -> ClipboardPicture {
|
||||
match wordcraft_geom::picture::probe(&bytes).map(|p| (p.width, p.height)) {
|
||||
Ok((w, h)) if pastes(w, h) => ClipboardPicture::Encoded(bytes),
|
||||
Ok((w, h)) if pastes(w, h) && probe_fits(&bytes) => ClipboardPicture::Encoded(bytes),
|
||||
Ok((w, h)) => too_large_to_paste(w, h),
|
||||
Err(e) => ClipboardPicture::Unreadable(e.to_string()),
|
||||
}
|
||||
@@ -258,6 +263,22 @@ mod tests {
|
||||
image::GrayImage::new(20_000, 1).write_to(&mut std::io::Cursor::new(&mut wide), image::ImageFormat::Png).unwrap();
|
||||
assert!(matches!(encoded_picture(wide), ClipboardPicture::Unreadable(e) if e.contains("too large")));
|
||||
assert!(matches!(encoded_picture(b"not a picture".to_vec()), ClipboardPicture::Unreadable(_)));
|
||||
// 8000 × 6000 passes the pixel limits, but 16-bit RGBA needs 12 bytes a pixel to decode.
|
||||
let mut big = Vec::new();
|
||||
image::DynamicImage::ImageRgba16(image::ImageBuffer::new(1, 1))
|
||||
.write_to(&mut std::io::Cursor::new(&mut big), image::ImageFormat::Png)
|
||||
.unwrap();
|
||||
big[16..20].copy_from_slice(&8000u32.to_be_bytes());
|
||||
big[20..24].copy_from_slice(&6000u32.to_be_bytes());
|
||||
let mut crc = 0xFFFF_FFFFu32;
|
||||
for &b in &big[12..29] {
|
||||
crc ^= u32::from(b);
|
||||
for _ in 0..8 {
|
||||
crc = if crc & 1 == 1 { 0xEDB8_8320 ^ (crc >> 1) } else { crc >> 1 };
|
||||
}
|
||||
}
|
||||
big[29..33].copy_from_slice(&(!crc).to_be_bytes());
|
||||
assert!(matches!(encoded_picture(big), ClipboardPicture::Unreadable(e) if e.contains("too large")));
|
||||
// BGRA in memory: a half-transparent red pixel and an opaque blue one.
|
||||
let ClipboardPicture::Pixels { width, height, rgba } = dib_picture(dib(2, 1, &[[0, 0, 255, 128], [255, 0, 0, 255]])) else {
|
||||
panic!("pixels")
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -55,7 +55,7 @@ esac
|
||||
# Only binaries for this machine run here (the --version check at the end); a FreeBSD target built
|
||||
# on another OS never does.
|
||||
HOST_ARCH="$(release_arch "$(uname -m)" 2>/dev/null)" || HOST_ARCH="$(uname -m)"
|
||||
if [ -n "${CARGO_BUILD_TARGET:-}" ] && [ "$(uname -s)" != FreeBSD ]; then
|
||||
if [[ -n "${CARGO_BUILD_TARGET:-}" ]] && [[ "$(uname -s)" != FreeBSD ]]; then
|
||||
HOST_ARCH="$(uname -s) $HOST_ARCH"
|
||||
fi
|
||||
BASENAME="wordcraft-$VERSION-freebsd-$ARCH"
|
||||
@@ -96,12 +96,12 @@ mkdir -p "$DIST"
|
||||
tar -C "$WORK" -czf "$DIST/$BASENAME.tar.gz" "$BASENAME"
|
||||
echo "wrote $DIST/$BASENAME.tar.gz"
|
||||
|
||||
if [ -z "${CARGO_BUILD_TARGET:-}" ] && [ "$ARCH" = "$HOST_ARCH" ]; then
|
||||
if [[ -z "${CARGO_BUILD_TARGET:-}" ]] && [[ "$ARCH" = "$HOST_ARCH" ]]; then
|
||||
"$STAGE/bin/wordcraft-cli" --version
|
||||
else
|
||||
# They cannot run here, so check what they are instead: executables, not libraries or objects.
|
||||
# An older file(1) calls a PIE a shared object; its interpreter still marks it as a program.
|
||||
want="$ARCH"; [ "$ARCH" != x86_64 ] || want=x86-64
|
||||
want="$ARCH"; [[ "$ARCH" != x86_64 ]] || want=x86-64
|
||||
for b in "$STAGE"/bin/*; do
|
||||
case "$(file -b "$b")" in
|
||||
ELF*" executable, "*"$want"*FreeBSD* | ELF*"$want"*FreeBSD*interpreter*) ;;
|
||||
|
||||
Reference in New Issue
Block a user