Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions .agents/scripts/pulse-wrapper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -225,16 +225,28 @@ check_dedup() {

# Underfilled stale recovery: if the pulse process has been running long
# enough and worker pool is below target, recycle now instead of waiting
# for the full stale threshold. This prevents prolonged underfill windows
# when a pulse session is alive but not generating new workers.
local max_workers active_workers
# for the full stale threshold. Adapt timeout by underfill severity to
# recover capacity faster during deep underfill while keeping tolerance
# for minor underfill blips.
local max_workers active_workers deficit_pct adaptive_timeout
max_workers=$(get_max_workers_target)
active_workers=$(count_active_workers)
[[ "$max_workers" =~ ^[0-9]+$ ]] || max_workers=1
[[ "$active_workers" =~ ^[0-9]+$ ]] || active_workers=0
deficit_pct=0
adaptive_timeout="$PULSE_UNDERFILLED_STALE_RECOVERY_TIMEOUT"

if [[ "$active_workers" -lt "$max_workers" ]]; then
deficit_pct=$(((max_workers - active_workers) * 100 / max_workers))
if [[ "$deficit_pct" -ge 50 ]]; then
adaptive_timeout=300
elif [[ "$deficit_pct" -ge 25 ]]; then
adaptive_timeout=450
fi
Comment on lines +237 to +245
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Don't let the moderate tier relax a stricter configured timeout.

With the validator at Line 119, PULSE_UNDERFILLED_STALE_RECOVERY_TIMEOUT=300..449 is valid. Lines 241-244 then force moderate underfill to 450s, which can make recovery slower than the configured baseline instead of faster.

🛠️ Suggested fix
 	adaptive_timeout="$PULSE_UNDERFILLED_STALE_RECOVERY_TIMEOUT"

 	if [[ "$active_workers" -lt "$max_workers" ]]; then
 		deficit_pct=$(((max_workers - active_workers) * 100 / max_workers))
-		if [[ "$deficit_pct" -ge 50 ]]; then
+		if [[ "$deficit_pct" -ge 50 && "$adaptive_timeout" -gt 300 ]]; then
 			adaptive_timeout=300
-		elif [[ "$deficit_pct" -ge 25 ]]; then
+		elif [[ "$deficit_pct" -ge 25 && "$adaptive_timeout" -gt 450 ]]; then
 			adaptive_timeout=450
 		fi
 	fi
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
adaptive_timeout="$PULSE_UNDERFILLED_STALE_RECOVERY_TIMEOUT"
if [[ "$active_workers" -lt "$max_workers" ]]; then
deficit_pct=$(((max_workers - active_workers) * 100 / max_workers))
if [[ "$deficit_pct" -ge 50 ]]; then
adaptive_timeout=300
elif [[ "$deficit_pct" -ge 25 ]]; then
adaptive_timeout=450
fi
adaptive_timeout="$PULSE_UNDERFILLED_STALE_RECOVERY_TIMEOUT"
if [[ "$active_workers" -lt "$max_workers" ]]; then
deficit_pct=$(((max_workers - active_workers) * 100 / max_workers))
if [[ "$deficit_pct" -ge 50 && "$adaptive_timeout" -gt 300 ]]; then
adaptive_timeout=300
elif [[ "$deficit_pct" -ge 25 && "$adaptive_timeout" -gt 450 ]]; then
adaptive_timeout=450
fi
fi
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In @.agents/scripts/pulse-wrapper.sh around lines 237 - 245, The current logic
can raise adaptive_timeout above the configured
PULSE_UNDERFILLED_STALE_RECOVERY_TIMEOUT; change it so the computed candidate
time (300 for >=50% deficit, 450 for >=25% deficit) only shortens the configured
timeout — do not increase it. In practice, compute candidate time based on
deficit_pct (using the existing deficit_pct branches), then set adaptive_timeout
to the smaller of candidate and the existing adaptive_timeout (adaptive_timeout
= min(candidate, adaptive_timeout)), keeping the original adaptive_timeout when
the candidate would be longer; reference variables: adaptive_timeout,
PULSE_UNDERFILLED_STALE_RECOVERY_TIMEOUT, deficit_pct, max_workers,
active_workers.

fi

if [[ "$elapsed_seconds" -gt "$PULSE_UNDERFILLED_STALE_RECOVERY_TIMEOUT" && "$active_workers" -lt "$max_workers" ]]; then
echo "[pulse-wrapper] Recycling stale pulse process $old_pid early (running ${elapsed_seconds}s, underfilled ${active_workers}/${max_workers}, threshold ${PULSE_UNDERFILLED_STALE_RECOVERY_TIMEOUT}s)" >>"$LOGFILE"
if [[ "$elapsed_seconds" -gt "$adaptive_timeout" && "$active_workers" -lt "$max_workers" ]]; then
echo "[pulse-wrapper] Recycling stale pulse process $old_pid early (running ${elapsed_seconds}s, underfilled ${active_workers}/${max_workers} [${deficit_pct}%], threshold ${adaptive_timeout}s)" >>"$LOGFILE"
_kill_tree "$old_pid" || true
sleep 2
if kill -0 "$old_pid" 2>/dev/null; then
Expand Down
Loading