Safe Knowledge Graph Entry Deletion #24
matsilva
started this conversation in
Technical Design Proposals
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Below is the technical design document I spent today crafting and plan on implementing to address: #12 (comment)
Objective
Enable users to safely soft-delete one or more knowledge graph nodes within a specified project using an MCP tool, rebuild a clean graph, optionally recalculate affected summary nodes based on referenced node IDs, and create a timestamped backup of the existing graph before recompilation. If dependent (child) nodes exist, return their details to the actor for user confirmation before proceeding with deletion. This addresses user needs to remove accidental, junk, or experimental entries (e.g., typos, irrelevant thoughts, or temporary spikes) while preserving data for recovery and maintaining graph integrity.
User Problems Addressed
Requirements
knowledge_graph.deleted.ndjsonfor recovery.getDependentNodes), log warnings, and return their details in the JSON response. The actor presents an overview and prompts the user to decide on deleting them.knowledge_graph.ndjsonexcluding deleted nodes.recalculateSummariesis true, identify summary nodes referencing deleted node IDs, queue their recalculation, and recompile the graph.knowledge_graph.ndjsonin./data/backup/before recompilation.projectContext.knowledge_graph.deleted.ndjsonand via logger.delete_nodes).KnowledgeGraphManagermethods (getNode,streamDagNodes,allDagNodes,appendEntity,parseDagNode) for efficiency and consistency.System Context
knowledge_graph.ndjson(append-only NDJSON). Nodes have anodeId,project,thought,tags,artifacts,parents,children,createdAt,role(actor, critic, summary), and optional fields likeverdict,target,summarizedSegment. No branch labels; summaries reference node IDs viasummarizedSegment.KnowledgeGraphManager,ActorCriticEngine, and agents (Actor,Critic,SummarizationAgent).Implementation Details
1. Input Schema (
DeleteNodeSchema)Validates user input for the
delete_nodesMCP tool.{ "nodeIds": ["node123", "node124"], "projectContext": "/home/user/projects/my-project", "reason": "junk entry", "recalculateSummaries": true }2. Output Schema (
DeletedNodeSchema)Defines the structure of deleted node records in
knowledge_graph.deleted.ndjson.{ "nodeId": "node123", "project": "my-project", "reason": "junk entry", "timestamp": "2025-05-17T19:12:00-04:00", "nodeData": { "id": "node123", "project": "my-project", "projectContext": "/home/user/projects/my-project", "thought": "Add login feature", "role": "actor", "tags": ["feature", "auth"], "artifacts": [{ "name": "login.ts", "path": "src/login.ts" }], "parents": ["node122"], "children": ["node125"], "createdAt": "2025-05-17T10:00:00-04:00" } }3. Response Schema (
DeleteResultSchema)Defines the JSON response for the
delete_nodestool, including dependent nodes if present.{ "success": false, "deletedNodeIds": [], "dependentNodes": [ { "nodeId": "node125", "thought": "Implement login validation", "tags": ["auth", "task"], "role": "actor", "createdAt": "2025-05-17T11:00:00-04:00" } ], "warnings": ["Node node123 has 1 dependent node"], "backupPath": null }{ "success": true, "deletedNodeIds": ["node123", "node124"], "dependentNodes": [], "warnings": [], "backupPath": "./data/backup/knowledge_graph_20250517-191200.ndjson" }4. Knowledge Graph Enhancements (
src/engine/KnowledgeGraph.ts)Update
KnowledgeGraphManagerto reuse existing APIs and add new methods:deleteNodes(input: z.infer<typeof DeleteNodeSchema>): Promise<z.infer<typeof DeleteResultSchema>>:DeleteNodeSchema.projectContext(existing codebase logic).knowledge_graph.ndjsonusingcreateBackup.getDependentNodes. If any exist:logger.warn({ nodeId, dependents }, 'Node has dependent nodes');.getNodeDetailsto fetch details (nodeId, thought, tags, role, createdAt).{ success: false, deletedNodeIds: [], dependentNodes, warnings, backupPath }.getNodeto fetch each node.DeletedNodeSchema-compliant records and appends toknowledge_graph.deleted.ndjsonusingappendEntity-style logic with file locking.rebuildGraphto excludenodeIds.recalculateSummaries, usesgetAffectedSummariesandqueueSummaryRecalculation, then recompiles the graph.{ success: true, deletedNodeIds, dependentNodes: [], warnings, backupPath }.getDependentNodes(nodeId: string): Promise<string[]>:streamDagNodesto iterate over nodes in the project.parentsincludesnodeId.getNodeDetails(nodeIds: string[]): Promise<z.infer<typeof DependentNodeSchema>[]>:getNodeto fetch each node.nodeId,thought,tags,role,createdAtfor the response.rebuildGraph(excludeNodeIds: string[]): Promise<void>:streamDagNodesto readknowledge_graph.ndjson.excludeNodeIds.proper-lockfilefor safe renaming, similar toappendEntity.getSummaryNodesByProject(project: string): Promise<string[]>:allDagNodesto fetch nodes in the project.role: "summary".getAffectedSummaries(nodeIds: string[], project: string): Promise<string[]>:getSummaryNodesByProjectto get summary nodes.getNodeto checksummarizedSegmentfor anynodeIds.queueSummaryRecalculation(summaryNodeIds: string[], project: string): Promise<void>:SummarizationAgent, processing sequentially.createBackup(): Promise<string>:knowledge_graph.ndjsonto./data/backup/knowledge_graph_20250517-191200.ndjson../data/backup/exists (fs.mkdirSync).fs.copyFilefor efficient copying.restoreNodes(nodeIds: string[]): Promise<RestoreResult>:knowledge_graph.deleted.ndjsonusingreadline.appendEntityto restore nodes toknowledge_graph.ndjson.Persistence:
knowledge_graph.deleted.ndjson: Append-only, usingappendEntity-style logic.proper-lockfilefor all NDJSON operations.API Reuse:
getNode: Fetch individual nodes for deletion and details.streamDagNodes: Stream nodes forgetDependentNodesandrebuildGraph.allDagNodes: Fetch summary nodes ingetSummaryNodesByProject.appendEntity: Append deleted nodes toknowledge_graph.deleted.ndjsonand restored nodes toknowledge_graph.ndjson.parseDagNode: Parse NDJSON lines for consistency.DagNodeSchema: Validate node data.5. MCP Tool Integration (
src/index.ts)Add
delete_nodestool:Description:
Implementation:
@modelcontextprotocol/sdk.DeleteNodeSchema.KnowledgeGraphManager.deleteNodes.DeleteResultSchema.Example Input:
{ "tool": "delete_nodes", "input": { "nodeIds": ["node123", "node124"], "projectContext": "/home/user/projects/my-project", "reason": "experimental spike cleanup", "recalculateSummaries": true } }Actor Handling:
dependentNodesis non-empty, the actor presents an overview (e.g., node ID, thought, tags, role, creation date) to the user and prompts: “Node node123 has 1 dependent node. Delete it? [y/N]”.delete_nodesagain, including the dependent node IDs.6. Summarization Integration (
src/agents/Summarize.ts)Update
SummarizationAgent:checkAndTriggerSummarization:KnowledgeGraphManager.getNode), excluding deleted nodes.logger.info({ nodeId }, 'Recalculated summary node');.7. Auditing and Logging (
src/logger.ts)8. Documentation Updates
Development Steps
KnowledgeGraphManagermethods:deleteNodes,rebuildGraph,createBackup,getAffectedSummaries,getNodeDetails,getDependentNodes,getSummaryNodesByProject,queueSummaryRecalculation, andrestoreNodes.getNode,streamDagNodes,allDagNodes,appendEntity, andparseDagNode.deleteNodes.createBackupto copyknowledge_graph.ndjsonbefore recompilation.rebuildGraphandcreateBackup.SummarizationAgentto supportqueueSummaryRecalculation.checkAndTriggerSummarizationfor targeted summary recalculation.delete_nodesMCP tool insrc/index.ts.@modelcontextprotocol/sdkand useDeleteResultSchema.logger.ts.docs/OVERVIEW.mdanddocs/INSTALL_GUIDE.md.pnpm test).restoreNodesand backup integrity.Risks and Mitigations
getDependentNodesandgetNodeDetailswith complex DAGs.streamDagNodesandreadline; test with 10,000+ nodes.Future Considerations
nodeIdsby project or tags.restore_nodesMCP tool for recovery.Beta Was this translation helpful? Give feedback.
All reactions