Chat: the server continues a usage-refused Claude chat at the reset
When Claude Code refuses an operator chat's turn for a usage limit and the server does not continue it on another account, the server now queues a durable prompt (auto-continue-<refusal id>, sent as Pipelines (automatic)) for a minute after the reset: the refused operator message when the turn did no work, else "continue". The reset comes from the refusal's text, else the rejected rate_limit_event. Refusals of turns Claude Code started itself are handled too, with no client attached; a restarted server or a device scheduling the same refusal adds nothing. The choice is kept per Claude home beside auto-switch (or by the server for chats without one), read and set with chat_auto_continue, on by default. The app's limit banner now finds the refusal as the latest turn, ignoring notices, agent reports and activity rows after it, reads and writes the server's choice, and only schedules its own prompt for older servers. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -19,16 +19,17 @@ import (
|
||||
// usage left (and the operator left automatic switching on), the server
|
||||
// switches the home to that account and queues a prompt, marked as sent
|
||||
// automatically, that asks the agent to check it is still on track before it
|
||||
// continues. It works with no client attached. The app's own
|
||||
// auto-continue-at-reset prompt for that refusal is taken back out. When no
|
||||
// account has usage left nothing more happens, and auto-continue at the reset
|
||||
// still applies.
|
||||
// continues. It works with no client attached. Any auto-continue-at-reset
|
||||
// prompt for that refusal is taken back out. When no account has usage left
|
||||
// (or switching is off), the chat continues at the reset instead
|
||||
// (chat_auto_continue.go).
|
||||
|
||||
const (
|
||||
// chatAutoSwitchPrefix starts the operation id of the continue prompt.
|
||||
chatAutoSwitchPrefix = "auto-switch-"
|
||||
// chatAutoContinuePrefix starts the app's auto-continue-at-reset prompt
|
||||
// ids ("auto-continue-<refusal message id>").
|
||||
// chatAutoContinuePrefix starts the auto-continue-at-reset prompt ids
|
||||
// ("auto-continue-<refusal message id>"), the server's and an older
|
||||
// app's alike.
|
||||
chatAutoContinuePrefix = "auto-continue-"
|
||||
// chatAutoSwitchClient is the sender label of the continue prompt.
|
||||
chatAutoSwitchClient = "Pipelines (automatic)"
|
||||
@@ -129,19 +130,22 @@ func (h *chatHub) continueOnAnotherAccount(broker chatBroker, refusal claude.Usa
|
||||
}
|
||||
switcher, ok := broker.(chatAutoSwitcher)
|
||||
if !ok {
|
||||
h.continueAtReset(ctx, broker, refusal)
|
||||
return
|
||||
}
|
||||
result, err := switcher.AutoSwitchAccount(ctx, refusal)
|
||||
if err != nil {
|
||||
if !errors.Is(err, claude.ErrAutoSwitchOff) && !errors.Is(err, claude.ErrNoConfigDir) {
|
||||
if !errors.Is(err, claude.ErrAutoSwitchOff) && !errors.Is(err, claude.ErrNoConfigDir) &&
|
||||
!errors.Is(err, claude.ErrNoAccountLeft) && !errors.Is(err, errNotClaudeChat) {
|
||||
log.Printf("claude auto-switch session=%s: %v", refusal.SessionID, err)
|
||||
}
|
||||
h.continueAtReset(ctx, broker, refusal)
|
||||
return
|
||||
}
|
||||
operationID := chatAutoSwitchPrefix + refusalKey(refusal)
|
||||
h.autoSwitch.supersede(refusal, operationID)
|
||||
h.dropAutoContinue(ctx, refusal.SessionID)
|
||||
if err := h.queueServerPrompt(ctx, broker, refusal.SessionID, operationID, chatAutoSwitchText(result.From, result.To)); err != nil {
|
||||
if err := h.queueServerPrompt(ctx, broker, refusal.SessionID, operationID, chatAutoSwitchText(result.From, result.To), time.Time{}); err != nil {
|
||||
log.Printf("claude auto-switch session=%s: queue the continue prompt: %v", refusal.SessionID, err)
|
||||
return
|
||||
}
|
||||
@@ -229,10 +233,13 @@ func (h *chatHub) dropAutoContinue(ctx context.Context, sessionID string) {
|
||||
}
|
||||
|
||||
// queueServerPrompt hands the session a prompt the server composed: it runs
|
||||
// once the current turn has ended (queued durably behind it), and every
|
||||
// attached client sees it under the server's own sender label.
|
||||
func (h *chatHub) queueServerPrompt(ctx context.Context, broker chatBroker, sessionID, operationID, text string) error {
|
||||
if duplicate, _, _ := h.duplicateOp(sessionID, operationID); duplicate {
|
||||
// once the current turn has ended (queued durably behind it), or not before
|
||||
// sendAt when that is set, and every attached client sees it under the
|
||||
// server's own sender label (a scheduled one through the queue, as
|
||||
// "Scheduled", until it is sent). A prompt the session already holds under
|
||||
// operationID is not queued again.
|
||||
func (h *chatHub) queueServerPrompt(ctx context.Context, broker chatBroker, sessionID, operationID, text string, sendAt time.Time) error {
|
||||
if duplicate, _, _ := h.duplicateOp(sessionID, operationID); duplicate || h.holdsQueued(sessionID, operationID) {
|
||||
return nil
|
||||
}
|
||||
p := &chatQueuedPrompt{
|
||||
@@ -242,12 +249,18 @@ func (h *chatHub) queueServerPrompt(ctx context.Context, broker chatBroker, sess
|
||||
client: chatAutoSwitchClient,
|
||||
noSteer: true,
|
||||
}
|
||||
if sendAt.After(h.clock()) {
|
||||
p.sendAt = sendAt
|
||||
}
|
||||
h.rememberOp(sessionID, operationID)
|
||||
admission, err := h.admitPrompt(ctx, sessionID, broker, p)
|
||||
if admission == chatAdmitRefused {
|
||||
h.forgetOp(sessionID, operationID)
|
||||
return err
|
||||
}
|
||||
if !p.sendAt.IsZero() {
|
||||
return nil
|
||||
}
|
||||
h.broadcastUser(protocol.ChatEventFrame{
|
||||
SessionID: sessionID,
|
||||
MessageID: p.messageID,
|
||||
|
||||
Reference in New Issue
Block a user