"Send now" on a queued message (`POST
/api/v2/chats/{chat}/queue/{queuedMessage}/promote`) now just moves it
to the end of the steer lane. It never interrupts the chat, so it can't
cancel the step that would have answered it. The endpoint's request
doesn't change. Before, it moved the message to the head of the queue
with `MIN(position) - 1`, so if you pressed "Send now" on two messages,
the second one went first. With a queue of `Q1 Q2 Q3 S4`, "Send now" on
`Q1` then `Q3` now delivers `S4 Q1 Q3`, and `Q2` keeps waiting.
If the chat has errored or is paused, the steer lane goes in straight
away, like a send. Otherwise it goes in at the next delivery point:
before the next model call, when the turn ends, or when an interrupt
finishes. Stop is the only thing that interrupts.
"Send now" and "Remove" on a message that's already been delivered now
return a 404 instead of a 409, the same as editing one, and "Remove" on
an archived chat returns a 400.
In chatstate, `SteerQueuedMessage` replaces `PromoteQueuedMessage`. It
moves the message to the end of the steer lane, and on an errored or
paused chat it also delivers the steer lane and resumes the chat, like a
send does. The `SteerChatQueuedMessage` query sets `position =
nextval(...)` and replaces `ReorderChatQueuedMessageToHead`. Most of the
diff is tests.
#31174 then makes `steer` the API default and removes `interrupt` as a
busy behaviour, #31175 makes `message_agent` always steer, and #31173
updates the site.
Part of CODAGT-1140
136 lines
3.8 KiB
Go
136 lines
3.8 KiB
Go
package chatstate
|
|
|
|
import (
|
|
"slices"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// AllowedExecutionTransitionsFrom returns a deterministic slice of
|
|
// transitions legal from `from`. Mostly used by tests to enumerate the
|
|
// matrix without leaking the internal map.
|
|
func AllowedExecutionTransitionsFrom(from ExecutionState) []Transition {
|
|
allowed := transitionMatrix[from]
|
|
out := make([]Transition, 0, len(allowed))
|
|
for _, t := range AllExecutionTransitions {
|
|
if _, ok := allowed[t]; ok {
|
|
out = append(out, t)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// AllowedInputStates returns a deterministic slice of execution states
|
|
// from which `tr` is legal per transitionMatrix. Mostly used by tests
|
|
// to enumerate the matrix without leaking the internal map.
|
|
func AllowedInputStates(tr Transition) []ExecutionState {
|
|
var out []ExecutionState
|
|
for _, from := range AllExecutionStates {
|
|
if isExecutionTransitionAllowed(tr, from) {
|
|
out = append(out, from)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// AllowedExecutionTransitionOutputs returns the set of classified
|
|
// post-states that the transition `tr` may produce from `from` per
|
|
// transitionMatrix. The returned slice is a copy so callers may mutate
|
|
// it without affecting the underlying matrix.
|
|
//
|
|
// When `tr` is not allowed from `from`, an empty (nil) slice is
|
|
// returned. Tests use this helper to enumerate the (transition, from,
|
|
// want) triples that must be exercised by the row-level matrix tests.
|
|
func AllowedExecutionTransitionOutputs(from ExecutionState, tr Transition) []ExecutionState {
|
|
allowed, ok := transitionMatrix[from]
|
|
if !ok {
|
|
return nil
|
|
}
|
|
outputs, ok := allowed[tr]
|
|
if !ok {
|
|
return nil
|
|
}
|
|
return slices.Clone(outputs)
|
|
}
|
|
|
|
// ChatID returns the chat ID this machine is scoped to.
|
|
func (m *ChatMachine) ChatID() uuid.UUID { return m.chatID }
|
|
|
|
// snapshotPending returns a snapshot of the buffered messages for
|
|
// tests via [PublishBuffer.BufferedChannels]. The returned slice is a
|
|
// copy and safe to inspect without holding the buffer lock.
|
|
func (b *PublishBuffer) snapshotPending() []bufferedMessage {
|
|
b.mu.Lock()
|
|
defer b.mu.Unlock()
|
|
out := make([]bufferedMessage, len(b.pending))
|
|
copy(out, b.pending)
|
|
return out
|
|
}
|
|
|
|
// BufferedChannels returns just the channels of the pending messages
|
|
// in order. Primarily useful for assertions in tests.
|
|
func (b *PublishBuffer) BufferedChannels() []string {
|
|
pending := b.snapshotPending()
|
|
out := make([]string, len(pending))
|
|
for i, m := range pending {
|
|
out[i] = m.Channel
|
|
}
|
|
return out
|
|
}
|
|
|
|
// AllExecutionTransitions is the canonical enumeration of every
|
|
// execution-state transition that has an entry in transitionMatrix.
|
|
// Ownership transitions (Acquire, Abandon) are intentionally not part
|
|
// of this slice because they are validated independently and do not
|
|
// have a (from->to) execution mapping.
|
|
var AllExecutionTransitions = []Transition{
|
|
TransitionCreateChat,
|
|
TransitionSetArchived,
|
|
TransitionSetProject,
|
|
TransitionSendMessage,
|
|
TransitionEditMessage,
|
|
TransitionRequestCompaction,
|
|
TransitionClearContext,
|
|
TransitionDeleteQueuedMessage,
|
|
TransitionSteerQueuedMessage,
|
|
TransitionEditQueuedMessage,
|
|
TransitionInterrupt,
|
|
TransitionCompleteRequiresAction,
|
|
TransitionRecordGenerationAttempt,
|
|
TransitionRecordRetryState,
|
|
TransitionDeliverSteerMessages,
|
|
TransitionCommitStep,
|
|
TransitionEnterRequiresAction,
|
|
TransitionFinishInterruption,
|
|
TransitionFinishTurn,
|
|
TransitionFinishError,
|
|
TransitionCancelRequiresAction,
|
|
TransitionReconcileInvalidState,
|
|
}
|
|
|
|
// AllExecutionStates is the canonical enumeration of every value the
|
|
// classifier can return. Tests rely on this list to iterate over every
|
|
// state when verifying transition coverage.
|
|
var AllExecutionStates = []ExecutionState{
|
|
StateN,
|
|
StateW,
|
|
StateE0,
|
|
StateE1,
|
|
StateE1P,
|
|
StateR0,
|
|
StateR1,
|
|
StateR1P,
|
|
StateI0,
|
|
StateI1,
|
|
StateI1P,
|
|
StateA0,
|
|
StateA1,
|
|
StateA1P,
|
|
StateP,
|
|
StateXW,
|
|
StateXE0,
|
|
StateXE1,
|
|
StateXE1P,
|
|
StateInvalid,
|
|
}
|