Skip to content

Fix race condition in task result caching and wire result streamer#1

Merged
chaholl merged 4 commits into
mainfrom
fix/race-condition-result-caching
Nov 25, 2025
Merged

Fix race condition in task result caching and wire result streamer#1
chaholl merged 4 commits into
mainfrom
fix/race-condition-result-caching

Conversation

@chaholl

@chaholl chaholl commented Nov 25, 2025

Copy link
Copy Markdown
Contributor

Pull Request Summary

Type of Change

  • �� Bug fix (non-breaking change that fixes an issue)
  • ✨ New feature (non-breaking change that adds functionality)
  • 🧪 Adding or improving tests

Component(s) Affected

  • Coordinator
  • Worker

Description

What does this PR do?
Fixes a race condition where tasks were marked as 'completed' before their results were cached, and wires the result streamer to the task queue to enable proper result publishing.

Why is this change needed?
E2E tests were failing with "Task completed but result not available in cache" errors. The root cause was:

  1. Task state was updated to 'completed' before the result was cached
  2. Result streamer was never connected to the task queue (nil at runtime)

Changes Made

Code Changes

  • Reordered handleTaskSuccess() to cache result BEFORE updating task state
  • Created taskQueueResultStreamerAdapter to bridge coordinator and taskqueue interfaces
  • Added SetResultStreamer() call in server initialization
  • Added retry logic in task.get_result handler for race window mitigation
  • Completed Phase 3: Artifact Management (artifact.get MCP tool)
  • Added comprehensive test coverage (97.2% for artifact handler)

Testing

Test Coverage

  • I have added unit tests for my changes
  • Existing tests pass with my changes
  • I have tested this manually

Test Results

  • All automated tests pass
  • E2E tests pass (including artifact creation and retrieval)
  • 97.2% test coverage on artifact.get handler

Documentation

Documentation Updates

  • I have updated relevant documentation
  • Added artifact.get to MCP tools reference
  • Updated README and API documentation

Checklist

  • My code follows the project's coding standards
  • I have performed a self-review of my code
  • I have added tests that prove my fix is effective
  • New and existing unit tests pass locally with my changes

- Fix race condition where tasks marked 'completed' before result cached
  - Reordered handleTaskSuccess() to publish/cache result BEFORE updating state
  - Prevents clients from seeing 'completed' status before result available

- Wire result streamer to task queue
  - Created taskQueueResultStreamerAdapter to bridge coordinator and taskqueue interfaces
  - Added SetResultStreamer() call in server initialization
  - Converts between coordinator and taskqueue types (TaskResult, TaskProgress, TaskResultNotification)

- Add retry logic in task.get_result handler
  - Retries cache lookup for completed/failed tasks to handle remaining race window
  - Returns clear error if result genuinely missing

- Complete Phase 3: Artifact Management implementation
  - Added artifact.get MCP tool (coordinator handler and worker implementation)
  - Created gRPC Artifact Service for registration and listing
  - Updated documentation with artifact.get tool reference
  - Added E2E test for artifact creation and retrieval

All tests passing including E2E integration tests.
- Added 8 test cases covering all code paths in get_handler.go
- Tests cover: no workers, missing params, success case, session creation,
  error handling for enqueue/get task failures, and context session ID
- Achieved 97.2% test coverage on artifact handler
- Fixed config tools test to include new artifact.get tool (expected 9 tools instead of 8)

Coverage breakdown:
- artifact.get handler: 97.2% coverage
- All new code paths tested with proper mocks
- Follows existing test patterns from python/task handlers
- Add tests for taskQueueResultStreamerAdapter in coordinator
- Add TestTaskExecutorArtifactGet for artifact.get execution path
- Add TestHandleArtifactGetTyped with 3 test cases covering success and error paths
- Artifact handler tests achieve comprehensive coverage of new code
@chaholl
chaholl merged commit 4c8b5d1 into main Nov 25, 2025
4 checks passed
@chaholl
chaholl deleted the fix/race-condition-result-caching branch November 25, 2025 20:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant