Build Context from Blocks
Build structured context by validating connected Blocks, traversing declared ancestry, and retrieving the content associated with reachable Block identities.
ALX supplies deterministic identities and graph structure. Applications select which reachable content to include, how to order the content, and how to interpret the resulting context.
Create Connected Blocks
import {
createBlock,
validateBlock,
} from '@alx-protocol/sdk';
import {
buildGraph,
validateGraph,
} from '@alx-protocol/sdk/lineage';
import { traceAttribution } from '@alx-protocol/sdk/trace';
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];
Each child declares only its direct parent. The connected parent relationships preserve the path from the decision to the analysis and source.
Validate Blocks and Relationships
Validate each Block before using the Block in a graph. Then validate the available graph structure.
const blocksAreValid = blocks.every(
(block) => validateBlock(block).ok,
);
if (!blocksAreValid) {
throw new Error('Block identity validation failed');
}
const graph = buildGraph(blocks);
const graphResult = validateGraph(graph);
if (!graphResult.ok) {
throw new Error('Graph validation failed');
}
Block validation recomputes deterministic identities. Graph validation checks parent resolution, duplicate parent relationships, self-references, and cycles across the supplied Block set.
Traverse Declared Ancestry
Start from the Block whose context the application needs.
const trace = traceAttribution(decision.blockHash, graph);
console.log(trace.nodes);
console.log(trace.edges);
console.log(trace.maxDepth);
The trace contains the selected Block, every reachable ancestor in the supplied graph, the traversed edges, graph depths, path counts, and cycle status.
Assemble Application Context
Map the reachable identities back to the available Blocks and exclude the selected output when only prior work should become context.
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);
The trace order is deterministic by minimum depth and Block hash. Application requirements determine semantic ordering, context limits, retrieval policy, content filtering, and model-specific formatting.
Verification Boundary
A valid trace establishes reachable declared relationships within the supplied graph. A valid trace does not establish content truth, causal completeness, relevance, authority, or permission to use the content.
Applications must also evaluate Block availability, access control, trust policy, and any additional evidence required by the workflow.
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.