Skip to content

fix: handle bare numeric and negative durations in Wait action handler#412

Open
nankingjing wants to merge 3 commits into
zai-org:mainfrom
nankingjing:fix-wait-action-duration
Open

fix: handle bare numeric and negative durations in Wait action handler#412
nankingjing wants to merge 3 commits into
zai-org:mainfrom
nankingjing:fix-wait-action-duration

Conversation

@nankingjing

@nankingjing nankingjing commented Jul 10, 2026

Copy link
Copy Markdown

Summary

Fixes a bug in ActionHandler._handle_wait where the Wait action silently failed when the model returned a bare numeric duration (int/float) instead of a string like "3 seconds".

Root cause

_handle_wait always calls .replace("seconds", "") on action.get("duration"). When a model emits do(action="Wait", duration=3), ast.literal_eval returns the integer 3. Calling .replace() on an int raises AttributeError, which the except ValueError clause does not catch, so the action falls through to execute()'s generic handler and reports success=False with Action failed: 'int' object has no attribute 'replace' — no actual wait happens. A negative duration string (e.g. "-5 seconds") is a second latent defect: it parses to -5.0 and time.sleep(-5.0) raises ValueError.

Fix (phone_agent/actions/handler.py)

  • Check isinstance(duration_raw, (int, float)) before calling string methods; convert numeric input to float directly.
  • Broaden the exception clause to except (ValueError, TypeError).
  • Clamp negative durations to 1.0 so time.sleep cannot raise.

The documented happy path duration="N seconds" is unchanged.

Test (tests/test_wait_action.py)

Seven deterministic tests driving ActionHandler.execute, covering: string with unit, string without unit, missing key (default), bare int (the bug), bare float, unparseable fallback, and negative clamp. time.sleep is patched — no device or network access.

Verification

@nankingjing

Copy link
Copy Markdown
Author

Reviewed — isinstance check before .replace and the negative clamp are both correct, and the tests cover int/float/string/missing/negative. Ready for review.

NaN and infinite durations would be accepted by time.sleep but produce
unexpected behaviour (no wait or an indefinite hang). Clamp them to the
default 1.0 second alongside negative values, and add regression tests.
@nankingjing

Copy link
Copy Markdown
Author

Pushed an additional commit to clamp non-finite durations (NaN/inf) to the default 1 s. time.sleep accepts them as valid arguments, but NaN produces no wait and inf would hang the agent. Added regression tests for NaN, inf, and zero-second waits.

@nankingjing
nankingjing force-pushed the fix-wait-action-duration branch from a462f03 to 46f1012 Compare July 16, 2026 14:34
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