devist 0.26.0

Project bootstrap CLI for AI-assisted development. Spin up new projects from templates, manage backends, and keep your codebase comprehensible.
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
//! Periodic memory consolidation — Reso's "sleep cycle".
//!
//! Reads all active memories, asks Claude to evaluate each against the
//! same MUST / MUST NOT bar that gates fact extraction, then applies
//! the verdicts: keep / update / merge / delete.
//!
//! Triggers (whichever fires first):
//! - **Hourly tick**: 60-minute interval after last successful run.
//! - **Accumulation gate**: 10+ new memories created since last run.
//!
//! Safety rails:
//! - `source='user'` rows are NEVER auto-deleted or demoted.
//! - `priority='constraint'` rows are NEVER auto-demoted.
//! - All mutations use soft-delete (`deleted_at`); restore is a SQL UPDATE.
//! - One Claude call per cycle; no per-memory loop.

use anyhow::{anyhow, Context, Result};
use chrono::Local;
use serde_json::Value;
use std::time::{Duration, Instant};

use crate::worker::claude::ClaudeCli;
use crate::worker::config::WorkerConfig;
use crate::worker::supabase::{MemoryRow, SupabaseClient};

const TICK_INTERVAL_SECS: u64 = 30;
const HOUR: Duration = Duration::from_secs(3600);
const NEW_MEMORY_TRIGGER: usize = 10;
/// Cap on how many memories we send to Claude in one cycle. If the
/// store grows past this we slice the oldest first; future cycles
/// pick up the rest.
const MAX_PER_CYCLE: usize = 60;

pub fn run(cfg: WorkerConfig) -> Result<()> {
    let supabase = match make_supabase(&cfg) {
        Some(s) => s,
        None => {
            log_line("[consolidate] Supabase not configured — thread idle");
            return Ok(());
        }
    };
    let claude = ClaudeCli::new(cfg.claude_bin.clone());

    log_line("[consolidate] thread up");

    let mut last_run = Instant::now() - HOUR; // run once shortly after startup
    let mut last_seen_count = 0usize;
    let mut last_heartbeat = Instant::now() - Duration::from_secs(60);

    loop {
        std::thread::sleep(Duration::from_secs(TICK_INTERVAL_SECS));

        if last_heartbeat.elapsed() >= Duration::from_secs(30) {
            let _ = supabase.heartbeat("consolidate");
            last_heartbeat = Instant::now();
        }

        let now = Instant::now();
        let elapsed = now.duration_since(last_run);

        let current_count = match supabase.list_all_memories() {
            Ok(rows) => rows.len(),
            Err(e) => {
                log_line(&format!("[consolidate] count err: {}", e));
                continue;
            }
        };
        let new_since_last = current_count.saturating_sub(last_seen_count);
        let hourly_due = elapsed >= HOUR;
        let accumulation_due = new_since_last >= NEW_MEMORY_TRIGGER;

        if !hourly_due && !accumulation_due {
            continue;
        }

        let trigger = if hourly_due { "hourly" } else { "accumulation" };
        log_line(&format!(
            "[consolidate] {} trigger ({} new since last run, total {})",
            trigger, new_since_last, current_count
        ));

        match run_once(&supabase, &claude, &cfg.advice_locale, &cfg.project_aliases) {
            Ok(summary) => log_line(&format!("[consolidate] {}", summary)),
            Err(e) => log_line(&format!("[consolidate] cycle err: {}", e)),
        }

        last_run = now;
        // Refresh count post-cycle so the accumulation gate uses
        // post-deletion totals, not the inflated pre-cycle one.
        last_seen_count = supabase.list_all_memories().map(|r| r.len()).unwrap_or(0);
    }
}

fn make_supabase(cfg: &WorkerConfig) -> Option<SupabaseClient> {
    let (url, key) = match (&cfg.supabase_url, &cfg.supabase_key) {
        (Some(u), Some(k)) if !u.is_empty() && !k.is_empty() => (u.as_str(), k.as_str()),
        _ => return None,
    };
    let client_id = cfg
        .client_id
        .as_deref()
        .filter(|s| !s.is_empty())
        .unwrap_or("unknown");
    SupabaseClient::new(url, key, client_id).ok()
}

/// One-shot entry point: run a single consolidation cycle and return
/// a summary. Used by the `devist worker run-task consolidate` CLI
/// subcommand. Same logic the daemon thread invokes on its hourly
/// schedule, just decoupled from the loop.
pub fn run_task(cfg: &WorkerConfig) -> Result<String> {
    let supabase = make_supabase(cfg)
        .ok_or_else(|| anyhow!("Supabase not configured (supabase_url/supabase_key)"))?;
    let claude = ClaudeCli::new(cfg.claude_bin.clone());
    run_once(&supabase, &claude, &cfg.advice_locale, &cfg.project_aliases)
}

fn run_once(
    supabase: &SupabaseClient,
    claude: &ClaudeCli,
    locale: &str,
    aliases: &std::collections::HashMap<String, String>,
) -> Result<String> {
    let mut rows = supabase.list_all_memories()?;
    if rows.is_empty() {
        return Ok("nothing to consolidate".into());
    }
    rows.sort_by(|a, b| a.id.cmp(&b.id));
    if rows.len() > MAX_PER_CYCLE {
        rows.truncate(MAX_PER_CYCLE);
    }

    let prompt = build_prompt(&rows, locale);
    let raw = claude
        .ask_json(&prompt, Duration::from_secs(120))
        .context("claude consolidate call")?;
    let verdicts = raw
        .get("verdicts")
        .and_then(|v| v.as_array())
        .ok_or_else(|| anyhow!("no verdicts[] in response"))?;

    let mut kept = 0usize;
    let mut updated = 0usize;
    let mut merged = 0usize;
    let mut deleted = 0usize;
    let mut skipped_protected = 0usize;
    let mut errors = 0usize;

    for v in verdicts {
        let id = match v.get("id").and_then(|x| x.as_i64()) {
            Some(id) => id,
            None => {
                errors += 1;
                continue;
            }
        };
        let action = v.get("action").and_then(|x| x.as_str()).unwrap_or("keep");
        let row = match rows.iter().find(|r| r.id == id) {
            Some(r) => r,
            None => continue,
        };

        let protected = is_protected(row);
        let mutating = matches!(action, "delete" | "merge" | "update");
        if protected && mutating && !is_safe_protected_update(row, action, v) {
            skipped_protected += 1;
            continue;
        }

        match action {
            "keep" => kept += 1,
            "update" => {
                let new_text = v.get("text").and_then(|x| x.as_str());
                let new_scope = v.get("scope").and_then(|x| x.as_str());
                let new_priority = v.get("priority").and_then(|x| x.as_str());
                // Apply project_aliases on Claude's suggestion so a
                // verdict can't sneak the raw folder case (e.g.
                // "devist") back into the store.
                //   None             → don't touch project field
                //   Some(None)       → set to NULL (verdict had explicit null)
                //   Some(Some("..")) → set to canonical (alias-mapped) value
                let new_project: Option<Option<&str>> = v.get("project").map(|x| {
                    x.as_str().map(|p| {
                        aliases.get(p).map(|a| a.as_str()).unwrap_or(p)
                    })
                });
                if let Err(e) =
                    supabase.update_memory(id, new_text, new_scope, new_priority, new_project)
                {
                    log_line(&format!("[consolidate] update #{} err: {}", id, e));
                    errors += 1;
                } else {
                    updated += 1;
                }
            }
            "merge" => {
                if let Err(e) = supabase.soft_delete_memory(id) {
                    log_line(&format!("[consolidate] merge #{} err: {}", id, e));
                    errors += 1;
                } else {
                    merged += 1;
                }
            }
            "delete" => {
                if let Err(e) = supabase.soft_delete_memory(id) {
                    log_line(&format!("[consolidate] delete #{} err: {}", id, e));
                    errors += 1;
                } else {
                    deleted += 1;
                }
            }
            _ => kept += 1,
        }
    }

    // Phase 7: cross-project promotions. Inserted as status='proposed'
    // tech-scope memories — user reviews each in the dashboard before
    // they go live. Source project rows are intentionally NOT deleted
    // so the promotion can be rejected without data loss.
    let mut promoted = 0usize;
    if let Some(promotions) = raw.get("promotions").and_then(|v| v.as_array()) {
        for p in promotions {
            let text = p.get("text").and_then(|x| x.as_str()).unwrap_or("").trim();
            let priority = p.get("priority").and_then(|x| x.as_str()).unwrap_or("strong");
            let reason = p.get("reason").and_then(|x| x.as_str()).unwrap_or("");
            let tech: Vec<String> = p
                .get("tech")
                .and_then(|x| x.as_array())
                .map(|arr| {
                    arr.iter()
                        .filter_map(|t| t.as_str().map(String::from))
                        .collect()
                })
                .unwrap_or_default();
            if text.is_empty() || tech.is_empty() {
                continue;
            }
            match supabase.insert_memory_with_status(
                text,
                "tech",
                priority,
                "claude",
                None,
                &tech,
                "proposed",
                Some(reason),
            ) {
                Ok(id) => {
                    promoted += 1;
                    log_line(&format!(
                        "[consolidate] promoted to tech memory #{} ({}): {}",
                        id, tech.join(","), reason
                    ));
                }
                Err(e) => {
                    errors += 1;
                    log_line(&format!("[consolidate] promote err: {}", e));
                }
            }
        }
    }

    Ok(format!(
        "{} reviewed → keep {}, update {}, merge {}, delete {}, promote {} (protected skipped {}, errors {})",
        rows.len(),
        kept,
        updated,
        merged,
        deleted,
        promoted,
        skipped_protected,
        errors,
    ))
}

fn is_protected(r: &MemoryRow) -> bool {
    r.source == "user" || r.priority == "constraint"
}

/// Even for protected rows we allow non-destructive metadata fixes
/// (e.g. wrong project, wrong scope category). Demotion below the
/// row's current priority and outright deletion stay forbidden.
fn is_safe_protected_update(row: &MemoryRow, action: &str, verdict: &Value) -> bool {
    if action != "update" {
        return false;
    }
    if let Some(np) = verdict.get("priority").and_then(|x| x.as_str()) {
        // Allow re-classification within "strong" / "constraint" / "preference"
        // EXCEPT demoting a constraint to anything weaker, or demoting
        // user-source rows to weaker-than-strong.
        let cur = row.priority.as_str();
        if cur == "constraint" && np != "constraint" {
            return false;
        }
        if row.source == "user" && matches!(np, "preference" | "info") {
            return false;
        }
    }
    true
}

fn build_prompt(rows: &[MemoryRow], locale: &str) -> String {
    let mut listing = String::new();
    for r in rows {
        let project = r.project.as_deref().unwrap_or("-");
        let tech = if r.tech.is_empty() {
            "-".into()
        } else {
            r.tech.join(",")
        };
        listing.push_str(&format!(
            "#{} [scope={} priority={} source={} project={} tech={}]\n  {}\n\n",
            r.id, r.scope, r.priority, r.source, project, tech, r.text
        ));
    }

    format!(
        r#"You are the consolidation pass for the Reso memory store.
You will be given the full list of currently active memories. Evaluate
each one against a strict bar and return a JSON verdict per memory.

============================================================
THE BAR — what counts as a real, durable memory
============================================================
A memory should remain ONLY IF a future maintainer or AI assistant
WOULD WANT TO KNOW IT but CANNOT learn it by reading the code. Valid
categories:
  1. Decision rationale (WHY a non-obvious choice was made)
  2. Hard constraints / invariants (rules future code MUST follow)
  3. Cross-file invariants & contracts
  4. Past incidents / scars (bugs that motivated current shape)

DELETE memories that are:
  - Restatements of file structure, dependencies, build flags
  - "This project uses X" / "X is implemented with Y" observations
  - Already covered by docs (CLAUDE.md, README, module docstrings)
  - Stale references to removed features
  - Snapshots of "current architecture" that go stale on next release

MERGE memories that say semantically the same thing — keep the
strongest (highest priority, most specific text), delete the rest.

UPDATE memories where:
  - scope is wrong: `project` (specific repo), `tech` (Rust/React/...
    ecosystem), `user` (cross-project user preference)
  - priority is wrong: `constraint` (hard MUST), `strong` (well-
    established), `preference` (semantic-search retrievable),
    `info` (archival)
  - text is verbose / repetitive — tighten it

KEEP if it already passes the bar with correct metadata.

PROMOTE when the same intent appears as `scope=project` memories in
THREE OR MORE different projects: emit one PROMOTE verdict
(separately, listed at the top level alongside `verdicts`) suggesting
a new `scope=tech` memory that captures the cross-project pattern.
The original project rows stay (we don't auto-delete them); the new
tech memory is inserted as `status=proposed` for user review.

============================================================
PROTECTED ROWS — be respectful
============================================================
Rows with `source=user` were created by a human deliberately.
Rows with `priority=constraint` are explicit hard rules.
For these:
  - You may suggest scope/project corrections
  - You may NOT delete or merge them
  - You may NOT demote priority below its current level
  - You may shorten text only if the meaning is preserved exactly

============================================================
INPUT — current memories
============================================================
{listing}
============================================================
OUTPUT — STRICT JSON, no markdown fences
============================================================
{{
  "verdicts": [
    {{"id": <int>, "action": "keep|update|merge|delete", "reason": "<short, in {locale}>", "text": "<new text if action=update>", "scope": "project|tech|user", "priority": "constraint|strong|preference|info", "project": "<canonical name or null>"}}
  ],
  "promotions": [
    {{"text": "<new tech-scope memory text in {locale}>", "tech": ["<tag>"], "priority": "constraint|strong", "reason": "<cite the source memory ids: 'merged from #12, #34, #56'>", "source_ids": [<int>, <int>, ...]}}
  ]
}}

Every memory in the input MUST appear exactly once in `verdicts`.
Fields beyond `id`, `action`, `reason` are only required when action=update.
For action=merge, set `reason` to "merged into #<other_id>".

Promotions are OPTIONAL and rare — only emit when ≥3 project-scope
memories across ≥3 distinct projects say semantically the same thing.
The user reviews each promotion in the dashboard before it activates.

Reasons should be brief — ~12 Korean characters or 8 English words.
"#,
    )
}

fn log_line(msg: &str) {
    let now = Local::now().format("%Y-%m-%d %H:%M:%S");
    println!("{} {}", now, msg);
}