Skip to content

Answer a sentence or webhook request right away in task.wait_until() - #936

Closed
craigbarratt wants to merge 1 commit into
masterfrom
wait-until-request-answer
Closed

craigbarratt wants to merge 1 commit into
masterfrom
wait-until-request-answer

Conversation

@craigbarratt

@craigbarratt craigbarratt commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

task.wait_until(sentence_trigger=...), new with #869, wakes the waiting task with the matched slots, but nothing answers the sentence. The voice assistant then waits for the sentence trigger's timeout, which is the same as wait_until's timeout=, so timeout=600 leaves it waiting until Home Assistant gives up. After that, pyscript logs sentence_trigger ... timed out and the assistant says "Done". task.wait_until(webhook_handler=...) has the same problem: the HTTP request waits for the timeout and gets a 504.

No function runs to give a response, so WaitUntilDecoratorManager.dispatch() now answers the request as soon as it wakes the waiter, as if a function had returned None: the default spoken response, or an empty 200. Only the trigger that fired answers. Other triggers don't answer requests, so they're unchanged.

  • Tests: test_sentence_trigger_wait_until and test_webhook_handler_wait_until check that the waiter gets the slots or payload and the request is answered within a second. Both fail on master.
  • Docs: the task.wait_until() argument list now includes sentence_trigger and webhook_handler and says how they're answered.

@dmamelin, you mentioned planning a fix for this; does this match what you had in mind? @contagon, does an immediate default answer make sense for wait_until(sentence_trigger=...), or would you rather it be rejected in wait_until for now?

🤖 Generated with Claude Code

task.wait_until(sentence_trigger=...) or (webhook_handler=...) woke the
waiting task, but nothing answered the request, so the voice assistant
or HTTP client waited for the trigger's timeout (which is the same as
wait_until's timeout), and then got "timed out" handling. No function
runs to give a response, so answer as if a function had returned None:
the default spoken response, or an empty 200.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dmamelin

dmamelin commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

@craigbarratt, thanks for picking this up. Yes, I was planning to fix it, but differently: rather than making request/response triggers work in task.wait_until(), I wanted them rejected there.

I don't think @sentence_trigger, @webhook_handler and similar decorators make sense in wait_until. Their whole point is that the function's result becomes the answer, and wait_until has no way to give one. Answering with a fixed default means the user can never control the response, so a function with @sentence_trigger / @webhook_handler is the right tool for that anyway. webhook_trigger is different: it answers right away without waiting for a result, so it fits wait_until and stays supported.

That they work at all is a side effect: wait_until takes any TriggerDecorator by name. It isn't in the docs or the stubs. Both list only state/time/event/mqtt/webhook_trigger. As far as I can tell, it's only been noticed by AI tools reading the code, not by users, so nobody should depend on it yet, and rejecting it now breaks nothing. Documenting these triggers, as this PR does, would make them public API we'd have to keep.

What I had in mind is to make it explicit: a WaitUntilDecorator mixin (or a flag on the class) for the trigger decorators that wait_until supports. wait_until builds only those and raises a clear error for the rest, e.g. task.wait_until() doesn't support sentence_trigger. That also stops new trigger decorators from showing up in wait_until by accident.

One more thing with the current approach: a second request that arrives while the first one is being dispatched (e.g. the webhook body is still being read) finds the future already done and isn't answered, so it waits for the full timeout and gets a 504. Request/response triggers just don't fit one-shot waiting well.

That's how I see it, but I'm open to discussing it. Happy to go another way if you or @contagon see a good use for these triggers in wait_until.

@craigbarratt

Copy link
Copy Markdown
Member Author

@dmamelin I find your reasoning sound, and I'm happy to reject this PR in favor of your approach. Let's wait for @contagon to weigh in before proceeding.

I'm trying to close out remaining issues so I can do a new release, since it has so many changes already. If we simply reject them now, we can add your proposed behavior later.

@contagon

contagon commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

I'm more on the fence if it should be supported in some form or another. I think there are some contexts where sentence_trigger may be useful in wait_until. Something like this for examples,

...first half of automation

task.wait_until(sentence_trigger="ok, proceed to next step")

...second half of automation

where you could allow an automation to continue based on a voice command. It probably makes less sense for webhook_handler, as without the response it's basically just webhook_trigger. There's just no similar alternative to sentence_trigger.

This may not be a typical usecase though, so if we don't want to support it long-term that makes sense to me as well. I imagine there's workarounds that could be done if we choose not to.

@dmamelin

dmamelin commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

@contagon, thanks, that's a valuable point, and it brought back an old idea of mine. When I first saw #672, I thought it would be great to have a flow API for this kind of trigger, something like:

def choose_name(flow):
    ...
    return f"Turned on {flow.sentence}"

def confirm(flow):
    if flow.sentence == "yes":
        name = flow.prev.slots.name
        ...
        return f"Turned on {name}"
    else:
        return SentenceFlow("What should I turn on then?", choose_name)

@sentence_trigger("turn on {name}")
def voice_on(sentence, name):
    return SentenceFlow(f"Turn on {name}?", confirm)

The function answers with a question, and the user's next sentence goes to the callback. The callback returns either the final response or another SentenceFlow, so a dialog can go as deep as needed. To me this is much clearer, more convenient and more flexible than task.wait_until(): the conversation stays in one place, each step can give its own response, and it could also work for other request/response triggers, for example a future telegram_trigger.

I never worked out the details, because it wasn't possible with the legacy trigger system, and I moved on to building DecoratorManager instead. So I don't know yet whether it would actually work this way. It's just an idea for now.

@dmamelin

dmamelin commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

@craigbarratt, I agree, there are a lot of changes already and it's time for a release.

I can't provide my task.wait_until change right now because I'm traveling. I should be back to active work in about a week. We could hold the release until my PR, but I don't think it's a blocker: sentence_trigger and webhook_handler aren't documented for wait_until, so their current behavior there is undocumented and nobody should rely on it.

I also remember the outdated error messages in DecoratorManager. They don't block the release either, it's just cosmetics.

@craigbarratt

Copy link
Copy Markdown
Member Author

Thanks @dmamelin and @contagon. Closing this in favor of #938, which tracks it after the release. For 3.0.0, sentence_trigger and webhook_handler in task.wait_until() are unsupported, and the release notes will say the behavior will likely change.

@craigbarratt
craigbarratt deleted the wait-until-request-answer branch December 2, 2026 16:39
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.

3 participants