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
Context
AgentRuntimeinsrc/runtime/agent-runtime.tsstores only task IDs ininFlightTasks, anddrainInFlightTasks()busy-waits by sleepingMath.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 havestop()await them with a timeout (e.g.Promise.raceagainst the configureddrainTimeoutMs) 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.drainTimeoutMsstill leavestop()unblocked, and the runtime reports the stranded state.getInFlightTaskCount()semantics and the drain test intests/runtime/stress-hardening.test.tsremain green.agent-runtime.ts.Suggested Label
DX
ETA: 24 hours