singularity-forge/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
..
resources feat(graph): implement knowledge graph system (closes #4202) 2026-04-15 02:20:49 +02:00
tests fix(gsd): open project DB in headless query 2026-04-14 19:25:51 +02:00
web fix(security): activate auth middleware and harden shutdown/update routes (#4023) 2026-04-13 07:52:43 -04:00
app-paths.ts
bundled-extension-paths.ts
bundled-resource-path.ts
claude-cli-check.ts fix(providers): route Anthropic subscription users through Claude Code CLI (#3772) 2026-04-08 07:20:20 -05:00
cli-web-branch.ts refactor(cli): slim down top-level src/ — dedup, unused fallbacks, onboarding 2026-04-14 01:51:22 +00:00
cli.ts feat(graph): implement knowledge graph system (closes #4202) 2026-04-15 02:20:49 +02:00
extension-discovery.ts
extension-registry.ts
headless-answers.ts
headless-context.ts
headless-events.ts fix(headless): keep idle timeout off during interactive tools 2026-04-12 14:04:15 +02:00
headless-query.ts fix(gsd): open project DB in headless query 2026-04-14 19:25:51 +02:00
headless-types.ts
headless-ui.ts
headless.ts fix(headless): keep idle timeout off during interactive tools 2026-04-12 14:04:15 +02:00
help-text.ts feat(graph): implement knowledge graph system (closes #4202) 2026-04-15 02:20:49 +02:00
loader.ts fix(auto): resolve resource-loader.js from GSD_PKG_ROOT on resume (#3949) 2026-04-10 20:00:46 -05:00
logo.ts refactor(cli): slim down top-level src/ — dedup, unused fallbacks, onboarding 2026-04-14 01:51:22 +00:00
mcp-server.ts fix(mcp): expose every registered tool and fix SDK subpath resolution 2026-04-13 01:40:05 +00:00
models-resolver.ts
onboarding.ts refactor(cli): slim down top-level src/ — dedup, unused fallbacks, onboarding 2026-04-14 01:51:22 +00:00
pi-migration.ts
project-sessions.ts
provider-migrations.ts fix(cli): preserve anthropic api provider 2026-04-11 18:10:09 +02:00
remote-questions-config.ts
resource-loader.ts fix(cli): use junction symlinks in merged node_modules path 2026-04-13 20:44:08 -05:00
rtk.ts
security-overrides.ts fix(security): add configurable overrides for command allowlist and SSRF blocklist 2026-04-02 13:45:05 +02:00
startup-model-validation.ts Merge remote-tracking branch 'upstream/main' into claude/model-agnostic-selection-rmDX3 2026-04-13 10:22:16 -05:00
startup-timings.ts
tool-bootstrap.ts
update-check.ts fix(gsd): use bun for update when installed via Bun (#4145) 2026-04-14 00:52:08 +02:00
update-cmd.ts fix(gsd): use bun for update when installed via Bun (#4145) 2026-04-14 00:52:08 +02:00
web-mode.ts fix(windows): harden portability across runtime and tooling 2026-04-10 20:33:18 -05:00
welcome-screen.ts fix(ui): remove 200-column cap on welcome screen width 2026-04-05 17:41:21 -05:00
wizard.ts
worktree-cli.ts
worktree-name-gen.ts