Search⌘ K
AI Features

Coding Challenge on Debugging a Failing On-Call Agent

Explore how to debug a failing on-call agent by separating context rot, buried instructions, and conflicting runbooks. Use trace hooks, one-item ablations, repeated answer checks, and unit tests to identify the true cause and build a date-based filter for stale knowledge.

We'll cover the following...

In this chapter, we built a set of tools for understanding why a context goes wrong. We learned about context rot and the position debate, wrote four checks that label a failing trace, and built hooks and an ablation harness that prove which item caused a failure.

In this challenge, we’ll use all of them on one messy trace. The on-call assistant gave a wrong first step during a checkout incident. The trace contains three things that could each explain the mistake. Our job is to find out which one actually caused it, and then write a fix that a set of unit tests can confirm.

The failing incident

Here’s what happened. At 14:05, checkout latency spiked. The team’s incident channel filled up with alert messages, and partway through, the team lead posted a clear instruction: follow the May 2026 runbook (note-1) and don’t restart Redis today. Then someone asked the assistant our running question: “Checkout latency spiked at 14:05. What changed, and what should we roll back?”

The assistant answered: “Restart the Redis cache cluster first [note-4], then roll back the latest checkout deploy.” That’s the step the 2024 runbook recommends, and it’s exactly what the team lead said not to do.

The pre-inference snapshot for this call holds these parts:

  • Incident log: 16 channel messages. Fifteen are alert-bot lines about latency and error rates.

  • Buried instruction: The team lead’s message is entry 9 of 16, in the middle of the log.

  • Retrieved notes: The top five results from search_documents(), including both note-1 (May 2026) and note-4 (2024).

  • Facts: The first two steps that extract_facts() pulled from the notes.

Let’s rebuild this snapshot with the hook and pipeline code from the previous lesson, and run the classifier on it.

incident_trace.py · Python 3.14.0
from engine_config import SYSTEM_PROMPT
from hooks import capture_trace
from note_store import search_documents
from pipeline import QUERY, extract_facts
INSTRUCTION = "14:09 team-lead: follow the May 2026 checkout runbook [note-1]. Do not restart Redis today."
LOG_LINES = [f"14:{m:02d} alert-bot: checkout p99 latency {0.9 + m / 10:.1f}s (threshold 0.8s)" for m in range(1, 9)]
LOG_LINES += [f"14:{m:02d} alert-bot: checkout error rate {m / 10:.1f}% and rising" for m in range(10, 17)]
INCIDENT_LOG = LOG_LINES[:8] + [INSTRUCTION] + LOG_LINES[8:]
def build_incident_snapshot(log, notes, query=QUERY):
block = "\n".join(f"[{n['doc_id']}] {n['text']}" for n in notes)
user = "Incident channel:\n" + "\n".join(log) + f"\n\nRetrieved notes:\n{block}\n\nQuestion: {query}"
messages = [{"role": "system", "content": SYSTEM_PROMPT}, {"role": "user", "content": user}]
return capture_trace("pre_inference", query=query, tool_call="search_documents", incident_log=log,
notes=notes, fact_values=extract_facts(notes), messages=messages)
FAILING = build_incident_snapshot(INCIDENT_LOG, search_documents(QUERY, top_k=5))
FAILING_ANSWER = "Restart the Redis cache cluster first [note-4], then roll back the latest checkout deploy."
if __name__ == "__main__":
from classify import classify_failure
print("log entries:", len(INCIDENT_LOG), "| instruction at entry", INCIDENT_LOG.index(INSTRUCTION) + 1)
print("notes:", [note["doc_id"] for note in FAILING["notes"]])
print("answer given:", FAILING_ANSWER)
print("verdict:", classify_failure(FAILING))

In the code above:

  • Line 6: We write the team lead’s instruction as one channel message.

  • Lines 7–8: We generate 15 alert-bot messages, first about latency and then about error rates.

  • Line 9: We build INCIDENT_LOG by placing the instruction after the first eight alerts, so it lands at entry 9. ...