Repository navigation
Closed
Description
Overview
Implement the context assembly system that gathers relevant knowledge graph data and packages it into a structured prompt for external agents. This is what makes external agents knowledge-aware — they receive curated graph context rather than having to discover everything through MCP tool calls.
Context assembly is invoked when a user starts an ACP session with selected nodes. It produces a ContextPacket consumed by the ACP session initialization.
Acceptance Criteria
-
ContextAssemblertrait from Shared types, traits, and interface contracts for agent subsystems #998 fully implemented - Selected nodes included with full content
- Semantic expansion finds relevant neighbors (top 5 per seed, deduplicated)
- 1-hop relationship traversal includes connections with type and direction
- Token budget enforced: output never exceeds budget (50k default)
- Priority-based truncation: selected nodes full, neighbors summarized, relationships listed
- System prompt includes MCP tool usage instructions
- Handles edge cases: no selected nodes, disconnected nodes, very large node bodies
- Token estimation within 10% of actual model tokenizer count
- Unit tests with mock NodeService and NodeEmbeddingService
- Code passes
bun run quality:fix
Technical Specifications
New Module
packages/desktop-app/src-tauri/src/acp/context_assembly.rs
Assembly Pipeline
- Node Selection: Accept list of node IDs (from user selection in UI)
- Content Fetch: Load full content for each selected node via
NodeService - Semantic Expansion: For each selected node, query
NodeEmbeddingServicefor top 5 semantic neighbors (deduplicate across seeds) - Relationship Traversal: For all nodes in expanded set, fetch direct relationships (1-hop) via
NodeService - Formatting: Convert to structured text
- Budget Enforcement: Estimate tokens, truncate to fit budget
Content Formatting
## NodeSpace Context
You have access to the NodeSpace knowledge graph via MCP tools on localhost:3100.
### Selected Nodes
#### {title} [{type}] (id: {id})
{body, truncated to 2000 chars}
Properties: {key: value pairs}
### Related Context (auto-discovered)
#### {title} [{type}] (id: {id})
{body, truncated to 500 chars}
### Relationships
- "{from_title}" -> {relationship_type} -> "{to_title}"
### MCP Access
Query the graph using these MCP tools: search_semantic, search_nodes, get_node, create_nodes_from_markdown, update_node, delete_node.Token Budgeting
- Default budget: 50,000 tokens
- Priority order: selected nodes (full) > relationships (listed) > semantic neighbors (summarized)
- Estimation: ~4 chars per token heuristic (sufficient for budget enforcement)
- Drop lowest-relevance items first when over budget
Reference Files
packages/core/src/services/embedding_service.rs— NodeEmbeddingService APIpackages/core/src/services/— NodeService APIpackages/core/src/mcp/handlers/search.rs— existing semantic search patterns
Exports
GraphContextAssemblerstruct implementingContextAssembler
Dependencies
- Shared types, traits, and interface contracts for agent subsystems #998 (types/traits)
- Uses existing
NodeServiceandNodeEmbeddingService(no dependency on other new issues)
Related Issues
- ACP Integration and Local LLM Capability #997 (parent epic)
Activity
malibio commentedon Apr 3, 2026
Baseline: Frontend tests have pre-existing vitest startup error (TypeError: Attempted to assign to readonly property). This is a known issue unrelated to this task. Rust compilation is the critical path.
feat: context assembly for building rich prompts from knowledge graph (…
feat: context assembly for building rich prompts from knowledge graph (…
feat: context assembly for building rich prompts from knowledge graph (…