Agentic ⏱ 8 min read 📁 examples/12_knowledge_graph.mjs

Lesson 33: The knowledge graph: a contract that can say no

the typed graph: mechanical pass, model enrichment, negative/conditional/evidenced edges, the decision gate, a budgeted answer

1. Why This Feature Exists

A vendor contract has to be read, not just indexed. The mechanical pass needs no model: sections, definitions, obligations, a prohibition and the condition behind the suspension right all land as typed nodes with source pointers. The model adds only the *issue* the reader must answer and the edges that answer it, through knowledge_patch, which is validated before it can touch the graph. The graph then records a rejected reading and a conditional right, and one asserted edge must survive a decision gate before it is admitted.

What this shows:

2. What the Example Demonstrates

A — mechanical
buildGraph() on a supply agreement; validateGraph(); saveGraph().
B — semantic
the model adds an issue and the depends_on edges that answer it.
C — shapes
a negative edge (the rejected reading), a conditional edge (only under the payment clause), an evidenced edge (one edge, two sources).
D — gate
createDecisionGate() admits or rejects one asserted edge.
E — closed
gatePolicy: 'closed' refuses to read an uncalibrated null verdict as approval.
F — read
a fresh session answers from the budgeted block alone. Public API: buildGraph, validateGraph, applyPatch, edgeText, renderKnowledgeBlock, retrieveSubgraph, graphSha, loadGraph, saveGraph, createDecisionGate, getDecisionModel, providerDescriptorFor, .withKnowledge, .withDecisionGate. Run: node examples/12_knowledge_graph.mjs Needs: PROVIDER plus its credentials in secret.env. @version 0.27.0
G — curated
strict source validation, a frozen read-only graph, folded-bigram BM25, weighted outgoing traversal, bounded neighbourhoods, and optional hub damping. Public API: KnowledgeComponent, tokensOf, bm25Scores, rankNodes, queryGraph, retrieveSubgraph, validateGraph.
imported from 'merci-sdk'
createDecisionGate, defaultDisplay, getDecisionModel, initializeClient, loadCredentials, log, providerDescriptorFor

3. How the Core Call Works

The entry point, condensed. The full runnable file is linked below.

import { mkdir, rm } from 'node:fs/promises';
import { join } from 'node:path';
import {
    createDecisionGate,
    defaultDisplay,
    getDecisionModel,
    initializeClient,
    loadCredentials,
    log,
    providerDescriptorFor,
} from 'merci-sdk';
import {
    applyPatch, bm25Scores, buildGraph, edgeText, graphSha, KnowledgeComponent, loadGraph,
    queryGraph, rankNodes, renderKnowledgeBlock, retrieveSubgraph, saveGraph, tokensOf, validateGraph,
} from 'merci-sdk/knowledge';
import { reportUsage, resolveExample, runMain, trackUsage, warnEmptyAnswer, wireClient } from './_shared/example-utils.mjs';
import { SCRATCH_DIR } from './_shared/real-tools.mjs';

const STATE_DIR = join(SCRATCH_DIR, '12-knowledge-graph');
const GATE_DIR = join(STATE_DIR, 'gate');
const CLOSED_DIR = join(STATE_DIR, 'closed');

/** Sections, definitions, an obligation, a prohibition, a permission behind a condition. */
const CONTRACT = `# Supply agreement

This Agreement is between Northwind Ltd (the "Customer") and Helios GmbH (the "Supplier").

## 1. Definitions

"Deliverable" means the software listed in Schedule A.
"Defect" means a failure of a Deliverable to conform to Schedule A.

## 2. Delivery

The Supplier shall deliver each Deliverable by the date in Schedule A.
The Supplier shall not subcontract any Deliverable without the Customer's written consent.
Section 3 limits the Customer's remedies for a late Deliverable.

Read the full example: examples/12_knowledge_graph.mjs →

4. Behavior the Code Fixes

the typed graph: mechanical pass, model enrichment, negative/conditional/evidenced edges, the decision gate, a budgeted answer

5. Run the Example

Execute the runnable example from the repository root:

node examples/12_knowledge_graph.mjs
What it does: the typed graph: mechanical pass, model enrichment, negative/conditional/evidenced edges, the decision gate, a budgeted answer