Agent Context Governance: Building an Editable, Non-Linear Conversation Map

    This article explains why long-running AI agent sessions inevitably suffer from context pollution, and how to treat context as a first-class, editable object rather than a byproduct of history. It introduces a context graph model where "an edge is context," covering node types, edge semantics, and a minimal pseudocode example, then clarifies how the graph sits upstream of the execution engine. Practical guidance on token budgets, branch conventions, observability metrics, and team permissions is included, along with common pitfalls to avoid.

    1. The Problem: Why Long Sessions Drift Into Context Pollution

    When an agent task grows from a few turns to dozens or even hundreds, the signal-to-noise ratio of the conversation history degrades quickly. A typical mix contains four kinds of content: hypotheses already disproved by experiments, large volumes of tool output produced during debugging, branch explorations unrelated to the current goal, and the decisions and constraints that genuinely need to be retained long term. In a linear transcript, all of these are chained together by timestamp into a single “timeline” that cannot be split apart.

    Linear history has two inherent flaws. First, it is not addressable: you cannot point at turn 37 and say “the conclusion here still holds, but please ignore the logs from turns 38 to 42.” Second, it is not prunable: even when a framework supports automatic compaction or summarization, the compaction policy is decided by the system, and the user has no explicit control over what the next turn actually carries.

    Two concepts that are often conflated need to be separated here:

    • History: what happened, the basis for auditing and replay.
    • Context: what is actually fed to the model for the next inference step, the source of both cost and quality.

    The session logs and trajectory views of mainstream agents are good at answering the first question — “what did we just do.” The harder problem in engineering practice is the second one — “before the next turn starts, can I decide what it should carry forward.” Promoting context from a byproduct of history to a first-class object that humans can edit directly is the core idea of this article.

    2. Core Model: A Continuously Updated Context Graph

    Open the linear session into a graph, and the rules compress into one sentence: an edge is context.

    2.1 Node Types

    • Turn nodes: one round of question and answer from a real session, keeping the original reference rather than copying content.
    • Material nodes: external inputs such as documents, web excerpts, log fragments, and sample API responses.
    • Note nodes: human-distilled conclusions, constraints, to-dos, and decision records.

    Nodes only carry content; they do not decide whether it takes effect. Whether something enters the next turn is determined entirely by edges.

    2.2 Edge Semantics

    An edge pointing from a node to the question node means “this content will be passed to the model as context for this turn.” Three operational semantics follow:

    1. Attach: connect a material or a prior conclusion into the current question, which completes a context injection.
    2. Detach: remove an edge so that branch no longer enters subsequent requests, while the content stays on the canvas and can be reconnected at any time.
    3. Condense: compress a long conversation into a shorter new path without overwriting the original record, producing a traceable derived view.

    The key advantage of this design is that it is non-destructive: every pruning operation happens at the view layer, the audit chain stays intact, and rollback costs nothing.

    2.3 A Minimal Illustration

    The pseudocode below expresses the essence of the model, not the interface of any specific product:

    1. class ContextNode:
    2. id: str
    3. kind: str # "turn" | "material" | "note"
    4. payload: str
    5. ref: str | None # reference to the original session record
    6. class ContextGraph:
    7. nodes: dict[str, ContextNode]
    8. edges: set[tuple[str, str]] # (source, target=question_id)
    9. def build_prompt(self, question_id: str) -> list[ContextNode]:
    10. # only nodes connected to the current question form the context
    11. return [n for (s, t) in self.edges
    12. if t == question_id
    13. for n in [self.nodes[s]]]

    The output order of build_prompt can be sorted by edge priority or timestamp, and this step directly determines the narrative structure the model sees. In practice, put “hard-constraint notes” first and “reference material” last to reduce the risk of mid-context information being ignored in long contexts.

    3. Relationship to the Execution Engine: The Graph Decides What to Carry, the Engine Decides What to Do

    The context graph should not replace the execution layer; it sits upstream of it. A sensible division of labor:

    • Graph: defines the input boundary and decides how the token budget is allocated.
    • Execution engine: actually runs one round of the task under the given context, can keep calling its configured tool chain, and produces structured results.
    • Logs: execution results are written back to the session record at the same time, keeping everything auditable.

    Asking a question on the canvas is therefore still a real agent execution, not a simulation. The answer flows back into the graph as a new node while remaining in the execution layer’s session log, forming a “graph — execution — log” loop. This layering decouples context governance from tool capabilities: swapping the execution framework does not require rebuilding the context model.

    4. Engineering Implementation

    4.1 Context Budget and Priority

    Set a token limit per turn and fill it by priority: hard constraints > current goal > key conclusions > reference material > raw logs. When the budget is exceeded, drop from the lowest priority upward instead of letting the system apply indiscriminate summarization.

    4.2 Branch Conventions

    Agree on naming and lifecycle, for example exp-* for experiment branches that are detached once validation fails, and dec-* for decision nodes that are retained long term and attached to all subsequent questions. Periodically clean up “orphan nodes,” meaning material nodes with no outgoing edges.

    4.3 Observability

    Three metrics are worth tracking: average context size, the share of detached branches, and the repeat-question rate for the same task before and after governance. If context size drops while task success rate holds, the governance is working.

    4.4 Teams and Permissions

    For multi-person collaboration, the graph should support read-only snapshots and edit locks to avoid concurrent edits causing context drift; key decision nodes should be placed under version control, aligned with the code review process.

    5. Common Pitfalls

    • Treating the graph as visual decoration: if edges do not participate in prompt construction, the graph is just another way of drawing the logs.
    • Over-condensing: compressing multiple rounds of reasoning into one sentence easily loses key constraints from failed paths, so keeping references to the original text is advisable.
    • Ignoring order: set semantics solve “what to carry” but not “how to arrange it,” so ordering still needs explicit design.

    6. Conclusion

    The essence of context pollution is not that history is too long, but that there is no explicit control plane for context. Rebuilding the session as a non-linear map where “an edge is context,” so that visibility, editability, and auditability all hold at once, is the key engineering practice for keeping long-cycle agent tasks stable. Start from the smallest closed loop: first make edges genuinely affect the prompt, then gradually introduce budget policies, branch conventions, and quantitative evaluation. If running in the cloud, graph snapshots can be stored in object storage, while log services and monitoring alerts track changes in context size, giving this governance mechanism the ability to operate sustainably.