ironbridge-cli 2.0.1

Universal chat session manager - harvest, merge, and analyze AI chat history from VS Code, Cursor, and other editors
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
// Copyright (c) 2024-2026 Nervosys LLC
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-IronBridge-Commercial
//! GraphQL API module
//!
//! Provides a GraphQL endpoint alongside REST for flexible querying.

use actix_web::{web, HttpResponse, Responder};
use async_graphql::{
    Context, EmptySubscription, FieldResult, InputObject, Object, Schema, SimpleObject, ID,
};
use async_graphql_actix_web::{GraphQLRequest, GraphQLResponse};
use chrono::{DateTime, Utc};
use rusqlite::OptionalExtension;
use std::sync::Arc;

use super::state::AppState;

/// GraphQL schema type
pub type IronBridgeSchema = Schema<QueryRoot, MutationRoot, EmptySubscription>;

// ============================================================================
// GraphQL Types
// ============================================================================

/// Workspace type for GraphQL
#[derive(SimpleObject, Clone)]
pub struct Workspace {
    /// Unique identifier
    pub id: ID,
    /// Workspace name
    pub name: String,
    /// Workspace path
    pub path: String,
    /// Provider (e.g., "vscode", "cursor")
    pub provider: String,
    /// Number of sessions
    pub session_count: i32,
    /// Last harvested timestamp
    pub last_harvested: Option<DateTime<Utc>>,
    /// Creation timestamp
    pub created_at: DateTime<Utc>,
}

/// Session type for GraphQL
#[derive(SimpleObject, Clone)]
pub struct Session {
    /// Unique identifier
    pub id: ID,
    /// Session title
    pub title: String,
    /// Workspace ID
    pub workspace_id: Option<ID>,
    /// Provider (e.g., "copilot", "cursor")
    pub provider: String,
    /// Model used
    pub model: Option<String>,
    /// Number of messages
    pub message_count: i32,
    /// Total tokens
    pub token_count: i32,
    /// Whether archived
    pub archived: bool,
    /// Tags
    pub tags: Vec<String>,
    /// Creation timestamp
    pub created_at: DateTime<Utc>,
    /// Last updated timestamp
    pub updated_at: DateTime<Utc>,
}

/// Message type for GraphQL
#[derive(SimpleObject, Clone)]
pub struct Message {
    /// Unique identifier
    pub id: ID,
    /// Session ID
    pub session_id: ID,
    /// Message role (user, assistant, system)
    pub role: String,
    /// Message content
    pub content: String,
    /// Model used
    pub model: Option<String>,
    /// Token count
    pub token_count: i32,
    /// Creation timestamp
    pub created_at: DateTime<Utc>,
}

/// Provider type for GraphQL
#[derive(SimpleObject, Clone)]
pub struct Provider {
    /// Unique identifier
    pub id: ID,
    /// Provider name
    pub name: String,
    /// Provider type
    pub provider_type: String,
    /// Whether enabled
    pub enabled: bool,
    /// Base URL
    pub base_url: Option<String>,
}

/// Agent type for GraphQL
#[derive(SimpleObject, Clone)]
pub struct Agent {
    /// Unique identifier
    pub id: ID,
    /// Agent name
    pub name: String,
    /// Description
    pub description: Option<String>,
    /// System prompt
    pub system_prompt: Option<String>,
    /// Provider ID
    pub provider_id: Option<ID>,
    /// Model
    pub model: String,
    /// Temperature
    pub temperature: f64,
    /// Creation timestamp
    pub created_at: DateTime<Utc>,
}

/// Statistics overview
#[derive(SimpleObject, Clone)]
pub struct StatsOverview {
    /// Total sessions
    pub total_sessions: i32,
    /// Total messages
    pub total_messages: i32,
    /// Total tokens
    pub total_tokens: i64,
    /// Active providers
    pub active_providers: i32,
    /// Total workspaces
    pub workspaces: i32,
    /// Sessions today
    pub sessions_today: i32,
    /// Messages today
    pub messages_today: i32,
}

/// Search result
#[derive(SimpleObject, Clone)]
pub struct SearchResult {
    /// Sessions matching query
    pub sessions: Vec<Session>,
    /// Messages matching query
    pub messages: Vec<Message>,
    /// Total count
    pub total: i32,
}

// ============================================================================
// Input Types
// ============================================================================

/// Input for creating a session
#[derive(InputObject)]
pub struct CreateSessionInput {
    pub title: String,
    pub workspace_id: Option<ID>,
    pub provider: String,
}

/// Input for updating a session
#[derive(InputObject)]
pub struct UpdateSessionInput {
    pub title: Option<String>,
    pub tags: Option<Vec<String>>,
    pub archived: Option<bool>,
}

/// Input for creating an agent
#[derive(InputObject)]
pub struct CreateAgentInput {
    pub name: String,
    pub description: Option<String>,
    pub system_prompt: Option<String>,
    pub provider_id: Option<ID>,
    pub model: String,
    pub temperature: Option<f64>,
}

/// Filter for sessions
#[derive(InputObject, Default)]
pub struct SessionFilter {
    pub workspace_id: Option<ID>,
    pub provider: Option<String>,
    pub archived: Option<bool>,
    pub search: Option<String>,
}

/// Pagination input
#[derive(InputObject, Default)]
pub struct Pagination {
    pub limit: Option<i32>,
    pub offset: Option<i32>,
}

// ============================================================================
// Query Root
// ============================================================================

pub struct QueryRoot;

/// Seconds since epoch -> `DateTime<Utc>`, clamping nonsense to the epoch
/// rather than panicking inside a resolver.
fn ts(seconds: i64) -> DateTime<Utc> {
    chrono::TimeZone::timestamp_opt(&Utc, seconds, 0)
        .single()
        .unwrap_or_else(|| chrono::TimeZone::timestamp_opt(&Utc, 0, 0).unwrap())
}

/// Clamp a caller-supplied page size. GraphQL exposes the same data as REST,
/// so it gets the same protection against one query draining a table.
fn page_size(requested: Option<i32>, default: i32) -> i32 {
    requested.unwrap_or(default).clamp(1, 500)
}

/// Two schemas for `sessions`/`messages` exist in the wild.
///
/// `sql/schema.sql` defines the richer shape (model, token_count, archived).
/// The harvest pipeline in `commands/harvest.rs` defines a narrower one, and
/// because both use `CREATE TABLE IF NOT EXISTS`, a database first written by
/// harvest keeps the narrow tables while still gaining the tables only
/// `schema.sql` declares. Selecting a column that is missing is a runtime
/// error, so the optional ones are probed and substituted with literals.
/// Note the implementation: `PRAGMA table_info`, not a trial `SELECT`.
/// SQLite treats a double-quoted identifier that resolves to no column as a
/// string literal, so `SELECT "model" FROM sessions` *succeeds* on a table
/// with no `model` column and would report every column as present.
fn has_column(conn: &rusqlite::Connection, table: &str, column: &str) -> bool {
    let Ok(mut stmt) = conn.prepare(&format!("PRAGMA table_info(\"{}\")", table)) else {
        return false;
    };
    let Ok(rows) = stmt.query_map([], |row| row.get::<_, String>(1)) else {
        return false;
    };
    // Collected rather than folded inline: the iterator borrows `stmt`, which
    // must be released before returning.
    let names: Vec<String> = rows.filter_map(Result::ok).collect();
    names.iter().any(|name| name.eq_ignore_ascii_case(column))
}

/// SQL fragments for the session columns that only one schema has.
struct SessionCols {
    model: &'static str,
    tokens: &'static str,
    archived: &'static str,
}

impl SessionCols {
    fn probe(conn: &rusqlite::Connection) -> Self {
        Self {
            model: if has_column(conn, "sessions", "model") {
                "model"
            } else {
                "NULL"
            },
            tokens: if has_column(conn, "sessions", "token_count") {
                "COALESCE(token_count, 0)"
            } else {
                "0"
            },
            archived: if has_column(conn, "sessions", "archived") {
                "COALESCE(archived, 0)"
            } else {
                "0"
            },
        }
    }

    /// The ten-column projection every session query shares, in the order the
    /// row mapper expects.
    fn select_list(&self) -> String {
        format!(
            "id, COALESCE(title, ''), workspace_id, provider, {}, \
             COALESCE(message_count, 0), {}, {}, \
             COALESCE(created_at, 0), COALESCE(updated_at, 0)",
            self.model, self.tokens, self.archived
        )
    }
}

fn session_from_row(row: &rusqlite::Row<'_>) -> rusqlite::Result<Session> {
    Ok(Session {
        id: ID::from(row.get::<_, String>(0)?),
        title: row.get(1)?,
        workspace_id: row.get::<_, Option<String>>(2)?.map(ID::from),
        provider: row.get(3)?,
        model: row.get(4)?,
        message_count: row.get::<_, i64>(5)? as i32,
        token_count: row.get::<_, i64>(6)? as i32,
        archived: row.get::<_, i64>(7)? != 0,
        tags: Vec::new(),
        created_at: ts(row.get::<_, i64>(8)?),
        updated_at: ts(row.get::<_, i64>(9)?),
    })
}

/// Message projection, likewise tolerant of the narrower harvest schema.
/// `id` is cast because harvest declares it `INTEGER AUTOINCREMENT`.
fn message_select_list(conn: &rusqlite::Connection) -> String {
    let model = if has_column(conn, "messages", "model") {
        "model"
    } else {
        "NULL"
    };
    let tokens = if has_column(conn, "messages", "token_count") {
        "COALESCE(token_count, 0)"
    } else {
        "0"
    };
    format!(
        "CAST(id AS TEXT), session_id, role, COALESCE(content, ''), {}, {}, \
         COALESCE(created_at, 0)",
        model, tokens
    )
}

fn message_from_row(row: &rusqlite::Row<'_>) -> rusqlite::Result<Message> {
    Ok(Message {
        id: ID::from(row.get::<_, String>(0)?),
        session_id: ID::from(row.get::<_, String>(1)?),
        role: row.get(2)?,
        content: row.get(3)?,
        model: row.get(4)?,
        token_count: row.get::<_, i64>(5)? as i32,
        created_at: ts(row.get::<_, i64>(6)?),
    })
}

#[Object]
impl QueryRoot {
    /// Get all workspaces
    async fn workspaces(
        &self,
        ctx: &Context<'_>,
        pagination: Option<Pagination>,
    ) -> FieldResult<Vec<Workspace>> {
        let state = ctx.data::<Arc<AppState>>()?;
        let pagination = pagination.unwrap_or_default();
        let limit = page_size(pagination.limit, 20);
        let offset = pagination.offset.unwrap_or(0).max(0);

        let db = state.db.lock().map_err(|_| "database lock poisoned")?;
        let mut stmt = db.conn.prepare(
            "SELECT w.id, w.name, COALESCE(w.path, ''), COALESCE(w.provider, ''),
                    COUNT(s.id) AS session_count,
                    COALESCE(w.created_at, 0),
                    COALESCE(MAX(s.updated_at), w.updated_at, 0)
             FROM workspaces w
             LEFT JOIN sessions s ON w.id = s.workspace_id
             GROUP BY w.id
             ORDER BY 7 DESC
             LIMIT ?1 OFFSET ?2",
        )?;
        let rows = stmt
            .query_map([limit as i64, offset as i64], |row| {
                Ok(Workspace {
                    id: ID::from(row.get::<_, String>(0)?),
                    name: row.get(1)?,
                    path: row.get(2)?,
                    provider: row.get(3)?,
                    session_count: row.get::<_, i64>(4)? as i32,
                    last_harvested: None,
                    created_at: ts(row.get::<_, i64>(5)?),
                })
            })?
            .collect::<rusqlite::Result<Vec<_>>>()?;
        Ok(rows)
    }

    /// Get a workspace by ID
    async fn workspace(&self, ctx: &Context<'_>, id: ID) -> FieldResult<Option<Workspace>> {
        let state = ctx.data::<Arc<AppState>>()?;
        let db = state.db.lock().map_err(|_| "database lock poisoned")?;

        let found = db
            .conn
            .query_row(
                "SELECT w.id, w.name, COALESCE(w.path, ''), COALESCE(w.provider, ''),
                        COUNT(s.id), COALESCE(w.created_at, 0)
                 FROM workspaces w
                 LEFT JOIN sessions s ON w.id = s.workspace_id
                 WHERE w.id = ?1
                 GROUP BY w.id",
                [id.to_string()],
                |row| {
                    Ok(Workspace {
                        id: ID::from(row.get::<_, String>(0)?),
                        name: row.get(1)?,
                        path: row.get(2)?,
                        provider: row.get(3)?,
                        session_count: row.get::<_, i64>(4)? as i32,
                        last_harvested: None,
                        created_at: ts(row.get::<_, i64>(5)?),
                    })
                },
            )
            .optional()?;
        Ok(found)
    }

    /// Get all sessions with optional filtering
    async fn sessions(
        &self,
        ctx: &Context<'_>,
        filter: Option<SessionFilter>,
        pagination: Option<Pagination>,
    ) -> FieldResult<Vec<Session>> {
        let state = ctx.data::<Arc<AppState>>()?;
        let filter = filter.unwrap_or_default();
        let pagination = pagination.unwrap_or_default();
        let limit = page_size(pagination.limit, 20);
        let offset = pagination.offset.unwrap_or(0).max(0);

        let db = state.db.lock().map_err(|_| "database lock poisoned")?;
        let cols = SessionCols::probe(&db.conn);
        // NULL-guarded predicates keep this one prepared statement rather than
        // building SQL per filter combination.
        let sql = format!(
            "SELECT {} FROM sessions
             WHERE (?1 IS NULL OR provider = ?1)
               AND (?2 IS NULL OR workspace_id = ?2)
             ORDER BY updated_at DESC
             LIMIT ?3 OFFSET ?4",
            cols.select_list()
        );
        let mut stmt = db.conn.prepare(&sql)?;
        let rows = stmt
            .query_map(
                rusqlite::params![
                    filter.provider,
                    filter.workspace_id.map(|w| w.to_string()),
                    limit as i64,
                    offset as i64
                ],
                session_from_row,
            )?
            .collect::<rusqlite::Result<Vec<_>>>()?;
        Ok(rows)
    }

    /// Get a session by ID
    async fn session(&self, ctx: &Context<'_>, id: ID) -> FieldResult<Option<Session>> {
        let state = ctx.data::<Arc<AppState>>()?;
        let db = state.db.lock().map_err(|_| "database lock poisoned")?;

        let cols = SessionCols::probe(&db.conn);
        let found = db
            .conn
            .query_row(
                &format!("SELECT {} FROM sessions WHERE id = ?1", cols.select_list()),
                [id.to_string()],
                session_from_row,
            )
            .optional()?;
        Ok(found)
    }

    /// Get messages for a session
    async fn messages(
        &self,
        ctx: &Context<'_>,
        session_id: ID,
        pagination: Option<Pagination>,
    ) -> FieldResult<Vec<Message>> {
        let state = ctx.data::<Arc<AppState>>()?;
        let pagination = pagination.unwrap_or_default();
        let limit = page_size(pagination.limit, 100);
        let offset = pagination.offset.unwrap_or(0).max(0);

        let db = state.db.lock().map_err(|_| "database lock poisoned")?;
        let sql = format!(
            "SELECT {} FROM messages
             WHERE session_id = ?1
             ORDER BY created_at ASC, rowid ASC
             LIMIT ?2 OFFSET ?3",
            message_select_list(&db.conn)
        );
        let mut stmt = db.conn.prepare(&sql)?;
        let rows = stmt
            .query_map(
                rusqlite::params![session_id.to_string(), limit as i64, offset as i64],
                message_from_row,
            )?
            .collect::<rusqlite::Result<Vec<_>>>()?;
        Ok(rows)
    }

    /// Get all providers actually present in the harvested data
    async fn providers(&self, ctx: &Context<'_>) -> FieldResult<Vec<Provider>> {
        let state = ctx.data::<Arc<AppState>>()?;
        let db = state.db.lock().map_err(|_| "database lock poisoned")?;

        // Derived from stored sessions rather than a hardcoded list, so the
        // result describes this installation instead of the product brochure.
        let mut stmt = db.conn.prepare(
            "SELECT DISTINCT provider FROM sessions WHERE provider IS NOT NULL ORDER BY provider",
        )?;
        let rows = stmt
            .query_map([], |row| {
                let name: String = row.get(0)?;
                Ok(Provider {
                    id: ID::from(name.clone()),
                    name: name.clone(),
                    provider_type: name,
                    enabled: true,
                    base_url: None,
                })
            })?
            .collect::<rusqlite::Result<Vec<_>>>()?;
        Ok(rows)
    }

    /// Get all agents
    async fn agents(&self, ctx: &Context<'_>) -> FieldResult<Vec<Agent>> {
        let state = ctx.data::<Arc<AppState>>()?;
        let db = state.db.lock().map_err(|_| "database lock poisoned")?;

        let mut stmt = db.conn.prepare(
            "SELECT id, name, COALESCE(description, ''), COALESCE(model, ''),
                    instruction, provider, temperature, COALESCE(created_at, 0)
             FROM agents ORDER BY updated_at DESC",
        )?;
        let rows = stmt
            .query_map([], |row| {
                Ok(Agent {
                    id: ID::from(row.get::<_, String>(0)?),
                    name: row.get(1)?,
                    description: Some(row.get::<_, String>(2)?),
                    model: row.get(3)?,
                    system_prompt: row.get::<_, Option<String>>(4)?,
                    provider_id: row.get::<_, Option<String>>(5)?.map(ID::from),
                    temperature: row.get::<_, Option<f64>>(6)?.unwrap_or(0.7),
                    created_at: ts(row.get::<_, i64>(7)?),
                })
            })?
            .collect::<rusqlite::Result<Vec<_>>>()?;
        Ok(rows)
    }

    /// Get an agent by ID
    async fn agent(&self, ctx: &Context<'_>, id: ID) -> FieldResult<Option<Agent>> {
        let state = ctx.data::<Arc<AppState>>()?;
        let db = state.db.lock().map_err(|_| "database lock poisoned")?;

        let found = db
            .conn
            .query_row(
                "SELECT id, name, COALESCE(description, ''), COALESCE(model, ''),
                        instruction, provider, temperature, COALESCE(created_at, 0)
                 FROM agents WHERE id = ?1",
                [id.to_string()],
                |row| {
                    Ok(Agent {
                        id: ID::from(row.get::<_, String>(0)?),
                        name: row.get(1)?,
                        description: Some(row.get::<_, String>(2)?),
                        model: row.get(3)?,
                        system_prompt: row.get::<_, Option<String>>(4)?,
                        provider_id: row.get::<_, Option<String>>(5)?.map(ID::from),
                        temperature: row.get::<_, Option<f64>>(6)?.unwrap_or(0.7),
                        created_at: ts(row.get::<_, i64>(7)?),
                    })
                },
            )
            .optional()?;
        Ok(found)
    }

    /// Get statistics overview
    async fn stats(&self, ctx: &Context<'_>) -> FieldResult<StatsOverview> {
        let state = ctx.data::<Arc<AppState>>()?;
        let db = state.db.lock().map_err(|_| "database lock poisoned")?;

        let scalar =
            |sql: &str| -> rusqlite::Result<i64> { db.conn.query_row(sql, [], |r| r.get(0)) };

        let day_ago = Utc::now().timestamp() - 86_400;
        Ok(StatsOverview {
            total_sessions: scalar("SELECT COUNT(*) FROM sessions")? as i32,
            total_messages: scalar("SELECT COUNT(*) FROM messages")? as i32,
            // Absent from the harvest schema; reported as 0 rather than
            // failing the whole stats query.
            total_tokens: if has_column(&db.conn, "sessions", "token_count") {
                scalar("SELECT COALESCE(SUM(token_count), 0) FROM sessions")?
            } else {
                0
            },
            active_providers: scalar(
                "SELECT COUNT(DISTINCT provider) FROM sessions WHERE provider IS NOT NULL",
            )? as i32,
            workspaces: scalar("SELECT COUNT(*) FROM workspaces")? as i32,
            sessions_today: db.conn.query_row(
                "SELECT COUNT(*) FROM sessions WHERE created_at >= ?1",
                [day_ago],
                |r| r.get::<_, i64>(0),
            )? as i32,
            messages_today: db.conn.query_row(
                "SELECT COUNT(*) FROM messages WHERE created_at >= ?1",
                [day_ago],
                |r| r.get::<_, i64>(0),
            )? as i32,
        })
    }

    /// Search across sessions and messages
    async fn search(
        &self,
        ctx: &Context<'_>,
        query: String,
        limit: Option<i32>,
    ) -> FieldResult<SearchResult> {
        let state = ctx.data::<Arc<AppState>>()?;
        let limit = page_size(limit, 20);

        if query.trim().is_empty() {
            return Ok(SearchResult {
                sessions: vec![],
                messages: vec![],
                total: 0,
            });
        }

        let db = state.db.lock().map_err(|_| "database lock poisoned")?;
        // LIKE with an escaped pattern rather than FTS: the FTS table is
        // optional in this schema, and a missing one would error at runtime.
        let pattern = format!(
            "%{}%",
            query
                .replace('\\', "\\\\")
                .replace('%', "\\%")
                .replace('_', "\\_")
        );

        let cols = SessionCols::probe(&db.conn);
        let sess_sql = format!(
            "SELECT {} FROM sessions
             WHERE title LIKE ?1 ESCAPE '\\'
             ORDER BY updated_at DESC LIMIT ?2",
            cols.select_list()
        );
        let mut sess_stmt = db.conn.prepare(&sess_sql)?;
        let sessions = sess_stmt
            .query_map(rusqlite::params![pattern, limit as i64], session_from_row)?
            .collect::<rusqlite::Result<Vec<_>>>()?;

        let msg_sql = format!(
            "SELECT {} FROM messages
             WHERE content LIKE ?1 ESCAPE '\\'
             ORDER BY created_at DESC LIMIT ?2",
            message_select_list(&db.conn)
        );
        let mut msg_stmt = db.conn.prepare(&msg_sql)?;
        let messages = msg_stmt
            .query_map(rusqlite::params![pattern, limit as i64], message_from_row)?
            .collect::<rusqlite::Result<Vec<_>>>()?;

        Ok(SearchResult {
            total: (sessions.len() + messages.len()) as i32,
            sessions,
            messages,
        })
    }
}

// ============================================================================
// Mutation Root
// ============================================================================

pub struct MutationRoot;

#[Object]
impl MutationRoot {
    /// Create a new session
    async fn create_session(
        &self,
        ctx: &Context<'_>,
        input: CreateSessionInput,
    ) -> FieldResult<Session> {
        let state = ctx.data::<Arc<AppState>>()?;
        let db = state.db.lock().map_err(|_| "database lock poisoned")?;

        let id = uuid::Uuid::new_v4().to_string();
        let now = Utc::now().timestamp();
        db.conn.execute(
            "INSERT INTO sessions (id, workspace_id, provider, title, message_count,
                                   token_count, created_at, updated_at, archived)
             VALUES (?1, ?2, ?3, ?4, 0, 0, ?5, ?5, 0)",
            rusqlite::params![
                id,
                input.workspace_id.as_ref().map(|w| w.to_string()),
                input.provider,
                input.title,
                now
            ],
        )?;

        Ok(Session {
            id: ID::from(id),
            title: input.title,
            workspace_id: input.workspace_id,
            provider: input.provider,
            model: None,
            message_count: 0,
            token_count: 0,
            archived: false,
            tags: Vec::new(),
            created_at: ts(now),
            updated_at: ts(now),
        })
    }

    /// Update a session
    ///
    /// Returns the row as it stands after the write rather than echoing the
    /// input back, so the caller cannot be told a field changed when it did
    /// not.
    async fn update_session(
        &self,
        ctx: &Context<'_>,
        id: ID,
        input: UpdateSessionInput,
    ) -> FieldResult<Session> {
        let state = ctx.data::<Arc<AppState>>()?;
        let sid = id.to_string();

        {
            let db = state.db.lock().map_err(|_| "database lock poisoned")?;
            let changed = db.conn.execute(
                "UPDATE sessions
                 SET title    = COALESCE(?2, title),
                     archived = COALESCE(?3, archived),
                     updated_at = ?4
                 WHERE id = ?1",
                rusqlite::params![
                    sid,
                    input.title,
                    input.archived.map(|a| a as i64),
                    Utc::now().timestamp()
                ],
            )?;
            if changed == 0 {
                return Err(format!("no session '{}'", sid).into());
            }
        }

        // `tags` has no column in this schema; accepting it silently would
        // imply a write that never happens.
        if input.tags.is_some() {
            return Err("session tags are not stored by this schema".into());
        }

        self.session_by_id(state, &sid)
    }

    /// Delete a session
    async fn delete_session(&self, ctx: &Context<'_>, id: ID) -> FieldResult<bool> {
        let state = ctx.data::<Arc<AppState>>()?;
        let db = state.db.lock().map_err(|_| "database lock poisoned")?;

        // Messages cascade via the schema's foreign key.
        let changed = db
            .conn
            .execute("DELETE FROM sessions WHERE id = ?1", [id.to_string()])?;
        Ok(changed > 0)
    }

    /// Archive a session
    async fn archive_session(&self, ctx: &Context<'_>, id: ID) -> FieldResult<Session> {
        let state = ctx.data::<Arc<AppState>>()?;
        let sid = id.to_string();

        {
            let db = state.db.lock().map_err(|_| "database lock poisoned")?;
            let changed = db.conn.execute(
                "UPDATE sessions SET archived = 1, updated_at = ?2 WHERE id = ?1",
                rusqlite::params![sid, Utc::now().timestamp()],
            )?;
            if changed == 0 {
                return Err(format!("no session '{}'", sid).into());
            }
        }

        self.session_by_id(state, &sid)
    }

    /// Create an agent
    async fn create_agent(&self, ctx: &Context<'_>, input: CreateAgentInput) -> FieldResult<Agent> {
        let state = ctx.data::<Arc<AppState>>()?;
        let db = state.db.lock().map_err(|_| "database lock poisoned")?;

        let id = uuid::Uuid::new_v4().to_string();
        let now = Utc::now().timestamp();
        let temperature = input.temperature.unwrap_or(0.7);

        // `instruction` is NOT NULL in the schema.
        let instruction = input.system_prompt.clone().unwrap_or_default();

        db.conn.execute(
            "INSERT INTO agents (id, name, description, instruction, model, provider,
                                 temperature, is_active, created_at, updated_at)
             VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, 1, ?8, ?8)",
            rusqlite::params![
                id,
                input.name,
                input.description,
                instruction,
                input.model,
                input.provider_id.as_ref().map(|p| p.to_string()),
                temperature,
                now
            ],
        )?;

        Ok(Agent {
            id: ID::from(id),
            name: input.name,
            description: input.description,
            system_prompt: input.system_prompt,
            provider_id: input.provider_id,
            model: input.model,
            temperature,
            created_at: ts(now),
        })
    }

    /// Delete an agent
    async fn delete_agent(&self, ctx: &Context<'_>, id: ID) -> FieldResult<bool> {
        let state = ctx.data::<Arc<AppState>>()?;
        let db = state.db.lock().map_err(|_| "database lock poisoned")?;

        let changed = db
            .conn
            .execute("DELETE FROM agents WHERE id = ?1", [id.to_string()])?;
        Ok(changed > 0)
    }

    /// Trigger a harvest
    async fn harvest(
        &self,
        _ctx: &Context<'_>,
        _providers: Option<Vec<String>>,
    ) -> FieldResult<i32> {
        // Harvesting scans provider directories on the host and can run for
        // minutes; it belongs behind the CLI or the REST job endpoint, not a
        // synchronous GraphQL field. Returning 0 here previously looked like a
        // harvest that found nothing.
        Err(
            "harvest is not exposed over GraphQL; use `ironbridge harvest run` or POST /api/harvest"
                .into(),
        )
    }

    /// Trigger sync
    async fn sync(&self, _ctx: &Context<'_>) -> FieldResult<bool> {
        Err(
            "sync is not exposed over GraphQL; use `ironbridge sync` or the REST sync endpoints"
                .into(),
        )
    }
}

impl MutationRoot {
    /// Re-read a session after a write. Not a GraphQL field: it lives in a
    /// plain `impl` block so `#[Object]` does not expose it.
    fn session_by_id(&self, state: &Arc<AppState>, id: &str) -> FieldResult<Session> {
        let db = state.db.lock().map_err(|_| "database lock poisoned")?;
        let cols = SessionCols::probe(&db.conn);
        let session = db
            .conn
            .query_row(
                &format!("SELECT {} FROM sessions WHERE id = ?1", cols.select_list()),
                [id],
                session_from_row,
            )
            .optional()?
            .ok_or_else(|| format!("no session '{}'", id))?;
        Ok(session)
    }
}

// ============================================================================
// HTTP Handlers
// ============================================================================

/// GraphQL endpoint handler
pub async fn graphql_handler(
    schema: web::Data<IronBridgeSchema>,
    req: GraphQLRequest,
) -> GraphQLResponse {
    schema.execute(req.into_inner()).await.into()
}

/// GraphQL Playground UI
pub async fn graphql_playground() -> impl Responder {
    HttpResponse::Ok()
        .content_type("text/html")
        .body(GRAPHQL_PLAYGROUND_HTML)
}

/// GraphQL introspection endpoint
pub async fn graphql_sdl(schema: web::Data<IronBridgeSchema>) -> impl Responder {
    HttpResponse::Ok()
        .content_type("text/plain")
        .body(schema.sdl())
}

/// Create the GraphQL schema
pub fn create_schema(state: Arc<AppState>) -> IronBridgeSchema {
    Schema::build(QueryRoot, MutationRoot, EmptySubscription)
        .data(state)
        // Bound query cost. async-graphql imposes no depth or complexity limit
        // by default, so a single deeply-nested query -- cheap to send, and
        // cheaper still if any type refers back to another -- could force an
        // arbitrarily large amount of resolver work. These ceilings are well
        // above any honest query the UI issues and turn that DoS into a
        // rejected query.
        .limit_depth(20)
        .limit_complexity(2000)
        .finish()
}

/// Configure GraphQL routes
pub fn configure_graphql_routes(cfg: &mut web::ServiceConfig, schema: IronBridgeSchema) {
    cfg.app_data(web::Data::new(schema))
        .service(
            web::resource("/graphql")
                .route(web::get().to(graphql_handler))
                .route(web::post().to(graphql_handler)),
        )
        .route("/graphql/playground", web::get().to(graphql_playground))
        .route("/graphql/sdl", web::get().to(graphql_sdl));
}

/// Embedded GraphQL Playground HTML
const GRAPHQL_PLAYGROUND_HTML: &str = r#"<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>IronBridge GraphQL Playground</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            height: 100vh;
            overflow: hidden;
        }
        .custom-header {
            background: linear-gradient(135deg, #e535ab 0%, #9c27b0 100%);
            color: white;
            padding: 12px 24px;
            display: flex;
            align-items: center;
            gap: 16px;
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
        }
        .custom-header h1 {
            margin: 0;
            font-size: 20px;
            font-weight: 600;
        }
        .custom-header .badge {
            background: rgba(255,255,255,0.2);
            padding: 4px 12px;
            border-radius: 12px;
            font-size: 12px;
        }
        .custom-header a {
            color: white;
            text-decoration: none;
            margin-left: auto;
            opacity: 0.9;
            font-size: 14px;
        }
        .custom-header a:hover {
            opacity: 1;
        }
        #graphiql {
            height: calc(100vh - 48px);
        }
    </style>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/graphiql@3/graphiql.min.css" />
</head>
<body>
    <div class="custom-header">
        <h1>◈ IronBridge GraphQL</h1>
        <span class="badge">v1.3.0</span>
        <a href="/docs">REST API →</a>
    </div>
    <div id="graphiql"></div>
    <script crossorigin src="https://cdn.jsdelivr.net/npm/react@18/umd/react.production.min.js"></script>
    <script crossorigin src="https://cdn.jsdelivr.net/npm/react-dom@18/umd/react-dom.production.min.js"></script>
    <script crossorigin src="https://cdn.jsdelivr.net/npm/graphiql@3/graphiql.min.js"></script>
    <script>
        const fetcher = GraphiQL.createFetcher({
            url: '/graphql',
        });

        const root = ReactDOM.createRoot(document.getElementById('graphiql'));
        root.render(
            React.createElement(GraphiQL, {
                fetcher,
                defaultEditorToolsVisibility: true,
            })
        );
    </script>
</body>
</html>
"#;

#[cfg(test)]
mod complexity_tests {
    use super::*;

    fn schema() -> IronBridgeSchema {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("gql.db");
        crate::commands::create_harvest_database(&path).unwrap();
        let db = crate::ChatDatabase::open(&path).unwrap();
        // The dir must outlive the schema for the file to stay open; leak it,
        // this is a test process.
        std::mem::forget(dir);
        create_schema(Arc::new(AppState::new(db, path)))
    }

    /// An ordinary query runs; a query-complexity bomb is rejected rather than
    /// executed. Without `limit_complexity` the second query would run every
    /// one of its thousands of resolvers.
    #[tokio::test]
    async fn an_overly_complex_query_is_refused() {
        let schema = schema();

        let ok = schema.execute("{ stats { totalSessions } }").await;
        assert!(ok.errors.is_empty(), "normal query failed: {:?}", ok.errors);

        let bomb = format!(
            "{{ {} }}",
            (0..2200)
                .map(|i| format!("a{i}: stats {{ totalSessions }}"))
                .collect::<Vec<_>>()
                .join(" ")
        );
        let res = schema.execute(bomb).await;
        assert!(
            res.errors.iter().any(|e| e.message.contains("too complex")),
            "complexity bomb was not rejected: {:?}",
            res.errors
        );
    }
}