Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

chunks: deleting a page's chunks clears its text projection seal

garrytan0f185e85db06
diff --git a/src/core/engine-sql/chunks.ts b/src/core/engine-sql/chunks.ts
index 10ed413d1a..746ded31b9 100644
--- a/src/core/engine-sql/chunks.ts
+++ b/src/core/engine-sql/chunks.ts
@@ -127,7 +127,8 @@ export async function upsertChunksOnce(
if ((await exec.unsafe(invalidation.sql, invalidation.params)).rows.length > 0) break;
}
if (chunks.length === 0) {
- await exec.run(sqlFragment`DELETE FROM content_chunks WHERE page_id = ${pageId}`);
+ await exec.run(sqlFragment`WITH gone AS (DELETE FROM content_chunks WHERE page_id = ${pageId} RETURNING page_id)
+ UPDATE pages SET text_projection_revision = NULL WHERE id = (SELECT page_id FROM gone LIMIT 1) AND text_projection_revision IS NOT NULL`);
return;
}
} else if (chunks.length === 0) return;
@@ -804,7 +805,8 @@ export async function listStaleChunks(exec: ScopedRead, column: string, opts?: {
* which are chunkless by design, and pages whose current revision's text
* projection is sealed (#8473): the writer chunked that revision to nothing,
* as for a body holding only a protected takes fence, so a heal cannot
- * produce a chunk. An embed_skip page is not excluded: every write keeps
+ * produce a chunk. Deleting a page's chunks clears its seal (deleteChunks, an
+ * empty fragment replacement), so a page emptied after sealing stays healable. An embed_skip page is not excluded: every write keeps
* its chunks for keyword search (#6722), so one with none (an older gbrain's
* size skip, or a backfill rewrite that never landed) is drift the safety net
* chunks; its chunks get no vectors (#7174).
@@ -1034,9 +1036,16 @@ export async function listEmbeddingStateRows(exec: ScopedRead, column: string,
export async function deleteChunks(exec: SqlExecutor, slug: string, opts?: { sourceId?: string }): Promise<void> {
const sourceId = opts?.sourceId ?? 'default';
// Source-qualify the page-id subquery; slugs are only unique per source.
+ // Removing chunks voids the text projection seal that certified them; a
+ // writer that reinstalls the projection seals it again (#8502).
await exec.run(sqlFragment`
- DELETE FROM content_chunks
- WHERE page_id = (SELECT id FROM pages WHERE slug = ${slug} AND source_id = ${sourceId})
+ WITH gone AS (
+ DELETE FROM content_chunks
+ WHERE page_id = (SELECT id FROM pages WHERE slug = ${slug} AND source_id = ${sourceId})
+ RETURNING page_id
+ )
+ UPDATE pages SET text_projection_revision = NULL
+ WHERE id = (SELECT page_id FROM gone LIMIT 1) AND text_projection_revision IS NOT NULL
`);
}
diff --git a/test/embedding-state-projection-empty-8473.test.ts b/test/embedding-state-projection-empty-8473.test.ts
index dd3681bb6d..eed7044edd 100644
--- a/test/embedding-state-projection-empty-8473.test.ts
+++ b/test/embedding-state-projection-empty-8473.test.ts
@@ -6,7 +6,8 @@
* chunkless_healable and `embed --stale` planned a heal that can never
* produce a chunk. A sealed revision with no chunks is empty; an unsealed
* chunkless page and a sealed embed_skip page with no chunks (#7174) stay
- * healable.
+ * healable. Deleting a page's chunks (deleteChunks, or a fragment replacement
+ * with no chunks) voids the seal, so a page emptied that way stays healable.
*/
import { afterAll, beforeAll, beforeEach, expect, test } from 'bun:test';
import { PGLiteEngine } from '../src/core/pglite-engine.ts';
@@ -36,14 +37,35 @@ test('a page holding only a takes fence is empty, not healable', async () => {
expect(await engine.listChunklessPagesWithContent()).toEqual([]);
});
-test('a chunkless page whose current revision was never projected stays healable', async () => {
- await importFromContent(engine, 'notes/prose', '---\ntitle: Prose\ntype: note\n---\nA paragraph about acme-example.\n', { noEmbed: true });
- await engine.executeRaw("DELETE FROM content_chunks WHERE page_id = (SELECT id FROM pages WHERE slug = 'notes/prose')");
- await engine.executeRaw("UPDATE pages SET text_projection_revision = NULL WHERE slug = 'notes/prose'");
+const PROSE = '---\ntitle: Prose\ntype: note\n---\nA paragraph about acme-example.\n';
+const sealed = async (slug: string) => (await engine.executeRaw<{ sealed: boolean }>(
+ 'SELECT (text_projection_revision = knowledge_revision) IS TRUE AS sealed FROM pages WHERE slug = $1', [slug]))[0]?.sealed;
+
+test('a sealed page whose chunks deleteChunks removed stays healable', async () => {
+ const written = await importFromContent(engine, 'notes/prose', PROSE, { noEmbed: true });
+ expect(written.chunks).toBeGreaterThan(0);
+ expect(await sealed('notes/prose')).toBe(true);
+ await engine.deleteChunks('notes/prose');
+ expect(await sealed('notes/prose')).toBe(false);
+ expect(await state()).toMatchObject({ chunkless_healable: 1, empty: 0 });
+ expect(await engine.countChunklessPagesWithContent()).toBe(1);
+});
+
+test('a sealed page emptied by a fragment chunk replacement stays healable', async () => {
+ await importFromContent(engine, 'notes/prose', PROSE, { noEmbed: true });
+ await engine.upsertChunks('notes/prose', []);
+ expect(await sealed('notes/prose')).toBe(false);
expect(await state()).toMatchObject({ chunkless_healable: 1, empty: 0 });
expect(await engine.countChunklessPagesWithContent()).toBe(1);
});
+test('deleteChunks on a page with no chunks keeps its empty seal', async () => {
+ await importFromContent(engine, 'notes/takes-only', `---\ntitle: Takes only\ntype: note\n---\n${TAKES}\n`, { noEmbed: true });
+ await engine.deleteChunks('notes/takes-only');
+ expect(await sealed('notes/takes-only')).toBe(true);
+ expect(await state()).toMatchObject({ chunkless_healable: 0, empty: 1 });
+});
+
test('a sealed embed_skip page with no chunks stays healable (#7174)', async () => {
await importFromContent(engine, 'notes/skipped', '---\ntitle: Skipped\ntype: note\n---\nA paragraph about acme-example.\n', { noEmbed: true });
await engine.executeRaw("DELETE FROM content_chunks WHERE page_id = (SELECT id FROM pages WHERE slug = 'notes/skipped')");
diff --git a/test/fixtures/goldens/sql-text/chunks.json b/test/fixtures/goldens/sql-text/chunks.json
index 32619c09b7..a6686a7835 100644
--- a/test/fixtures/goldens/sql-text/chunks.json
+++ b/test/fixtures/goldens/sql-text/chunks.json
@@ -79,8 +79,8 @@
"string",
"string"
],
- "sql": "DELETE FROM content_chunks WHERE page_id = (SELECT id FROM pages WHERE slug = $1 AND source_id = $2)",
- "textSha256": "68789189f60510eba7195b82bea7d612a9e2f10c6799178a8d605ea0901482d4"
+ "sql": "WITH gone AS ( DELETE FROM content_chunks WHERE page_id = (SELECT id FROM pages WHERE slug = $1 AND source_id = $2) RETURNING page_id ) UPDATE pages SET text_projection_revision = NULL WHERE id = (SELECT page_id FROM gone LIMIT 1) AND text_projection_revision IS NOT NULL",
+ "textSha256": "7a650092284476e1dbedca3f5bec457882002e4444e5dadf9a37eddbee8d1c65"
}
]
},
@@ -1193,8 +1193,8 @@
"params": [
"number"
],
- "sql": "DELETE FROM content_chunks WHERE page_id = $1",
- "textSha256": "c1aa06e79b30c5c75dcef60a4e4560f6eb57e61586dc7c6a21036577d8dccb9d"
+ "sql": "WITH gone AS (DELETE FROM content_chunks WHERE page_id = $1 RETURNING page_id) UPDATE pages SET text_projection_revision = NULL WHERE id = (SELECT page_id FROM gone LIMIT 1) AND text_projection_revision IS NOT NULL",
+ "textSha256": "83f4e3a978fada158c958a09cfb84641ded612cabd6bf6cc023415842d9de7d0"
},
{
"event": "commit",