| 283 | } |
| 284 | |
| 285 | func startCompactionDebugRun( |
| 286 | ctx context.Context, |
| 287 | options CompactionOptions, |
| 288 | ) (context.Context, func(error)) { |
| 289 | if options.DebugSvc == nil || options.ChatID == uuid.Nil { |
| 290 | return ctx, func(error) {} |
| 291 | } |
| 292 | |
| 293 | parentRun, ok := chatdebug.RunFromContext(ctx) |
| 294 | if !ok { |
| 295 | return ctx, func(error) {} |
| 296 | } |
| 297 | |
| 298 | historyTipMessageID := options.HistoryTipMessageID |
| 299 | if historyTipMessageID == 0 { |
| 300 | historyTipMessageID = parentRun.HistoryTipMessageID |
| 301 | } |
| 302 | |
| 303 | // Use a separate short-lived context for the debug insert so a |
| 304 | // slow or locked DB cannot consume the compaction timeout budget |
| 305 | // and turn debug slowness into a compaction failure via |
| 306 | // model.Generate hitting a deadline exceeded. Detached from the |
| 307 | // parent so cancellation of the compaction run still lets the |
| 308 | // insert reach a terminal state, matching the best-effort |
| 309 | // contract of debug instrumentation. |
| 310 | createRunCtx, createRunCancel := context.WithTimeout( |
| 311 | context.WithoutCancel(ctx), compactionDebugCreateRunTimeout, |
| 312 | ) |
| 313 | run, err := options.DebugSvc.CreateRun(createRunCtx, chatdebug.CreateRunParams{ |
| 314 | ChatID: options.ChatID, |
| 315 | RootChatID: parentRun.RootChatID, |
| 316 | ParentChatID: parentRun.ParentChatID, |
| 317 | ModelConfigID: parentRun.ModelConfigID, |
| 318 | TriggerMessageID: parentRun.TriggerMessageID, |
| 319 | HistoryTipMessageID: historyTipMessageID, |
| 320 | Kind: chatdebug.KindCompaction, |
| 321 | Status: chatdebug.StatusInProgress, |
| 322 | Provider: parentRun.Provider, |
| 323 | Model: parentRun.Model, |
| 324 | }) |
| 325 | createRunCancel() |
| 326 | if err != nil { |
| 327 | // Debug instrumentation must not surface as a compaction failure. |
| 328 | return ctx, func(error) {} |
| 329 | } |
| 330 | |
| 331 | compactionCtx := chatdebug.ContextWithRun(ctx, &chatdebug.RunContext{ |
| 332 | RunID: run.ID, |
| 333 | ChatID: options.ChatID, |
| 334 | RootChatID: parentRun.RootChatID, |
| 335 | ParentChatID: parentRun.ParentChatID, |
| 336 | ModelConfigID: parentRun.ModelConfigID, |
| 337 | TriggerMessageID: parentRun.TriggerMessageID, |
| 338 | HistoryTipMessageID: historyTipMessageID, |
| 339 | Kind: chatdebug.KindCompaction, |
| 340 | Provider: parentRun.Provider, |
| 341 | Model: parentRun.Model, |
| 342 | }) |