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
issue and the depends_on edges that answer it.null verdict as approval.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.0KnowledgeComponent, tokensOf, bm25Scores, rankNodes, queryGraph, retrieveSubgraph, validateGraph.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.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