fusillade 17.3.0

A daemon implementation for sending batched LLM requests with efficient request coalescing
Documentation
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
//! Integration tests for the response_steps storage layer.
//!
//! Each test gets a fresh sqlx pool via `#[sqlx::test]`, which automatically
//! runs every migration in `migrations/`. We insert a minimal request row by
//! hand (avoiding the full file/template/batch chain) since these tests are
//! scoped to the response_step module only.
//!
//! The new semantics (see plan
//! `docs/plans/2026-04-30-response-steps-request-linkage.md`):
//!
//! - `response_steps.request_id` is `Option<RequestId>` and points at the
//!   sub-request fusillade row created for *this* step's HTTP fire (one
//!   row per `model_call`); `None` for `tool_call` steps.
//! - `parent_step_id` points at the head (root) step of the chain;
//!   `None` only on the head itself.
//! - `prev_step_id` is a tree edge — multiple steps may share one
//!   (parallel tool_calls; sub-agent head + outer continuation).
//! - There is no chain-uniqueness constraint; idempotency is the
//!   transition function's responsibility (chain-walk frontier check).

use fusillade::request::RequestId;
use fusillade::response_step::{CreateStepInput, ResponseStepStore, StepKind, StepState};
use fusillade::{PostgresResponseStepManager, TestDbPools};
use serde_json::json;
use uuid::Uuid;

/// Insert a minimal `requests` row so `response_steps.request_id`'s FK is
/// satisfied. Each call returns a fresh `RequestId` so callers can model
/// "one fusillade row per model_call step" naturally.
async fn insert_request(pool: &sqlx::PgPool) -> RequestId {
    let template_id = Uuid::new_v4();
    let request_id = Uuid::new_v4();

    sqlx::query(
        "INSERT INTO request_templates \
         (id, file_id, custom_id, endpoint, method, path, body, model, api_key, body_byte_size) \
         VALUES ($1, NULL, NULL, $2, 'POST', '/v1/responses', '{}', 'test-model', '', 0)",
    )
    .bind(template_id)
    .bind("http://upstream")
    .execute(pool)
    .await
    .expect("insert template");

    sqlx::query(
        "INSERT INTO requests \
         (id, batch_id, template_id, model, custom_id, state, created_by) \
         VALUES ($1, NULL, $2, 'test-model', NULL, 'pending', 'test-user')",
    )
    .bind(request_id)
    .bind(template_id)
    .execute(pool)
    .await
    .expect("insert request");

    RequestId(request_id)
}

#[sqlx::test]
async fn create_and_fetch_head_step(pool: sqlx::PgPool) {
    let request_id = insert_request(&pool).await;
    let pools = TestDbPools::new(pool).await.unwrap();
    let store = PostgresResponseStepManager::new(pools);

    let step_id = store
        .create_step(CreateStepInput {
            id: None,
            request_id: Some(request_id),
            prev_step_id: None,
            parent_step_id: None,
            step_kind: StepKind::ModelCall,
            step_sequence: 1,
            request_payload: json!({"messages": [{"role": "user", "content": "hi"}]}),
        })
        .await
        .unwrap();

    let step = store.get_step(step_id).await.unwrap().unwrap();
    assert_eq!(step.id, step_id);
    assert_eq!(step.request_id, Some(request_id));
    assert_eq!(step.parent_step_id, None);
    assert_eq!(step.prev_step_id, None);
    assert_eq!(step.step_kind, StepKind::ModelCall);
    assert_eq!(step.state, StepState::Pending);
    assert_eq!(step.step_sequence, 1);
}

#[sqlx::test]
async fn tool_call_step_has_null_request_id(pool: sqlx::PgPool) {
    let head_request_id = insert_request(&pool).await;
    let pools = TestDbPools::new(pool).await.unwrap();
    let store = PostgresResponseStepManager::new(pools);

    let head_id = store
        .create_step(CreateStepInput {
            id: None,
            request_id: Some(head_request_id),
            prev_step_id: None,
            parent_step_id: None,
            step_kind: StepKind::ModelCall,
            step_sequence: 1,
            request_payload: json!({"messages": []}),
        })
        .await
        .unwrap();

    // tool_call step: request_id is None (tool dispatch lives outside requests).
    let tool_step_id = store
        .create_step(CreateStepInput {
            id: None,
            request_id: None,
            prev_step_id: Some(head_id),
            parent_step_id: Some(head_id),
            step_kind: StepKind::ToolCall,
            step_sequence: 2,
            request_payload: json!({"name": "get_weather", "args": {"city": "Paris"}}),
        })
        .await
        .unwrap();

    let step = store.get_step(tool_step_id).await.unwrap().unwrap();
    assert_eq!(step.request_id, None);
    assert_eq!(step.step_kind, StepKind::ToolCall);
}

#[sqlx::test]
async fn step_kind_request_id_check_constraint_rejects_invalid_combos(pool: sqlx::PgPool) {
    // The DB-level CHECK constraint enforces:
    //   model_call ⇒ request_id IS NOT NULL  (the sub-request fusillade row)
    //   tool_call  ⇒ request_id IS NULL      (analytics live in tool_call_analytics)
    // Without it, get_step_by_request and analytics attribution would
    // have to defend against malformed rows at every read.
    let head_request_id = insert_request(&pool).await;
    let pools = TestDbPools::new(pool).await.unwrap();
    let store = PostgresResponseStepManager::new(pools);

    // model_call with NULL request_id is rejected.
    let model_no_request = store
        .create_step(CreateStepInput {
            id: None,
            request_id: None,
            prev_step_id: None,
            parent_step_id: None,
            step_kind: StepKind::ModelCall,
            step_sequence: 1,
            request_payload: json!({}),
        })
        .await;
    assert!(
        model_no_request.is_err(),
        "model_call with NULL request_id should violate the CHECK constraint"
    );

    // tool_call with non-NULL request_id is rejected.
    let tool_with_request = store
        .create_step(CreateStepInput {
            id: None,
            request_id: Some(head_request_id),
            prev_step_id: None,
            parent_step_id: None,
            step_kind: StepKind::ToolCall,
            step_sequence: 1,
            request_payload: json!({}),
        })
        .await;
    assert!(
        tool_with_request.is_err(),
        "tool_call with non-NULL request_id should violate the CHECK constraint"
    );
}

#[sqlx::test]
async fn list_chain_returns_head_plus_descendants_in_order(pool: sqlx::PgPool) {
    let pools = TestDbPools::new(pool.clone()).await.unwrap();
    let store = PostgresResponseStepManager::new(pools);

    // Head (model_call). request_id = first sub-request row.
    let head_req = insert_request(&pool).await;
    let head_id = store
        .create_step(CreateStepInput {
            id: None,
            request_id: Some(head_req),
            prev_step_id: None,
            parent_step_id: None,
            step_kind: StepKind::ModelCall,
            step_sequence: 1,
            request_payload: json!({"step": "head"}),
        })
        .await
        .unwrap();

    // tool_call (no request_id).
    let tool_id = store
        .create_step(CreateStepInput {
            id: None,
            request_id: None,
            prev_step_id: Some(head_id),
            parent_step_id: Some(head_id),
            step_kind: StepKind::ToolCall,
            step_sequence: 2,
            request_payload: json!({"step": "tool"}),
        })
        .await
        .unwrap();

    // Second model_call (its own sub-request row).
    let second_req = insert_request(&pool).await;
    let _second_id = store
        .create_step(CreateStepInput {
            id: None,
            request_id: Some(second_req),
            prev_step_id: Some(tool_id),
            parent_step_id: Some(head_id),
            step_kind: StepKind::ModelCall,
            step_sequence: 3,
            request_payload: json!({"step": "second_model"}),
        })
        .await
        .unwrap();

    let chain = store.list_chain(head_id).await.unwrap();
    assert_eq!(chain.len(), 3);
    assert_eq!(chain[0].step_sequence, 1);
    assert_eq!(chain[0].id, head_id);
    assert_eq!(chain[0].parent_step_id, None);
    assert_eq!(chain[1].step_sequence, 2);
    assert_eq!(chain[1].step_kind, StepKind::ToolCall);
    assert_eq!(chain[1].parent_step_id, Some(head_id));
    assert_eq!(chain[2].step_sequence, 3);
    assert_eq!(chain[2].request_id, Some(second_req));
    assert_eq!(chain[2].parent_step_id, Some(head_id));
}

#[sqlx::test]
async fn list_chain_isolates_chains_from_one_another(pool: sqlx::PgPool) {
    // Two independent responses: each is its own head + child. list_chain on
    // one head must not return the other's steps.
    let pools = TestDbPools::new(pool.clone()).await.unwrap();
    let store = PostgresResponseStepManager::new(pools);

    let r1 = insert_request(&pool).await;
    let head1 = store
        .create_step(CreateStepInput {
            id: None,
            request_id: Some(r1),
            prev_step_id: None,
            parent_step_id: None,
            step_kind: StepKind::ModelCall,
            step_sequence: 1,
            request_payload: json!({"chain": 1}),
        })
        .await
        .unwrap();
    let r1b = insert_request(&pool).await;
    let _child1 = store
        .create_step(CreateStepInput {
            id: None,
            request_id: Some(r1b),
            prev_step_id: Some(head1),
            parent_step_id: Some(head1),
            step_kind: StepKind::ModelCall,
            step_sequence: 2,
            request_payload: json!({"chain": 1, "step": "child"}),
        })
        .await
        .unwrap();

    let r2 = insert_request(&pool).await;
    let head2 = store
        .create_step(CreateStepInput {
            id: None,
            request_id: Some(r2),
            prev_step_id: None,
            parent_step_id: None,
            step_kind: StepKind::ModelCall,
            step_sequence: 1,
            request_payload: json!({"chain": 2}),
        })
        .await
        .unwrap();

    let chain1 = store.list_chain(head1).await.unwrap();
    assert_eq!(chain1.len(), 2);
    let chain2 = store.list_chain(head2).await.unwrap();
    assert_eq!(chain2.len(), 1);
    assert_eq!(chain2[0].id, head2);
}

#[sqlx::test]
async fn get_step_by_request_finds_model_call_owner(pool: sqlx::PgPool) {
    let head_req = insert_request(&pool).await;
    let pools = TestDbPools::new(pool.clone()).await.unwrap();
    let store = PostgresResponseStepManager::new(pools);

    let head_id = store
        .create_step(CreateStepInput {
            id: None,
            request_id: Some(head_req),
            prev_step_id: None,
            parent_step_id: None,
            step_kind: StepKind::ModelCall,
            step_sequence: 1,
            request_payload: json!({}),
        })
        .await
        .unwrap();

    let found = store.get_step_by_request(head_req).await.unwrap().unwrap();
    assert_eq!(found.id, head_id);

    // A non-step-bound request (e.g. a single-step chat/completions row) returns None.
    let unrelated = insert_request(&pool).await;
    assert!(
        store
            .get_step_by_request(unrelated)
            .await
            .unwrap()
            .is_none()
    );
}

#[sqlx::test]
async fn parallel_tool_calls_with_same_prev_succeed(pool: sqlx::PgPool) {
    // Branch point: a model_call returning multiple tool_calls. All
    // tool_call children share (parent_step_id, prev_step_id, step_kind);
    // there is no DB-level uniqueness, so insertion should succeed.
    let head_req = insert_request(&pool).await;
    let pools = TestDbPools::new(pool.clone()).await.unwrap();
    let store = PostgresResponseStepManager::new(pools);

    let head_id = store
        .create_step(CreateStepInput {
            id: None,
            request_id: Some(head_req),
            prev_step_id: None,
            parent_step_id: None,
            step_kind: StepKind::ModelCall,
            step_sequence: 1,
            request_payload: json!({}),
        })
        .await
        .unwrap();

    for seq in 2..=4 {
        store
            .create_step(CreateStepInput {
                id: None,
                request_id: None,
                prev_step_id: Some(head_id),
                parent_step_id: Some(head_id),
                step_kind: StepKind::ToolCall,
                step_sequence: seq,
                request_payload: json!({"tool_index": seq - 2}),
            })
            .await
            .unwrap_or_else(|e| panic!("parallel tool_call insert failed at seq {seq}: {e}"));
    }

    let chain = store.list_chain(head_id).await.unwrap();
    assert_eq!(chain.len(), 4);
    let tool_calls: Vec<_> = chain
        .iter()
        .filter(|s| s.step_kind == StepKind::ToolCall)
        .collect();
    assert_eq!(tool_calls.len(), 3);
}

#[sqlx::test]
async fn lifecycle_transitions(pool: sqlx::PgPool) {
    let request_id = insert_request(&pool).await;
    let pools = TestDbPools::new(pool).await.unwrap();
    let store = PostgresResponseStepManager::new(pools);

    let step_id = store
        .create_step(CreateStepInput {
            id: None,
            request_id: Some(request_id),
            prev_step_id: None,
            parent_step_id: None,
            step_kind: StepKind::ModelCall,
            step_sequence: 1,
            request_payload: json!({}),
        })
        .await
        .unwrap();

    assert_eq!(
        store.get_step(step_id).await.unwrap().unwrap().state,
        StepState::Pending
    );

    store.mark_step_processing(step_id).await.unwrap();
    let step = store.get_step(step_id).await.unwrap().unwrap();
    assert_eq!(step.state, StepState::Processing);
    assert!(step.started_at.is_some());

    // Idempotency: re-marking processing on an already-processing step is a no-op.
    store.mark_step_processing(step_id).await.unwrap();

    store
        .complete_step(step_id, json!({"result": "ok"}))
        .await
        .unwrap();
    let step = store.get_step(step_id).await.unwrap().unwrap();
    assert_eq!(step.state, StepState::Completed);
    assert_eq!(step.response_payload, Some(json!({"result": "ok"})));
    assert!(step.completed_at.is_some());

    // Completing again is rejected (terminal state).
    let err = store
        .complete_step(step_id, json!({"second": true}))
        .await
        .unwrap_err()
        .to_string();
    assert!(err.contains("not in completable state"));
}

#[sqlx::test]
async fn fail_step_records_error_payload(pool: sqlx::PgPool) {
    let request_id = insert_request(&pool).await;
    let pools = TestDbPools::new(pool).await.unwrap();
    let store = PostgresResponseStepManager::new(pools);

    let step_id = store
        .create_step(CreateStepInput {
            id: None,
            request_id: Some(request_id),
            prev_step_id: None,
            parent_step_id: None,
            step_kind: StepKind::ModelCall,
            step_sequence: 1,
            request_payload: json!({}),
        })
        .await
        .unwrap();

    store
        .fail_step(step_id, json!({"type": "upstream_error", "code": 503}))
        .await
        .unwrap();
    let step = store.get_step(step_id).await.unwrap().unwrap();
    assert_eq!(step.state, StepState::Failed);
    assert!(step.failed_at.is_some());
    assert_eq!(
        step.error,
        Some(json!({"type": "upstream_error", "code": 503}))
    );
}

#[sqlx::test]
async fn cancel_step_records_canceled_at_and_blocks_terminal_transitions(pool: sqlx::PgPool) {
    let request_id = insert_request(&pool).await;
    let pools = TestDbPools::new(pool).await.unwrap();
    let store = PostgresResponseStepManager::new(pools);

    let step_id = store
        .create_step(CreateStepInput {
            id: None,
            request_id: Some(request_id),
            prev_step_id: None,
            parent_step_id: None,
            step_kind: StepKind::ModelCall,
            step_sequence: 1,
            request_payload: json!({}),
        })
        .await
        .unwrap();

    store.cancel_step(step_id).await.unwrap();
    let step = store.get_step(step_id).await.unwrap().unwrap();
    assert_eq!(step.state, StepState::Canceled);
    assert!(step.canceled_at.is_some());

    // Subsequent terminal transitions are rejected.
    let err = store
        .complete_step(step_id, json!({}))
        .await
        .unwrap_err()
        .to_string();
    assert!(err.contains("not in completable state"));
}

#[sqlx::test]
async fn requeue_for_retry_increments_attempt(pool: sqlx::PgPool) {
    let request_id = insert_request(&pool).await;
    let pools = TestDbPools::new(pool).await.unwrap();
    let store = PostgresResponseStepManager::new(pools);

    let step_id = store
        .create_step(CreateStepInput {
            id: None,
            request_id: Some(request_id),
            prev_step_id: None,
            parent_step_id: None,
            step_kind: StepKind::ModelCall,
            step_sequence: 1,
            request_payload: json!({}),
        })
        .await
        .unwrap();
    store.mark_step_processing(step_id).await.unwrap();

    store.requeue_step_for_retry(step_id).await.unwrap();
    let step = store.get_step(step_id).await.unwrap().unwrap();
    assert_eq!(step.state, StepState::Pending);
    assert_eq!(step.retry_attempt, 1);
    assert!(step.started_at.is_none());
}