singularity-forge/packages/mcp-server/src
Nils Reeh 15bccca78f feat(graph): implement knowledge graph system (closes #4202)
Ports the v1 graphify system to v2 as a native TypeScript implementation.
The knowledge graph builds semantic relationships between milestones, slices,
tasks, and knowledge entries — and injects relevant subgraphs automatically
into every agent dispatch prompt.

## Core implementation (packages/mcp-server/src/readers/graph.ts)

- `buildGraph(projectDir)` — walks all .gsd/ artifacts (STATE.md,
  milestone PLANs, slice PLANs, KNOWLEDGE.md), extracts nodes and edges
  with confidence tiers (EXTRACTED / INFERRED / AMBIGUOUS). Parse errors
  skip the node rather than crashing.
- `writeGraph(gsdRoot, graph)` — atomic write via tmp file + rename.
- `writeSnapshot(gsdRoot)` — saves a diff baseline before each rebuild.
- `graphQuery(projectDir, term, budget?)` — BFS subgraph search with
  case-insensitive matching on label + description; trims AMBIGUOUS edges
  first, then INFERRED, respecting the token budget (default 4 000).
- `graphStatus(projectDir)` — freshness check; stale = older than 24 h.
- `graphDiff(projectDir)` — compares current graph to last snapshot,
  returns added / removed / changed counts for nodes and edges.

## MCP tool (packages/mcp-server/src/server.ts)

Registers `gsd_graph` immediately after `gsd_knowledge` with four modes:
build | query | status | diff. All errors returned as isError: true.

## CLI subcommand (src/cli.ts, src/help-text.ts)

`gsd graph build|status|query <term>|diff` — follows the established
`if (cliFlags.messages[0] === '...')` dispatch pattern. Uses
`resolveGsdRoot()` for git-root-aware path resolution (not a naive
`.gsd` append). Help text updated with correct positional argument format.

## Auto-rebuild after slice completion
(src/resources/extensions/gsd/tools/complete-slice.ts)

Fire-and-forget `buildGraph → writeGraph` triggered after every slice
completion. Uses `@gsd-build/mcp-server` package import (not a relative
src path) and `resolveGsdRoot()` for correct path resolution in monorepos.

## Graph-aware dispatch injection
(src/resources/extensions/gsd/graph-context.ts,
 src/resources/extensions/gsd/auto-prompts.ts)

`inlineGraphSubgraph(projectDir, term, { budget })` queries the graph and
formats the result as a `### Knowledge Graph Context` markdown block,
consistent with all other inlined context blocks. Adds a stale warning
annotation when the graph is older than 24 h. Returns null (graceful
skip) when graph.json is missing, the query returns zero nodes, or the
import fails — no agent dispatch is ever blocked by graph availability.

Injected into three prompt builders:
- `buildResearchSlicePrompt` — 3 000 token budget
- `buildPlanSlicePrompt`     — 3 000 token budget
- `buildExecuteTaskPrompt`   — 2 000 token budget

## Tests

- 22 tests for the core graph reader (graph.test.ts)
- 14 tests for the dispatch injection helper (graph-context.test.ts)
- All tests use real on-disk fixtures (no module mocking needed)
- Full suite: 6 318 passed, 0 failed

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-15 02:20:49 +02:00
..
readers feat(graph): implement knowledge graph system (closes #4202) 2026-04-15 02:20:49 +02:00
cli.ts fix(mcp-server): hydrate model credentials into env 2026-04-10 17:31:07 -05:00
env-writer.test.ts feat(mcp-server): add secure_env_collect tool via MCP form elicitation 2026-04-11 11:51:43 -05:00
env-writer.ts feat(mcp-server): add secure_env_collect tool via MCP form elicitation 2026-04-11 11:51:43 -05:00
import-candidates.test.ts test(mcp-server): add regression tests for importLocalModule candidate resolution 2026-04-10 19:33:00 -05:00
index.ts feat(graph): implement knowledge graph system (closes #4202) 2026-04-15 02:20:49 +02:00
mcp-server.test.ts feat(mcp-server): expose ask_user_questions via elicitation 2026-04-10 15:44:08 -05:00
secure-env-collect.test.ts feat(mcp-server): add secure_env_collect tool via MCP form elicitation 2026-04-11 11:51:43 -05:00
server.ts feat(graph): implement knowledge graph system (closes #4202) 2026-04-15 02:20:49 +02:00
session-manager.ts feat: Headless Integration Hardening & Release (M002) (#2811) 2026-03-26 23:33:22 -06:00
tool-credentials.test.ts fix(mcp-server): hydrate model credentials into env 2026-04-10 17:31:07 -05:00
tool-credentials.ts fix(mcp-server): hydrate model credentials into env 2026-04-10 17:31:07 -05:00
types.ts feat: Headless Integration Hardening & Release (M002) (#2811) 2026-03-26 23:33:22 -06:00
workflow-tools.test.ts fix(mcp-server): open the DB for inline workflow tools 2026-04-11 17:44:17 +02:00
workflow-tools.ts fix(gsd): route quality gates through a per-turn registry 2026-04-12 21:13:16 -05:00