Pi’s loop contains no product logic. Context compaction, permission prompts, API key refresh, model switching, and graceful shutdown all live in nine config hooks — transformContext, beforeToolCall, afterToolCall, prepareNextTurn, shouldStopAfterTurn, and queue pollers — that the loop calls at well-defined points. The coding-agent product wires its policies through these hooks without forking the loop, which is why the same loop can ship as a standalone npm package.
Related: