Compress turns (/compress, /compact) never emit the "turn complete" log line — result events look orphaned to log-based monitoring
AI disclosure: This issue was drafted and filed by an AI agent (Claude Code) on behalf of the reporter, based on a real production incident. The investigation and proposed fix below were agent-produced and operator-reviewed.
Summary
/compress (alias /compact) runs through a dedicated event loop (cmdCompress → runCompress → processCompressEvents), which consumes the turn's final type=result event and returns without ever logging msg="turn complete" — that line is emitted only by processInteractiveEvents (sole occurrence of "turn complete" in core/engine.go). The compress flow contains no other completion log either: zero slog.Info/slog.Warn between runCompress and the end of processCompressEvents.
Why it matters
turn complete is the engine's own vocabulary for "a turn finished and was consumed". Log-based monitors that pair the adapter's claudeSession: event type=result line with a subsequent turn complete (the natural pairing for turn-finality tracking) will treat every finished compress turn as an orphaned result:
- a healthy
/compresscompletes (~1 min on a large session),type=resultis logged,turn completenever comes - if the instance stays quiet afterwards (no other chats active), the "orphan result" age grows without bound
- we hit exactly this in production today: a health monitor false-positived and restarted a healthy gateway ~26 min after a successful manual
/compact(result delivered, context compacted, session lock released — everything verified healthy after the fact)
Auto-triggered compaction (runCompress(..., auto=true)) has the same shape.
Reproduction
- Start a session with some history, send
/compact(or/compress) - In the log,
claudeSession: event ... type=resultappears when the CLI finishes compacting (DEBUG level,agent/claudecode/session.gohandleReadLoopLine) - No
msg="turn complete"is ever emitted for that turn - Any result-pairing monitor now sees an orphan that never resolves
Suggested fix
Log a turn complete line from processCompressEvents on every exit path — a defer at the top of the function survives future edits:
defer func() {
slog.Info("turn complete",
"session", session.ID,
"agent_session", session.GetAgentSessionID(),
"compress", true,
"auto", auto,
"turn_duration", time.Since(started),
)
}()Properties that make this safe:
- an "early" complete line is harmless to result-pairing monitors: it only clears results logged before it
- the
compress=truefield gives monitors/metrics an explicit filter hook - one
defercovers manual and auto compaction and every terminal path (result / error / EOF / idle-timeout / stop / shutdown) - log volume: one INFO line per compress
We have been running this in production since today, with tests pinning the result / error / idle-timeout exit paths (plus a mutation check that removing the defer fails all three), and verified the completion line lands ~0.6s after the result event on a real /compact.
Happy to send a PR if this approach sounds right.
Source: chenhg5/cc-connect