export async function deleteDocumentChildren(attachmentId, session) {
    // 1. Drop Section children (any secondary label). Detach kills HAS_SECTION,
    //    NEXT, and any anchor edges (HAS_POSITION/ATTENDED/etc.) attached to
    //    them. earlier:Chunk overflow descendants drop with them.
    const sectionResult = await session.run(`MATCH (d:KnowledgeDocument { attachmentId: $attachmentId })-[:HAS_SECTION]->(s:Section)
     OPTIONAL MATCH (s)-[:HAS_CHUNK]->(c:Chunk)
     DETACH DELETE s, c
     RETURN count(DISTINCT s) AS sections, count(DISTINCT c) AS sectionChunks`, { attachmentId });
    const sections = sectionResult.records[0]?.get("sections")?.toNumber?.() ?? 0;
    const sectionChunks = sectionResult.records[0]?.get("sectionChunks")?.toNumber?.() ?? 0;
    // 2. Drop standalone nodes that originated from this document
    //    (currently: Project). Provenance-stamped, so the discriminator catches
    //    them while sparing MERGEd shared entities (Organization, Person).
    const standaloneResult = await session.run(`MATCH (n)
     WHERE n.sourceDocumentId = $attachmentId
       AND n.createdByAgent   = 'document-ingest'
       AND NOT n:Section
       AND NOT n:KnowledgeDocument
     OPTIONAL MATCH (n)-[:HAS_CHUNK]->(c:Chunk)
     DETACH DELETE n, c
     RETURN count(DISTINCT n) AS typed, count(DISTINCT c) AS typedChunks`, { attachmentId });
    const typed = standaloneResult.records[0]?.get("typed")?.toNumber?.() ?? 0;
    const typedChunks = standaloneResult.records[0]?.get("typedChunks")?.toNumber?.() ?? 0;
    // 3. Drop document-level edges off the KnowledgeDocument. Targets are MERGEd
    //    shared entities so they stay; only the edges go. Two predicates:
    //      (a) Legacy edge-type whitelist — PARTY only. earlier contracts
    //          may carry PARTY edges that predate provenance stamping; matching
    //          by name guarantees they drop on re-ingest. PARTY is single-use:
    //          only `writeDocumentEdge` writes it, so the whitelist cannot
    //          accidentally over-delete a third party's PARTY edge.
    //      (b) Provenance match — any edge stamped sourceDocumentId=<this attachment>.
    //          Catches classifier-emitted edges (PARTICIPANT/FROM/TO/CC/SPEAKER,
    //          stamped by writeDocumentEdge) agent-emitted brief-wired
    //          edges (MENTIONS/REFERENCES/etc., stamped by database-operator on
    //          each memory-write call), and any future stamped edge type without
    //          a code change here.
    //
    //    REFERENCES is deliberately NOT on the legacy whitelist (review):
    //    it became a live forward-going edge type for brief-wired Task/Event/
    //    Service/KnowledgeDocument cross-links, so a name-match would silently
    //    drop ad-hoc REFERENCES edges that other pipelines (e.g. operator-driven
    //    graph ops) wrote off this KD without a sourceDocumentId stamp. New
    //    REFERENCES writes from the document-ingest pipeline carry the stamp and
    //    are caught by predicate (b). earlier unstamped legacy REFERENCES
    //    edges persist on re-ingest — acceptable because Phase 0 has no
    //    production legacy state and the over-delete risk outweighs cleaning
    //    those legacy edges.
    const refsResult = await session.run(`MATCH (d:KnowledgeDocument { attachmentId: $attachmentId })-[r]->()
     WHERE type(r) = 'PARTY'
        OR r.sourceDocumentId = $attachmentId
     DELETE r
     RETURN count(r) AS refs`, { attachmentId });
    const references = refsResult.records[0]?.get("refs")?.toNumber?.() ?? 0;
    return {
        sections,
        chunks: sectionChunks + typedChunks,
        typed,
        references,
    };
}
//# sourceMappingURL=document-hierarchy.js.map