Skip to content

Under Postgres, knowledge.inspect counts the ingestion jobs ClickHouse holds at an older version - #466

Merged
zlareb1 merged 1 commit into
mainfrom
feat/knowledge-inspect-pg-jobs
Nov 10, 2026
Merged

zlareb1 merged 1 commit into
mainfrom
feat/knowledge-inspect-pg-jobs

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

Related: #160

Changelog category (leave one):

  • Improvement

Changelog entry (one or two sentences a user of Loom can read without the diff; the release pull request collects these into the release notes):

Under the Postgres authority, knowledge.inspect says how many ingestion jobs ClickHouse holds at an older version than Postgres, so the job counts and listed jobs are not read as current when they lag.

Why

Under the Postgres authority, ingestion jobs live in loom.knowledge_jobs. loom.knowledge_ingestions in ClickHouse holds a best-effort copy of each saved version, and the next save, or the next offer of an ended job, repairs a copy that did not land. knowledge.inspect's ingestion sections (the counts by state, the sources without a document, each listed document's jobs) read that copy. Until now nothing told the reader that a job might be at an older state, for example a job shown pending that Postgres already holds as failed. The document sections already say what ClickHouse lacks (documents.projection, listed.heads), and the job sections now do the same.

Scope and risk

  • ingest_jobs.unprojected reads from Postgres (pg_jobs.versions) the versions of up to 1,000 of the namespace's open jobs and its 1,000 most recently saved non-pending ones (a processing job can come from both and counts once). Each statement takes the predicate and order of the partial index Under Postgres, knowledge ingestion jobs live in loom.knowledge_jobs, each save fenced by its run's token #447 made for it (knowledge_jobs_open, knowledge_jobs_served), so its LIMIT is an index scan within the namespace, and asks one row more, to learn whether there are more. It then reads ClickHouse's versions of those ids (ingestions.versions: FINAL, ingestion_id IN {ids:Array(UUID)}, in the chunks ch_params.chunks cuts to fit one form field, so 2,000 ids go in two statements) and counts the jobs ClickHouse holds older or not at all. The ids are sorted in the order ClickHouse keeps a UUID key (its last 8 bytes, then its first 8), so each chunk reads one stretch of the namespace's granules. The read uses the same default consistency as the job sections it checks, not select_sequential_consistency. On a single server or a replicated cluster, where each Loom process reads one replica, the later sections never trail it, so a lag can only overcount, which "not ... yet" allows. Where reads may reach another replica, a section can still show a job this read found current. When neither read finds more than its page, every live job is compared. When either does, the bounded line says the comparison stopped short.
  • Postgres cost, measured with EXPLAIN (ANALYZE, BUFFERS) on a local Postgres 16 holding a 200,000-job namespace, a 20,000-job namespace saved 25 days ago, and 500 small ones. At the shipped page (LIMIT 1001), a single ORDER BY updated_at over all live jobs, which no full index serves, walked the deployment-wide updated_at index for the 20,000-job namespace: 200,021 rows removed by filter, 7,245 buffers, 35 ms. The newest-saved read on knowledge_jobs_served took 71 buffers (0.4 ms) there. The generic plans Postgres can switch to for asyncpg's prepared statements (plan_cache_mode = force_generic_plan, PREPARE ... EXPLAIN (ANALYZE, BUFFERS) EXECUTE) stay on the same indexes: 258 to 275 buffers for the open read and 54 to 71 for the saved read, each under 1 ms. The open-jobs read in the index's order used knowledge_jobs_open in every shape measured, including 1,001 open jobs at the end of a 306,000-row table behind 50 other namespaces (1,025 buffers, 3.8 ms); the newest-saved read for one of those busy namespaces took 1,006 buffers (2.6 ms).
  • ClickHouse cost, from system.query_log on a local ClickHouse 26.10:
    • FINAL against max(version) ... GROUP BY ingestion_id, measured with 100-id batches over 2,000 jobs at two versions in one namespace beside 200,000 jobs in 100 others: both read 42,048 rows (1.04 MiB), and FINAL took 113 ms in total against 143 ms, so the FINAL read stays.
    • 2,000 ids of a 200,000-job namespace, in the shipped two chunks sorted in key order: 201,952 rows (5 MiB), about the namespace once, 11 ms per statement warm (about 187 ms on a process's first run), 6.9 MB peak memory. For comparison, in 20 batches of 100 ids the same read took 1,617,920 rows in random order and 236,928 rows sorted.
  • knowledge.inspect adds ingestions.projection: <n> ingestion job(s) whose latest state ClickHouse does not hold yet; ... to skipped when any are behind. When either read finds more than its page, it adds a second line saying the comparison was bounded. A comparison that fails adds ingestions.projection: <reason>: the jobs were not compared ..., with POSTGRES_UNAVAILABLE or the exception's name as the reason. A Postgres that does not answer goes through the report's existing single outage line, so the later listing check and the memory wait do not run against it. When Postgres was already down before the comparison, the same not-compared line names it under the section's own name. The outage line itself keeps naming the document and memory sections only: the not-compared line covers the ingestion sections whenever their jobs went uncompared. The skipped field's description (its first sentence, which the operations reference shows) now names the sections read in part, and under Postgres what a read section may miss, and docs/reference/operations/knowledge.md is regenerated. The features page links the two storage-authority sections that name those lines.
  • The counts themselves still come from ClickHouse. A job ClickHouse holds newer than Postgres (written by an older build) is not counted. A job whose copy is still in flight right after its commit may be counted for that moment, which the wording ("not ... yet") allows.
  • Dashboard: for any ingestions.projection line but the bounded one (so not for an outage found later by the listing check), the ingestion section says "Some jobs may be missing or show an older state" when the job counts were read, and the listing says "Some listed jobs may be missing or show an older state" when the listed jobs were read. The notes are shown in addition to the Skipped list, which still prints every line.
  • Under the ClickHouse authority nothing changes: no read is made.

Compatibility, rollout and rollback

Tests

  • Unit (tests/unit/test_knowledge_inspect.py, 73 passed; with doc links and public copy, 289): counted jobs and the dashboard's match; the bounded-page line; a failed comparison with Postgres down (one outage line, no later Postgres read) and with another error; the down path skips the comparison; unprojected counts older and missing copies; the two Postgres statements (each index's predicate, the live filter, newest first for the saved read, one row past the page, a job found by both counted once at the later read's version); the ClickHouse read's statement, its key order and its chunks within one form field; the key order, its tie-break included, against ids as ClickHouse returned them and against clickhouse-local's ORDER BY over 300 random UUIDs plus three that share their last 8 bytes; the dashboard's prefixes against the server's names (the UUID binding against a real server is covered by the integration test); the not-compared line when Postgres is down before or at the comparison or the comparison fails otherwise, and none when the outage comes after it; an unstorable namespace key makes no Postgres read; a ClickHouse outage at the comparison fails the call. A gate bundle of analyzer corpus, package contracts, statement digests, knowledge ingest and delivery, pg authority and agent instructions: 937 passed, 2 skipped.
  • Integration on a local Postgres-authority server: a job saved in Postgres whose projection is made to fail is counted in knowledge.inspect's skipped, while ClickHouse still shows it pending. test_knowledge_pg_heads.py and test_knowledge_pg_jobs.py: 50 passed. -k "knowledge or inspect" quick pass: 132 passed, 29 skipped.
  • Frontend: Assistant.test.tsx 98 passed (the notes show for a count line or a not-compared line, not for the bounded line or a later outage, and only where the counts or listed jobs were read); build and bundle budget OK. make format, lint and typecheck clean.

How the measurements were made

Postgres (a local postgres:16 primary with Loom's migrations), rows inserted directly:

INSERT INTO loom.knowledge_jobs (org, namespace, ingestion_id, source_uri, state, next_attempt_at, created_at, updated_at, user_id, version, row)
SELECT 'benchorg', 'big', gen_random_uuid()::text, 'repo://f' || g, (ARRAY['pending','processing','ready','failed','ready','ready'])[1 + g % 6],
  now(), now() - (g || ' seconds')::interval, now() - ((g % 2000000) || ' seconds')::interval, '-', 1, '{}'::jsonb
FROM generate_series(1, 200000) g;
-- 'mid': 20,000 jobs saved 25 days ago; 'small0'..'small499': 5,000 jobs; and for the
-- heap-end case 300,000 older jobs across 'old0'..'old49' then 6,000 jobs in 'fresh'.
ANALYZE loom.knowledge_jobs;
EXPLAIN (ANALYZE, BUFFERS) SELECT ingestion_id, version FROM loom.knowledge_jobs
WHERE org = 'benchorg' AND namespace = 'mid' AND state <> 'pending'
  AND updated_at >= now() - make_interval(days => 30) ORDER BY updated_at DESC LIMIT 1001;
EXPLAIN (ANALYZE, BUFFERS) SELECT ingestion_id, version FROM loom.knowledge_jobs
WHERE org = 'benchorg' AND namespace = 'old7' AND state IN ('pending', 'processing')
  AND updated_at >= now() - make_interval(days => 30)
ORDER BY source_uri, created_at, ingestion_id LIMIT 1001;

ClickHouse (a local server with Loom's migrations): 2,000 jobs at version 1 and again at version 2 in namespace busy, plus 200,000 jobs in 100 other namespaces. Then ingestions.versions("benchorg", "busy", ids) was called under set_query_tag("measure-final"), followed by SYSTEM FLUSH LOGS and:

SELECT log_comment, count(), sum(read_rows), formatReadableSize(sum(read_bytes)), max(memory_usage),
       sum(query_duration_ms), max(query_duration_ms)
FROM system.query_log WHERE type = 'QueryFinish' AND log_comment LIKE '%measure-%' GROUP BY log_comment;

AI assistance in the code (leave one; writing this description with AI is fine):

  • AI-generated (an agent wrote most of the diff): Claude Code. The design follows the document sections' existing projection lines; checked against the job store's projection and repair rules.

  • I can explain every line of this diff and how it interacts with the rest of Loom.

Checklist

  • make format-check, make lint, make typecheck and make test-unit pass; the integration suite passes if server behaviour changed.
  • Generated files are regenerated in this PR, not by hand (docs/reference/operations/knowledge.md).
  • A new migration is additive and numbered after the latest on main (none).
  • No plugin file changed.
  • The frontend builds and its tests pass.
  • Docs under docs/ describe the system as it now is.
  • Repository text has no dates, people, host names, internal namespaces, ticket or decision numbers.
  • Commits are signed off.

🤖 Generated with Claude Code

@zlareb1
zlareb1 force-pushed the feat/knowledge-inspect-pg-jobs branch from ce5158c to d44cf76 Compare November 10, 2026 07:17
…e holds at an older version

knowledge.inspect's ingestion sections and listed jobs read loom.knowledge_ingestions,
the best-effort projection of loom.knowledge_jobs. The report now compares the
versions of up to 1,000 of the namespace's open jobs and its 1,000 most recently saved
non-pending ones (each read on its partial index) with ClickHouse's, and names in
`skipped` the jobs ClickHouse holds older or not at all (`ingestions.projection: ...`),
a bounded line when a read finds more than its page, and a not-compared line when the
comparison failed. The dashboard notes the count and not-compared lines.

Refs #160

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
@zlareb1
zlareb1 force-pushed the feat/knowledge-inspect-pg-jobs branch from d44cf76 to 0eb860f Compare November 10, 2026 07:47
@zlareb1
zlareb1 merged commit 6f3ef6f into main Nov 10, 2026
35 of 38 checks passed
@zlareb1
zlareb1 deleted the feat/knowledge-inspect-pg-jobs branch November 10, 2026 08:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant