All courses

Long sessions: lost instructions and drift

Explain how a filling context weakens attention to early constraints, how summarising or compacting the context can drop them, and how a pile of failed attempts pollutes later reasoning. Recognise drift when the agent starts violating rules it followed at the start.

Builds on: What the agent sees: its context

Read it card by card Start the whole course

It obeyed your rules, until it didn't

Hour one: you told the agent that every DB call takes `ctx`, errors get wrapped with `%w`, and `log.Fatal` is never used. Its first changes follow all three. Hour three comes after many test runs, a few failed fixes and a 'context compacted' notice. Now the diff breaks rules the agent obeyed earlier, and its summary still says it followed project conventions.

Dilution, compaction, pollution

**Dilution:** your turn-1 rule is now a few lines among tens of thousands of tokens of files and test output. As context grows, models follow such instructions less reliably, especially when recent material (a failing test, the current file) pulls another way. **Compaction:** when the context fills, the tool swaps old turns for a summary. The summary keeps the task but can drop 'minor' rules. **Pollution:** failed attempts stay in context, and the agent reasons from them. **Later cost:** rule breaks land quietly in a change you trusted.

Tells of drift and what to do

**Tells:** - A rule followed in early files is broken in later ones. - Conventions shift mid-session: a different error style, logger or library. - Behaviour changes after a 'context compacted' notice. - The agent retries a fix it already tried, or revives an approach you rejected. **Later cost:** each tell means rule breaks your tests don't check, merged under a summary that claims compliance. **Response:** check every file touched in the long stretch against your original rules. Restate the rules. Put lasting ones in an instruction file. If failures pile up, start fresh with a short brief.

Turn 60 of a sync bug hunt

At turn 5 the agent wrote `QueryContext(ctx, q)` and wrapped errors with `%w`, as your first message asked. By turn 60 it has made three failed fixes and the context has been compacted once. It now rewrites `Sync` (next card). The build is green, and the tests never trigger a DB error, so nothing turns red. The tell is the contrast with the agent's own earlier work. **Better:** point to the rules, have it restore all three, and add them to the instruction file.

The rewrite that broke all three rules

The removed lines obeyed your rules, and the added ones break all three. `Query` drops `ctx`, so cancellation and deadlines no longer reach the database. The error is no longer wrapped with `%w` and returned, so callers never get to handle it. `log.Fatal` logs and then calls `os.Exit(1)`, so deferred cleanup never runs and the whole process exits. **Later cost:** one transient DB error during a sync takes the service down, and a hung query can no longer be cancelled.
// Go
--- a/store/sync.go
+++ b/store/sync.go
@@ -12,4 +12,4 @@ func (s *Store) Sync(
-	rows, err := s.db.QueryContext(ctx, q)
-	if err != nil {
-		return fmt.Errorf("sync: %w", err)
-	}
+	rows, err := s.db.Query(q)
+	if err != nil {
+		log.Fatal(err)
+	}

Long sessions drift

As a session grows, early rules get diluted, compaction can drop them, and failed attempts steer later reasoning. When the agent breaks a rule it obeyed at the start, re-check everything since then. Restate the rule somewhere it persists, and consider a fresh session with a short brief.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.