The 15-week technical battle of LogiFlow — a company waking up from the illusion created by artificial intelligence and returning to real engineering.
The Story
9:00 AM. The War Room atmosphere was as solemn as a funeral home. The team had gathered around Emre — the lead coder of AI's pride and joy, the route-optimizer-v2 service.
"The code is clean," said Emre. "Variable names are meaningful, JSDoc comments are complete, error handling is everywhere. I prompted AI to write 'production-ready, robust, clean code.'"
Staff Engineer Defne set her coffee mug down on the table.
"Emre, this isn't an infrastructure team problem. The problem is that what you think is 'business logic' is actually Infrastructure Soup."
Technical Autopsy
The logistics industry's most critical rule was this: If a truck carries hazardous materials (Hazmat), it cannot pass through tunnels and is legally required to take a 2-hour rest break every 4 hours.
// "Clean" But Architecturally Rotten Code Generated by AI
export async function calculateHazmatRouteAndEta(
truckId: string, destination: Coordinates
) {
// 1. Fetch truck info from database (Infra)
const truck = await prisma.truck.findUnique({
where: { id: truckId }
});
// 2. Fetch real-time tunnel restrictions from Redis (Infra)
const tunnelRestrictions = await redisClient.get(
`region:${truck.region}:tunnels`
);
// 3. Get route from Map API (External Infra)
const routeData = await mapboxApi.getRoute(
truck.currentLocation, destination
);
// 4. BUSINESS LOGIC — sprinkled in between
let finalEta = routeData.duration;
if (truck.isHazmat) {
const mandatoryBreaks = Math.floor(
routeData.duration / (4 * 60)
);
finalEta += (mandatoryBreaks * 2 * 60);
}
// 5. Save to database (Infra)
await prisma.route.create({
data: { truckId, eta: finalEta }
});
// 6. Invalidate cache (Infra)
await redisClient.del(`truck:${truckId}:current_route`);
return { success: true, eta: finalEta };
}
Defne turned to the whiteboard. "Imagine you want to test this code. You just want a simple Unit Test that asks: 'Is 2 hours added to the Hazmat truck's ETA?' What do we need to do?"
Emre answered: "We'll mock Prisma. We'll mock the Redis client. We'll intercept Mapbox API with nock."
"If you need to mock 3 different external dependencies to test a business rule, that code is untestable."
The Surgery: Hexagonal Architecture
Defne drew three concentric circles on the whiteboard:
- Innermost Circle (Domain): Pure business rules. Doesn't know what a database or HTTP is.
- Middle Circle (Application / Use Cases): Orchestrates business workflows. Uses "Ports."
- Outermost Circle (Adapters): Prisma, Redis, Mapbox. These are just Adapters.
1. Domain — Pure Business Logic
// domain/RoutingPolicy.ts
export interface RouteCalculation {
durationInMinutes: number;
}
export interface TruckProfile {
isHazmat: boolean;
}
// Pure Business Rule: Needs zero mocks to test!
export function calculateFinalEta(
route: RouteCalculation, truck: TruckProfile
): number {
let finalEta = route.durationInMinutes;
if (truck.isHazmat) {
const breaks = Math.floor(route.durationInMinutes / 240);
finalEta += (breaks * 120);
}
return finalEta;
}
2. Ports — Interfaces
// ports/Repositories.ts
export interface TruckRepository {
findById(truckId: string): Promise<TruckProfile | null>;
}
export interface MapRoutingService {
getRoute(
from: Coordinates, to: Coordinates
): Promise<RouteCalculation>;
}
3. Application Use Case — Orchestrator
// application/CalculateHazmatRouteUseCase.ts
export class CalculateHazmatRouteUseCase {
constructor(
private truckRepo: TruckRepository,
private mapService: MapRoutingService,
private routeRepo: RouteRepository,
private restrictionService: RestrictionService
) {}
async execute(
truckId: string, destination: Coordinates
): Promise<number> {
const truck = await this.truckRepo.findById(truckId);
if (!truck) throw new DomainError('Truck not found');
const restrictedZones = truck.isHazmat
? await this.restrictionService.getTunnelRestrictions(
truck.region
)
: [];
const rawRoute = await this.mapService.getRoute(
truck.location, destination, restrictedZones
);
// Pure domain call
const finalEta = calculateFinalEta(rawRoute, truck);
await this.routeRepo.saveRoute(truckId, finalEta);
return finalEta;
}
}
Lessons from Episode 2
1. Infrastructure Soup: AI tends to embed business logic (Domain) inside database queries and API calls for quick results.
2. Mocking Cost: If you need to mock more than 3 external dependencies to test a business rule, Dependency Inversion is missing.
3. Hexagonal Architecture: AI's weakest point is "boundaries." Keeping the Domain layer as pure functions is the greatest value a human can add to a system.
This is Episode 2 of the "Back to Code" series. Next up: Episode 3 — The Lost Craft: TDD and "Intent."
Series: back.to.code · 2026
Top comments (0)