Work effectively with AI coding agents. Understand them as language models running in a tool loop with limited context, which is why they guess, game the visible signal and report confidently. Recognise the shortcut and correctness tells in their code across Python, TypeScript/JavaScript, Rust, Java and Go, and know the damage each one does later. Verify changes instead of trusting them, review diffs fast for quality and security, judge when work is truly finished, and choose, design and steer tasks so agents can finish them well.
Sleeps, retries and longer timeouts as 'fixes'
card 5 of 6
Sleep versus waiting on the event (Go)
builds on: Patching the symptom instead of the cause, Race conditions and interleavings
The agent first tried 10 ms, then 200 ms when CI still failed. The tell: `time.Sleep` sits between starting work and reading its result, and nothing ties 200 ms to the refresh finishing. On a loaded runner the refresh takes 250 ms: red. BETTER waits on the event itself. In Go, closing a channel is ordered before a receive that sees the close. So the refresh's writes are visible to the check.
// Go
// BAD: the agent's fix
c.StartRefresh(ctx)
time.Sleep(200 * time.Millisecond)
if c.Len() != 3 {
t.Fatalf("len = %d", c.Len())
}
// BETTER
done := c.StartRefresh(ctx) // returns a channel closed when refresh ends
<-done
if c.Len() != 3 {
t.Fatalf("len = %d", c.Len())
}
Terms on this card
- CI
- Continuous integration: a server that builds the code and runs the tests automatically on every push or pull request.
- tell
- An observable sign in a diff, transcript or summary that suggests a shortcut or an error.
- channel
- A Go (or Rust) pipe between concurrent tasks; sending, receiving or closing it also orders their steps.
- red
- A test or check that is failing. Green means passing.
- time.Sleep
- A Go function that pauses the current goroutine for a given duration. It does not synchronize anything.
Lesson goal: Recognise a race 'fixed' with a sleep, a flaky call wrapped in a retry loop, or a raised timeout. Explain why these pass locally and in CI but fail under load or on slower machines, and what a real ordering fix looks like. ·
the whole lesson