ironflow-api 2.31.4

REST API for ironflow run management and observability
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
//! `GET /api/v1/runs/:id/logs` -- Retrieve persisted log lines for a run.

use std::collections::HashMap;

use axum::Json;
use axum::extract::{Path, Query, State};
use axum::response::IntoResponse;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use uuid::Uuid;

use ironflow_auth::extractor::Authenticated;
#[cfg(feature = "openapi")]
use ironflow_store::entities::LogEntry;
use ironflow_store::entities::{LogFilter, LogStream};
use ironflow_types::{ApiMeta, ApiResponse};

use crate::error::ApiError;
use crate::state::AppState;

const MAX_LIMIT: u32 = 1000;
const DEFAULT_LIMIT: u32 = 100;

/// Query parameters for listing run logs.
#[derive(Debug, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::IntoParams, utoipa::ToSchema))]
pub struct GetRunLogsQuery {
    /// Filter by step ID.
    pub step_id: Option<Uuid>,
    /// Filter by output stream (`stdout`, `stderr`, `system`).
    pub stream: Option<LogStream>,
    /// Cursor for pagination (last entry ID from previous page).
    pub cursor: Option<Uuid>,
    /// Number of entries to return (default: 100, max: 1000).
    pub limit: Option<u32>,
}

/// Cursor-based pagination metadata for log entries.
#[derive(Debug, Serialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
pub struct LogCursorMeta {
    /// Cursor to pass for the next page. `None` when there are no more entries.
    pub next_cursor: Option<Uuid>,
    /// Whether more entries exist after this page.
    pub has_more: bool,
}

/// Retrieve persisted log lines for a run with cursor-based pagination.
///
/// Returns log entries ordered by time (UUID v7 ascending). Use the
/// `cursor` query parameter with the last entry's `id` to fetch the
/// next page.
#[cfg_attr(
    feature = "openapi",
    utoipa::path(
        get,
        path = "/api/v1/runs/{id}/logs",
        tags = ["runs"],
        params(
            ("id" = Uuid, Path, description = "Run ID"),
            GetRunLogsQuery,
        ),
        responses(
            (status = 200, description = "Log entries with cursor-based pagination", body = Vec<LogEntry>),
            (status = 401, description = "Unauthorized"),
            (status = 404, description = "Run not found")
        ),
        security(("Bearer" = []))
    )
)]
pub async fn get_run_logs(
    _auth: Authenticated,
    State(state): State<AppState>,
    Path(run_id): Path<Uuid>,
    Query(params): Query<GetRunLogsQuery>,
) -> Result<impl IntoResponse, ApiError> {
    state.get_run_or_404(run_id).await?;

    let limit = match params.limit {
        Some(0) | None => DEFAULT_LIMIT,
        Some(l) => l.min(MAX_LIMIT),
    };

    let filter = LogFilter {
        step_id: params.step_id,
        stream: params.stream,
    };

    let entries = state
        .store
        .get_logs(run_id, filter, params.cursor, limit + 1)
        .await?;

    let has_more = entries.len() > limit as usize;
    let entries: Vec<_> = entries.into_iter().take(limit as usize).collect();
    let next_cursor = if has_more {
        entries.last().map(|e| e.id)
    } else {
        None
    };

    let cursor_meta = LogCursorMeta {
        next_cursor,
        has_more,
    };
    let extra = match serde_json::to_value(cursor_meta) {
        Ok(Value::Object(map)) => map.into_iter().collect(),
        _ => HashMap::new(),
    };

    Ok(Json(ApiResponse {
        data: entries,
        meta: Some(ApiMeta {
            page: None,
            per_page: None,
            total: None,
            extra,
        }),
    }))
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;
    use std::sync::Arc;

    use axum::Router;
    use axum::body::Body;
    use axum::http::{Request, StatusCode};
    use axum::routing::get;
    use http_body_util::BodyExt;
    use serde_json::{Value as JsonValue, from_slice, json};
    use tokio::sync::broadcast;
    use tower::ServiceExt;
    use uuid::Uuid;

    use ironflow_auth::jwt::{AccessToken, JwtConfig};
    use ironflow_core::providers::claude::ClaudeCodeProvider;
    use ironflow_engine::engine::Engine;
    use ironflow_engine::notify::Event;
    use ironflow_store::entities::{LogStream, NewLogEntries, NewRun, TriggerKind};
    use ironflow_store::memory::InMemoryStore;

    use super::*;

    fn test_state() -> AppState {
        let store = Arc::new(InMemoryStore::new());
        let provider = Arc::new(ClaudeCodeProvider::new());
        let engine = Arc::new(Engine::new(store.clone(), provider));
        let jwt_config = Arc::new(JwtConfig {
            secret: "test-secret".to_string(),
            access_token_ttl_secs: 900,
            refresh_token_ttl_secs: 604800,
            cookie_domain: None,
            cookie_secure: false,
        });
        let (event_sender, _) = broadcast::channel::<Event>(1);
        AppState::new(
            store,
            engine,
            jwt_config,
            "test-worker-token".to_string(),
            event_sender,
        )
    }

    fn make_auth_header(state: &AppState) -> String {
        let user_id = Uuid::now_v7();
        let token = AccessToken::for_user(user_id, "testuser", false, &state.jwt_config).unwrap();
        format!("Bearer {}", token.0)
    }

    async fn create_run(state: &AppState) -> Uuid {
        state
            .store
            .create_run(NewRun {
                created_by: None,
                workflow_name: "test".to_string(),
                trigger: TriggerKind::Manual,
                payload: json!({}),
                max_retries: 0,
                handler_version: None,
                labels: HashMap::new(),
                scheduled_at: None,
                idempotency_key: None,
                max_cost_usd: None,
            })
            .await
            .unwrap()
            .into_run()
            .id
    }

    async fn push_logs(state: &AppState, run_id: Uuid, step_id: Uuid, stream: LogStream, n: usize) {
        state
            .store
            .append_logs(NewLogEntries {
                run_id,
                step_id,
                step_name: "build".to_string(),
                stream,
                lines: (0..n).map(|i| format!("line {i}")).collect(),
            })
            .await
            .unwrap();
    }

    #[tokio::test]
    async fn returns_persisted_logs() {
        let state = test_state();
        let auth_header = make_auth_header(&state);
        let run_id = create_run(&state).await;
        let step_id = Uuid::now_v7();

        push_logs(&state, run_id, step_id, LogStream::Stdout, 3).await;

        let app = Router::new()
            .route("/runs/{id}/logs", get(get_run_logs))
            .with_state(state);

        let req = Request::builder()
            .uri(format!("/runs/{run_id}/logs"))
            .header("authorization", auth_header)
            .body(Body::empty())
            .unwrap();

        let resp = app.oneshot(req).await.unwrap();
        assert_eq!(resp.status(), StatusCode::OK);

        let body = resp.into_body().collect().await.unwrap().to_bytes();
        let json_val: JsonValue = from_slice(&body).unwrap();
        assert_eq!(json_val["data"].as_array().unwrap().len(), 3);
        assert_eq!(json_val["data"][0]["line"], "line 0");
        assert_eq!(json_val["data"][0]["stream"], "stdout");
        assert_eq!(json_val["meta"]["has_more"], false);
        assert!(json_val["meta"]["next_cursor"].is_null());
    }

    #[tokio::test]
    async fn filters_by_step_id() {
        let state = test_state();
        let auth_header = make_auth_header(&state);
        let run_id = create_run(&state).await;
        let step_a = Uuid::now_v7();
        let step_b = Uuid::now_v7();

        push_logs(&state, run_id, step_a, LogStream::Stdout, 2).await;
        push_logs(&state, run_id, step_b, LogStream::Stdout, 3).await;

        let app = Router::new()
            .route("/runs/{id}/logs", get(get_run_logs))
            .with_state(state);

        let req = Request::builder()
            .uri(format!("/runs/{run_id}/logs?step_id={step_a}"))
            .header("authorization", auth_header)
            .body(Body::empty())
            .unwrap();

        let resp = app.oneshot(req).await.unwrap();
        assert_eq!(resp.status(), StatusCode::OK);

        let body = resp.into_body().collect().await.unwrap().to_bytes();
        let json_val: JsonValue = from_slice(&body).unwrap();
        assert_eq!(json_val["data"].as_array().unwrap().len(), 2);
    }

    #[tokio::test]
    async fn filters_by_stream() {
        let state = test_state();
        let auth_header = make_auth_header(&state);
        let run_id = create_run(&state).await;
        let step_id = Uuid::now_v7();

        push_logs(&state, run_id, step_id, LogStream::Stdout, 2).await;
        push_logs(&state, run_id, step_id, LogStream::Stderr, 1).await;

        let app = Router::new()
            .route("/runs/{id}/logs", get(get_run_logs))
            .with_state(state);

        let req = Request::builder()
            .uri(format!("/runs/{run_id}/logs?stream=stderr"))
            .header("authorization", auth_header)
            .body(Body::empty())
            .unwrap();

        let resp = app.oneshot(req).await.unwrap();
        let body = resp.into_body().collect().await.unwrap().to_bytes();
        let json_val: JsonValue = from_slice(&body).unwrap();
        assert_eq!(json_val["data"].as_array().unwrap().len(), 1);
        assert_eq!(json_val["data"][0]["stream"], "stderr");
    }

    #[tokio::test]
    async fn cursor_based_pagination() {
        let state = test_state();
        let auth_header = make_auth_header(&state);
        let run_id = create_run(&state).await;
        let step_id = Uuid::now_v7();

        push_logs(&state, run_id, step_id, LogStream::Stdout, 5).await;

        let app = Router::new()
            .route("/runs/{id}/logs", get(get_run_logs))
            .with_state(state);

        let req = Request::builder()
            .uri(format!("/runs/{run_id}/logs?limit=2"))
            .header("authorization", &auth_header)
            .body(Body::empty())
            .unwrap();

        let resp = app.clone().oneshot(req).await.unwrap();
        let body = resp.into_body().collect().await.unwrap().to_bytes();
        let page1: JsonValue = from_slice(&body).unwrap();
        assert_eq!(page1["data"].as_array().unwrap().len(), 2);
        assert_eq!(page1["meta"]["has_more"], true);

        let cursor = page1["meta"]["next_cursor"].as_str().unwrap();

        let req = Request::builder()
            .uri(format!("/runs/{run_id}/logs?limit=2&cursor={cursor}"))
            .header("authorization", &auth_header)
            .body(Body::empty())
            .unwrap();

        let resp = app.clone().oneshot(req).await.unwrap();
        let body = resp.into_body().collect().await.unwrap().to_bytes();
        let page2: JsonValue = from_slice(&body).unwrap();
        assert_eq!(page2["data"].as_array().unwrap().len(), 2);
        assert_eq!(page2["data"][0]["line"], "line 2");
        assert_eq!(page2["meta"]["has_more"], true);

        let cursor = page2["meta"]["next_cursor"].as_str().unwrap();

        let req = Request::builder()
            .uri(format!("/runs/{run_id}/logs?limit=2&cursor={cursor}"))
            .header("authorization", &auth_header)
            .body(Body::empty())
            .unwrap();

        let resp = app.oneshot(req).await.unwrap();
        let body = resp.into_body().collect().await.unwrap().to_bytes();
        let page3: JsonValue = from_slice(&body).unwrap();
        assert_eq!(page3["data"].as_array().unwrap().len(), 1);
        assert_eq!(page3["meta"]["has_more"], false);
        assert!(page3["meta"]["next_cursor"].is_null());
    }

    #[tokio::test]
    async fn run_not_found_returns_404() {
        let state = test_state();
        let auth_header = make_auth_header(&state);
        let app = Router::new()
            .route("/runs/{id}/logs", get(get_run_logs))
            .with_state(state);

        let req = Request::builder()
            .uri(format!("/runs/{}/logs", Uuid::now_v7()))
            .header("authorization", auth_header)
            .body(Body::empty())
            .unwrap();

        let resp = app.oneshot(req).await.unwrap();
        assert_eq!(resp.status(), StatusCode::NOT_FOUND);
    }

    #[tokio::test]
    async fn unauthenticated_returns_401() {
        let state = test_state();
        let run_id = create_run(&state).await;
        let app = Router::new()
            .route("/runs/{id}/logs", get(get_run_logs))
            .with_state(state);

        let req = Request::builder()
            .uri(format!("/runs/{run_id}/logs"))
            .body(Body::empty())
            .unwrap();

        let resp = app.oneshot(req).await.unwrap();
        assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
    }

    #[tokio::test]
    async fn limit_capped_at_1000() {
        let state = test_state();
        let auth_header = make_auth_header(&state);
        let run_id = create_run(&state).await;
        let step_id = Uuid::now_v7();

        push_logs(&state, run_id, step_id, LogStream::Stdout, 2).await;

        let app = Router::new()
            .route("/runs/{id}/logs", get(get_run_logs))
            .with_state(state);

        let req = Request::builder()
            .uri(format!("/runs/{run_id}/logs?limit=5000"))
            .header("authorization", auth_header)
            .body(Body::empty())
            .unwrap();

        let resp = app.oneshot(req).await.unwrap();
        assert_eq!(resp.status(), StatusCode::OK);

        let body = resp.into_body().collect().await.unwrap().to_bytes();
        let json_val: JsonValue = from_slice(&body).unwrap();
        assert_eq!(json_val["data"].as_array().unwrap().len(), 2);
    }

    #[tokio::test]
    async fn empty_logs_returns_empty_array() {
        let state = test_state();
        let auth_header = make_auth_header(&state);
        let run_id = create_run(&state).await;

        let app = Router::new()
            .route("/runs/{id}/logs", get(get_run_logs))
            .with_state(state);

        let req = Request::builder()
            .uri(format!("/runs/{run_id}/logs"))
            .header("authorization", auth_header)
            .body(Body::empty())
            .unwrap();

        let resp = app.oneshot(req).await.unwrap();
        assert_eq!(resp.status(), StatusCode::OK);

        let body = resp.into_body().collect().await.unwrap().to_bytes();
        let json_val: JsonValue = from_slice(&body).unwrap();
        assert_eq!(json_val["data"].as_array().unwrap().len(), 0);
        assert_eq!(json_val["meta"]["has_more"], false);
    }
}