Skip to content

Bug: Concurrent knowledge-entry writes leave multiple current facts for one topic #3602

Closed

Description

@yannickmonney

Concurrent knowledge-entry writes leave multiple current facts for one topic

Severity: P2. Reproduced locally at 44e0dcf254975286c0e9dea443b5cd1ca6b23347.

Two colleagues correcting a fact together can create conflicting current answers and inconsistent version history/backing-document content. Importers making parallel same-topic writes also violate uniqueness.

Expected: One current entry per normalized topic and one current successor per revision chain. Concurrent stale edits should conflict or serialize into a coherent version history.

Actual: Ordinary transactions use unlocked reads; the topic index is not unique. Both creations succeed. Concurrent corrections leave two active successors with different content but the same document, so one current fact points to bytes for another.

Reproduction:

  1. Two editors simultaneously create the same normalized topic (e.g. Refund policy and REFUND POLICY) with different content. Both inner transaction lookups see no row and both inserts commit; there are two active entries and two backing documents.
  2. Alternatively start from one active Support hours entry. Two editors open that version and concurrently save different corrections. Both transactions read it active before either supersedes it; both create active successors, sharing one document. The document holds only one successor's content.
  3. Local probe synchronizes after both actual PostgreSQL SELECTs return, preserving real INSERT/UPDATE/commit behavior. Sequential duplicate creation returns 409; editing an already-superseded version returns 409.

Source:

Local proof:

Fresh 3/3 external Vitest tests passed (818ms), including two controlled real PostgreSQL interleavings. Create race yields two active rows with two document IDs; edit race yields two active rows sharing one document and only one matching blob. Unicode markdown create/list/blob-copy import roundtrip and blank/120/121/8000/8001 boundaries pass; sequential duplicate and superseded controls refuse 409.

it('simultaneous edits of one active version leave two active successors sharing one document', async () => {
  const initial = await createKnowledgeEntry(sql, { ...writer, topic: 'Support hours', content: '9–5' });
  const raced = interleaveTransactions((query) => query.includes('SELECT ke.id, ke.status, ke.superseded_by'));
  const results = await Promise.all([
    updateKnowledgeEntry(raced, { ...writer, entryId: initial.id, topic: 'Support hours', content: '9–6' }),
    updateKnowledgeEntry(raced, { ...writer, entryId: initial.id, topic: 'Support hours', content: '10–7' }),
  ]);
  const rows = await sql`SELECT id,content,document_id FROM app.knowledge_entries WHERE org_id=${org} AND topic_key='support hours' AND status='active'`;
  expect(rows).toHaveLength(2);
  expect(new Set(results.map((row) => row.documentId))).toEqual(new Set([initial.documentId]));
  const document = (await sql`SELECT file_ref FROM app.documents WHERE id=${initial.documentId}`)[0];
  expect(rows.filter((row) => row.content === blobs.get(document.file_ref))).toHaveLength(1);
  await expect(updateKnowledgeEntry(sql, { ...writer, entryId: initial.id, topic: 'Support hours', content: 'Later' })).rejects.toMatchObject({ code: 'KNOWLEDGE_ENTRY_SUPERSEDED', status: 409 });
});

Frozen, test-specific command:

PATH=/Users/yannick/.npm/_npx/22f2fe8d8bc13000/node_modules/.bin:$PATH bunx vitest run --config /Users/yannick/.codex/notes/tale-100-findings/knowledge-proof/vitest.config.ts entry-transactions.test.ts

Limits: Local service and real PostgreSQL fixture schema mirror relevant tables/indexes. Object storage is an in-memory PUT capture and indexing/release enqueue is mocked; no external fetch, embedding or browser was used. Proof does not claim observed downstream search output. One root cause combines create/update manifestations.

Dedup: Searched existing-issues.json (931 rows) for knowledge entry/topic duplicate/current-version terms; no equivalent issue. Related exclusions: #69 closed original knowledge-entry feature; #2056 closed duplicate-toast/error-envelope issue; #3061 closed duplicate search passages from a different cause.

Acceptance: serialize or otherwise arbitrate same-topic creation and stale-version edits. Concurrent create must leave one active row/document; concurrent stale edits must yield one coherent active successor, with the normal 409 conflict for the loser. Keep sequential idempotent updates, topic renames and history retention intact. Add real transaction regression coverage.

Activity

  1. added a commit that references this issue on Oct 4, 2026
    e6f7727
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugservice: platform

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions