harn-serve 0.10.166

Shared outbound workflow server core for Harn adapters
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
//! HTTP adapter tests for the session-store router.

use std::sync::Arc;

use harn_session_store::{
    AppendEvent, CreateSession, MemorySessionStore, SessionEventKind, SessionMeta,
    SharedSessionStore, StoreHooks,
};
use serde_json::json;

use super::api;

#[tokio::test]
async fn http_boundary_read_drives_an_acknowledged_fork() {
    use axum::body::Body;
    use axum::http::{Request, StatusCode};
    use harn_session_store::{CanonicalHistoryBoundaries, CanonicalSessionBoundary, ReadRange};
    use tower::ServiceExt as _;

    let directory = tempfile::tempdir().unwrap();
    let database = directory.path().join("saved-session.sqlite");
    let store: SharedSessionStore =
        Arc::new(harn_session_store::SqliteSessionStore::open(&database).unwrap());
    let router = api::sessions_router(store.clone());
    let get_boundaries = |id: &str| {
        Request::builder()
            .uri(format!("/sessions/{id}/boundaries"))
            .body(Body::empty())
            .unwrap()
    };
    let missing = router
        .clone()
        .oneshot(get_boundaries("missing"))
        .await
        .unwrap();
    assert_eq!(missing.status(), StatusCode::NOT_FOUND);

    let parent = store.create(CreateSession::default()).await.unwrap();
    let response = router
        .clone()
        .oneshot(get_boundaries(&parent.id))
        .await
        .unwrap();
    assert_eq!(response.status(), StatusCode::OK);
    let bytes = axum::body::to_bytes(response.into_body(), 1 << 20)
        .await
        .unwrap();
    let empty: CanonicalHistoryBoundaries = serde_json::from_slice(&bytes).unwrap();
    assert_eq!(empty.tip, CanonicalSessionBoundary::empty(&parent.id));
    assert!(empty.positions.is_empty());

    let mut message = AppendEvent::new(
        SessionEventKind::Message,
        json!({"raw_message":{"role":"user","content":"first context"}}),
    );
    message
        .headers
        .insert("source_event_id".into(), "opaque-source-first".into());
    let first = store.append(&parent.id, message).await.unwrap();
    let receipt = store
        .append(
            &parent.id,
            AppendEvent::new(SessionEventKind::Receipt, json!({"status":"completed"})),
        )
        .await
        .unwrap();
    // Reopen the saved session without an active producer or in-memory journal.
    drop(router);
    drop(store);
    let store: SharedSessionStore =
        Arc::new(harn_session_store::SqliteSessionStore::open(&database).unwrap());
    let router = api::sessions_router(store.clone());
    let response = router
        .clone()
        .oneshot(get_boundaries(&parent.id))
        .await
        .unwrap();
    assert_eq!(response.status(), StatusCode::OK);
    let bytes = axum::body::to_bytes(response.into_body(), 1 << 20)
        .await
        .unwrap();
    let acknowledged: CanonicalHistoryBoundaries = serde_json::from_slice(&bytes).unwrap();
    assert_eq!(
        acknowledged.tip,
        CanonicalSessionBoundary::acknowledged(&receipt)
    );
    assert_eq!(acknowledged.positions.len(), 1);
    assert_eq!(
        acknowledged.positions[0].source_event_id,
        "opaque-source-first"
    );
    assert_eq!(
        acknowledged.positions[0].boundary,
        CanonicalSessionBoundary::acknowledged(&first)
    );

    let fork_request = |child: &str, boundary: CanonicalSessionBoundary| {
        Request::builder()
            .method("POST")
            .uri(format!("/sessions/{}/fork", parent.id))
            .header("content-type", "application/json")
            .body(Body::from(
                json!({"canonical_boundary":boundary,"child_session_id":child}).to_string(),
            ))
            .unwrap()
    };
    let mut wrong_hash = acknowledged.positions[0].boundary.clone();
    wrong_hash.record_hash = Some("unacknowledged-hash".into());
    let refused = router
        .clone()
        .oneshot(fork_request("refused-child", wrong_hash))
        .await
        .unwrap();
    assert_eq!(refused.status(), StatusCode::CONFLICT);
    assert!(matches!(
        store.describe("refused-child").await,
        Err(harn_session_store::StoreError::NotFound(_))
    ));

    let mut foreign = acknowledged.positions[0].boundary.clone();
    foreign.session_id = "foreign-parent".into();
    let refused = router
        .clone()
        .oneshot(fork_request("foreign-child", foreign))
        .await
        .unwrap();
    assert_eq!(refused.status(), StatusCode::CONFLICT);
    assert!(matches!(
        store.describe("foreign-child").await,
        Err(harn_session_store::StoreError::NotFound(_))
    ));

    let response = router
        .oneshot(fork_request(
            "acknowledged-child",
            acknowledged.positions[0].boundary.clone(),
        ))
        .await
        .unwrap();
    assert_eq!(response.status(), StatusCode::CREATED);
    let child = store.describe("acknowledged-child").await.unwrap();
    assert_eq!(child.parent_session_id.as_deref(), Some(parent.id.as_str()));
    let copied = store.read(&child.id, ReadRange::default()).await.unwrap();
    assert_eq!(copied.events.len(), 1);
    assert_eq!(
        copied.events[0].payload["raw_message"]["content"],
        "first context"
    );
}

#[tokio::test]
async fn http_router_round_trips_events() {
    use axum::body::Body;
    use axum::http::{Request, StatusCode};
    use tower::ServiceExt as _;

    let store: SharedSessionStore = Arc::new(MemorySessionStore::new());
    let router = api::sessions_router(store.clone());

    let response = router
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/sessions")
                .header("content-type", "application/json")
                .body(Body::from(
                    serde_json::to_string(&CreateSession::default()).unwrap(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(response.status(), StatusCode::CREATED);
    let bytes = axum::body::to_bytes(response.into_body(), 1 << 20)
        .await
        .unwrap();
    let meta: SessionMeta = serde_json::from_slice(&bytes).unwrap();

    let response = router
        .clone()
        .oneshot(
            Request::builder()
                .method("PATCH")
                .uri(format!("/sessions/{}", meta.id))
                .header("content-type", "application/json")
                .body(Body::from(json!({"title": "Canonical title"}).to_string()))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(response.status(), StatusCode::OK);
    let bytes = axum::body::to_bytes(response.into_body(), 1 << 20)
        .await
        .unwrap();
    let updated: SessionMeta = serde_json::from_slice(&bytes).unwrap();
    assert_eq!(updated.title.as_deref(), Some("Canonical title"));

    let body = json!({
        "kind": {"kind": "message"},
        "payload": {
            "transcript_event": {
                "kind": "message",
                "role": "user",
                "visibility": "public",
                "text": "hello",
                "metadata": {}
            }
        },
        "headers": {"run_id": "run-http", "turn_id": "turn-http"},
    });
    let response = router
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri(format!("/sessions/{}/events", meta.id))
                .header("content-type", "application/json")
                .body(Body::from(serde_json::to_string(&body).unwrap()))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(response.status(), StatusCode::CREATED);

    let response = router
        .clone()
        .oneshot(
            Request::builder()
                .uri(format!("/sessions/{}/timeline", meta.id))
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(response.status(), StatusCode::OK);
    let bytes = axum::body::to_bytes(response.into_body(), 1 << 20)
        .await
        .unwrap();
    let wire: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
    assert_eq!(wire["nodes"][0]["children"], json!([]));
    assert_eq!(
        wire["nodes"][0]["links"],
        json!([
            {"kind": "run", "targetId": "run-http"},
            {"kind": "turn", "targetId": "turn-http"}
        ])
    );
    assert!(wire["nodes"][0].get("attributes").is_some());
    let timeline: harn_vm::session_timeline::SessionTimelineSnapshot =
        serde_json::from_slice(&bytes).unwrap();
    assert_eq!(timeline.query.session_id.as_deref(), Some(meta.id.as_str()));
    assert_eq!(timeline.nodes.len(), 1);
    assert_eq!(timeline.nodes[0].category, "message");

    let response = router
        .clone()
        .oneshot(
            Request::builder()
                .uri(format!("/sessions/{}/recap?limit=10", meta.id))
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(response.status(), StatusCode::OK);
    let bytes = axum::body::to_bytes(response.into_body(), 1 << 20)
        .await
        .unwrap();
    let recap: harn_vm::session_recap::SessionRecapAvailability =
        serde_json::from_slice(&bytes).unwrap();
    let harn_vm::session_recap::SessionRecapAvailability::Available { snapshot } = recap else {
        panic!("existing session must return an available recap")
    };
    assert_eq!(snapshot.coverage.scanned, 1);
    assert_eq!(snapshot.coverage.matched, 1);
    assert_eq!(snapshot.turns[0].prompts[0].text, "hello");

    let response = router
        .clone()
        .oneshot(
            Request::builder()
                .uri("/sessions/missing/recap")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(response.status(), StatusCode::OK);
    let bytes = axum::body::to_bytes(response.into_body(), 1 << 20)
        .await
        .unwrap();
    assert_eq!(
        serde_json::from_slice::<serde_json::Value>(&bytes).unwrap(),
        json!({"state": "unavailable", "reason": "session_missing"})
    );

    let response = router
        .clone()
        .oneshot(
            Request::builder()
                .method("GET")
                .uri(format!("/sessions/{}/events", meta.id))
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(response.status(), StatusCode::OK);
    let bytes = axum::body::to_bytes(response.into_body(), 1 << 20)
        .await
        .unwrap();
    let body: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
    assert_eq!(body["object"], "list");
    assert_eq!(body["data"].as_array().unwrap().len(), 1);
}

#[tokio::test]
async fn http_router_returns_session_view() {
    use axum::body::Body;
    use axum::http::{Request, StatusCode};
    use tower::ServiceExt as _;

    let store: SharedSessionStore = Arc::new(MemorySessionStore::new());
    let router = api::sessions_router(store.clone());
    let meta = store
        .create(CreateSession::default())
        .await
        .expect("create");
    store
        .append(
            &meta.id,
            AppendEvent::new(SessionEventKind::Message, json!({"text": "hello"})),
        )
        .await
        .expect("append");

    let response = router
        .oneshot(
            Request::builder()
                .method("GET")
                .uri(format!("/sessions/{}/view", meta.id))
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(response.status(), StatusCode::OK);
    let bytes = axum::body::to_bytes(response.into_body(), 1 << 20)
        .await
        .unwrap();
    let body: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
    assert_eq!(body["schema"], "harn.session_view.v1");
    assert_eq!(body["session"]["session_id"], meta.id);
    assert_eq!(body["session"]["last_event_id"], 1);
    assert_eq!(body["metadata"]["event_count"], 1);
    assert!(body["projection"]["projection_hash"]
        .as_str()
        .unwrap()
        .starts_with("sha256:"));
}

#[tokio::test]
async fn http_router_searches_the_canonical_store() {
    use axum::body::Body;
    use axum::http::{Request, StatusCode};
    use tower::ServiceExt as _;

    let store: SharedSessionStore = Arc::new(MemorySessionStore::with_hooks(StoreHooks::default()));
    let router = api::sessions_router(store.clone());
    let meta = store
        .create(CreateSession {
            tenant_id: Some("tenant-a".to_string()),
            project_scope: Some("project-a".to_string()),
            ..CreateSession::default()
        })
        .await
        .expect("create");
    store
        .append(
            &meta.id,
            AppendEvent::new(
                SessionEventKind::Message,
                json!({"text": "canonical transcript needle"}),
            ),
        )
        .await
        .expect("append");

    let response = router
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/sessions/search")
                .header("content-type", "application/json")
                .body(Body::from(
                    json!({
                        "query": "transcript needle",
                        "mode": "hybrid",
                        "filter": {
                            "tenant_id": "tenant-a",
                            "project_scope": "project-a",
                            "session_id": meta.id,
                        },
                    })
                    .to_string(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(response.status(), StatusCode::OK);
    let bytes = axum::body::to_bytes(response.into_body(), 1 << 20)
        .await
        .unwrap();
    let body: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
    assert_eq!(body["requested_mode"], "hybrid");
    assert_eq!(body["effective_mode"], "fts");
    assert_eq!(body["semantic_floor"], true);
    assert_eq!(body["hits"].as_array().unwrap().len(), 1);
    assert_eq!(body["hits"][0]["session_id"], meta.id);
    assert_eq!(body["hits"][0]["event_id"], 1);
}