You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix prediction response timestamp population and webhook finalization (#219)
* Fix prediction response timestamp population and webhook finalization
This PR addresses critical issues with timestamp population in prediction responses and consolidates webhook finalization logic to ensure consistent behavior across sync and async predictions.
Core Infrastructure Changes
Time Format Standardization
- Add TimeFormat constant to config package using microsecond precision with explicit timezone offset
- Ensures consistent timestamp formatting across all prediction responses and webhook payloads
PredictionResponse Structure Overhaul
- Remove deprecated WebhookURL field from PredictionResponse struct
- Add dedicated CreatedAt, StartedAt, CompletedAt timestamp fields with proper JSON tags
- Change Metrics field from any to map[string]any for type safety
- Add finalizeResponse() method to set CompletedAt timestamp and calculate predict_time metric
- Add populateFromRequest() method to copy core fields (ID, Input, CreatedAt, StartedAt) from request
Prediction Lifecycle Management
Manager API Restructuring
- Rename Manager.Predict() to Manager.PredictSync() for clarity
- Update Manager.PredictAsync() to return initial populated response instead of void
- Refactor internal predict() method to return both channel and initial response
- Add async parameter to assignReqToRunner() to control setup wait behavior
Runner Prediction Flow
- Update runner.predict() signature to return initial response populated with request fields
- Fix timestamp population in predict() method by setting CreatedAt/StartedAt on pending request
- Add proper populateFromRequest() calls in handleResponseWebhooksAndCompletion after response overwrites
- Ensure terminal responses call finalizeResponse() before sending through safeSend()
Webhook and Response Handling
- Consolidate terminal webhook logic into unified sendTerminalWebhook() method
- Fix race conditions by ensuring response finalization before webhook sending
- Update subprocess monitoring to use populateFromRequest() for failed prediction responses
- Ensure all webhook payloads include complete timestamp information
Test Infrastructure Improvements
Response Validation Framework
- Add comprehensive unit tests for finalizeResponse() covering edge cases and error conditions
- Add unit tests for populateFromRequest() method with field overwrite scenarios
- Update all integration tests to validate CreatedAt, StartedAt, CompletedAt fields
- Add ValidateTerminalWebhookResponse helper function for consistent terminal response validation
Test Harness Enhancements
- Introduce testHarnessResponse type with string Logs field for easier test assertions
- Update webhook receiver to use testHarnessResponse for simplified log comparisons
- Retrofit existing tests to use new validation patterns without changing test logic
Bug Fixes
Timestamp Population Issues
- Fix missing timestamps in sync prediction responses by ensuring populateFromRequest() calls
- Resolve timestamp loss during response overwrites in handleResponseWebhooksAndCompletion
- Ensure webhook payloads contain complete timestamp information for terminal events
Metrics and Finalization
- Fix missing predict_time metric in terminal responses by calling finalizeResponse()
- Ensure Metrics map initialization before metric calculation
- Handle time parsing errors gracefully in finalizeResponse() method
Concurrency and Error Handling
- Fix potential race conditions in webhook sending by proper response finalization order
- Ensure cleanup of failed predictions includes populated response fields
- Update error responses in runner stop scenarios to include request-derived fields
Backward Compatibility
- Maintain existing API signatures where possible
- Preserve webhook payload structure while adding timestamp fields
- Keep existing test behavior while enhancing validation capabilities
* Refactor handleResponseWebhooksAndCompletion to eliminate duplicate response handling
- Consolidate response update logic into single operation to avoid duplicate overwrites
- Extract webhook sending logic into dedicated sendStatusWebhook function
- Extract terminal completion handling into handleTerminalCompletion function
- Ensure proper lock context around populateFromRequest calls accessing pending.request
- Maintain race condition protection by using local response copy for safeSend
- Remove TODO comment about function being "a mess" - now clean and maintainable
This eliminates the scattered response object handling and reduces lock contention
while preserving all existing functionality and thread safety guarantees.
* nit fixes
// allocatePrediction reserves a slot in the runner for the prediction
386
322
func (m*Manager) allocatePrediction(runner*Runner, reqPredictionRequest) { //nolint:contextcheck // we do not use this context for the prediction see note below
0 commit comments