stateset-http 1.22.0

HTTP service layer (REST + SSE) for the StateSet commerce engine
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
//! Negotiation endpoints for autonomous agent-to-agent price negotiation.

use axum::{
    Json, Router,
    extract::{Path, State},
    http::{HeaderMap, StatusCode},
    routing::{get, post},
};
use chrono::{Duration, Utc};
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use stateset_a2a::negotiation::NegotiationEngine;
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use utoipa::ToSchema;
use uuid::Uuid;

use crate::error::{ErrorBody, HttpError};
use crate::state::{AppState, tenant_id_from_headers};

/// Store key: negotiations are scoped per tenant (`None` = default tenant) so
/// one tenant can never read or mutate another tenant's negotiations.
type StoreKey = (Option<String>, Uuid);

/// In-memory negotiation store.
///
/// The V9 tables `a2a_negotiations` and `a2a_negotiation_offers` exist in
/// `stateset-migrations`, but no repository exposes them yet —
/// [`stateset_core::traits::A2ACommerceRepository`] only covers quotes and
/// purchases. Wiring DB persistence requires new negotiation repository
/// methods on both the SQLite and Postgres backends; until then this store is
/// process-local and negotiations do not survive a restart.
type NegotiationStore = Arc<RwLock<HashMap<StoreKey, stateset_a2a::negotiation::Negotiation>>>;

/// Build the negotiations sub-router.
pub fn router() -> Router<AppState> {
    let store: NegotiationStore = Arc::new(RwLock::new(HashMap::new()));
    Router::new()
        .route(
            "/negotiations",
            post({
                let store = store.clone();
                move |state, headers, body| create_negotiation(state, headers, body, store)
            }),
        )
        .route(
            "/negotiations/{id}",
            get({
                let store = store.clone();
                move |state, headers, path| get_negotiation(state, headers, path, store)
            }),
        )
        .route(
            "/negotiations/{id}/counter-offer",
            post({
                let store = store.clone();
                move |state, headers, path, body| counter_offer(state, headers, path, body, store)
            }),
        )
        .route(
            "/negotiations/{id}/accept",
            post({
                let store = store.clone();
                move |state, headers, path| accept_negotiation(state, headers, path, store)
            }),
        )
        .route(
            "/negotiations/{id}/reject",
            post(move |state, headers, path| reject_negotiation(state, headers, path, store)),
        )
}

/// Request body for creating a negotiation.
#[derive(Debug, Clone, Deserialize, Serialize, ToSchema)]
pub struct CreateNegotiationRequest {
    pub buyer_agent_id: String,
    pub seller_agent_id: String,
    #[schema(value_type = String)]
    pub initial_offer: Decimal,
    pub currency: Option<String>,
    pub max_rounds: Option<u32>,
    #[schema(value_type = Option<String>)]
    pub auto_accept_below: Option<Decimal>,
    #[schema(value_type = Option<String>)]
    pub auto_reject_above: Option<Decimal>,
    pub expires_in_hours: Option<u32>,
}

/// Request body for a counter-offer.
#[derive(Debug, Clone, Deserialize, Serialize, ToSchema)]
pub struct CounterOfferRequest {
    pub from_agent_id: String,
    #[schema(value_type = String)]
    pub amount: Decimal,
    pub message: Option<String>,
}

/// Response body for a negotiation.
#[derive(Debug, Clone, Serialize, ToSchema)]
pub struct NegotiationResponse {
    pub id: String,
    pub buyer_agent_id: String,
    pub seller_agent_id: String,
    pub status: String,
    #[schema(value_type = String)]
    pub current_offer: Decimal,
    pub currency: String,
    pub rounds: u32,
    pub max_rounds: u32,
    pub offer_count: usize,
    pub created_at: String,
}

fn neg_to_response(n: &stateset_a2a::negotiation::Negotiation) -> NegotiationResponse {
    NegotiationResponse {
        id: n.id.to_string(),
        buyer_agent_id: n.buyer_agent_id.clone(),
        seller_agent_id: n.seller_agent_id.clone(),
        status: n.status.to_string(),
        current_offer: n.current_offer,
        currency: n.currency.clone(),
        rounds: n.rounds,
        max_rounds: n.max_rounds,
        offer_count: n.offers.len(),
        created_at: n.created_at.to_rfc3339(),
    }
}

/// `POST /api/v1/negotiations`
#[utoipa::path(post, path = "/api/v1/negotiations", tag = "negotiations",
    request_body = CreateNegotiationRequest,
    responses((status = 201, body = NegotiationResponse), (status = 400, body = ErrorBody)))]
#[tracing::instrument(skip_all)]
pub(crate) async fn create_negotiation(
    State(_state): State<AppState>,
    headers: HeaderMap,
    Json(req): Json<CreateNegotiationRequest>,
    store: NegotiationStore,
) -> Result<(StatusCode, Json<NegotiationResponse>), HttpError> {
    if req.initial_offer <= Decimal::ZERO {
        return Err(HttpError::BadRequest("Invalid amount: initial_offer must be positive".into()));
    }
    let tenant_id = tenant_id_from_headers(&headers);
    let expires = Utc::now() + Duration::hours(i64::from(req.expires_in_hours.unwrap_or(24)));

    let neg = NegotiationEngine::create(
        req.buyer_agent_id,
        req.seller_agent_id,
        req.initial_offer,
        req.currency.as_deref().unwrap_or("USD"),
        req.max_rounds.unwrap_or(10),
        expires,
        req.auto_accept_below,
        req.auto_reject_above,
    );

    let resp = neg_to_response(&neg);
    let mut guard = store.write().map_err(|_| HttpError::InternalError("Lock poisoned".into()))?;
    guard.insert((tenant_id, neg.id), neg);

    Ok((StatusCode::CREATED, Json(resp)))
}

/// `GET /api/v1/negotiations/:id`
#[utoipa::path(get, path = "/api/v1/negotiations/{id}", tag = "negotiations",
    params(("id" = String, Path, description = "Negotiation ID (UUID)")),
    responses((status = 200, body = NegotiationResponse), (status = 404, body = ErrorBody)))]
#[tracing::instrument(skip_all)]
pub(crate) async fn get_negotiation(
    State(_state): State<AppState>,
    headers: HeaderMap,
    Path(id): Path<Uuid>,
    store: NegotiationStore,
) -> Result<Json<NegotiationResponse>, HttpError> {
    let tenant_id = tenant_id_from_headers(&headers);
    let guard = store.read().map_err(|_| HttpError::InternalError("Lock poisoned".into()))?;
    let neg = guard
        .get(&(tenant_id, id))
        .ok_or_else(|| HttpError::NotFound(format!("Negotiation {id} not found")))?;
    Ok(Json(neg_to_response(neg)))
}

/// `POST /api/v1/negotiations/:id/counter-offer`
#[utoipa::path(post, path = "/api/v1/negotiations/{id}/counter-offer", tag = "negotiations",
    params(("id" = String, Path, description = "Negotiation ID (UUID)")),
    request_body = CounterOfferRequest,
    responses((status = 200, body = NegotiationResponse), (status = 400, body = ErrorBody),
        (status = 404, body = ErrorBody)))]
#[tracing::instrument(skip_all)]
pub(crate) async fn counter_offer(
    State(_state): State<AppState>,
    headers: HeaderMap,
    Path(id): Path<Uuid>,
    Json(req): Json<CounterOfferRequest>,
    store: NegotiationStore,
) -> Result<Json<NegotiationResponse>, HttpError> {
    if req.amount <= Decimal::ZERO {
        return Err(HttpError::BadRequest("Invalid amount: amount must be positive".into()));
    }
    let tenant_id = tenant_id_from_headers(&headers);

    let mut guard = store.write().map_err(|_| HttpError::InternalError("Lock poisoned".into()))?;
    let key = (tenant_id, id);
    let neg = guard
        .remove(&key)
        .ok_or_else(|| HttpError::NotFound(format!("Negotiation {id} not found")))?;

    let neg = NegotiationEngine::counter_offer(neg, req.from_agent_id, req.amount, req.message)
        .map_err(|e| HttpError::BadRequest(format!("Counter-offer failed: {e}")))?;

    let resp = neg_to_response(&neg);
    guard.insert(key, neg);
    Ok(Json(resp))
}

/// `POST /api/v1/negotiations/:id/accept`
#[utoipa::path(post, path = "/api/v1/negotiations/{id}/accept", tag = "negotiations",
    params(("id" = String, Path, description = "Negotiation ID (UUID)")),
    responses((status = 200, body = NegotiationResponse), (status = 400, body = ErrorBody),
        (status = 404, body = ErrorBody)))]
#[tracing::instrument(skip_all)]
pub(crate) async fn accept_negotiation(
    State(_state): State<AppState>,
    headers: HeaderMap,
    Path(id): Path<Uuid>,
    store: NegotiationStore,
) -> Result<Json<NegotiationResponse>, HttpError> {
    let tenant_id = tenant_id_from_headers(&headers);
    let mut guard = store.write().map_err(|_| HttpError::InternalError("Lock poisoned".into()))?;
    let key = (tenant_id, id);
    let neg = guard
        .remove(&key)
        .ok_or_else(|| HttpError::NotFound(format!("Negotiation {id} not found")))?;

    let neg = NegotiationEngine::accept(neg)
        .map_err(|e| HttpError::BadRequest(format!("Accept failed: {e}")))?;

    let resp = neg_to_response(&neg);
    guard.insert(key, neg);
    Ok(Json(resp))
}

/// `POST /api/v1/negotiations/:id/reject`
#[utoipa::path(post, path = "/api/v1/negotiations/{id}/reject", tag = "negotiations",
    params(("id" = String, Path, description = "Negotiation ID (UUID)")),
    responses((status = 200, body = NegotiationResponse), (status = 400, body = ErrorBody),
        (status = 404, body = ErrorBody)))]
#[tracing::instrument(skip_all)]
pub(crate) async fn reject_negotiation(
    State(_state): State<AppState>,
    headers: HeaderMap,
    Path(id): Path<Uuid>,
    store: NegotiationStore,
) -> Result<Json<NegotiationResponse>, HttpError> {
    let tenant_id = tenant_id_from_headers(&headers);
    let mut guard = store.write().map_err(|_| HttpError::InternalError("Lock poisoned".into()))?;
    let key = (tenant_id, id);
    let neg = guard
        .remove(&key)
        .ok_or_else(|| HttpError::NotFound(format!("Negotiation {id} not found")))?;

    let neg = NegotiationEngine::reject(neg, None)
        .map_err(|e| HttpError::BadRequest(format!("Reject failed: {e}")))?;

    let resp = neg_to_response(&neg);
    guard.insert(key, neg);
    Ok(Json(resp))
}

#[cfg(test)]
mod tests {
    use super::*;
    use axum::body::Body;
    use axum::http::Request;
    use stateset_embedded::Commerce;
    use tower::ServiceExt;

    fn app() -> Router {
        router().with_state(AppState::new(Commerce::new(":memory:").expect("in-memory Commerce")))
    }

    async fn json_body(resp: axum::response::Response) -> serde_json::Value {
        let body = axum::body::to_bytes(resp.into_body(), usize::MAX).await.unwrap();
        serde_json::from_slice(&body).unwrap()
    }

    fn post_json(uri: &str, tenant: Option<&str>, body: &serde_json::Value) -> Request<Body> {
        let mut builder = Request::post(uri).header("content-type", "application/json");
        if let Some(tenant) = tenant {
            builder = builder.header("x-tenant-id", tenant);
        }
        builder.body(Body::from(serde_json::to_vec(body).unwrap())).unwrap()
    }

    fn get_request(uri: &str, tenant: Option<&str>) -> Request<Body> {
        let mut builder = Request::get(uri);
        if let Some(tenant) = tenant {
            builder = builder.header("x-tenant-id", tenant);
        }
        builder.body(Body::empty()).unwrap()
    }

    async fn create_negotiation(app: &Router, tenant: Option<&str>, offer: &str) -> String {
        let body = serde_json::json!({
            "buyer_agent_id": "agent-buyer",
            "seller_agent_id": "agent-seller",
            "initial_offer": offer,
        });
        let resp = app.clone().oneshot(post_json("/negotiations", tenant, &body)).await.unwrap();
        assert_eq!(resp.status(), StatusCode::CREATED);
        let json = json_body(resp).await;
        json["id"].as_str().unwrap().to_string()
    }

    #[tokio::test]
    async fn create_negotiation_serializes_offer_as_decimal_string() {
        let app = app();
        let body = serde_json::json!({
            "buyer_agent_id": "agent-buyer",
            "seller_agent_id": "agent-seller",
            "initial_offer": "100.50",
        });

        let resp = app.oneshot(post_json("/negotiations", None, &body)).await.unwrap();
        assert_eq!(resp.status(), StatusCode::CREATED);

        let json = json_body(resp).await;
        assert_eq!(json["current_offer"], "100.50");
        assert_eq!(json["currency"], "USD");
        assert_eq!(json["status"], "open");
        assert_eq!(json["rounds"], 1);
    }

    #[tokio::test]
    async fn create_negotiation_accepts_numeric_amounts() {
        let app = app();
        let body = serde_json::json!({
            "buyer_agent_id": "agent-buyer",
            "seller_agent_id": "agent-seller",
            "initial_offer": 49.99,
        });

        let resp = app.oneshot(post_json("/negotiations", None, &body)).await.unwrap();
        assert_eq!(resp.status(), StatusCode::CREATED);

        let json = json_body(resp).await;
        assert_eq!(json["current_offer"], "49.99");
    }

    #[tokio::test]
    async fn create_negotiation_preserves_high_precision_amounts() {
        // A value f64 cannot represent exactly must round-trip unchanged.
        let app = app();
        let body = serde_json::json!({
            "buyer_agent_id": "agent-buyer",
            "seller_agent_id": "agent-seller",
            "initial_offer": "1234567890.123456789",
        });

        let resp = app.oneshot(post_json("/negotiations", None, &body)).await.unwrap();
        assert_eq!(resp.status(), StatusCode::CREATED);

        let json = json_body(resp).await;
        assert_eq!(json["current_offer"], "1234567890.123456789");
    }

    #[tokio::test]
    async fn create_negotiation_rejects_non_positive_offer() {
        let app = app();
        for offer in ["0", "-5.00"] {
            let body = serde_json::json!({
                "buyer_agent_id": "agent-buyer",
                "seller_agent_id": "agent-seller",
                "initial_offer": offer,
            });
            let resp = app.clone().oneshot(post_json("/negotiations", None, &body)).await.unwrap();
            assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
        }
    }

    #[tokio::test]
    async fn counter_offer_uses_decimal_amount() {
        let app = app();
        let id = create_negotiation(&app, None, "100.00").await;

        let body = serde_json::json!({
            "from_agent_id": "agent-seller",
            "amount": "95.25",
        });
        let resp = app
            .oneshot(post_json(&format!("/negotiations/{id}/counter-offer"), None, &body))
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::OK);

        let json = json_body(resp).await;
        assert_eq!(json["current_offer"], "95.25");
        assert_eq!(json["status"], "counter_offered");
        assert_eq!(json["rounds"], 2);
        assert_eq!(json["offer_count"], 2);
    }

    #[tokio::test]
    async fn counter_offer_rejects_non_positive_amount() {
        let app = app();
        let id = create_negotiation(&app, None, "100.00").await;

        let body = serde_json::json!({
            "from_agent_id": "agent-seller",
            "amount": "-1",
        });
        let resp = app
            .oneshot(post_json(&format!("/negotiations/{id}/counter-offer"), None, &body))
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
    }

    #[tokio::test]
    async fn accept_negotiation_returns_accepted_status() {
        let app = app();
        let id = create_negotiation(&app, None, "100.00").await;

        let body = serde_json::json!({});
        let resp = app
            .oneshot(post_json(&format!("/negotiations/{id}/accept"), None, &body))
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::OK);

        let json = json_body(resp).await;
        assert_eq!(json["status"], "accepted");
    }

    #[tokio::test]
    async fn reject_negotiation_returns_rejected_status() {
        let app = app();
        let id = create_negotiation(&app, None, "100.00").await;

        let body = serde_json::json!({});
        let resp = app
            .oneshot(post_json(&format!("/negotiations/{id}/reject"), None, &body))
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::OK);

        let json = json_body(resp).await;
        assert_eq!(json["status"], "rejected");
    }

    #[tokio::test]
    async fn get_negotiation_unknown_id_returns_404() {
        let app = app();
        let resp = app
            .oneshot(get_request(&format!("/negotiations/{}", Uuid::new_v4()), None))
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::NOT_FOUND);
    }

    #[tokio::test]
    async fn tenant_cannot_read_other_tenants_negotiation() {
        let app = app();
        let id = create_negotiation(&app, Some("tenant-a"), "100.00").await;

        // Tenant B must not see tenant A's negotiation.
        let resp = app
            .clone()
            .oneshot(get_request(&format!("/negotiations/{id}"), Some("tenant-b")))
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::NOT_FOUND);

        // Tenant A still sees it.
        let resp = app
            .oneshot(get_request(&format!("/negotiations/{id}"), Some("tenant-a")))
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
    }

    #[tokio::test]
    async fn tenant_cannot_mutate_other_tenants_negotiation() {
        let app = app();
        let id = create_negotiation(&app, Some("tenant-a"), "100.00").await;

        let counter = serde_json::json!({
            "from_agent_id": "agent-seller",
            "amount": "95.00",
        });
        for uri in [
            format!("/negotiations/{id}/counter-offer"),
            format!("/negotiations/{id}/accept"),
            format!("/negotiations/{id}/reject"),
        ] {
            let resp =
                app.clone().oneshot(post_json(&uri, Some("tenant-b"), &counter)).await.unwrap();
            assert_eq!(resp.status(), StatusCode::NOT_FOUND, "{uri} must 404 for tenant-b");
        }

        // The negotiation is untouched for tenant A.
        let resp = app
            .oneshot(get_request(&format!("/negotiations/{id}"), Some("tenant-a")))
            .await
            .unwrap();
        let json = json_body(resp).await;
        assert_eq!(json["status"], "open");
        assert_eq!(json["rounds"], 1);
    }

    #[tokio::test]
    async fn default_tenant_is_isolated_from_named_tenants() {
        let app = app();
        let id = create_negotiation(&app, None, "100.00").await;

        let resp = app
            .clone()
            .oneshot(get_request(&format!("/negotiations/{id}"), Some("tenant-a")))
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::NOT_FOUND);

        let resp = app.oneshot(get_request(&format!("/negotiations/{id}"), None)).await.unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
    }
}