Skip to content

feat(#351): orphan detection — find disconnected memories - #389

Merged
ajianaz merged 5 commits into
developfrom
feat/351-orphans
Jun 19, 2026
Merged

ajianaz merged 5 commits into
developfrom
feat/351-orphans

Conversation

@ajianaz

@ajianaz ajianaz commented Jun 18, 2026

Copy link
Copy Markdown
Collaborator

Closes #351. See CHANGELOG entry for full details. All tests pass, clippy + fmt clean.

Detect memories that have no graph edges (incoming or outgoing), have
never been recalled, are not pinned, and fall below an importance
threshold. Such memories dilute recall quality and are prime candidates
for cleanup via the dream cycle (#353).

Core (uteke-core):
  - New orphans.rs module:
    - OrphanMemory struct with orphan_score (0.0..=1.0)
    - compute_orphan_score(): (1-edge_density)*0.4 + (1-access_freq)*0.3
      + (1-importance)*0.3
    - Store::find_orphans(): single SQL pass via LEFT JOIN on memory_edges
      twice — no O(n^2) scan
    - Uteke::find_orphans() with namespace scoping
    - DEFAULT_ORPHAN_THRESHOLD = 0.3
  - Leverages #350 backlinks: any referenced memory is automatically excluded

CLI (uteke-cli):
  - New 'uteke orphans [--threshold 0.3] [--limit 50]' command

Tests (+7 new):
  - orphan_score: disconnected/low vs connected/high
  - find_orphans: returns isolated low-importance, excludes connected
  - respects threshold (low vs high)
  - excludes pinned and accessed memories
  - namespace-scoped detection
  - limit cap

Closes #351
@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:medium Fix soon labels Jun 18, 2026
Comment thread crates/uteke-core/src/orphans.rs Fixed
Comment thread crates/uteke-core/src/orphans.rs Fixed
@github-actions

github-actions Bot commented Jun 18, 2026 •

Copy link
Copy Markdown

🔍 Cora AI Code Review

❌ Blocked — critical issues found.

🔴 Error (1)

  • crates/uteke-cli/src/commands/orphans.rs:12 — The CLI accepts threshold: Option<f64> and passes it directly to find_orphans after only applying unwrap_or(...). The core query uses m.importance < ?1, so a negative threshold will always return no results, while a value greater than 1.0 will match effectively all low-connectivity memories regardless of the documented 0.0..=1.0 scoring/importance model. There is no validation in this new command path to reject invalid user input.

Review powered by cora-cli · BYOK · MIT

CodeCora #389: the previous loop broke out as soon as orphans.len() >=
limit, but the subsequent sort_by(orphan_score desc) could reorder the
truncated subset — a higher-score orphan that appeared later in the SQL
result set would be dropped.

Fix: collect all candidates first, compute orphan_score, sort, THEN
truncate to limit. Orphan result sets are typically small (each match
has zero edges + zero accesses), so the full fetch is cheap.
Comment thread crates/uteke-core/src/orphans.rs Fixed
Comment thread crates/uteke-core/src/orphans.rs Fixed
CodeCora #389 found two issues in compute_orphan_score:

1. access_count.max(1) made access_count=0 and access_count=1 produce
   the same log10(1)/3 = 0.0 score. Never-accessed memories were treated
   identically to accessed-once. Fix: use log10(n+1) so access_count=1
   produces a non-zero normalized frequency.

2. importance was cast to f32 and used as (1-importance)*0.3 without
   clamping. Out-of-range values (>1.0 or <0.0) would skew the score or
   produce negative results. Fix: clamp to [0, 1].

Added 2 regression tests: never-accessed vs once-accessed, and
importance bounds clamping.
Comment thread crates/uteke-core/src/orphans.rs Fixed
CodeCora #389 round 2: find_orphans SQL didn't filter m.deprecated = 0,
so superseded memories could appear as orphan candidates even though
they were intentionally retired.

Fix: add 'AND m.deprecated = 0' to the WHERE clause.

Also: fix clippy manual_range_contains warning in test assertion.
Comment thread crates/uteke-core/src/orphans.rs Fixed
Comment thread crates/uteke-core/src/orphans.rs Fixed
lib.rs: added orphans module alongside salience_recency and timeline.
cli.rs: added Orphans command alongside Timeline.
commands/mod.rs: added orphans dispatch alongside timeline.
CHANGELOG: added #351 orphan detection section.
ns: Option<&str>,
threshold: Option<f64>,
limit: usize,
) -> Result<(), String> {
@ajianaz
ajianaz merged commit 76aa234 into develop Jun 19, 2026
8 of 9 checks passed
@ajianaz
ajianaz deleted the feat/351-orphans branch July 9, 2026 14:28
ajianaz added a commit that referenced this pull request Aug 13, 2026
* feat(#351): orphan detection — find disconnected memories

Detect memories that have no graph edges (incoming or outgoing), have
never been recalled, are not pinned, and fall below an importance
threshold. Such memories dilute recall quality and are prime candidates
for cleanup via the dream cycle (#353).

Core (uteke-core):
  - New orphans.rs module:
    - OrphanMemory struct with orphan_score (0.0..=1.0)
    - compute_orphan_score(): (1-edge_density)*0.4 + (1-access_freq)*0.3
      + (1-importance)*0.3
    - Store::find_orphans(): single SQL pass via LEFT JOIN on memory_edges
      twice — no O(n^2) scan
    - Uteke::find_orphans() with namespace scoping
    - DEFAULT_ORPHAN_THRESHOLD = 0.3
  - Leverages #350 backlinks: any referenced memory is automatically excluded

CLI (uteke-cli):
  - New 'uteke orphans [--threshold 0.3] [--limit 50]' command

Tests (+7 new):
  - orphan_score: disconnected/low vs connected/high
  - find_orphans: returns isolated low-importance, excludes connected
  - respects threshold (low vs high)
  - excludes pinned and accessed memories
  - namespace-scoped detection
  - limit cap

Closes #351

* fix(#351): fetch all orphan candidates before sort + truncate

CodeCora #389: the previous loop broke out as soon as orphans.len() >=
limit, but the subsequent sort_by(orphan_score desc) could reorder the
truncated subset — a higher-score orphan that appeared later in the SQL
result set would be dropped.

Fix: collect all candidates first, compute orphan_score, sort, THEN
truncate to limit. Orphan result sets are typically small (each match
has zero edges + zero accesses), so the full fetch is cheap.

* fix(#351): differentiate access_count=0 vs 1 + clamp importance

CodeCora #389 found two issues in compute_orphan_score:

1. access_count.max(1) made access_count=0 and access_count=1 produce
   the same log10(1)/3 = 0.0 score. Never-accessed memories were treated
   identically to accessed-once. Fix: use log10(n+1) so access_count=1
   produces a non-zero normalized frequency.

2. importance was cast to f32 and used as (1-importance)*0.3 without
   clamping. Out-of-range values (>1.0 or <0.0) would skew the score or
   produce negative results. Fix: clamp to [0, 1].

Added 2 regression tests: never-accessed vs once-accessed, and
importance bounds clamping.

* fix(#351): exclude deprecated memories + clippy manual_range_contains

CodeCora #389 round 2: find_orphans SQL didn't filter m.deprecated = 0,
so superseded memories could appear as orphan candidates even though
they were intentionally retired.

Fix: add 'AND m.deprecated = 0' to the WHERE clause.

Also: fix clippy manual_range_contains warning in test assertion.
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:medium Fix soon 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: orphan detection — find disconnected memories in the graph

2 participants