Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
4146786
AI-3597: a scheduled call's turn refused for money fails the run
kanat Nov 23, 2026
6c2f2eb
Merge remote-tracking branch 'origin/main' into kanat/ai-3597-voice-m…
kanat Nov 23, 2026
a57d94f
AI-3597: leave the changelog to the release
kanat Nov 23, 2026
e0190c0
Merge remote-tracking branch 'origin/main' into kanat/ai-3597-voice-m…
kanat Nov 23, 2026
c5734ce
AI-3597: a call refused while money cannot be checked rings again on …
kanat Nov 23, 2026
f6b245e
AI-3597: a call whose session cannot open for money records the money…
kanat Nov 23, 2026
b0850f5
AI-3597: the scheduled-tasks skill says a call refused for now rings …
kanat Nov 23, 2026
62c4521
Merge remote-tracking branch 'origin/main' into kanat/ai-3597-voice-m…
kanat Nov 23, 2026
23106a6
Merge remote-tracking branch 'origin/main' into kanat/ai-3597-voice-m…
kanat Nov 23, 2026
d6eab92
AI-3597: a placed call refused while money cannot be checked is given…
kanat Nov 23, 2026
0cfa2a1
AI-3597: a call's turn refused again on its retry stays a refused turn
kanat Nov 23, 2026
e38cf65
AI-3597: a call's first refused turn ends the walk; any answer on a c…
kanat Nov 23, 2026
7b9031c
AI-3597: say a placed call refused while money cannot be checked is g…
kanat Nov 23, 2026
a4fb68d
Merge remote-tracking branch 'origin/main' into kanat/ai-3597-voice-m…
kanat Nov 23, 2026
82e36aa
AI-3597: a placed call is rung once: a turn refused while money canno…
kanat Nov 23, 2026
aca2a3f
AI-3597: say a placed call is rung once
kanat Nov 23, 2026
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
4 changes: 2 additions & 2 deletions .claude/skills/scheduled-tasks/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ Run `error` codes (`internal/scheduling/scheduler.go:105`):
| `no_agent_number` | The config has no SMS line to ring from | yes |
| `not_answered` | Nobody picked up within the 90 s ring | no |
| `already_called` | A call was placed for this occurrence already, and its end is not recorded; no channel after `phone` is tried. That call may have carried the task out, so the run leaves the failure streak as it is and the task moves on as after a run that did not fail (a one-shot is `completed`) | no |
| `account_suspended`, `insufficient_credits`, `system_limit_reached`, `spend_limit_reached`, `user_limit_reached` | A step the organization may not pay for was refused for money (suspended, no credit left, or a spent limit): an SMS, WhatsApp or iMessage text or a thread task's reply (`dlc.ErrUnpaid`, or its reservation), the task's session opening or being carried on, a turn the model refuses, on a text channel (an `agent.Error` in `captureReply`, which ends the walk over the channels, `refusedTurn`), in the conversation (`followUpInConversation` waits for the turn to end and reads it from the session's events; a turn that ends any other way is still `delivered`) or on a thread task (`deliverOnThread` reads it from the session's events), or its call. The run records the refusal's own code (`deliveryReason`, `scheduled_delivery.go:334`), never `channel_unreachable` or `delivery_failed`, and the router warns, naming the task | no (`unpaid`, `scheduled_delivery.go:355`) |
| `spend_check_unavailable` | The router could not check what the organization has spent, so it refused the step for now (`policy.Transient`). A turn it refused runs once more on the retry, under `st_<run>_r1`: the scheduler hands the next attempt the reason this one failed under (`deliverWithRetries`), a hand-off to the router holding the session carries it (`scheduledHandOff.Earlier`), and `replyCommand` reruns a failed reply after a money code. A conversation rerun refused as well fails the attempt under the same code (`followUpInConversation`), so the run fails once its retries are spent. A reply that failed for any other reason is read back as it was | yes (`unpaid` leaves it retryable) |
| `account_suspended`, `insufficient_credits`, `system_limit_reached`, `spend_limit_reached`, `user_limit_reached` | A step the organization may not pay for was refused for money (suspended, no credit left, or a spent limit): an SMS, WhatsApp or iMessage text or a thread task's reply (`dlc.ErrUnpaid`, or its reservation), the task's session opening or being carried on, a turn the model refuses, on a text channel (an `agent.Error` in `captureReply`, which ends the walk over the channels, `refusedTurn`), in the conversation (`followUpInConversation` waits for the turn to end and reads it from the session's events; a turn that ends any other way is still `delivered`) or on a thread task (`deliverOnThread` reads it from the session's events), or its call, placed or not: a placed call whose session cannot open fails under the refusal's code (`carryOutOnCall`); on an answered call, a turn refused before the agent answered anything hangs up (`carryOutAnswered`), and a refusal after that is a later turn's, still `delivered`. The run records the refusal's own code (`deliveryReason`, `scheduled_delivery.go:334`), never `channel_unreachable` or `delivery_failed`, and the router warns, naming the task | no (`unpaid`, `scheduled_delivery.go:355`) |
| `spend_check_unavailable` | The router could not check what the organization has spent, so it refused the step for now (`policy.Transient`). A turn it refused runs once more on the retry, under `st_<run>_r1`: the scheduler hands the next attempt the reason this one failed under (`deliverWithRetries`), a hand-off to the router holding the session carries it (`scheduledHandOff.Earlier`), and `replyCommand` reruns a failed reply after a money code. A conversation rerun refused as well fails the attempt under the same code (`followUpInConversation`), so the run fails once its retries are spent. A reply that failed for any other reason is read back as it was. A placed call is rung once: refused this way, its session or its answered turn fails the run under this code, final, with the call kept on the run (`callEnded`), so the user is not rung again | yes (`unpaid` leaves it retryable) |
| `task_paused` | On a `missed` run: a one-shot whose time came while it was paused, or an occurrence the cut-over of a move skipped whose run the source had started | — |

A failure is retried after 30 s, 2 min and 10 min (`DeliveryRetries`) only when the failure
Expand Down
32 changes: 27 additions & 5 deletions acceleration/internal/api/scheduled_delivery.go
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,8 @@ func (s *Server) openTaskSession(ctx context.Context, delivery scheduledDelivery
// is the one the user texts this agent from; the number rung from is the
// agent's own text line. An occurrence rings once: the call's session is
// taken on the run before it is placed, so neither a retry nor a worker
// taking the run over rings the user again (callEnded). A call the
// taking the run over rings the user again (callEnded): a placed call is rung
// once, whatever refuses it. A call the
// occurrence has no time left to ring and hold is missed, not placed: it
// would be hung up mid-sentence when the occurrence runs out. The call is
// taken before anything else is checked, so an attempt after the one that
Expand Down Expand Up @@ -1212,13 +1213,18 @@ func callSpec(config store.AgentConfig, delivery scheduledDelivery, agentID stri

// carryOutOnCall opens the session of a call a task placed and holds the
// call until it is over. The user has been rung by now, so a failure is
// final: another attempt would ring them again.
// final: another attempt would ring them again, even after a refusal because
// the money could not be checked (callEnded).
func (s *Server) carryOutOnCall(ctx context.Context, delivery scheduledDelivery, spec session.Spec) (string, string, error) {
created, err := s.sessions.Create(ctx, spec)
if _, unanswered := errors.AsType[*session.CallUnansweredError](err); unanswered {
return "", "", s.callEnded(ctx, delivery, spec.ID, undeliveredFinally(scheduling.ReasonNotAnswered, err))
}
if err != nil {
// A session the organization may not pay for fails under the refusal's code (unpaid).
if _, refused := moneyError(err); refused {
return "", "", s.callEnded(ctx, delivery, spec.ID, unpaid(err))
}
return "", "", s.callEnded(ctx, delivery, spec.ID, undeliveredFinally(scheduling.ReasonFailed, err))
}
if err := s.store.ScheduledTasks.SetSession(ctx, delivery.CustomerID, delivery.TaskID, created.ID()); err != nil {
Expand All @@ -1239,12 +1245,16 @@ func (s *Server) carryOutOnCall(ctx context.Context, delivery scheduledDelivery,

// callEnded records on the run the reason the call callID failed under, and
// answers that failure: a worker taking the run over after this one stopped
// finds the call placed, and records how it went rather than that it was.
// finds the call placed, and records how it went rather than that it was. The
// user has been rung, so every failure is final, a refusal because the money
// could not be checked (unpaid) included: a retry would ring nobody, and a
// second ring is the most intrusive way to try again (AI-3597).
func (s *Server) callEnded(ctx context.Context, delivery scheduledDelivery, callID string, err error) error {
var failure *scheduling.Failure
if !errors.As(err, &failure) {
return err
}
failure.Final = true
if err := s.store.ScheduledTaskRuns.SetCallReason(context.WithoutCancel(ctx), delivery.CustomerID, delivery.RunID, callID, failure.Reason); err != nil {
s.logger.Error("could not record how a scheduled task's call ended", "run", delivery.RunID, "error", err)
}
Expand Down Expand Up @@ -1299,7 +1309,12 @@ func (s *Server) holdCall(ctx context.Context, created *session.Session, deliver

// carryOutAnswered sends the task as the first turn of a call somebody
// picked up, and stays on it until it ends, or until scheduledCallLimit,
// when the agent hangs up.
// when the agent hangs up. A turn the organization may not pay for, refused
// before the agent answered anything on the call, leaves the agent silent:
// the attempt fails under the money (turnRefusal), final (callEnded), and the
// agent hangs up. A refusal because the money could not be checked is final
// too: a placed call is rung once. A refusal after the agent answered is a
// later turn's: the task was said.
func (s *Server) carryOutAnswered(ctx context.Context, created *session.Session, delivery scheduledDelivery, events <-chan session.Event) error {
// The call's turn has an id of its own: a channel tried before the call
// left its turn in the conversation under the run's, which would be taken
Expand All @@ -1310,14 +1325,21 @@ func (s *Server) carryOutAnswered(ctx context.Context, created *session.Session,
}
limit := time.NewTimer(scheduledCallLimit)
defer limit.Stop()
answered := false
for {
select {
case <-ctx.Done():
return ctx.Err()
case _, open := <-events:
case event, open := <-events:
if !open {
return nil
}
if refusal := turnRefusal(event); refusal != nil && !answered {
return refusal
}
if _, ok := event.(agent.Responded); ok {
answered = true
}
case <-limit.C:
return nil
}
Expand Down
Loading
Loading