feat: make /stop cancel the running chat turn

/stop could not reach a running turn, and the reason was worse than a
missing cancel. go-telegram/bot runs one update worker that calls
handlers synchronously (defaultWorkers = 1, never overridden), and the
chat turn ran on it. While a turn streamed -- or sat in a shell tool --
the worker was blocked and /stop sat unread in the update channel until
the turn it was meant to kill had already finished.

Add gateway.Turns: a per-session serial lane that turns are submitted to
and that the event loop never waits on. The worker is free again in
microseconds, so /stop is serviced immediately; ordering comes from the
lane being serial rather than from the loop being blocked.

- Bare /stop cancels the running turn and clears the backlog behind it,
  with no approval step. Approval guards destructive commands, and
  gating the brake behind a confirmation defeats it. /stop <name> keeps
  the existing process-termination behaviour and its approval.
- Cancellation propagates through the turn context, so it reaches the
  model stream and any tool underneath -- a shell command built with
  exec.CommandContext dies with the turn.
- Queues are unbounded and never shed. A dropped message is a silent
  failure that surfaces later as an agent that ignored something.
- Turns are recovered per-lane: the channel's panic middleware wraps the
  event-loop goroutine, which no longer runs the turn.

Also raise the chat step budget. It was hardcoded at 8 -- roughly one
read, one grep and two edits -- so turns hit the cap and stopped
mid-task with no indication they had been cut off. It is now
chat.max_steps, defaulting to 100. Interruption is /stop's job.
This commit is contained in:
sam committed 2026-07-29 16:41:55 +10:00
1 parent e03cef17c9
commit 561e56b70a
10 files changed
+765 -34

No files matched your search

+14 -2
View File
@@ -71,7 +71,15 @@ func main() {
}
const (
defaultChatMaxSteps = 8
// defaultChatMaxSteps bounds the model/tool round-trips in one chat
// turn when [config.ChatConfig.MaxSteps] is unset.
//
// It was 8, which is roughly one read, one grep and a couple of edits
// -- less than a single small change to a Go package costs. Turns hit
// the cap and stopped mid-task with no indication they had been cut
// off. Interruption is /stop's job; this only has to stop a genuine
// runaway, so it is set well above real work.
defaultChatMaxSteps = 100
packagedGatewayChangelogPath = "/usr/share/archie/CHANGELOG.archied.md"
packagedRuntimeChangelogPath = "/usr/share/archie/CHANGELOG.archie.md"
)
@@ -87,15 +95,19 @@ var (
func chatGenerateOptions(
messages []chat.Message,
registry *tools.Registry,
maxSteps int,
) (core.GenerateOptions, error) {
toolSet, err := agentexec.BuildToolSet(registry)
if err != nil {
return core.GenerateOptions{}, err
}
if maxSteps <= 0 {
maxSteps = defaultChatMaxSteps
}
return core.GenerateOptions{
Messages: messages,
Tools: toolSet,
MaxSteps: defaultChatMaxSteps,
MaxSteps: maxSteps,
}, nil
}
+15 -2
View File
@@ -38,7 +38,7 @@ func TestChatGenerateOptionsIncludesToolsAndMultipleSteps(t *testing.T) {
}
messages := []chat.Message{{Role: chat.RoleUser, Content: "remember this"}}
options, err := chatGenerateOptions(messages, registry)
options, err := chatGenerateOptions(messages, registry, 0)
if err != nil {
t.Fatal(err)
}
@@ -64,11 +64,24 @@ func TestChatGenerateOptionsReportsInvalidToolSchemas(t *testing.T) {
}); err != nil {
t.Fatal(err)
}
if _, err := chatGenerateOptions(nil, registry); err == nil {
if _, err := chatGenerateOptions(nil, registry, 0); err == nil {
t.Fatal("chatGenerateOptions() error = nil, want invalid tool schema error")
}
}
// TestChatGenerateOptionsHonoursConfiguredMaxSteps keeps the step budget
// deployment-controlled. A cap that cannot be raised without a rebuild is
// the situation this replaced.
func TestChatGenerateOptionsHonoursConfiguredMaxSteps(t *testing.T) {
options, err := chatGenerateOptions(nil, tools.NewRegistry(), 250)
if err != nil {
t.Fatal(err)
}
if options.MaxSteps != 250 {
t.Errorf("MaxSteps = %d, want the configured 250", options.MaxSteps)
}
}
func TestGatewaysStartOnlyAfterCapabilityHostAndDaemonStartup(t *testing.T) {
source, err := os.ReadFile("main.go")
if err != nil {
+1 -1
View File
@@ -180,7 +180,7 @@ func makeTelegramLLMResponder(ctx context.Context, tg *telegram.Gateway, s teleg
compressed = append(compressed, gateway.CompressedMessage{Role: role, Content: h.Text})
}
view := gateway.CompressHistory(compressed, gateway.DefaultCompressionConfig())
options, err := chatGenerateOptions(nil, s.ToolReg)
options, err := chatGenerateOptions(nil, s.ToolReg, s.Cfg.Chat.MaxSteps)
if err != nil {
return "", fmt.Errorf("build chat tools: %w", err)
}