Remember Your Trace: Memory-Guided Long-Horizon Agentic Framework for Consistent and Hierarchical Repository-Level Code Documentation

Suyoung Bae, Jaehoon Lee, Changkyu Choi, YunSeok Choi, Jee-Hyong LeeNeurIPS 2026

TL;DR

Ask an AI to document an entire software repository today, and it acts like it has amnesia: it reads the same files over and over, contradicts itself across pages, and misses the forest for the trees. MemDocAgent fixes this by treating codebase documentation as a single, continuous journey guided by a shared memory and a strict bottom-up roadmap. The result is faster, cohesive documentation—spanning individual functions up to high-level architecture—that is detailed enough for another AI to rebuild the working code from scratch.

What Problem Does This Paper Address?

Good documentation is the map that helps both humans and AI coding agents navigate massive codebases. Writing it manually is exhausting, so researchers have turned to Large Language Models (LLMs) to automate the job.

However, existing automated tools suffer from short-term thinking. They chop a repository into isolated pieces and document each one from a blank slate. Because there is no shared state between steps, two major problems emerge:

  1. Wasted effort and self-contradiction: Systems repeatedly fetch the exact same source files (averaging 50% redundant reads) and frequently describe the same underlying logic in conflicting ways across different documents (around a 13% inconsistency rate).
  2. Flat, incomplete views: Current tools either write microscopic function docstrings (losing the big picture) or macroscopic project overviews (omitting crucial implementation details), failing to connect them into a unified hierarchy.
Figure 1: Limitations in existing documentation systems. While existing systems suffer from sub- stantial repeated source-file retrieval and cross-document inconsistencies, MemDocAgent eliminates repeated retrieval and reduces inconsistency by 75.5%. N/A denotes closed-source systems whose internal pipelines are inaccessible, so repeated retrieval cannot be measured. The cross-document inconsistency rate is computed post hoc over the final document set by a separate LLM-judge (Claude Haiku 4.5), applied identically to all systems: it is the fraction of dependency-linked document pairs whose descriptions the judge deems contradictory.

Methods

Instead of running hundreds of independent, short-lived tasks, the authors formulate documentation as a single long-horizon agentic trajectory—often spanning over a thousand reasoning steps. While using a ReAct-style (Reason + Act) loop is standard for AI agents, MemDocAgent introduces two novel mechanisms to keep the agent organized over long stretches:

  • Dependency-Aware Traversal Guiding: Before writing a word, the system parses the code's Abstract Syntax Tree (AST) to map out dependencies (calls, imports, inheritance), collapsing circular loops along the way. Unlike prior tools that only sort function-level dependencies, this creates a strict bottom-up schedule across three granularities: components (functions/classes) are documented after their dependencies, modules (directories) after their components, and the repository overview last.
  • Memory-Guided Agentic Interaction (RepoMemory): As the agent works through its schedule, it interacts with an active, shared database called RepoMemory using four actions:
  • READ: Looks up previously generated docs or cached web searches in $O(1)$ time, avoiding raw code re-reads.
  • WRITE: Drafts structured documentation tailored to the current hierarchy level.
  • VERIFY: Acts as a quality gatekeeper. Beyond standard LLM self-evaluation, it breaks the draft into atomic claims and uses a Natural Language Inference (NLI) model to check for contradictions against already-verified dependency docs. If conflicts arise, the agent must revise the draft.
  • FINISH: Commits the verified doc to RepoMemory and clears the local working window so the agent doesn't run out of context space on the next unit.
Figure 2: Overview of MemDocAgent. (A) Dependency-aware traversal guiding first computes a traversal order that respects both dependency relations and the granularity hierarchy. Following this order, (B) memory-guided agentic interaction treats each unit as a multi-turn sub-task, where the agent interacts with RepoMemory through READ, WRITE, and VERIFY to generate di and commits the verified document upon FINISH. The local context is refreshed before moving to the next sub-task, and this loop repeats until all units are documented, producing the final hierarchical documentation.

Datasets

MemDocAgent is an inference-time framework, so no training datasets are used. For evaluation, the authors used DevEval, selecting 20 Python repositories via stratified sampling across 10 diverse domains, chosen specifically for their structural depth, complex dependencies, and unit tests.

To test practical utility, they also curated a benchmark of 564 component-level test cases from DevEval to introduce a new execution-based metric called Information Sufficiency: stripping out a function's code body and testing whether a separate LLM can regenerate passing code using only the function signature and the generated documentation.

Key Insights and Ideas

  • Memory saves time, not just context: You might expect a single-trajectory agent with verification loops to be painfully slow. Instead, because RepoMemory turns repeated raw-code inspections into instant lookups of already-verified summaries, it actually speeds up the pipeline.
  • AI docs can beat human docstrings for code reconstruction: When regenerating stripped-out code, models fed MemDocAgent's outputs outperformed models fed human-written ground-truth docstrings. Humans write terse, high-level reminders; MemDocAgent writes exhaustive, dependency-aware specifications.
  • Architecture matters for writing local code: In the code regeneration tests, feeding the coding model the module and repository-level docs alongside the function doc steadily boosted pass rates. High-level architectural context genuinely helps an AI write better low-level code that integrates cleanly.
Figure 3: Information sufficiency comparison results. Pass@1, @3 and CodeBLEU when regenerating code from each method’s documentation alone, averaged over two code generators, with the x-axis denoting the backbone used for each open-source baseline.

Key Results

Tested with both open-source (Qwen3-Coder-30B) and closed-source (GPT-5-mini) backbones against commercial tools (DeepWiki, Claude-Code) and academic baselines (RepoAgent, DocAgent, CodeWiki), MemDocAgent swept the board:

  • Zero Redundancy, High Consistency: Eliminated redundant source-file reads (0.0%) and slashed cross-document inconsistency by 75.5% (down to just 3.1%).
  • Superior Quality: Using Qwen3-Coder, it achieved 0.979 completeness, 0.916 truthfulness, and 0.690 helpfulness—beating the strongest open-source baselines by 15.9%, 9.7%, and 9.9%, respectively, while also outperforming closed-source commercial tools.
  • Code Reconstruction: Achieved up to 0.47 Pass@1 and 0.49 Pass@3 on Information Sufficiency (with GPT-5-mini), comfortably beating baselines (~0.26–0.36) and human docstrings.
  • Faster Execution: Cut retrieval time by 41% compared to previous state-of-the-art DocAgent (13.76s vs. 23.44s per document), dropping total generation time from 60.86s to 50.51s per document.

Limitations and Future Work

  • Trajectory caps and reasoning loops: To keep compute costs and context windows in check, the system enforces hard limits on steps and revisions. Occasionally, an agent gets stuck in a repetitive reasoning loop and hits this ceiling before passing verification (though this happened in only 0.08% of components). The authors suggest adaptive stopping criteria and better recovery mechanisms for future work.
  • Evaluation scope: Because DevEval only has human-curated unit tests at the function level, Information Sufficiency could only be directly executed at the component level—leaving the utility of module and repository docs to be proven indirectly rather than through full-module regeneration.

Summary updated

Abstract

Automated code documentation is essential for modern software development, providing the contextual grounding that both human developers and coding agents rely on to navigate large codebases. Existing repository-level approaches process components independently, causing redundant retrieval and conflicting descriptions across documents while producing outputs that lack hierarchical structure. Therefore, we propose MemDocAgent, a long-horizon agentic framework that generates documentation within a single, integrated context spanning the entire repository. It combines two components: (i) Dependency-Aware Traversal Guiding that predetermines a traversal order respecting dependency and granularity hierarchies; (ii) Memory-Guided Agentic Interaction, in which the agent interacts with RepoMemory, a shared memory accumulating prior work traces through read, write, and verify operations. Through an in-depth multi-criteria evaluation, MemDocAgent achieves the best performance over both open- and closed-source baselines and demonstrates practical applicability in real software development workflows.
BibTeX
@inproceedings{bae2026remember,
  author = {Suyoung Bae and Jaehoon Lee and Changkyu Choi and YunSeok Choi and Jee-Hyong Lee},
  title = {{Remember Your Trace: Memory-Guided Long-Horizon Agentic Framework for Consistent and Hierarchical Repository-Level Code Documentation}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2026},
  url = {https://openreview.net/forum?id=Ae5q87uyOE},
}
Figure