Skip to content

armgit / kwant

Public Imported from GitHub
0

Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.

0.9.0 should-fixes: #495 S1 budget body must be an object, #486 N3 slug example (#508)

Oleksandr Ivanchenko7005824c622e
diff --git a/src/server/bot_budget.rs b/src/server/bot_budget.rs
index 1e5d1089..21e283d9 100644
--- a/src/server/bot_budget.rs
+++ b/src/server/bot_budget.rs
@@ -488,6 +488,15 @@ fn some_value<'de, D: serde::Deserializer<'de>>(d: D) -> Result<Option<Option<f6
Option::<f64>::deserialize(d).map(Some)
}
+/// The body as a [`BudgetBody`], only when it is a JSON object: serde would read an array like
+/// `[1]` as the struct's fields in order and save a 1-token budget (#495 S1).
+pub fn parse_body(v: Value) -> Option<BudgetBody> {
+ if !v.is_object() {
+ return None;
+ }
+ serde_json::from_value(v).ok()
+}
+
/// The limits a body asks for, validated: (tokens, usd).
pub fn validate(body: &BudgetBody, current: (u64, f64)) -> Result<(u64, f64), String> {
let tokens = match body.budget_tokens {
@@ -511,9 +520,9 @@ pub fn validate(body: &BudgetBody, current: (u64, f64)) -> Result<(u64, f64), St
pub async fn set_budget(
State(state): State<AppState>,
Path(id): Path<String>,
- body: Result<Json<BudgetBody>, axum::extract::rejection::JsonRejection>,
+ body: Result<Json<Value>, axum::extract::rejection::JsonRejection>,
) -> Response {
- let Ok(Json(body)) = body else {
+ let Some(body) = body.ok().and_then(|Json(v)| parse_body(v)) else {
return err(StatusCode::BAD_REQUEST, "expected {budget_tokens, budget_usd}");
};
let kwant = match kwant_or_404(&state, &id).await {
@@ -639,6 +648,17 @@ mod tests {
assert_eq!(percent_of(99, 99.0, 0, 0.0), (0.0, None));
}
+ #[test]
+ fn only_an_object_is_a_budget_body() {
+ for bad in [json!([1]), json!([1000, 2.5]), json!(1), json!("x"), json!(null), json!(true)] {
+ assert!(parse_body(bad.clone()).is_none(), "{bad}");
+ }
+ let b = parse_body(json!({"budget_tokens": 5})).unwrap();
+ assert_eq!((b.budget_tokens, b.budget_usd), (Some(Some(5.0)), None));
+ assert!(parse_body(json!({})).is_some(), "an empty object changes nothing, as before");
+ assert!(parse_body(json!({"budget_tokens": "lots"})).is_none());
+ }
+
#[test]
fn validates_limits() {
let body = |t: Option<Option<f64>>, u: Option<Option<f64>>| BudgetBody { budget_tokens: t, budget_usd: u };
diff --git a/src/server/dev/worktrees.rs b/src/server/dev/worktrees.rs
index f63450d3..9ab17692 100644
--- a/src/server/dev/worktrees.rs
+++ b/src/server/dev/worktrees.rs
@@ -51,7 +51,7 @@ const SKIPPED_MAX: usize = 50;
/// A folder name from a branch or a folder name: lowercased, every run of characters outside
/// `[a-z0-9_]` turned into one `-`, trimmed of `-`, at most [`SLUG_MAX`] characters; `worktree`
-/// when nothing is left. `lin/H1 Worktrees` → `lin-h1-worktrees`. Never `.`/`..`, never hidden.
+/// when nothing is left. `Lin/H1-Worktrees` → `lin-h1-worktrees` (#486 N3: a git branch name has no spaces). Never `.`/`..`, never hidden.
pub fn slug(s: &str) -> String {
let mut out = String::new();
for c in s.chars().flat_map(char::to_lowercase) {
@@ -1291,6 +1291,7 @@ mod tests {
#[test]
fn branch_slugs_are_plain_folder_names() {
+ assert_eq!(slug("Lin/H1-Worktrees"), "lin-h1-worktrees");
assert_eq!(slug("lin/H1 Worktrees"), "lin-h1-worktrees");
assert_eq!(slug("pixel/inbox-empty-state"), "pixel-inbox-empty-state");
assert_eq!(slug("../../etc"), "etc");
diff --git a/tests/bot_budget_test.rs b/tests/bot_budget_test.rs
index 03a895cb..327fd613 100644
--- a/tests/bot_budget_test.rs
+++ b/tests/bot_budget_test.rs
@@ -482,6 +482,12 @@ async fn validates_limits_and_turns_off() {
json!({"budget_usd": -0.01}),
json!({"budget_usd": 100_001}),
json!({"budget_tokens": "lots"}),
+ // #495 S1: only an object is a budget; serde would read an array as the fields in order
+ json!([1]),
+ json!([1000, 2.5]),
+ json!(7),
+ json!("1000"),
+ json!(null),
] {
let (s, v) = call(&env.app, "POST", &format!("/api/kwants/{}/budget", env.ann), Some(bad.clone())).await;
assert_eq!(s, StatusCode::BAD_REQUEST, "{bad}: {v}");