Comment Agents are good at finding bugs, not writing code (Score 1) 32
I've had no success getting Agents to help write code for SQLite. All the code I've asked Claude to write for me is slop that didn't work or worked poorly and never got committed. On the flip side, I do ask Claude to review the code that I write prior to committing, and it is good at that, often finding serious oversights that I had missed.
This observation, that agents are good at code review but bad at writing original code, seems to carry through into AI-generated bug reports coming into SQLite from third parties. The bug reports are generally good, but the suggested fixes not so much.
One report from about a month ago illustrates this. The median() aggregate function in SQLite is implemented by collecting the input values into an array of doubles, using quicksort to put them in order, and return the one in the middle. The bug report showed a carefully devised sequence of 1 million inputs that caused pathological O(N*N) behavior in the quicksort algorithm, which caused recursion to go too deep and blow out the CPU stack. The AI's suggested fix: Add a depth parameter to the quicksort algorithm and fail over to a slower sorting algorithm if the recursion goes too deep. The correct solution (originally recognized in the 1975 by Robert Sedgewick, but unknown to me before this issue arose) was to only call quicksort recursively on the smaller of the two partitions, and sort the larger partition using a loop (tail recursion). If recursion only occurs on a partition that is half the size of the original or smaller, recursion depth is limited to LogN. That turned out to be trivial to implement, and is faster and uses less code than the AI-suggested approach. Furthermore, after seeing Sedgewick's tail recursion idea, Dan Kennedy recognized that we don't actually need to sort both partitions at each step of the algorithm, but only the partition that contains the median. Dan's observation more than doubled performance. Hence by ignoring the AI's suggested fix, we more than doubled the performance of the median() function in SQLite.
Before you ask: The median() function did originally use the standard library qsort(), but I changed that into a hand-coded quicksort some time ago because the hand-written variant did not require a callback for each comparison, and was thus way faster.