The UI thread consumes output in batches. Taking ownership of w->out under * the mutex keeps terminal writes outside the lock while preserving order. */
| 8218 | /* Perform the full compaction exchange and rebuild the live DS4 session from |
| 8219 | * the compacted transcript. Any failure invalidates live KV because the model |
| 8220 | * may have just seen private compaction instructions that are not part of the |
| 8221 | * real conversation. */ |
| 8222 | static bool agent_worker_compact(agent_worker *w, const char *reason, |
| 8223 | char *err, size_t err_len) { |
| 8224 | const int bottom = w->transcript.len; |
| 8225 | if (bottom <= 0) return true; |
| 8226 | |
| 8227 | ds4_tokens sys = {0}; |
| 8228 | agent_worker_build_system_tokens(w, &sys); |
| 8229 | if (bottom <= sys.len) { |
| 8230 | ds4_tokens_free(&sys); |
| 8231 | return true; |
| 8232 | } |
| 8233 | |
| 8234 | agent_publishf(w, |
| 8235 | "\n\x1b[1;95mCOMPACTING\x1b[0m %s: summarizing durable task state\n\x1b[38;5;245m", |
| 8236 | reason && reason[0] ? reason : "context"); |
| 8237 | |
| 8238 | char *prompt_text = agent_compact_make_prompt(reason); |
no test coverage detected