Build Context from Blocks
Build structured context by validating connected Blocks, traversing declared ancestry, and retrieving the content of reachable Blocks.
ALX supplies deterministic identities and Graph structure. Applications select the reachable content to include, order the content, and interpret the resulting context.
Create Connected Blocks
Create build-context.mjs. Each child declares only its direct parent. The chain of parent relationships preserves the path from the decision to the analysis and source.
import {
buildGraph,
createBlock,
traceAttribution,
validateBlock,
validateGraph,
} from '@alx-protocol/sdk';
const source = createBlock(
{ kind: 'source', text: 'Quarterly revenue increased.' },
[],
);
const analysis = createBlock(
{ kind: 'analysis', text: 'Revenue growth exceeded the forecast.' },
[source.blockHash],
);
const decision = createBlock(
{ kind: 'decision', text: 'Increase the next-quarter forecast.' },
[analysis.blockHash],
);
const blocks = [source, analysis, decision];
Validate Blocks and Relationships
Add to the same script. Compose Blocks into a Graph explains both calls. The script stops unless Graph verification returns valid for every Block reachable from decision.
if (!blocks.every((block) => validateBlock(block).ok)) {
throw new Error('Block identity validation failed');
}
const graphResult = validateGraph(
decision,
{ kind: 'finite', blocks },
{ protocolVersion: '1', validationMode: 'closed-world', graphRoot: decision.blockHash, externalParents: [] },
);
if (graphResult.status !== 'valid') {
throw new Error(`Graph verification returned ${graphResult.status}`);
}
const graph = buildGraph(blocks);
Traverse Declared Ancestry
Start from the Block whose context the application needs. trace.nodes lists decision and every reachable ancestor, sorted by minDepth and then by blockHash. trace.complete is false when a parent is missing, and a reachable cycle makes traceAttribution() throw ALX_GRAPH_CYCLE. Lineage and Attribution defines every Trace field.
const trace = traceAttribution(decision.blockHash, graph);
Assemble Application Context
Map the reachable identities back to the available Blocks and exclude the selected output when only prior work should become context. The Trace order is deterministic. Application requirements determine semantic ordering, context limits, retrieval policy, content filtering, and model-specific formatting. Add the following and run node build-context.mjs:
const blocksByHash = new Map(
blocks.map((block) => [block.blockHash, block]),
);
const context = trace.nodes
.filter(({ hash }) => hash !== decision.blockHash)
.map(({ hash, minDepth }) => ({
blockHash: hash,
minDepth,
content: blocksByHash.get(hash)?.content,
}));
console.log(context);
[
{
blockHash: '0xc1d4817b12522b80fef6b8d3fa73b885cb73255d38f6cbf84760c106cddf5a2b',
minDepth: 1,
content: { kind: 'analysis', text: 'Revenue growth exceeded the forecast.' }
},
{
blockHash: '0x3f36bb9f138c9e0d9022aa76b8f682e4643b3e2e94bbae01f6b3e7c52145b95d',
minDepth: 2,
content: { kind: 'source', text: 'Quarterly revenue increased.' }
}
]
Verification Boundary
A Trace establishes reachable declared relationships within the supplied Graph. Check trace.complete before treating the assembled context as complete. A Trace does not establish content truth, causal completeness, relevance, authority, or permission to use the content. Applications also evaluate Block availability, access control, trust policy, and any additional evidence the workflow requires.
When to Build Context
Use this workflow when an application or agent needs to retrieve sources, intermediate outputs, evaluations, decisions, or handoffs connected to a selected Block.