Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
test(large-pages): seed the drift tests' huge page through the owner steps
garrytanefae60cf2a82
diff --git a/test/helpers/owner-import.ts b/test/helpers/owner-import.tsnew file mode 100644index 0000000000..fe20b6e96d--- /dev/null+++ b/test/helpers/owner-import.ts@@ -0,0 +1,37 @@+/**+ * Seeds a managed file import through the owner's own steps (admit, claim, prepare, publish, project), the way+ * managed-streamed-import.test.ts drives the owner, for tests whose subject is a page over the single-string ceiling+ * rather than the import's reply. The 30 s reply wait of importManagedFile does not apply: on a loaded host a 25 MiB+ * page's projection can outlast it, and the reply is then rightly write_pending. Stop the source's consumer first,+ * so the claim is this request's.+ */+import { readFileSync } from 'node:fs';+import type { BrainEngine } from '../../src/core/engine.ts';+import type { OperationContext } from '../../src/core/ops/contract.ts';+import { buildSegmentedProjection } from '../../src/core/page-body/projection.ts';+import { submissionAuthority } from '../../src/core/persistence/authority.ts';+import { publishMutation } from '../../src/core/persistence/coordinator.ts';+import { sha256 } from '../../src/core/persistence/digest.ts';+import { localHostId } from '../../src/core/persistence/identity.ts';+import { prepareManagedImportMutation, type ManagedImportIntent } from '../../src/core/persistence/import-prepare.ts';+import { admitWrite, claimNextWrite } from '../../src/core/persistence/journal.ts';+import { getWorktreeBinding } from '../../src/core/persistence/ownership.ts';++/** Imports `file`, already at `sourcePath` in the source's canonical root, by reference; resolves once the page is sealed. */+export async function ownerImportFile(engine: BrainEngine, sourceId: string, file: string, sourcePath: string): Promise<void> {+ const binding = (await getWorktreeBinding(engine, sourceId))!;+ const slug = sourcePath.replace(/\.md$/, '');+ const bytes = readFileSync(file);+ const hash = sha256(bytes);+ const authority = await submissionAuthority({ engine, remote: false, sourceId } as OperationContext, 'put_page', sourceId, binding.source_incarnation, slug);+ const intent: ManagedImportIntent = { kind: 'managed_file_import', slug, sourcePath, path: sourcePath, inputPath: file, inputHash: hash, targetHash: hash,+ body_ref: { kind: 'input_file', input_path: file, sha256: hash, bytes: bytes.length }, ownerEpoch: String(binding.owner_epoch), noEmbed: true };+ await admitWrite(engine, { principal: authority.principal, operation: 'put_page', sourceId, sourceIncarnation: binding.source_incarnation,+ slug, pageId: null, requestId: crypto.randomUUID(), callerIntent: intent, intent, authority, worktreeId: binding.worktree_id, topologyGeneration: binding.topology_generation });+ const row = await claimNextWrite(engine, localHostId());+ if (row?.slug !== slug) throw new Error(`The owner claimed ${row?.slug ?? 'nothing'} instead of ${slug}.`);+ const done = await publishMutation(engine, row, await prepareManagedImportMutation(engine, row, { engine: engine.kind }), localHostId());+ if (done.state !== 'committed') throw new Error(`The import of ${sourcePath} ended ${done.state}: ${done.error_code ?? done.blocked_reason} ${done.error_message ?? ''}`);+ const projection = await buildSegmentedProjection(engine, { sourceId, slug });+ if (!projection.sealed) throw new Error(`The projection of ${slug} did not seal: ${JSON.stringify(projection)}`);+}diff --git a/test/large-pages/segmented-file-drift-ledger.test.ts b/test/large-pages/segmented-file-drift-ledger.test.tsindex 29b93324cb..f54f3e58c6 100644--- a/test/large-pages/segmented-file-drift-ledger.test.ts+++ b/test/large-pages/segmented-file-drift-ledger.test.ts@@ -16,6 +16,8 @@* 2. Fails when: the check refuses every fenced file whenever the source has* any withdrawal (page_body_segmented), or the file overlay differs from the* stored overlay.+ * The huge page is seeded through the owner's own steps (helpers/owner-import.ts), sealed before the test goes on: the+ * subject is the drift check, and on a loaded host the projection of a 24.5 MiB page can outlast importManagedFile's wait.* Runs on PGLite, and on Postgres when DATABASE_URL is set.*/import { afterAll, beforeAll, expect, test } from 'bun:test';@@ -41,6 +43,7 @@ import { disposePersistenceConsumer } from '../../src/core/persistence/service.timport { isolatedPersistencePostgres } from '../helpers/persistence-postgres.ts';import { withEnv } from '../helpers/with-env.ts';import { conversationPage } from '../helpers/conversation-page.ts';+import { ownerImportFile } from '../helpers/owner-import.ts';const home = mkdtempSync(join(tmpdir(), 'gbrain-segmented-file-drift-ledger-'));const engines: BrainEngine[] = [];@@ -84,7 +87,8 @@ async function managedSource(engine: BrainEngine): Promise<Fixture> {async function importHuge(engine: BrainEngine, f: Fixture, slug: string): Promise<string> {const file = join(f.root, `${slug}.md`);writeFileSync(file, huge);- expect(await importManagedFile(engine, file, `${slug}.md`, { sourceId: f.sourceId, noEmbed: true })).toMatchObject({ status: 'imported' });+ await disposePersistenceConsumer(engine);+ await ownerImportFile(engine, f.sourceId, file, `${slug}.md`);const [row] = await engine.executeRaw<{ body_storage: string; body_bytes: string }>('SELECT body_storage, body_bytes::text AS body_bytes FROM pages WHERE source_id=$1 AND slug=$2', [f.sourceId, slug]);expect(row?.body_storage).toBe('segmented');expect(Number(row?.body_bytes)).toBeGreaterThan(PAGE_MAX_BYTES);diff --git a/test/large-pages/segmented-file-drift.test.ts b/test/large-pages/segmented-file-drift.test.tsindex 5f0bfbd7f4..be0cadf3a5 100644--- a/test/large-pages/segmented-file-drift.test.ts+++ b/test/large-pages/segmented-file-drift.test.ts@@ -17,6 +17,8 @@* 3. Existing coverage: page-purge-segments.test.ts purges on a source with* no worktree, so no file is compared; no test restores such a page.* 4. No production seam: bodyDigestWork is the module's own work counter.+ * The huge page is seeded through the owner's own steps (helpers/owner-import.ts), sealed before the test goes on: the+ * subject is the drift check, and on a loaded host the projection of a 24.5 MiB page can outlast importManagedFile's wait.* Runs on PGLite, and on Postgres when DATABASE_URL is set.*/import { afterAll, beforeAll, expect, test } from 'bun:test';@@ -42,6 +44,7 @@ import { withEnv } from '../helpers/with-env.ts';import { withCoordinatedWrite } from '../../src/core/persistence/context.ts';import { TEST_WRITE_ATTRIBUTION } from '../helpers/write-attribution.ts';import { conversationPage } from '../helpers/conversation-page.ts';+import { ownerImportFile } from '../helpers/owner-import.ts';const home = mkdtempSync(join(tmpdir(), 'gbrain-segmented-file-drift-'));const engines: BrainEngine[] = [];@@ -85,7 +88,8 @@ async function managedSource(engine: BrainEngine): Promise<Fixture> {async function importHuge(engine: BrainEngine, f: Fixture, slug: string): Promise<string> {const file = join(f.root, `${slug}.md`);writeFileSync(file, huge);- expect(await importManagedFile(engine, file, `${slug}.md`, { sourceId: f.sourceId, noEmbed: true })).toMatchObject({ status: 'imported' });+ await disposePersistenceConsumer(engine);+ await ownerImportFile(engine, f.sourceId, file, `${slug}.md`);const [row] = await engine.executeRaw<{ body_storage: string; body_bytes: string }>('SELECT body_storage, body_bytes::text AS body_bytes FROM pages WHERE source_id=$1 AND slug=$2', [f.sourceId, slug]);expect(row?.body_storage).toBe('segmented');expect(Number(row?.body_bytes)).toBeGreaterThan(PAGE_MAX_BYTES);