Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: replay Catalog commit receipts before embedding

[._.]/ Adam Eivy5bf13576bcd2
diff --git a/server/routes/catalog.js b/server/routes/catalog.js
index 98fc0ffec2..d075d88e0a 100644
--- a/server/routes/catalog.js
+++ b/server/routes/catalog.js
@@ -166,6 +166,11 @@ router.post('/scraps/:id/commit', asyncHandler(async (req, res) => {
const scrap = await catalogDB.getScrap(req.params.id);
if (!scrap) throw new ServerError('Scrap not found', { status: 404 });
+ if (body.operationKey) {
+ const replay = await catalogDB.getScrapCommitReceipt({ ...body, scrapId: scrap.id });
+ if (replay) return res.status(201).json({ scrap, ingredients: replay });
+ }
+
// Embed all drafts in parallel (concurrency-4 inside embedBatch) before
// sequentially writing — LLM round-trips dominate, DB inserts don't. Embeds
// stay OUTSIDE the transaction: they're network round-trips to the provider,
@@ -176,7 +181,7 @@ router.post('/scraps/:id/commit', asyncHandler(async (req, res) => {
const created = await catalogDB.commitScrap({
scrapId: scrap.id,
- operationKey: body.operationKey,
+ ...(body.operationKey ? { operationKey: body.operationKey } : {}),
accepted: body.accepted,
...(body.relationships !== undefined ? { relationships: body.relationships } : {}),
embeds,
diff --git a/server/routes/catalog.test.js b/server/routes/catalog.test.js
index 3d1f630b1a..1ae3aa2977 100644
--- a/server/routes/catalog.test.js
+++ b/server/routes/catalog.test.js
@@ -505,6 +505,8 @@ describe.skipIf(!runDb)('POST /api/catalog/scraps/:id/commit — universe bindin
});
const restartedResult = JSON.parse(stdout.split('\n').find(line => line.startsWith('RECEIPT:')).slice(8));
expect(restartedResult).toEqual(first.body.ingredients);
+ const { embedBatch } = await import('../services/embeddings.js');
+ const embedCalls = embedBatch.mock.calls.length;
const replay = await post({ ...body, accepted: [
{ ...body.accepted[0], payload: { b: 2, a: 1 } }, body.accepted[1],
] });
@@ -513,6 +515,7 @@ describe.skipIf(!runDb)('POST /api/catalog/scraps/:id/commit — universe bindin
expect(await counts()).toEqual(originalCounts);
expect((await post({ ...body, accepted: [{ type: 'idea', name: 'Changed' }] })).status).toBe(409);
expect(await counts()).toEqual(originalCounts);
+ expect(embedBatch.mock.calls.length).toBe(embedCalls);
const nextKey = randomUUID();
receiptKeys.add(nextKey);
const intentional = await post({ ...body, operationKey: nextKey });
diff --git a/server/services/catalogDB/commit.js b/server/services/catalogDB/commit.js
index 4705b9692f..7f4ff68359 100644
--- a/server/services/catalogDB/commit.js
+++ b/server/services/catalogDB/commit.js
@@ -9,7 +9,7 @@ import { createHash } from 'node:crypto';
import { canonicalStringify } from '../../lib/objects.js';
import { ServerError } from '../../lib/errorHandler.js';
import { catalogScrapCommitSchema } from '../../lib/catalogValidation.js';
-import { withTransaction } from '../../lib/db.js';
+import { query, withTransaction } from '../../lib/db.js';
import { createIngredient } from './ingredients.js';
import { linkIngredientToSource, linkIngredientToRef, linkIngredientRelation, universeRefRoleForType } from './refs.js';
@@ -19,6 +19,32 @@ import { linkIngredientToSource, linkIngredientToRef, linkIngredientRelation, un
// 19,900 edges for every unordered pair at the cap.
const RELATION_BATCH_LIMIT = 25;
+function commitFingerprint({ scrapId, accepted = [], universeRef = null, role = null, relationships }) {
+ if (relationships !== undefined) {
+ ({ accepted, relationships } = catalogScrapCommitSchema.parse({ accepted, relationships }));
+ }
+ // Derived embedding output never changes a reviewed submission's identity.
+ return createHash('sha256').update(canonicalStringify({ scrapId, accepted, universeRef, role, relationships })).digest('hex');
+}
+
+async function readReceipt(exec, operationKey, fingerprint) {
+ const { rows: [receipt] } = await exec(
+ 'SELECT fingerprint, ingredients FROM catalog_commit_receipts WHERE operation_key = $1', [operationKey],
+ );
+ if (!receipt) return null;
+ if (receipt.fingerprint !== fingerprint) {
+ throw new ServerError('Commit operation key was already used for a different submission', { status: 409 });
+ }
+ return receipt.ingredients;
+}
+
+// Fast replay before provider work. The transactional claim below remains the
+// authority when two requests race before either has a committed receipt.
+export async function getScrapCommitReceipt(input) {
+ if (!input.operationKey) return null;
+ return readReceipt(query, input.operationKey, commitFingerprint(input));
+}
+
/**
* Persist every accepted extraction draft and its source link atomically.
* Embeddings are prepared by the caller before this DB-only transaction starts.
@@ -33,9 +59,8 @@ export async function commitScrap({ scrapId, accepted = [], embeds = [], univers
if (relationships !== undefined) {
({ accepted, relationships } = catalogScrapCommitSchema.parse({ accepted, relationships }));
}
- // Embeddings are derived provider output, not reviewed submission identity.
const fingerprint = operationKey
- ? createHash('sha256').update(canonicalStringify({ scrapId, accepted, universeRef, role, relationships })).digest('hex')
+ ? commitFingerprint({ scrapId, accepted, universeRef, role, relationships })
: null;
return withTransaction(async (client) => {
if (operationKey) {
@@ -47,13 +72,7 @@ export async function commitScrap({ scrapId, accepted = [], embeds = [], univers
[operationKey, fingerprint],
);
if (!claim.rowCount) {
- const { rows: [receipt] } = await client.query(
- 'SELECT fingerprint, ingredients FROM catalog_commit_receipts WHERE operation_key = $1', [operationKey],
- );
- if (receipt.fingerprint !== fingerprint) {
- throw new ServerError('Commit operation key was already used for a different submission', { status: 409 });
- }
- return receipt.ingredients;
+ return readReceipt(client.query.bind(client), operationKey, fingerprint);
}
}
const created = [];