Skip to content

Context assembly: build rich prompts from knowledge graph #1005

Closed

Description

@malibio

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

  • ContextAssembler trait 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

  1. Node Selection: Accept list of node IDs (from user selection in UI)
  2. Content Fetch: Load full content for each selected node via NodeService
  3. Semantic Expansion: For each selected node, query NodeEmbeddingService for top 5 semantic neighbors (deduplicate across seeds)
  4. Relationship Traversal: For all nodes in expanded set, fetch direct relationships (1-hop) via NodeService
  5. Formatting: Convert to structured text
  6. 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 API
  • packages/core/src/services/ — NodeService API
  • packages/core/src/mcp/handlers/search.rs — existing semantic search patterns

Exports

  • GraphContextAssembler struct implementing ContextAssembler

Dependencies

Related Issues

Activity

  1. malibio commented on Apr 3, 2026

    @malibio
    CollaboratorAuthor

    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.

  2. self-assigned this
    on Apr 3, 2026
  3. added 2 commits that reference this issue on Apr 3, 2026
    fda7ee9
    d76923e
  4. added a commit that references this issue on Jun 22, 2026
    8ae3605
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions