Skip to content

Stop cron from repeating its two most expensive no-op passes - #123

Open
MrJoy wants to merge 2 commits into
wandercom:mainfrom
MrJoy:perf/cron-hygiene-reindex
Open

MrJoy wants to merge 2 commits into
wandercom:mainfrom
MrJoy:perf/cron-hygiene-reindex

Conversation

@MrJoy

@MrJoy MrJoy commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

kin cron was pegging a core for 40-70s out of every five minutes on my machine (four profiles, ~13.5k active nodes). cProfile says nearly all of it is two passes that find nothing to do and then do it again next time:

  • _graph_hygiene: 41s of a 73s run. It runs fts_search on the title of every non-stale orphan, every pass. The FTS query ORs every title token, so each search ranks a big slice of the corpus (~70ms apiece), and the same ~600 orphans miss every time.
  • select_reindex_nodes(stale=True): 24s. _node_embedding_fresh joins node_vector_meta to the vec0 table, and SQLite scans the virtual table for that join once per node (~2.4ms each). It also reloaded sqlite_vec per node, which was another 5.7s on its own.

Fixes:

  1. Hygiene remembers which orphans missed, keyed by title, in one meta key (graph_hygiene.misses). A missed orphan isn't searched again until its title changes or 24h pass. The record is rebuilt each pass, so anything that stops being an orphan drops out.
  2. The stale scan reads the stored vector ids once, pulls metadata for each page of 500 in one query, and judges freshness with the same _embedding_rows_fresh predicate. _load_sqlite_vec skips the load when the connection already has it.

On a copy of my largest graph (8.6k active nodes, 10.8k vectors), 0.54.2 vs this branch:

0.54.2 branch
stale scan 43.8s 1.4s
hygiene, first pass 21.9s 17.0s
hygiene, second pass 17.0s 0.06s

Same results either way (0 stale, 0 archived, 0 linked). That machine was under heavy load at the time, so the absolute numbers are inflated... the ratios are the point.

test_the_stale_reindex_reaches_past_its_first_page monkeypatched _node_embedding_fresh, which the scan no longer calls, so it now patches _fresh_node_ids. New tests: the bulk selection agrees with the per-node predicate across fresh / missing vector / old text / old fingerprint / no metadata / complete and incomplete multi-chunk nodes, plus the hygiene skip, retry-after-window, forgetting de-orphaned nodes, and a corrupt record.

Full suite: 4964 passed, 1 failed. The failure is test_modern_plugin_second_session_does_not_inherit_first_goal_or_work, which needs Node >= 24 and I have 22 locally. It fails the same way on main.

One thing I'd like a second opinion on: the 24h retry means an orphan whose title would match a newly connected node can wait up to a day to get linked. That seemed like a fine trade to me, but say so if you'd rather key the retry on something graph-wide too.


Proof this PR
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

Graph hygiene ran a full-text search on the title of every unlinked
node on every cron pass. The query ORs every title token, so each one
ranks much of the corpus, and the same orphans missed every time. An
orphan that finds no match is now skipped until its title changes or a
day has passed. The misses are kept in one meta key and rebuilt each
pass, so nodes that are no longer orphans drop out.

The stale-embedding scan checked each active node with a query that
joins node_vector_meta to the vec0 table. SQLite scans the virtual
table for that join once per node, and the extension was reloaded for
every node too. Freshness for each page of 500 nodes is now judged from
one metadata query plus a single read of the stored vector ids, with the
same predicate (_embedding_rows_fresh). sqlite-vec is loaded only when
the connection does not have it yet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01USKFuLCYNe92BkyMEvDqcz
Copilot AI balanced review requested due to automatic review settings December 2, 2026 22:48

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues block the orphan-search caching and batched freshness-check changes.

No open issues 🎉

What changed in this PR

Reduces repeated no-op work in kin cron by caching unsuccessful orphan searches and batching embedding freshness checks.

Changes:

  • Caches orphan search misses until the title changes or 24 hours pass.
  • Batches vector metadata reads and avoids reloading an already-loaded SQLite extension.
  • Adds regression coverage for cache behavior and bulk freshness selection.
File Description
tests/​test_vectors.py Tests bulk freshness equivalence and missing-vector-table behavior.
tests/​test_trust_lookups_and_maintenance.py Updates pagination coverage to use the bulk freshness helper.
tests/​test_daemon.py Tests miss caching, retries, cleanup, and corrupt records.
src/​kindex/​vectors.py Batches stale-vector checks and reuses the loaded extension.
src/​kindex/​daemon.py Caches unsuccessful orphan-link searches.

Review effort: Balanced

@adaptcom adaptcom Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confidence Score: 4/5

Summary

Caches orphan FTS misses for 24h and judges embedding freshness in bulk with one metadata query per page. Logic verified equivalent locally; suite green except environment-only failures. Only convention nits remain.

Important Files Changed

File Overview
src/kindex/daemon.py Hygiene persists missed orphans in meta; skips FTS until title change or 24h
src/kindex/vectors.py Bulk freshness via one meta query per page; sqlite-vec loaded once per connection
tests/test_daemon.py Skip, retry-window, de-orphan forgetting and corrupt-record tests
tests/test_trust_lookups_and_maintenance.py Paging test now patches _fresh_node_ids
tests/test_vectors.py Bulk selection equals per-node predicate across seven freshness cases

Findings

  • Add a CHANGELOG.md [Unreleased] entry: cron no longer repeats these passes and orphan auto-linking can now lag up to 24h.
  • On the 24h question: keep it; a graph-wide invalidation key (edge or node count) changes on every pass of an active graph and would restore the full cost.

Reviewed 528566c...9ef9a21 · ↻ Re-run review · View in Adapt

Comment thread src/kindex/daemon.py Resolved
Comment thread src/kindex/vectors.py Resolved
The miss record is now a versioned envelope of _HygieneMiss models, like
the embedding quarantine, and an entry that fails validation is dropped
so that orphan is searched again. embedding_health uses _load_sqlite_vec
instead of its own copy. CHANGELOG notes the change and the up-to-a-day
lag it allows for auto-linking.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01USKFuLCYNe92BkyMEvDqcz
@MrJoy

MrJoy commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

CHANGELOG entry added in b533a82, including the up-to-a-day lag for auto-linking. Keeping the 24h window, for the reason you gave: a graph-wide key changes on every pass of an active graph and would put the full cost back.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants