asurada 0.2.0

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
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
//! Layer A — 프로젝트 brief synthesis.
//!
//! Asurada 가 raw event 를 카운트만 세는 데서 *해석* 으로 한 단계 진화하는 곳.
//! 최근 N일의 사용자 prompt + signal 을 모아 Claude (subprocess) 에 보내고,
//! 응답을 memory 로 저장 — Memory 페이지가 자연스럽게 채워지고 사고의 연료
//! 가 누적된다 (neural-like genius 비전의 첫 구현).

use anyhow::{Context, Result};
use chrono::{DateTime, Duration, Utc};
use rusqlite::{params, Connection};
use std::process::Stdio;
use std::time::Duration as StdDuration;
use tokio::io::AsyncWriteExt;
use tokio::process::Command;

const CLAUDE_TIMEOUT: StdDuration = StdDuration::from_secs(120);
const MAX_EVENTS: usize = 80;
const MAX_MEMORIES: usize = 30;

#[derive(Debug, Clone)]
struct ActivityRow {
    at: String,
    kind: String, // event_type
    detail: String,
}

/// 프로젝트의 최근 N일 활동을 Claude 에게 요약 요청 후 memory 로 저장.
/// 활동 0건이면 None — 침묵 원칙 (할 말 없을 때 안 말함).
pub async fn brief_project(
    brain: std::sync::Arc<std::sync::Mutex<Connection>>,
    user_id: String,
    project: String,
    days: i64,
) -> Result<Option<crate::db::memory::Memory>> {
    let (events, memories) = {
        let conn = brain.lock().expect("brain mutex poisoned");
        let evs = gather_events(&conn, &user_id, &project, days)?;
        if evs.is_empty() {
            return Ok(None);
        }
        let mems = gather_project_memories(&conn, &user_id, &project)?;
        (evs, mems)
    };

    let prompt = build_prompt(&project, days, &events, &memories);
    let summary = run_claude(&prompt).await?;
    let summary = summary.trim();
    if summary.is_empty() {
        tracing::warn!("[synthesis] claude returned empty for {}", project);
        return Ok(None);
    }

    let memory_text = format!(
        "[brief · 최근 {}일 · {}]\n{}",
        days,
        chrono::Local::now().format("%Y-%m-%d %H:%M"),
        summary
    );
    let m = {
        let conn = brain.lock().expect("brain mutex poisoned");
        crate::db::memory::insert(
            &conn,
            crate::db::memory::MemoryInput {
                user_id,
                text: memory_text,
                scope: "project".into(),
                priority: "info".into(),
                source: "asurada".into(),
                project: Some(project.clone()),
                tech: None,
                metadata: serde_json::json!({
                    "kind": "brief",
                    "days": days,
                    "events_seen": events.len(),
                    "prior_memories": memories.len(),
                }),
            },
        )?
    };
    Ok(Some(m))
}

fn gather_events(
    conn: &Connection,
    user_id: &str,
    project: &str,
    days: i64,
) -> Result<Vec<ActivityRow>> {
    let since = (Utc::now() - Duration::days(days)).to_rfc3339();
    let mut stmt = conn.prepare(
        r#"SELECT created_at, event_type,
                  json_extract(payload, '$.prompt'),
                  json_extract(payload, '$.tool_name'),
                  json_extract(payload, '$.prompt_preview'),
                  json_extract(payload, '$.patterns'),
                  json_extract(payload, '$.harness_id')
           FROM events
           WHERE user_id = ?1 AND project = ?2 AND created_at > ?3
             AND event_type IN (
                 'hook.user_prompt',
                 'signal.intervention',
                 'signal.redo',
                 'signal.harness_use',
                 'signal.context_injection'
             )
           ORDER BY created_at ASC
           LIMIT ?4"#,
    )?;
    let rows: Vec<ActivityRow> = stmt
        .query_map(
            params![user_id, project, since, MAX_EVENTS as i64],
            |r| {
                let at: String = r.get(0)?;
                let kind: String = r.get(1)?;
                let prompt: Option<String> = r.get(2)?;
                let tool: Option<String> = r.get(3)?;
                let preview: Option<String> = r.get(4)?;
                let patterns: Option<String> = r.get(5)?;
                let harness: Option<String> = r.get(6)?;
                let detail = match kind.as_str() {
                    "hook.user_prompt" => prompt.unwrap_or_default(),
                    "signal.intervention" => format!("교정: {}", patterns.unwrap_or_default()),
                    "signal.redo" => format!("반복: {}", preview.unwrap_or_default()),
                    "signal.harness_use" => {
                        format!("하네스 사용: {}", harness.unwrap_or_default())
                    }
                    "signal.context_injection" => "context 주입".into(),
                    _ => tool.unwrap_or_default(),
                };
                Ok(ActivityRow { at, kind, detail })
            },
        )?
        .filter_map(|r| r.ok())
        .collect();
    Ok(rows)
}

fn gather_project_memories(
    conn: &Connection,
    user_id: &str,
    project: &str,
) -> Result<Vec<String>> {
    let mut stmt = conn.prepare(
        r#"SELECT text FROM memories
           WHERE user_id = ?1
             AND status = 'active' AND deleted_at IS NULL
             AND (scope = 'project' AND project = ?2)
           ORDER BY updated_at DESC
           LIMIT ?3"#,
    )?;
    let rows: Vec<String> = stmt
        .query_map(params![user_id, project, MAX_MEMORIES as i64], |r| r.get(0))?
        .filter_map(|r| r.ok())
        .collect();
    Ok(rows)
}

fn build_prompt(
    project: &str,
    days: i64,
    events: &[ActivityRow],
    memories: &[String],
) -> String {
    let mut s = String::new();
    s.push_str(
        "당신은 Asurada — 사용자 곁에서 매 순간을 관찰하고 기억하는 AI 동반자입니다. \
         지금은 최근의 활동을 회고하는 시간. 사용자가 다시 자리에 돌아와 \
         \"내 곁에서 뭘 봤어?\" 라고 물을 때 짧고 단단하게 전할 수 있는 회고를 \
         작성해주세요. 다음 원칙을 따라:\n\n\
         - 자기 자신을 주어로 내세우지 말 것 (예: \"제가 봤어요\" X). \
         사용자/상황을 주어로.\n\
         - 차분하고 신중한 어조. 과장 / 호들갑 X.\n\
         - 사실에 기반. 추측은 \"...로 보입니다\" 같이 명시.\n\
         - 한국어 5~10줄. 마침표 있는 문장.\n\n\
         [출력 구조]\n\
         1. 이 기간 한 작업 (구체적으로 무엇을)\n\
         2. 현재 상태 (완료된 것 / 미해결인 것)\n\
         3. 다음으로 좋은 단계 (제안 — 단정하지 말고 선택지로)\n\
         4. (있으면) 이전과 다르게 관찰된 것\n\n",
    );
    s.push_str(&format!(
        "프로젝트: **{}**\n관찰 기간: 최근 **{}일**\n\n",
        project, days
    ));
    if !memories.is_empty() {
        s.push_str("[이미 알고 있는 것 — 누적 메모리]\n");
        for m in memories.iter().take(10) {
            let preview: String = m.chars().take(180).collect();
            let suffix = if m.chars().count() > 180 { "" } else { "" };
            s.push_str(&format!("- {}{}\n", preview, suffix));
        }
        s.push('\n');
    }
    s.push_str(&format!("[누적 활동 — {}건]\n", events.len()));
    for e in events {
        let local = DateTime::parse_from_rfc3339(&e.at)
            .map(|d| d.with_timezone(&chrono::Local).format("%m-%d %H:%M").to_string())
            .unwrap_or_else(|_| e.at.clone());
        let detail: String = e.detail.chars().take(120).collect();
        s.push_str(&format!("- [{}] {}: {}\n", local, e.kind, detail));
    }
    s.push_str("\n위 정보를 바탕으로 회고를 작성하세요.");
    s
}

/// `claude -p <prompt>` 를 subprocess 로 실행. timeout 120초.
async fn run_claude(prompt: &str) -> Result<String> {
    let mut child = Command::new("claude")
        .arg("-p")
        .arg("--permission-mode")
        .arg("plan")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .context(
            "claude CLI 실행 실패. PATH 에 claude 가 있는지 확인하세요. \
             https://claude.com/code",
        )?;

    if let Some(mut stdin) = child.stdin.take() {
        stdin.write_all(prompt.as_bytes()).await.ok();
        drop(stdin); // EOF
    }

    let res = tokio::time::timeout(CLAUDE_TIMEOUT, child.wait_with_output()).await;
    let output = match res {
        Ok(o) => o.context("claude wait")?,
        Err(_) => anyhow::bail!("claude 호출 timeout ({}s)", CLAUDE_TIMEOUT.as_secs()),
    };
    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        anyhow::bail!(
            "claude exit {} — {}",
            output.status.code().unwrap_or(-1),
            stderr.trim()
        );
    }
    let stdout = String::from_utf8_lossy(&output.stdout).to_string();
    Ok(stdout)
}

// ── Layer B — cross-project reflection (일/주 단위) ─────────────────

/// 모든 프로젝트의 최근 N일 활동을 통합해 회고 메모리 생성.
/// kind = 'reflection_daily' (days=1) 또는 'reflection_weekly' (days=7).
/// scope = 'user' (특정 프로젝트 종속 X — 사용자의 리듬/패턴).
pub async fn reflect_recent(
    brain: std::sync::Arc<std::sync::Mutex<Connection>>,
    user_id: String,
    days: i64,
) -> Result<Option<crate::db::memory::Memory>> {
    let kind = if days <= 1 {
        "reflection_daily"
    } else {
        "reflection_weekly"
    };
    let label = if days <= 1 { "오늘의 흐름" } else { "이번 주 패턴" };

    let (per_project, recent_briefs) = {
        let conn = brain.lock().expect("brain mutex poisoned");
        let pp = gather_per_project_summary(&conn, &user_id, days)?;
        if pp.is_empty() {
            return Ok(None);
        }
        let briefs = gather_recent_briefs(&conn, &user_id, days)?;
        (pp, briefs)
    };

    let prompt = build_reflection_prompt(label, days, &per_project, &recent_briefs);
    let summary = run_claude(&prompt).await?;
    let summary = summary.trim();
    if summary.is_empty() {
        return Ok(None);
    }

    let memory_text = format!(
        "[{} · 최근 {}일 · {}]\n{}",
        label,
        days,
        chrono::Local::now().format("%Y-%m-%d %H:%M"),
        summary
    );
    let m = {
        let conn = brain.lock().expect("brain mutex poisoned");
        crate::db::memory::insert(
            &conn,
            crate::db::memory::MemoryInput {
                user_id,
                text: memory_text,
                scope: "user".into(),
                priority: "info".into(),
                source: "asurada".into(),
                project: None,
                tech: None,
                metadata: serde_json::json!({
                    "kind": kind,
                    "days": days,
                    "projects_seen": per_project.len(),
                }),
            },
        )?
    };
    Ok(Some(m))
}

#[derive(Debug, Clone)]
struct ProjectSummary {
    project: String,
    activity_count: i64,
    intervention_count: i64,
    redo_count: i64,
    last_prompt: Option<String>,
}

fn gather_per_project_summary(
    conn: &Connection,
    user_id: &str,
    days: i64,
) -> Result<Vec<ProjectSummary>> {
    let since = (Utc::now() - Duration::days(days)).to_rfc3339();
    let mut stmt = conn.prepare(
        r#"SELECT project,
                  SUM(CASE WHEN event_type = 'hook.user_prompt' THEN 1 ELSE 0 END),
                  SUM(CASE WHEN event_type = 'signal.intervention' THEN 1 ELSE 0 END),
                  SUM(CASE WHEN event_type = 'signal.redo' THEN 1 ELSE 0 END)
           FROM events
           WHERE user_id = ?1 AND created_at > ?2
             AND event_type IN ('hook.user_prompt','signal.intervention','signal.redo')
           GROUP BY project
           HAVING SUM(CASE WHEN event_type = 'hook.user_prompt' THEN 1 ELSE 0 END) > 0
           ORDER BY SUM(CASE WHEN event_type = 'hook.user_prompt' THEN 1 ELSE 0 END) DESC"#,
    )?;
    let rows: Vec<(String, i64, i64, i64)> = stmt
        .query_map(params![user_id, since], |r| {
            Ok((r.get(0)?, r.get(1)?, r.get(2)?, r.get(3)?))
        })?
        .filter_map(|r| r.ok())
        .collect();

    let mut out = Vec::with_capacity(rows.len());
    for (project, activity, intervention, redo) in rows {
        // 마지막 prompt 한 줄 — 어떤 작업이었는지 가늠
        let last = conn
            .query_row(
                r#"SELECT json_extract(payload, '$.prompt')
                   FROM events
                   WHERE user_id = ?1 AND project = ?2 AND event_type = 'hook.user_prompt'
                     AND created_at > ?3
                   ORDER BY created_at DESC LIMIT 1"#,
                params![user_id, &project, since],
                |r| r.get::<_, Option<String>>(0),
            )
            .ok()
            .flatten();
        out.push(ProjectSummary {
            project,
            activity_count: activity,
            intervention_count: intervention,
            redo_count: redo,
            last_prompt: last,
        });
    }
    Ok(out)
}

fn gather_recent_briefs(
    conn: &Connection,
    user_id: &str,
    days: i64,
) -> Result<Vec<String>> {
    let since = (Utc::now() - Duration::days(days)).to_rfc3339();
    let mut stmt = conn.prepare(
        r#"SELECT text FROM memories
           WHERE user_id = ?1
             AND source = 'asurada'
             AND json_extract(metadata, '$.kind') = 'brief'
             AND created_at > ?2
             AND deleted_at IS NULL
           ORDER BY created_at DESC LIMIT 8"#,
    )?;
    let rows: Vec<String> = stmt
        .query_map(params![user_id, since], |r| r.get(0))?
        .filter_map(|r| r.ok())
        .collect();
    Ok(rows)
}

fn build_reflection_prompt(
    label: &str,
    days: i64,
    per_project: &[ProjectSummary],
    briefs: &[String],
) -> String {
    let mut s = String::new();
    s.push_str(
        "당신은 Asurada — 사용자 곁에서 매 순간을 지켜보는 AI 동반자입니다. \
         지금은 짧은 통합 회고. 여러 프로젝트를 가로지른 사용자의 *흐름* 과 \
         *리듬* 을 한 호흡으로 짚어주세요.\n\n\
         [원칙]\n\
         - 자기 자신 주어로 X. 사용자/상황을 주어로.\n\
         - 차분하고 단단한 어조. 과장 X.\n\
         - 한국어 5~8줄. 문장으로 (불릿 X).\n\
         - 단순 합산 X. *연결성* 과 *변화* 에 주목 — 무엇이 어떻게 이어졌는지.\n\n\
         [출력 구조]\n\
         1. 이 기간 사용자의 큰 흐름 (어디에 무게가 있었나)\n\
         2. 프로젝트 간 연결 / 충돌이 있었다면\n\
         3. 다듬어진 점 vs 미해결 채로 남은 점\n\
         4. 다음으로 권할 만한 무게 중심 (선택지로 부드럽게)\n\n",
    );
    s.push_str(&format!("회고 라벨: **{}** ({}일)\n\n", label, days));

    s.push_str("[프로젝트별 활동]\n");
    for p in per_project {
        let last: String = p
            .last_prompt
            .as_deref()
            .map(|s| s.chars().take(60).collect())
            .unwrap_or_else(|| "(이전 prompt 없음)".into());
        s.push_str(&format!(
            "- {} — prompt {}, 교정 {}, 반복 {}. 마지막: \"{}\"\n",
            p.project, p.activity_count, p.intervention_count, p.redo_count, last
        ));
    }

    if !briefs.is_empty() {
        s.push_str("\n[같은 기간의 프로젝트 brief 들 — 참고용]\n");
        for b in briefs.iter().take(5) {
            let preview: String = b.chars().take(400).collect();
            s.push_str(&format!("---\n{}\n", preview));
            if b.chars().count() > 400 {
                s.push_str("...(생략)\n");
            }
        }
    }

    s.push_str("\n위 정보를 바탕으로 통합 회고를 작성하세요.");
    s
}

/// 최근 24h 내 daily reflection 있는지 (dedup).
pub fn should_reflect_daily(conn: &Connection, user_id: &str) -> Result<bool> {
    let cutoff = (Utc::now() - Duration::hours(24)).to_rfc3339();
    let n: i64 = conn
        .query_row(
            r#"SELECT COUNT(*) FROM memories
               WHERE user_id = ?1 AND source = 'asurada'
                 AND json_extract(metadata, '$.kind') = 'reflection_daily'
                 AND created_at > ?2 AND deleted_at IS NULL"#,
            params![user_id, cutoff],
            |r| r.get(0),
        )
        .unwrap_or(0);
    Ok(n == 0)
}

pub fn should_reflect_weekly(conn: &Connection, user_id: &str) -> Result<bool> {
    let cutoff = (Utc::now() - Duration::days(7)).to_rfc3339();
    let n: i64 = conn
        .query_row(
            r#"SELECT COUNT(*) FROM memories
               WHERE user_id = ?1 AND source = 'asurada'
                 AND json_extract(metadata, '$.kind') = 'reflection_weekly'
                 AND created_at > ?2 AND deleted_at IS NULL"#,
            params![user_id, cutoff],
            |r| r.get(0),
        )
        .unwrap_or(0);
    Ok(n == 0)
}

// ── Issue capture (작업 세션 응결) ───────────────────────────

/// 마지막 issue 이후 (또는 N시간 전부터) 의 활동을 하나의 issue 로 압축.
/// title + summary + projects + event_count 를 Claude 가 만들어 issues 테이블에 저장.
pub async fn capture_issue(
    brain: std::sync::Arc<std::sync::Mutex<Connection>>,
    user_id: String,
    fallback_hours: i64,
) -> Result<Option<crate::db::issue::Issue>> {
    let (events, since_iso) = {
        let conn = brain.lock().expect("brain mutex poisoned");
        // 마지막 issue 끝 또는 fallback 시점부터
        let last_end = crate::db::issue::last_ended_at(&conn, &user_id)?;
        let since = last_end.unwrap_or_else(|| {
            (Utc::now() - Duration::hours(fallback_hours)).to_rfc3339()
        });
        let mut stmt = conn.prepare(
            r#"SELECT created_at, project, event_type,
                      json_extract(payload, '$.prompt'),
                      json_extract(payload, '$.tool_name'),
                      json_extract(payload, '$.prompt_preview'),
                      json_extract(payload, '$.patterns')
               FROM events
               WHERE user_id = ?1 AND created_at > ?2
                 AND event_type IN (
                     'hook.user_prompt',
                     'signal.intervention',
                     'signal.redo',
                     'signal.harness_use'
                 )
               ORDER BY created_at ASC LIMIT 200"#,
        )?;
        let rows: Vec<(String, String, String, Option<String>, Option<String>, Option<String>, Option<String>)> = stmt
            .query_map(params![&user_id, &since], |r| {
                Ok((
                    r.get(0)?, r.get(1)?, r.get(2)?,
                    r.get(3)?, r.get(4)?, r.get(5)?, r.get(6)?,
                ))
            })?
            .filter_map(|r| r.ok())
            .collect();
        (rows, since)
    };

    if events.is_empty() {
        return Ok(None);
    }

    let started_at = events.first().map(|e| e.0.clone()).unwrap_or(since_iso);
    let ended_at = events.last().map(|e| e.0.clone());
    let event_count = events.len() as i64;

    let prompt = build_issue_prompt(&events);
    let response = run_claude(&prompt).await?;
    let parsed = parse_issue_response(&response)?;

    let issue = {
        let conn = brain.lock().expect("brain mutex poisoned");
        crate::db::issue::insert(
            &conn,
            crate::db::issue::IssueInput {
                user_id,
                title: parsed.title,
                summary: parsed.summary,
                projects: parsed.projects,
                started_at,
                ended_at,
                event_count,
                metadata: serde_json::json!({"source": "auto_capture"}),
            },
        )?
    };
    Ok(Some(issue))
}

#[derive(Debug)]
struct ParsedIssue {
    title: String,
    summary: String,
    projects: Vec<String>,
}

fn parse_issue_response(text: &str) -> Result<ParsedIssue> {
    let mut title = String::new();
    let mut projects: Vec<String> = Vec::new();
    let mut summary_lines: Vec<String> = Vec::new();
    let mut in_summary = false;

    for raw in text.lines() {
        let line = raw.trim_start_matches('#').trim();
        if let Some(rest) = line.strip_prefix("TITLE:") {
            title = rest.trim().trim_matches('*').trim().to_string();
            continue;
        }
        if let Some(rest) = line.strip_prefix("PROJECTS:") {
            projects = rest
                .trim()
                .split(|c| c == ',' || c == '·' || c == '/')
                .map(|s| s.trim().to_string())
                .filter(|s| !s.is_empty())
                .collect();
            continue;
        }
        if line.starts_with("SUMMARY") {
            in_summary = true;
            continue;
        }
        if in_summary {
            summary_lines.push(raw.to_string());
        }
    }

    let summary = summary_lines.join("\n").trim().to_string();
    if title.is_empty() || summary.is_empty() {
        // 형식 못 따랐으면 fallback — 첫 줄을 title 로, 나머지 summary
        let mut iter = text.trim().lines();
        title = iter
            .next()
            .map(|s| s.trim().chars().take(60).collect())
            .unwrap_or_else(|| "(제목 없음)".into());
        let rest: String = iter.collect::<Vec<_>>().join("\n").trim().to_string();
        return Ok(ParsedIssue {
            title,
            summary: if rest.is_empty() { text.trim().to_string() } else { rest },
            projects: vec![],
        });
    }
    Ok(ParsedIssue {
        title,
        summary,
        projects,
    })
}

fn build_issue_prompt(
    events: &[(String, String, String, Option<String>, Option<String>, Option<String>, Option<String>)],
) -> String {
    let mut s = String::new();
    s.push_str(
        "당신은 Asurada — 사용자 곁에서 일하는 동반자. 최근의 작업을 \
         *하나의 issue (작업 단위)* 로 압축해주세요.\n\n\
         [원칙]\n\
         - 제목 한 줄 — 50자 이내, 동사형, 무엇을 했는가가 즉시 이해되게.\n\
         - 요약 — 5~10줄, markdown 가능, 결과 위주. 너무 세밀한 step 나열 X.\n\
         - 가장 큰 결정/산출물 1~2개 강조.\n\
         - 한국어, 차분하고 신중한 어조. 자기 자신 주어 X.\n\n\
         [출력 형식 — 정확히 이 형식으로]\n\
         TITLE: <한 줄 제목>\n\
         PROJECTS: <쉼표 구분 — 관여한 프로젝트들>\n\
         SUMMARY:\n\
         <markdown 요약>\n\n\
         [활동 — 시간순]\n",
    );
    for (at, project, kind, prompt, tool, preview, patterns) in events {
        let local = chrono::DateTime::parse_from_rfc3339(at)
            .map(|d| d.with_timezone(&chrono::Local).format("%m-%d %H:%M").to_string())
            .unwrap_or_else(|_| at.clone());
        let detail = match kind.as_str() {
            "hook.user_prompt" => prompt.clone().unwrap_or_default(),
            "signal.intervention" => format!("교정: {}", patterns.clone().unwrap_or_default()),
            "signal.redo" => format!("반복: {}", preview.clone().unwrap_or_default()),
            "signal.harness_use" => "하네스 사용".into(),
            _ => tool.clone().unwrap_or_default(),
        };
        let detail_short: String = detail.chars().take(120).collect();
        s.push_str(&format!("- [{}] {}/{}: {}\n", local, project, kind, detail_short));
    }
    s.push_str("\n위 활동을 하나의 issue 로 압축한 결과를 출력 형식대로 응답하세요.");
    s
}

/// 최근 24h 내 활동 있고 12h 내 brief 없는 프로젝트 자동 선별.
pub fn should_brief(conn: &Connection, user_id: &str, project: &str) -> Result<bool> {
    let recent_activity_cutoff = (Utc::now() - Duration::hours(24)).to_rfc3339();
    let activity: i64 = conn
        .query_row(
            r#"SELECT COUNT(*) FROM events
               WHERE user_id = ?1 AND project = ?2
                 AND event_type = 'hook.user_prompt'
                 AND created_at > ?3"#,
            params![user_id, project, recent_activity_cutoff],
            |r| r.get(0),
        )
        .unwrap_or(0);
    if activity == 0 {
        return Ok(false);
    }

    let last_brief_cutoff = (Utc::now() - Duration::hours(12)).to_rfc3339();
    let recent_brief: i64 = conn
        .query_row(
            r#"SELECT COUNT(*) FROM memories
               WHERE user_id = ?1 AND project = ?2
                 AND source = 'asurada'
                 AND json_extract(metadata, '$.kind') = 'brief'
                 AND created_at > ?3
                 AND deleted_at IS NULL"#,
            params![user_id, project, last_brief_cutoff],
            |r| r.get(0),
        )
        .unwrap_or(0);
    Ok(recent_brief == 0)
}