asurada 0.3.1

Asurada — a memory + cognition daemon that grows with the user. Local-first, BYOK, shared by Devist/Webchemist Core/etc.
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
// Supabase Postgres → brain.db pull.
//
// 알고리즘:
//   1. _sync_state 에서 테이블별 last_pulled_at 조회.
//   2. Supabase 에서 updated_at > last_pulled_at 인 row 가져옴.
//   3. brain.db 로 upsert. WHERE updated_at < excluded.updated_at 로 last-writer-wins.
//   4. synced_at = updated_at 로 마킹 (push 가 다시 같은 row 를 cloud 로 보내지 않도록).
//   5. last_pulled_at = max(updated_at) 갱신.

use anyhow::{Context, Result};
use chrono::{DateTime, Utc};
use rusqlite::params;

use crate::db::sync_state;

const PULL_BATCH: i64 = 200;

impl super::Sync {
    pub async fn pull_events(&self) -> Result<usize> {
        let cutoff = self.read_cutoff("events")?;
        let client = self.pg.get().await.context("get client")?;
        let rows = client
            .query(
                "SELECT id, user_id, project, event_type, path, payload, created_at, updated_at
                 FROM asurada.events
                 WHERE user_id = $1 AND updated_at > $2
                 ORDER BY updated_at ASC
                 LIMIT $3",
                &[&self.user_id, &cutoff, &PULL_BATCH],
            )
            .await
            .context("pull events query")?;

        if rows.is_empty() {
            return Ok(0);
        }

        let mut max_updated = cutoff;
        {
            let conn = self.brain.lock().unwrap();
            let tx = conn.unchecked_transaction()?;
            for row in &rows {
                let id: String = row.get("id");
                let user_id: String = row.get("user_id");
                let project: String = row.get("project");
                let event_type: String = row.get("event_type");
                let path: Option<String> = row.get("path");
                let payload: serde_json::Value = row.get("payload");
                let created: DateTime<Utc> = row.get("created_at");
                let updated: DateTime<Utc> = row.get("updated_at");

                let created_s = created.to_rfc3339();
                let updated_s = updated.to_rfc3339();

                tx.execute(
                    r#"INSERT INTO events
                        (id, user_id, project, event_type, path, payload,
                         created_at, updated_at, synced_at)
                       VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?8)
                       ON CONFLICT(id) DO UPDATE SET
                           event_type = excluded.event_type,
                           path = excluded.path,
                           payload = excluded.payload,
                           updated_at = excluded.updated_at,
                           synced_at = excluded.synced_at
                       WHERE events.updated_at < excluded.updated_at"#,
                    params![
                        id,
                        user_id,
                        project,
                        event_type,
                        path,
                        payload.to_string(),
                        created_s,
                        updated_s,
                    ],
                )?;

                if updated > max_updated {
                    max_updated = updated;
                }
            }
            sync_state::set_last_pulled(&tx, "events", &max_updated.to_rfc3339())?;
            tx.commit()?;
        }
        Ok(rows.len())
    }

    pub async fn pull_memories(&self) -> Result<usize> {
        let cutoff = self.read_cutoff("memories")?;
        let client = self.pg.get().await?;
        let rows = client
            .query(
                "SELECT id, user_id, text, scope, priority, source, project, tech, metadata,
                        status, created_at, updated_at, deleted_at
                 FROM asurada.memories
                 WHERE user_id = $1 AND updated_at > $2
                 ORDER BY updated_at ASC
                 LIMIT $3",
                &[&self.user_id, &cutoff, &PULL_BATCH],
            )
            .await
            .context("pull memories query")?;

        if rows.is_empty() {
            return Ok(0);
        }

        let mut max_updated = cutoff;
        {
            let conn = self.brain.lock().unwrap();
            let tx = conn.unchecked_transaction()?;
            for row in &rows {
                let id: String = row.get("id");
                let user_id: String = row.get("user_id");
                let text: String = row.get("text");
                let scope: String = row.get("scope");
                let priority: String = row.get("priority");
                let source: String = row.get("source");
                let project: Option<String> = row.get("project");
                let tech: Option<String> = row.get("tech");
                let metadata: serde_json::Value = row.get("metadata");
                let status: String = row.get("status");
                let created: DateTime<Utc> = row.get("created_at");
                let updated: DateTime<Utc> = row.get("updated_at");
                let deleted: Option<DateTime<Utc>> = row.get("deleted_at");

                let updated_s = updated.to_rfc3339();

                tx.execute(
                    r#"INSERT INTO memories
                        (id, user_id, text, scope, priority, source, project, tech, metadata,
                         status, created_at, updated_at, deleted_at, synced_at)
                       VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?12)
                       ON CONFLICT(id) DO UPDATE SET
                           text = excluded.text,
                           scope = excluded.scope,
                           priority = excluded.priority,
                           source = excluded.source,
                           project = excluded.project,
                           tech = excluded.tech,
                           metadata = excluded.metadata,
                           status = excluded.status,
                           updated_at = excluded.updated_at,
                           deleted_at = excluded.deleted_at,
                           synced_at = excluded.synced_at
                       WHERE memories.updated_at < excluded.updated_at"#,
                    params![
                        id,
                        user_id,
                        text,
                        scope,
                        priority,
                        source,
                        project,
                        tech,
                        metadata.to_string(),
                        status,
                        created.to_rfc3339(),
                        updated_s,
                        deleted.map(|d| d.to_rfc3339()),
                    ],
                )?;

                if updated > max_updated {
                    max_updated = updated;
                }
            }
            sync_state::set_last_pulled(&tx, "memories", &max_updated.to_rfc3339())?;
            tx.commit()?;
        }
        Ok(rows.len())
    }

    pub async fn pull_advice(&self) -> Result<usize> {
        let cutoff = self.read_cutoff("advice")?;
        let client = self.pg.get().await?;
        let rows = client
            .query(
                "SELECT id, user_id, project, text, severity, paths, verifiable, state,
                        confirmed_at, confirmed_by, metadata, created_at, updated_at
                 FROM asurada.advice
                 WHERE user_id = $1 AND updated_at > $2
                 ORDER BY updated_at ASC
                 LIMIT $3",
                &[&self.user_id, &cutoff, &PULL_BATCH],
            )
            .await
            .context("pull advice query")?;

        if rows.is_empty() {
            return Ok(0);
        }

        let mut max_updated = cutoff;
        {
            let conn = self.brain.lock().unwrap();
            let tx = conn.unchecked_transaction()?;
            for row in &rows {
                let id: String = row.get("id");
                let user_id: String = row.get("user_id");
                let project: String = row.get("project");
                let text: String = row.get("text");
                let severity: String = row.get("severity");
                let paths: serde_json::Value = row.get("paths");
                let verifiable: bool = row.get("verifiable");
                let state: String = row.get("state");
                let confirmed_at: Option<DateTime<Utc>> = row.get("confirmed_at");
                let confirmed_by: Option<String> = row.get("confirmed_by");
                let metadata: serde_json::Value = row.get("metadata");
                let created: DateTime<Utc> = row.get("created_at");
                let updated: DateTime<Utc> = row.get("updated_at");

                let updated_s = updated.to_rfc3339();

                tx.execute(
                    r#"INSERT INTO advice
                        (id, user_id, project, text, severity, paths, verifiable, state,
                         confirmed_at, confirmed_by, metadata, created_at, updated_at, synced_at)
                       VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?13)
                       ON CONFLICT(id) DO UPDATE SET
                           text = excluded.text,
                           severity = excluded.severity,
                           paths = excluded.paths,
                           verifiable = excluded.verifiable,
                           state = excluded.state,
                           confirmed_at = excluded.confirmed_at,
                           confirmed_by = excluded.confirmed_by,
                           metadata = excluded.metadata,
                           updated_at = excluded.updated_at,
                           synced_at = excluded.synced_at
                       WHERE advice.updated_at < excluded.updated_at"#,
                    params![
                        id,
                        user_id,
                        project,
                        text,
                        severity,
                        paths.to_string(),
                        verifiable as i32,
                        state,
                        confirmed_at.map(|d| d.to_rfc3339()),
                        confirmed_by,
                        metadata.to_string(),
                        created.to_rfc3339(),
                        updated_s,
                    ],
                )?;

                if updated > max_updated {
                    max_updated = updated;
                }
            }
            sync_state::set_last_pulled(&tx, "advice", &max_updated.to_rfc3339())?;
            tx.commit()?;
        }
        Ok(rows.len())
    }

    pub async fn pull_projects(&self) -> Result<usize> {
        let cutoff = self.read_cutoff("projects")?;
        let client = self.pg.get().await?;
        let rows = client
            .query(
                "SELECT user_id, name, path, metadata, created_at, updated_at
                 FROM asurada.projects
                 WHERE user_id = $1 AND updated_at > $2
                 ORDER BY updated_at ASC
                 LIMIT $3",
                &[&self.user_id, &cutoff, &PULL_BATCH],
            )
            .await
            .context("pull projects query")?;

        if rows.is_empty() {
            return Ok(0);
        }

        let mut max_updated = cutoff;
        {
            let conn = self.brain.lock().unwrap();
            let tx = conn.unchecked_transaction()?;
            for row in &rows {
                let user_id: String = row.get("user_id");
                let name: String = row.get("name");
                let path: String = row.get("path");
                let metadata: serde_json::Value = row.get("metadata");
                let created: DateTime<Utc> = row.get("created_at");
                let updated: DateTime<Utc> = row.get("updated_at");

                let updated_s = updated.to_rfc3339();

                tx.execute(
                    r#"INSERT INTO projects
                        (user_id, name, path, metadata, created_at, updated_at, synced_at)
                       VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?6)
                       ON CONFLICT(user_id, name) DO UPDATE SET
                           path = excluded.path,
                           metadata = excluded.metadata,
                           updated_at = excluded.updated_at,
                           synced_at = excluded.synced_at
                       WHERE projects.updated_at < excluded.updated_at"#,
                    params![
                        user_id,
                        name,
                        path,
                        metadata.to_string(),
                        created.to_rfc3339(),
                        updated_s,
                    ],
                )?;

                if updated > max_updated {
                    max_updated = updated;
                }
            }
            sync_state::set_last_pulled(&tx, "projects", &max_updated.to_rfc3339())?;
            tx.commit()?;
        }
        Ok(rows.len())
    }

    pub async fn pull_intents(&self) -> Result<usize> {
        let cutoff = self.read_cutoff("intents")?;
        let client = self.pg.get().await?;
        let rows = client
            .query(
                "SELECT id, user_id, project, strength, intent_text, source,
                        source_signal_ids, status, metadata, created_at, updated_at
                 FROM asurada.intents
                 WHERE user_id = $1 AND updated_at > $2
                 ORDER BY updated_at ASC
                 LIMIT $3",
                &[&self.user_id, &cutoff, &PULL_BATCH],
            )
            .await
            .context("pull intents query")?;

        if rows.is_empty() {
            return Ok(0);
        }

        let mut max_updated = cutoff;
        {
            let conn = self.brain.lock().unwrap();
            let tx = conn.unchecked_transaction()?;
            for row in &rows {
                let id: String = row.get("id");
                let user_id: String = row.get("user_id");
                let project: Option<String> = row.get("project");
                let strength: String = row.get("strength");
                let intent_text: String = row.get("intent_text");
                let source: String = row.get("source");
                let source_signal_ids: serde_json::Value = row.get("source_signal_ids");
                let status: String = row.get("status");
                let metadata: serde_json::Value = row.get("metadata");
                let created: DateTime<Utc> = row.get("created_at");
                let updated: DateTime<Utc> = row.get("updated_at");

                let updated_s = updated.to_rfc3339();

                tx.execute(
                    r#"INSERT INTO intents
                        (id, user_id, project, strength, intent_text, source,
                         source_signal_ids, status, metadata,
                         created_at, updated_at, synced_at)
                       VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?11)
                       ON CONFLICT(id) DO UPDATE SET
                           project = excluded.project,
                           strength = excluded.strength,
                           intent_text = excluded.intent_text,
                           source = excluded.source,
                           source_signal_ids = excluded.source_signal_ids,
                           status = excluded.status,
                           metadata = excluded.metadata,
                           updated_at = excluded.updated_at,
                           synced_at = excluded.synced_at
                       WHERE intents.updated_at < excluded.updated_at"#,
                    params![
                        id,
                        user_id,
                        project,
                        strength,
                        intent_text,
                        source,
                        source_signal_ids.to_string(),
                        status,
                        metadata.to_string(),
                        created.to_rfc3339(),
                        updated_s,
                    ],
                )?;

                if updated > max_updated {
                    max_updated = updated;
                }
            }
            sync_state::set_last_pulled(&tx, "intents", &max_updated.to_rfc3339())?;
            tx.commit()?;
        }
        Ok(rows.len())
    }

    pub async fn pull_patterns(&self) -> Result<usize> {
        let cutoff = self.read_cutoff("patterns")?;
        let client = self.pg.get().await?;
        let rows = client
            .query(
                "SELECT id, user_id, project, slug, title, description, reason,
                        file_paths, usage_count, last_used_at, evolution_log,
                        source_signal_ids, source_cluster_signature, status, metadata,
                        created_at, updated_at
                 FROM asurada.patterns
                 WHERE user_id = $1 AND updated_at > $2
                 ORDER BY updated_at ASC
                 LIMIT $3",
                &[&self.user_id, &cutoff, &PULL_BATCH],
            )
            .await
            .context("pull patterns query")?;

        if rows.is_empty() {
            return Ok(0);
        }

        let mut max_updated = cutoff;
        {
            let conn = self.brain.lock().unwrap();
            let tx = conn.unchecked_transaction()?;
            for row in &rows {
                let id: String = row.get("id");
                let user_id: String = row.get("user_id");
                let project: String = row.get("project");
                let slug: String = row.get("slug");
                let title: String = row.get("title");
                let description: String = row.get("description");
                let reason: String = row.get("reason");
                let file_paths: serde_json::Value = row.get("file_paths");
                let usage_count: i64 = row.get("usage_count");
                let last_used_at: Option<DateTime<Utc>> = row.get("last_used_at");
                let evolution_log: serde_json::Value = row.get("evolution_log");
                let source_signal_ids: serde_json::Value = row.get("source_signal_ids");
                let source_cluster_signature: Option<i64> = row.get("source_cluster_signature");
                let status: String = row.get("status");
                let metadata: serde_json::Value = row.get("metadata");
                let created: DateTime<Utc> = row.get("created_at");
                let updated: DateTime<Utc> = row.get("updated_at");

                let updated_s = updated.to_rfc3339();

                tx.execute(
                    r#"INSERT INTO patterns
                        (id, user_id, project, slug, title, description, reason,
                         file_paths, usage_count, last_used_at, evolution_log,
                         source_signal_ids, source_cluster_signature, status, metadata,
                         created_at, updated_at, synced_at)
                       VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11,
                               ?12, ?13, ?14, ?15, ?16, ?17, ?17)
                       ON CONFLICT(id) DO UPDATE SET
                           project = excluded.project,
                           slug = excluded.slug,
                           title = excluded.title,
                           description = excluded.description,
                           reason = excluded.reason,
                           file_paths = excluded.file_paths,
                           usage_count = excluded.usage_count,
                           last_used_at = excluded.last_used_at,
                           evolution_log = excluded.evolution_log,
                           source_signal_ids = excluded.source_signal_ids,
                           source_cluster_signature = excluded.source_cluster_signature,
                           status = excluded.status,
                           metadata = excluded.metadata,
                           updated_at = excluded.updated_at,
                           synced_at = excluded.synced_at
                       WHERE patterns.updated_at < excluded.updated_at"#,
                    params![
                        id,
                        user_id,
                        project,
                        slug,
                        title,
                        description,
                        reason,
                        file_paths.to_string(),
                        usage_count,
                        last_used_at.map(|d| d.to_rfc3339()),
                        evolution_log.to_string(),
                        source_signal_ids.to_string(),
                        source_cluster_signature,
                        status,
                        metadata.to_string(),
                        created.to_rfc3339(),
                        updated_s,
                    ],
                )?;

                if updated > max_updated {
                    max_updated = updated;
                }
            }
            sync_state::set_last_pulled(&tx, "patterns", &max_updated.to_rfc3339())?;
            tx.commit()?;
        }
        Ok(rows.len())
    }

    pub async fn pull_issues(&self) -> Result<usize> {
        let cutoff = self.read_cutoff("issues")?;
        let client = self.pg.get().await?;
        let rows = client
            .query(
                "SELECT id, user_id, title, summary, projects, status,
                        started_at, ended_at, event_count, metadata,
                        created_at, updated_at
                 FROM asurada.issues
                 WHERE user_id = $1 AND updated_at > $2
                 ORDER BY updated_at ASC
                 LIMIT $3",
                &[&self.user_id, &cutoff, &PULL_BATCH],
            )
            .await
            .context("pull issues query")?;

        if rows.is_empty() {
            return Ok(0);
        }

        let mut max_updated = cutoff;
        {
            let conn = self.brain.lock().unwrap();
            let tx = conn.unchecked_transaction()?;
            for row in &rows {
                let id: String = row.get("id");
                let user_id: String = row.get("user_id");
                let title: String = row.get("title");
                let summary: String = row.get("summary");
                let projects: serde_json::Value = row.get("projects");
                let status: String = row.get("status");
                let started: DateTime<Utc> = row.get("started_at");
                let ended: Option<DateTime<Utc>> = row.get("ended_at");
                let event_count: i64 = row.get("event_count");
                let metadata: serde_json::Value = row.get("metadata");
                let created: DateTime<Utc> = row.get("created_at");
                let updated: DateTime<Utc> = row.get("updated_at");

                let updated_s = updated.to_rfc3339();

                tx.execute(
                    r#"INSERT INTO issues
                        (id, user_id, title, summary, projects, status,
                         started_at, ended_at, event_count, metadata,
                         created_at, updated_at, synced_at)
                       VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?12)
                       ON CONFLICT(id) DO UPDATE SET
                           title = excluded.title,
                           summary = excluded.summary,
                           projects = excluded.projects,
                           status = excluded.status,
                           started_at = excluded.started_at,
                           ended_at = excluded.ended_at,
                           event_count = excluded.event_count,
                           metadata = excluded.metadata,
                           updated_at = excluded.updated_at,
                           synced_at = excluded.synced_at
                       WHERE issues.updated_at < excluded.updated_at"#,
                    params![
                        id,
                        user_id,
                        title,
                        summary,
                        projects.to_string(),
                        status,
                        started.to_rfc3339(),
                        ended.map(|d| d.to_rfc3339()),
                        event_count,
                        metadata.to_string(),
                        created.to_rfc3339(),
                        updated_s,
                    ],
                )?;

                if updated > max_updated {
                    max_updated = updated;
                }
            }
            sync_state::set_last_pulled(&tx, "issues", &max_updated.to_rfc3339())?;
            tx.commit()?;
        }
        Ok(rows.len())
    }

    pub async fn pull_all(&self) -> Result<PullSummary> {
        Ok(PullSummary {
            events: self.pull_events().await?,
            memories: self.pull_memories().await?,
            advice: self.pull_advice().await?,
            projects: self.pull_projects().await?,
            intents: self.pull_intents().await?,
            patterns: self.pull_patterns().await?,
            issues: self.pull_issues().await?,
        })
    }

    /// _sync_state 에서 last_pulled_at 읽어 cutoff 로 변환.
    /// 첫 실행 시 (값 없음) 1970년 epoch 사용.
    fn read_cutoff(&self, table: &str) -> Result<DateTime<Utc>> {
        let conn = self.brain.lock().unwrap();
        let last = sync_state::get_last_pulled(&conn, table)?;
        let s = last.unwrap_or_else(|| "1970-01-01T00:00:00Z".to_string());
        DateTime::parse_from_rfc3339(&s)
            .map(|d| d.with_timezone(&Utc))
            .with_context(|| format!("parse last_pulled_at '{}'", s))
    }
}

#[derive(Debug, Clone, serde::Serialize)]
pub struct PullSummary {
    pub events: usize,
    pub memories: usize,
    pub advice: usize,
    pub projects: usize,
    pub intents: usize,
    pub patterns: usize,
    pub issues: usize,
}