Making Legacy SAP Codebases Queryable: ADR Generation from ABAP
The self-documenting architecture pattern applied to enterprise SAP
The Hardest Test for Queryable Architecture
In Self-Documenting Architecture: When Code Becomes Queryable, I described a pattern where codebases become queryable in natural language - where you can ask "how do these services work together?" and get grounded answers with citations to actual code.
The examples were modern event-driven systems. Clean repos. Good documentation.
But that's not where the pain is.
The pain is in enterprise SAP shops with 8,000+ custom Z-programs accumulated over 18 years. The pain is when your two deepest ABAP experts are retiring next year and nobody documented why anything works the way it does. The pain is staring at a generic error message and spending hours tracing message flows that a senior developer could explain in thirty seconds - if they were still here.
So I tested the pattern on ABAP.
The Setup
SAP publishes a reference implementation called abap-platform-refscen-flight. It demonstrates the Unmanaged RAP (RESTful ABAP Programming) pattern - modern ABAP sitting on top of legacy function modules. It's small enough to understand, complex enough to be realistic.
I ingested it: behavior definitions, behavior pools, CDS views, function modules, the README. The system built code maps - architectural summaries that describe how the pieces connect.
Then I asked a question that any SAP developer working on a brownfield RAP implementation would eventually ask:
The Query
./scripts/outcome-ops-assist "In abap-platform-refscen-flight unmanaged scenario, why does the implementation wrap legacy function modules instead of implementing CRUD directly in the behavior pools? Generate an ADR documenting when to wrap legacy function modules versus implementing business logic directly in RAP." --advanced --topK 6
This isn't a code generation request. It's asking the system to reason about architectural decisions embedded in the implementation and produce documentation that captures the "why."
The Output
The system returned a complete ADR. Here's what it produced:
ADR: When to Wrap Legacy Function Modules vs. Implement Business Logic Directly in RAP
Context
In the abap-platform-refscen-flight unmanaged scenario, the behavior pools (/dmo/bp_travel_u, /dmo/bp_booking_u, /dmo/bp_bookingsupplement_u) wrap legacy function modules from the /DMO/FLIGHT_TRAVEL_API function group rather than implementing CRUD operations directly. According to the architectural documentation, this demonstrates "evolution from legacy ABAP to modern RAP architecture."
Decision
Wrap legacy function modules when:
MUST Wrap When:
- • Existing business logic investment - legacy function modules contain validated logic with established transactional patterns
- • Complex buffer management - legacy code manages sophisticated state patterns (the implementation uses a "Dual-Buffer Pattern" with main and temporary buffers for transaction isolation)
- • Cascade operations - business logic handles complex entity hierarchies (Delete Travel → Deletes all Bookings and BookingSupplements)
SHOULD Wrap When:
- • Reuse across multiple scenarios - the legacy layer serves both Read-Only and Unmanaged RAP patterns
- • Gradual migration strategy - enabling phased modernization without rewriting all logic
MAY Implement Directly When:
- • New business objects with no legacy code
- • Simple CRUD requiring minimal validation
- • RAP-native features needed (draft handling, side effects, determinations)
Validation Rules Preserved by Wrapping
| Entity | Constraint | Error Code |
|---|---|---|
| Travel | Agency must exist in /dmo/agency | 001 |
| Travel | Customer must exist in /dmo/customer | 002 |
| Travel | End date ≥ begin date | 015 |
| Booking | Booking date ≤ travel.begin_date | 019 |
| Booking | Flight must exist in /dmo/flight | 020 |
| BookingSupplement | Supplement ID immutable on update | 028 |
| BookingSupplement | Price/currency must update together | 029 |
Sources:
- • Code map - abap-platform-refscen-flight
- • README.md - abap-platform-refscen-flight
- • source-module-summary - abap-platform-refscen-flight
- • tests-summary - abap-platform-refscen-flight
Why This Matters
Look at what the system produced:
Grounded reasoning. Every claim cites a source. The "Dual-Buffer Pattern" reference points to actual code structure. The validation rules table lists specific error codes from the implementation.
Architectural insight. It didn't just describe what the code does. It explained why the pattern exists - legacy investment, buffer complexity, cascade behavior - and when to apply it versus alternatives.
Immediately useful documentation. A developer joining this codebase could read this ADR and understand the architectural intent in minutes. Without it, they'd spend hours reverse-engineering the same conclusions.
In What is an ADR?, I wrote that ADRs document the "why" that lives in places you can't grep: Slack threads, departed engineers, whiteboard sessions. This demonstrates extracting that "why" from the implementation itself.
The Pattern
This isn't magic. It's the same self-documenting architecture pattern, applied to ABAP:
- Ingest - Pull the repo: behavior definitions, pools, CDS views, function modules, documentation
- Embed - Convert to vectors with metadata (file type, path, relationships)
- Map - Generate code maps that summarize architectural structure
- Query - Ask natural language questions; retrieve relevant context
- Reason - Synthesize grounded answers with citations
The insight from the original post holds: when systems can explain themselves, organizations stop maintaining documentation and start maintaining understanding.
SAP codebases are notoriously hard to document. They're also notoriously valuable - decades of business logic encoded in custom programs. Making them queryable isn't just convenient. For organizations facing expert attrition, it's strategic.
The Question
The question isn't whether AI can write code. It's whether your systems expose enough context for AI to reason about them accurately.
When they do, the architecture starts explaining itself.
For the concept behind queryable architecture, see Self-Documenting Architecture: When Code Becomes Queryable.
For ADR fundamentals and bootstrapping practices, see What is an ADR?.
Enterprise Implementation
The Context Engineering methodology described in this post is open source. The production platform with autonomous agents, air-gapped deployment, and compliance features is available via enterprise engagements.
Learn More