Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/core/facts/derivation-inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,27 @@ function refKey(ref: DerivationRef): { table: string; id: string } {
/**
* Record every input of one derived row. Idempotent; call it inside the
* transaction that writes `row`. `sourceId` scopes the edges for cleanup when
* a source is purged.
* a source is purged. `replace`: the inputs are the row's complete current set
* (a re-publication), so edges to inputs no longer in its context are dropped.
*/
export async function recordDerivationInputs(tx: BrainEngine, row: DerivationRef & { sourceId?: string | null },
export async function recordDerivationInputs(tx: BrainEngine, row: DerivationRef & { sourceId?: string | null; replace?: boolean },
inputs: readonly DerivationRef[]): Promise<void> {
if (!inputs.length) return;
const derived = refKey(row);
if (!inputs.length) {
if (row.replace) await tx.executeRaw('DELETE FROM derivation_inputs WHERE derived_table=$1 AND derived_id=$2', [derived.table, derived.id]);
return;
}
if (inputs.length > DERIVATION_EDGE_LIMIT) throw new RangeError(`A derivation records at most ${DERIVATION_EDGE_LIMIT} inputs.`);
const edges = inputs.map(refKey);
const edgeJson = JSON.stringify(edges.map(e => ({ input_table: e.table, input_id: e.id })));
if (row.replace) {
await tx.executeRaw(`DELETE FROM derivation_inputs WHERE derived_table=$1 AND derived_id=$2 AND NOT EXISTS (
SELECT 1 FROM jsonb_to_recordset($3::text::jsonb) AS e(input_table text,input_id text)
WHERE e.input_table=derivation_inputs.input_table AND e.input_id=derivation_inputs.input_id)`, [derived.table, derived.id, edgeJson]);
}
await tx.executeRaw(`INSERT INTO derivation_inputs(derived_table,derived_id,input_table,input_id,source_id)
SELECT $1,$2,e.input_table,e.input_id,$3 FROM jsonb_to_recordset($4::text::jsonb) AS e(input_table text,input_id text)
ON CONFLICT DO NOTHING`, [derived.table, derived.id, row.sourceId ?? null, JSON.stringify(edges.map(e => ({ input_table: e.table, input_id: e.id })))]);
ON CONFLICT DO NOTHING`, [derived.table, derived.id, row.sourceId ?? null, edgeJson]);
}

export interface DerivedRow { table: string; id: string; depth: number }
Expand Down
2 changes: 1 addition & 1 deletion src/core/persistence/prepared-maintenance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ export async function prepareMaintenanceMutation(engine: BrainEngine, row: Write
.map(page => ({ table: 'pages' as const, id: Number(page.id) }));
if (derived) {
const lowered = await lowerToDerivedTier(tx, derived.table, [derived.id], trust);
if (declaration) await recordTaintEdges(tx, { ...derived, sourceId: row.source_id }, declaration.inputs);
if (declaration) await recordTaintEdges(tx, { ...derived, sourceId: row.source_id, replace: derived.table === 'pages' }, declaration.inputs);
// GST-2518: a publication whose labels were kept already filed its proposal with them; a second record would withdraw them.
if (lowered.length && prior?.tier && !outcome.privacy_labels_kept) await recordAgentPageLowering(tx, row, { tier: prior.tier, revision: prior.revision });
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/trust/taint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ export async function deriveTrust(engine: Sql, refs: readonly TaintRef[], input:
* transaction right after the row. Inputs read as `unknown` because they
* vanished keep their recorded reference.
*/
export async function recordTaintEdges(tx: BrainEngine, derived: { table: TaintTable; id: number; sourceId?: string | null },
export async function recordTaintEdges(tx: BrainEngine, derived: { table: TaintTable; id: number; sourceId?: string | null; replace?: boolean },
inputs: readonly Pick<TaintInput, 'table' | 'id'>[]): Promise<void> {
if (!inputs.length) return;
if (!inputs.length && !derived.replace) return;
await recordDerivationInputs(tx, derived, inputs.map(({ table, id }): DerivationRef => ({ table, id })));
}

Expand Down Expand Up @@ -176,7 +176,7 @@ export async function lowerDerivedPageIn(tx: BrainEngine, derivation: { trust: W
sourceId: string, slug: string): Promise<void> {
for (const row of await tx.executeRaw<{ id: number }>('SELECT id FROM pages WHERE source_id=$1 AND slug=$2 AND deleted_at IS NULL', [sourceId, slug])) {
await lowerToDerivedTier(tx, 'pages', [Number(row.id)], derivation.trust);
await recordTaintEdges(tx, { table: 'pages', id: Number(row.id), sourceId }, derivation.inputs);
await recordTaintEdges(tx, { table: 'pages', id: Number(row.id), sourceId, replace: true }, derivation.inputs);
}
}

Expand Down
23 changes: 23 additions & 0 deletions test/derivation-inputs-republish-9653.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { afterAll, beforeAll, expect, test } from 'bun:test';
import { PGLiteEngine } from '../src/core/pglite-engine.ts';
import { derivedRowsFrom } from '../src/core/facts/derivation-inputs.ts';
import { lowerDerivedPage } from '../src/core/trust/taint.ts';

let engine: PGLiteEngine;
beforeAll(async () => { engine = new PGLiteEngine(); await engine.connect({}); await engine.initSchema(); }, 60_000);
afterAll(async () => { await engine.disconnect(); });

test('a re-published page keeps only its current derivation inputs', async () => {
const put = (slug: string) => engine.putPage(slug, { type: 'note', title: slug, compiled_truth: `${slug} body text`, frontmatter: {} }, { sourceId: 'default' });
const concept = await put('concepts/c9653');
const oldAtom = await put('atoms/old-9653');
const newAtom = await put('atoms/new-9653');
const inputs = (...ids: number[]) => ({ trust: { tier: 'operator_curated' as const, origin: { channel: 'test' } },
inputs: ids.map(id => ({ table: 'pages' as const, id, tier: 'operator_curated' as const })) });
await lowerDerivedPage(engine, inputs(oldAtom.id), 'default', 'concepts/c9653');
await lowerDerivedPage(engine, inputs(newAtom.id), 'default', 'concepts/c9653');
const walked = await derivedRowsFrom(engine, [{ table: 'pages', id: oldAtom.id }]);
expect(walked.rows.map(r => r.id)).not.toContain(String(concept.id));
const kept = await derivedRowsFrom(engine, [{ table: 'pages', id: newAtom.id }]);
expect(kept.rows.map(r => r.id)).toContain(String(concept.id));
});
Loading