Skip to content

feat(#353): dream cycle — coordinated maintenance pipeline - #390

Merged
ajianaz merged 9 commits into
developfrom
feat/353-dream-cycle
Jun 19, 2026
Merged

ajianaz merged 9 commits into
developfrom
feat/353-dream-cycle

Conversation

@ajianaz

@ajianaz ajianaz commented Jun 18, 2026

Copy link
Copy Markdown
Collaborator

Closes #353. Single command that runs all 6 maintenance phases in dependency order (lint → backlinks → dedup → orphans → compact → verify). 4 new tests, clippy + fmt clean.

Add 'uteke dream', a single command that runs all 6 maintenance phases
in dependency order: lint → backlinks → dedup → orphans → compact → verify.
All local, zero LLM. Inspired by GBrain's overnight dream cycle.

Core (uteke-core):
  - New dream.rs module:
    - DreamPhase enum (6 phases) with all_in_order() + from_str_opt()
    - DreamReport with per-phase results, totals, dry_run flag, duration
    - PhaseResult / PhaseStatus types
    - Uteke::dream(namespace, dry_run, phases) orchestrator
  - Lint: count memories with unknown memory_type values
  - Backlinks: rebuild_backlinks() (#350)
  - Dedup: consolidate(threshold=0.92)
  - Orphans: inline SQL count (degrades gracefully if #351 not merged)
  - Compact: prune(TTL=30d)
  - Verify: existing verify() (db/index consistency)

CLI (uteke-cli):
  - New 'uteke dream [--phases P1,P2] [--skip P3] [--dry-run] [--quiet]'
  - Exits non-zero on errors (cron-friendly)
  - --quiet shows only warnings/errors

Tests (+4 new): phase roundtrip, phase order, dry-run, phase filter

Closes #353
@ajianaz ajianaz added this to the v0.2.1 — DX & Ecosystem milestone Jun 18, 2026
@ajianaz ajianaz added enhancement New feature or request scope:core Core engine work scope:cli CLI and UX type:feature New feature priority:low Fix when convenient labels Jun 18, 2026
Comment thread crates/uteke-core/src/dream.rs Fixed
Comment thread crates/uteke-core/src/dream.rs Fixed
Comment thread crates/uteke-core/src/dream.rs Fixed
Comment thread crates/uteke-core/src/dream.rs Fixed
@github-actions

github-actions Bot commented Jun 18, 2026 •

Copy link
Copy Markdown

🔍 Cora AI Code Review

❌ Blocked — critical issues found.

🔴 Error (2)

  • crates/uteke-core/src/dream.rs:130 — The code sorts selected with sort_by_key(|p| canonical.iter().position(|c| c == p).unwrap_or(usize::MAX)) and then calls selected.dedup(). Because sort_by_key is not guaranteed to preserve the relative order of elements with equal keys, identical phases can remain separated after sorting when the user passes duplicates in mixed order, causing the same phase to run more than once despite the comment run it once.
  • crates/uteke-core/src/dream.rs:229 — Both lint queries only count rows where memory_type NOT IN (...). In SQL, NULL NOT IN (...) evaluates to NULL rather than true, so memories with NULL memory_type will not be counted as invalid. That makes the phase report all types valid even when type data is missing.

Review powered by cora-cli · BYOK · MIT

CodeCora #390 flagged two issues:

1. dream() propagated the first phase error via '?', aborting the whole
   pipeline. Fix: wrap each phase in a match — failures are recorded as
   a PhaseStatus::Error result but subsequent phases still run.

2. phase_lint loaded every memory into RAM via load_all() just to count
   bad types. Fix: use SQL COUNT with NOT IN (...) — O(1) memory, no
   deserialization overhead.
Comment thread crates/uteke-core/src/dream.rs Fixed
Comment thread crates/uteke-core/src/dream.rs Fixed
Comment thread crates/uteke-core/src/dream.rs Fixed
Comment thread crates/uteke-core/src/dream.rs Fixed
…te SQL errors

CodeCora #390 found three issues:

1. Invalid --phases names were silently dropped via filter_map, causing
   'uteke dream --phases bogus' to run ALL phases. Fix: return error
   listing valid phase names.

2. phase_orphans ignored the namespace parameter (_namespace). Running
   'uteke dream --namespace X' counted orphans globally. Fix: add
   'AND m.namespace = ?2' when namespace is provided.

3. Multiple SQL queries used unwrap_or(0) which masked database errors
   as 'clean' results. Fix: propagate via map_err → Error::db. Pipeline
   catch loop converts to PhaseStatus::Error without aborting.
Comment thread crates/uteke-core/src/dream.rs Fixed
Comment thread crates/uteke-core/src/dream.rs Fixed
CodeCora #390 r2:

1. phase_lint ignored namespace, running global COUNT regardless of the
   --namespace flag. Fix: add WHERE namespace = ?1 when namespace is
   provided.

2. phase_orphans SQL didn't exclude deprecated memories, matching the
   same bug fixed in #351. Fix: add AND m.deprecated = 0 to both
   namespace and global queries.
Comment thread crates/uteke-core/src/dream.rs Fixed
Comment thread crates/uteke-core/src/dream.rs Fixed
CodeCora #390 r3: phase_dedup set warnings=1 when duplicates were found
but couldn't be merged (dry-run or threshold too high), yet status
remained Ok. Inconsistent with phase_lint and phase_orphans which
return Warning status for the same pattern.

Fix: set PhaseStatus::Warning when has_warnings is true.
Comment thread crates/uteke-core/src/dream.rs Fixed
Comment thread crates/uteke-core/src/dream.rs Fixed
CodeCora #390 r3 raised two architectural concerns:

1. Backlinks phase is global, not namespace-scoped. This is by design —
   rebuild_backlinks() operates on the edge graph which is not namespace-
   partitioned. Documented in the dream() doc comment.

2. Orphan detection in dry-run doesn't reflect projected post-maintenance
   state. This is by design — dry-run reports against current state.
   Documented in the dream() doc comment.

No code changes — documentation only.
Comment thread crates/uteke-core/src/dream.rs Fixed
Comment thread crates/uteke-core/src/dream.rs Fixed
CodeCora #390 r4: if user passed --phases verify,lint,backlinks, phases
executed in that order instead of dependency order. verify before lint
makes no sense and could produce misleading results.

Fix: sort selected phases by canonical position in all_in_order(), and
dedup duplicates.

Added 2 tests: reversed order, and duplicate phases.
Comment thread crates/uteke-core/src/dream.rs Fixed
CodeCora #390 r4: phase_compact always printed 'memories pruned' even
in dry-run mode, implying destructive changes already happened.

Fix: print 'would be pruned' when dry_run is true.
Comment thread crates/uteke-core/src/dream.rs Fixed
Comment thread crates/uteke-core/src/dream.rs Fixed
cli.rs: added Dream command alongside Orphans and Timeline.
commands/mod.rs: added dream dispatch alongside orphans and timeline.
CHANGELOG: added #353 dream cycle section at top.

Now that #351 (orphans) is merged into develop, dream's inline SQL
orphan detection coexists with the proper find_orphans() API.
) -> Result<DreamReport, Error> {
let start = std::time::Instant::now();
let mut selected: Vec<DreamPhase> = if phases.is_empty() {
DreamPhase::all_in_order().to_vec()
.store
.conn
.query_row("SELECT COUNT(*) FROM memories", [], |r| r.get(0))
.map_err(|e| Error::db("dream lint total count", e))?;
@ajianaz
ajianaz merged commit afe66b4 into develop Jun 19, 2026
8 of 9 checks passed
@ajianaz
ajianaz deleted the feat/353-dream-cycle branch July 9, 2026 14:28
ajianaz added a commit that referenced this pull request Aug 13, 2026
* feat(#353): dream cycle — coordinated maintenance pipeline

Add 'uteke dream', a single command that runs all 6 maintenance phases
in dependency order: lint → backlinks → dedup → orphans → compact → verify.
All local, zero LLM. Inspired by GBrain's overnight dream cycle.

Core (uteke-core):
  - New dream.rs module:
    - DreamPhase enum (6 phases) with all_in_order() + from_str_opt()
    - DreamReport with per-phase results, totals, dry_run flag, duration
    - PhaseResult / PhaseStatus types
    - Uteke::dream(namespace, dry_run, phases) orchestrator
  - Lint: count memories with unknown memory_type values
  - Backlinks: rebuild_backlinks() (#350)
  - Dedup: consolidate(threshold=0.92)
  - Orphans: inline SQL count (degrades gracefully if #351 not merged)
  - Compact: prune(TTL=30d)
  - Verify: existing verify() (db/index consistency)

CLI (uteke-cli):
  - New 'uteke dream [--phases P1,P2] [--skip P3] [--dry-run] [--quiet]'
  - Exits non-zero on errors (cron-friendly)
  - --quiet shows only warnings/errors

Tests (+4 new): phase roundtrip, phase order, dry-run, phase filter

Closes #353

* fix(#353): don't abort dream on phase error + lint via SQL

CodeCora #390 flagged two issues:

1. dream() propagated the first phase error via '?', aborting the whole
   pipeline. Fix: wrap each phase in a match — failures are recorded as
   a PhaseStatus::Error result but subsequent phases still run.

2. phase_lint loaded every memory into RAM via load_all() just to count
   bad types. Fix: use SQL COUNT with NOT IN (...) — O(1) memory, no
   deserialization overhead.

* fix(#353): error on invalid phase + namespace-aware orphans + propagate SQL errors

CodeCora #390 found three issues:

1. Invalid --phases names were silently dropped via filter_map, causing
   'uteke dream --phases bogus' to run ALL phases. Fix: return error
   listing valid phase names.

2. phase_orphans ignored the namespace parameter (_namespace). Running
   'uteke dream --namespace X' counted orphans globally. Fix: add
   'AND m.namespace = ?2' when namespace is provided.

3. Multiple SQL queries used unwrap_or(0) which masked database errors
   as 'clean' results. Fix: propagate via map_err → Error::db. Pipeline
   catch loop converts to PhaseStatus::Error without aborting.

* fix(#353): namespace-aware lint + exclude deprecated in orphan count

CodeCora #390 r2:

1. phase_lint ignored namespace, running global COUNT regardless of the
   --namespace flag. Fix: add WHERE namespace = ?1 when namespace is
   provided.

2. phase_orphans SQL didn't exclude deprecated memories, matching the
   same bug fixed in #351. Fix: add AND m.deprecated = 0 to both
   namespace and global queries.

* fix(#353): dedup phase returns Warning status when duplicates found

CodeCora #390 r3: phase_dedup set warnings=1 when duplicates were found
but couldn't be merged (dry-run or threshold too high), yet status
remained Ok. Inconsistent with phase_lint and phase_orphans which
return Warning status for the same pattern.

Fix: set PhaseStatus::Warning when has_warnings is true.

* docs(#353): document namespace scope and dry-run semantics

CodeCora #390 r3 raised two architectural concerns:

1. Backlinks phase is global, not namespace-scoped. This is by design —
   rebuild_backlinks() operates on the edge graph which is not namespace-
   partitioned. Documented in the dream() doc comment.

2. Orphan detection in dry-run doesn't reflect projected post-maintenance
   state. This is by design — dry-run reports against current state.
   Documented in the dream() doc comment.

No code changes — documentation only.

* fix(#353): canonical phase order + dedup

CodeCora #390 r4: if user passed --phases verify,lint,backlinks, phases
executed in that order instead of dependency order. verify before lint
makes no sense and could produce misleading results.

Fix: sort selected phases by canonical position in all_in_order(), and
dedup duplicates.

Added 2 tests: reversed order, and duplicate phases.

* fix(#353): compact phase shows 'would be pruned' in dry-run

CodeCora #390 r4: phase_compact always printed 'memories pruned' even
in dry-run mode, implying destructive changes already happened.

Fix: print 'would be pruned' when dry_run is true.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request priority:low Fix when convenient scope:cli CLI and UX scope:core Core engine work type:feature New feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: dream cycle — automated local maintenance pipeline (lint → backlinks → dedup → orphans)

2 participants