Cleanup removes the session workspace unless CHATCLI_AGENT_KEEP_TMPDIR=true. Safe to call multiple times.
()
| 201 | // Cleanup removes the session workspace unless CHATCLI_AGENT_KEEP_TMPDIR=true. |
| 202 | // Safe to call multiple times. |
| 203 | func (ws *SessionWorkspace) Cleanup() { |
| 204 | if ws == nil { |
| 205 | return |
| 206 | } |
| 207 | |
| 208 | sessionWorkspaceMu.Lock() |
| 209 | defer sessionWorkspaceMu.Unlock() |
| 210 | |
| 211 | // Unregister first so a late tool call can't land a file into a |
| 212 | // directory we're about to delete. |
| 213 | UnregisterAuxReadPath(ws.Root) |
| 214 | engine.UnregisterAuxPath(ws.Root) |
| 215 | SetBudgetResultDir("") // fall back to default |
| 216 | fireResultDirSetters("") |
| 217 | |
| 218 | if ws.keep { |
| 219 | if ws.logger != nil { |
| 220 | ws.logger.Info("Keeping session workspace (CHATCLI_AGENT_KEEP_TMPDIR=true)", |
| 221 | zap.String("root", ws.Root)) |
| 222 | } |
| 223 | } else { |
| 224 | if err := os.RemoveAll(ws.Root); err != nil && ws.logger != nil { |
| 225 | ws.logger.Warn("Failed to remove session workspace", |
| 226 | zap.String("root", ws.Root), |
| 227 | zap.Error(err)) |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | _ = os.Unsetenv("CHATCLI_AGENT_TMPDIR") |
| 232 | |
| 233 | if sessionWorkspace == ws { |
| 234 | sessionWorkspace = nil |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | // AuxAllowedPaths returns paths the engine and read validator should treat as |
| 239 | // inside the boundary, in addition to the workspace root. Includes the |