Skip to content

[Bounty: $100] Await in-flight task promises in AgentRuntime.stop() instead of busy-polling #259

Description

@talentedtee2021-ux

Context
AgentRuntime in src/runtime/agent-runtime.ts stores only task IDs in inFlightTasks, and drainInFlightTasks() busy-waits by sleeping Math.min(5, remaining) milliseconds in a loop until the deadline. This polling wastes CPU during every graceful stop and makes the wait granularity dependent on an arbitrary 5 ms tick rather than actual task completion.

Proposed Change
Track the promises of in-flight executeTask() calls (or a shared deferred) alongside the ID set, and have stop() await them with a timeout (e.g. Promise.race against the configured drainTimeoutMs) so shutdown completes as soon as work finishes without polling.

Acceptance Criteria

  • stop({ drainTimeoutMs }) returns promptly when in-flight tools finish early instead of waiting up to the full timeout in 5 ms slices.
  • Tasks that exceed drainTimeoutMs still leave stop() unblocked, and the runtime reports the stranded state.
  • getInFlightTaskCount() semantics and the drain test in tests/runtime/stress-hardening.test.ts remain green.
  • No busy-wait loop remains in agent-runtime.ts.

Suggested Label
DX

ETA: 24 hours

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions