Long-Horizon AI Programming in Practice: From Task Decomposition to Knowledge Graph Delivery
Building a knowledge graph system is a useful test of long-horizon AI coding agents, because every module must obey the same semantic constraints over an extended development cycle. This article explains how to turn a one-line requirement into an ontology-driven knowledge graph system—covering extraction, validation, graph storage, and interactive querying—by defining acceptance criteria first, splitting the work into eight independently verifiable stages, and keeping an independent acceptance baseline separate from the agent's self-testing loop. It also covers why "it runs end to end" is not the same as "it's production ready," and what observability, change control, and rollback discipline are required before going live.
1. The Hard Part of Long-Horizon Development Isn’t Writing Code—It’s Maintaining Cross-Stage Constraints
A knowledge graph page that displays nodes and edges does not mean the system is deliverable. If you swap in a new batch of texts, will people with the same name be incorrectly merged? After the extraction module adjusts its fields, can the ingestion logic recognize incompatible data? After a new relation type is added to the ontology, can old data still be interpreted under the original rules? These problems rarely show up in demos of individual modules, but they surface in clusters during system integration and continuous operation.
An ontology-driven knowledge graph is a good benchmark for long-horizon AI programming ability, because it requires multiple modules to obey the same set of semantic constraints over a long period. The extraction module must recognize entities and relations according to the ontology; the graph database must store validated results; the query service must return stable data structures; and the frontend must accurately convey relation direction, provenance, and uncertainty. If any single component drifts in its understanding of the rules, you get the classic failure mode: every module looks fine individually, yet the end-to-end result is wrong.
The key to this practice is not forcibly splitting planning, implementation, and evaluation into three separate agents. It is ensuring that all three stages leave auditable artifacts: the plan specifies the delivery order and acceptance criteria, the implementation produces code and data changes, and the evaluation decides whether to continue based on actual runtime results. A capable agent can do all three within one workflow—but it must not replace test evidence with a summary that says “all passed.”
2. Define Acceptable Results First, Then Let the Agent Choose the Approach
The task can start from a single sentence: build an ontology-based knowledge graph system that extracts entities and relations from text, writes them to a graph database, and supports interactive querying on a web page. However, a one-line requirement is good for kickstarting planning, not as an acceptance standard. Before formal implementation begins, at least the following four categories of constraints should be written into project documents and test cases:
- Input boundaries: Which text formats the first version handles, how long documents are split, and how documents and fragments are identified. Without stable source identifiers, it becomes very hard later to locate errors or rerun only the affected data.
- Semantic boundaries: Which entity types, relation directions, and attributes are allowed, and which information requires evidence. For example, “director” can be modeled as a role in a person’s relation to a movie, rather than as an entity category mutually exclusive with “person.” That way, when the same person both directs and acts, you don’t need to create two nodes.
- Write boundaries: Structurally invalid data, insufficient evidence, and uncertain entity identity are three different problems. The first can be rejected by rule; the latter two should go into a review queue. You must not write results into the production graph just because the generated output looks reasonable.
- Delivery boundaries: Which annotated samples verify extraction, which end-to-end case verifies ingestion and display, and which changes require human approval. Acceptance criteria should be fixed before implementation, to prevent the agent from “completing” the task by loosening assertions after tests fail.
The ontology here provides type, attribute, and relation constraints—it does not give the graph automatic fact-checking ability. For example, a “directed” relation with a person as its source and a movie as its target only proves the data structure conforms to the convention; it does not prove the person actually directed the movie. Factual credibility still requires source evidence and, where necessary, human review.
3. Split Development into Eight Independently Verifiable Stages
1. Establish the backend foundation. Build a minimal runnable service first, with clear configuration loading, database connection, error responses, and test entry points. Development, test, and production environments should use separate configurations, and credentials must never be committed into code. Stage acceptance should verify not only that the service starts, but also that when the database is unavailable, the task fails explicitly rather than returning what looks like a successful empty result.
2. Implement ontology validation. Maintain entity types, required attributes, relation directions, and applicability rules in one place, instead of hardcoding separate copies in the extraction, ingestion, and frontend modules. Record which ontology version applies at write time, and build tests for illegal relations, missing fields, and valid boundary cases. This lets you later distinguish “the rules changed” from “the implementation regressed” when the ontology is modified.
3. Connect extraction and ingestion. The processing pipeline should, in order, split documents, generate candidates, validate structure, disambiguate entities, and persist results. Candidate results should include the source document and evidence fragments, and the system should check that the evidence actually exists in the corresponding input. Consider the sentence “Lin directed The Far Shore, starring Zhou as the lead.” Valid outputs include “Lin —directed→ The Far Shore“ and “Zhou —acted in→ The Far Shore.” If the candidate output labels both people as directors, then even if the types and directions are correct, it must not pass factual acceptance.
4. Manage ontology changes. Provide controlled operations to add, modify, and deactivate entity types, relation types, and attributes. Adding an optional attribute and deleting a relation type that is in active use carry very different risks. The former usually does not require rewriting historical data; the latter requires first identifying affected data and queries, then deciding whether to migrate, keep the old version, or abort the change. Do not turn ontology management into a configuration editing page that can bypass review.
5. Provide structured export. Express facts in a subject–predicate–object structure, with stable conventions for entity identifiers, relation identifiers, and the representation of attribute values such as strings and dates. Acceptance must check not just whether a file can be downloaded, but whether the exported content can be correctly parsed outside the original system, with relation direction, data types, and agreed provenance information intact.
6. Build the frontend and query contract. First define the return structures for nodes, edges, and pagination or truncation markers, as well as empty-result and error states—then implement the page interactions. The frontend can be developed against sample data that conforms to the contract, while the backend uses contract tests to constrain its real responses. This way, integration effort focuses on business problems rather than repeatedly debugging inconsistent field names and types.
7. Implement explainable graph browsing. Start by default from a single entity and its limited neighborhood, letting users expand step by step by relation type, rather than loading the entire graph at once. Clicking a relation should show its direction and provenance, and users must be clearly told when a query is truncated. It is especially important to distinguish “no relation found” from “the query scope did not cover that relation”—otherwise users easily mistake display limits for missing facts.
8. Complete integration acceptance. Use fixed annotated texts to exercise the full pipeline—extraction, validation, writing, querying, display—and include failure scenarios such as duplicate submissions, illegal relations, database interruptions, and ontology version mismatches. One useful check is submitting the same document twice: the same fact from the same source must not be written twice due to a retry, yet independent evidence from a different document must not be wrongly deduplicated away.
4. Agents Can Self-Test and Self-Fix, but the Acceptance Baseline Must Be Independent
The value of a long-horizon agent lies in shortening the feedback loop. It doesn’t have to wait until the end of the task to run tests—it can check results after a small change, locate the failure, fix it, and rerun. Suppose an edge is missing from the page. The investigation can first confirm whether the extraction candidates include that relation, then check whether the ontology rejected it, then examine the write records, query responses, and frontend filter conditions. Locating issues in the order data flows through the system is usually more effective than immediately rewriting the visualization component.
However, being able to self-fix is not the same as being able to independently prove the fix is correct. An agent that writes the code may write the tests with the same misunderstanding. For example, it might merge all entities with the same name, then “prove” the merge works using a sample containing only one person with that name. Therefore, key acceptance samples and assertions should be stored independently and require review to modify. Extraction quality also cannot be measured by “returns success” alone—entity types, relation directions, evidence correspondence, and disambiguation results should each be checked, distinguishing false extractions from missed ones. For open text, metrics such as recall can only be computed on samples with explicitly annotated ranges; unannotated content must not be treated as nonexistent.
The session context should not carry the entire project state either. Record confirmed decisions, the current ontology version, the most recent passing tests, and unresolved issues in the project itself, so work can resume after an interruption. As tasks grow longer, set execution budgets and stop conditions: if the same failure cannot be stably reproduced after multiple fix attempts, or if a fix involves migrations, permission changes, or adjustments to acceptance criteria, pause and escalate to human judgment rather than expanding the scope of changes.
5. Working End to End Is Not the Same as Being Production Ready
Passing end-to-end cases proves the system has minimal delivery capability; a production environment additionally requires the system to be observable and recoverable when it fails. Extraction jobs should track document source, processing status, and rejection reasons; transient retryable failures and data errors requiring manual repair should be handled separately. On the operations side, monitor task backlogs, validation rejection rates, review queue sizes, and query latency—but no single metric should be interpreted out of context. For example, a rising rejection rate could mean extraction quality dropped, or that a new ontology version tightened the constraints.
Go live starting with a small, verifiable batch of data. The development agent can run tests and prepare migration plans in an isolated environment, but production writes, destructive migrations, and permission changes should be approved through a separate process. For new ontology versions, decide in advance whether historical data may coexist, needs rerunning, or must be migrated. For extraction logic rollbacks, distinguish between restoring a service version and fixing facts that were already written—the latter does not happen automatically when the code rolls back.
What this practice can simplify is task handoffs, repeated summaries, and status synchronization among agents. What it cannot simplify is independent acceptance, version management, and release control. The measure of success in long-horizon development is not how long an agent worked continuously in one session, but whether the team can still explain the results and restore operations under the same set of rules when the inputs change, a stage is handed off, or a change is rolled back.
