Skip to content

Commit ac36eba

Browse files
committed
feat: expand queue modes and followup backlog
1 parent 6160521 commit ac36eba

7 files changed

Lines changed: 883 additions & 72 deletions

File tree

‎docs/agent.md‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,16 @@ Legacy Pi/Tau session folders are **not** read.
7070

7171
## Steering while streaming
7272

73-
Incoming user messages are queued while the agent is streaming. The queue is checked **after each tool call**. If a queued message is present, remaining tool calls from the current assistant message are skipped (error tool results with "Skipped due to queued user message."), then the queued user message is injected before the next assistant response.
73+
When queue mode is `steer`, inbound messages are injected into the current run.
74+
The queue is checked **after each tool call**; if a queued message is present,
75+
remaining tool calls from the current assistant message are skipped (error tool
76+
results with "Skipped due to queued user message."), then the queued user
77+
message is injected before the next assistant response.
78+
79+
When queue mode is `followup` or `collect`, inbound messages are held until the
80+
current turn ends, then a new agent turn starts with the queued payloads. See
81+
`docs/queue.md` for mode + debounce/cap behavior.
82+
7483
Block streaming sends completed assistant blocks as soon as they finish; disable
7584
via `agent.blockStreamingDefault: "off"` if you only want the final response.
7685
Tune the boundary via `agent.blockStreamingBreak` (`text_end` vs `message_end`).

‎docs/configuration.md‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,16 @@ Controls how inbound messages behave when an agent run is already active.
156156
{
157157
routing: {
158158
queue: {
159-
mode: "interrupt", // global default: queue | interrupt
159+
mode: "collect", // steer | followup | collect | steer-backlog (steer+backlog ok) | interrupt (queue=steer legacy)
160+
debounceMs: 1000,
161+
cap: 20,
162+
drop: "summarize", // old | new | summarize
160163
bySurface: {
161-
whatsapp: "interrupt",
162-
telegram: "interrupt",
163-
discord: "queue",
164-
imessage: "interrupt",
165-
webchat: "queue"
164+
whatsapp: "collect",
165+
telegram: "collect",
166+
discord: "steer-backlog",
167+
imessage: "collect",
168+
webchat: "collect"
166169
}
167170
}
168171
}

‎docs/queue.md‎

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ summary: "Command queue design that serializes auto-reply command execution"
33
read_when:
44
- Changing auto-reply execution or concurrency
55
---
6-
# Command Queue (2025-11-25)
6+
# Command Queue (2026-01-03)
77

88
We now serialize command-based auto-replies (WhatsApp Web listener) through a tiny in-process queue to prevent multiple commands from running at once, while allowing safe parallelism across sessions.
99

@@ -19,30 +19,46 @@ We now serialize command-based auto-replies (WhatsApp Web listener) through a ti
1919
- Typing indicators (`onReplyStart`) still fire immediately on enqueue so user experience is unchanged while we wait our turn.
2020

2121
## Queue modes (per surface)
22-
Inbound messages can either queue or interrupt when a run is already active:
23-
- `queue`: serialize per session; if the agent is streaming, the new message is appended to the current run.
24-
- `interrupt`: abort the active run for that session, then run the newest message.
22+
Inbound messages can steer the current run, wait for a followup turn, or do both:
23+
- `steer`: inject immediately into the current run (cancels pending tool calls after the next tool boundary). If not streaming, falls back to followup.
24+
- `followup`: enqueue for the next agent turn after the current run ends.
25+
- `collect`: coalesce all queued messages into a **single** followup turn (default).
26+
- `steer-backlog` (aka `steer+backlog`): steer now **and** preserve the message for a followup turn.
27+
- `interrupt` (legacy): abort the active run for that session, then run the newest message.
28+
- `queue` (legacy alias): same as `steer`.
2529

2630
Defaults (when unset in config):
27-
- WhatsApp + Telegram → `interrupt`
28-
- Discord + WebChat → `queue`
31+
- All surfaces → `collect`
2932

3033
Configure globally or per surface via `routing.queue`:
3134

3235
```json5
3336
{
3437
routing: {
3538
queue: {
36-
mode: "interrupt",
37-
bySurface: { discord: "queue", telegram: "interrupt" }
39+
mode: "collect",
40+
debounceMs: 1000,
41+
cap: 20,
42+
drop: "summarize",
43+
bySurface: { discord: "steer-backlog" }
3844
}
3945
}
4046
}
4147
```
4248

49+
## Queue options
50+
Options apply to `followup`, `collect`, and `steer-backlog` (and to `steer` when it falls back to followup):
51+
- `debounceMs`: wait for quiet before starting a followup turn (prevents “continue, continue”).
52+
- `cap`: max queued messages per session.
53+
- `drop`: overflow policy (`old`, `new`, `summarize`).
54+
55+
Summarize keeps a short bullet list of dropped messages and injects it as a synthetic followup prompt.
56+
Defaults: `debounceMs: 1000`, `cap: 20`, `drop: summarize`.
57+
4358
## Per-session overrides
4459
- `/queue <mode>` as a standalone command stores the mode for the current session.
4560
- `/queue <mode>` embedded in a message applies **once** (no persistence).
61+
- Options can be combined: `/queue collect debounce:2s cap:25 drop:summarize`
4662
- `/queue default` or `/queue reset` clears the session override.
4763

4864
## Scope and guarantees

‎src/auto-reply/reply.directive.test.ts‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,18 @@ describe("directive parsing", () => {
9797
expect(res.cleaned).toBe("please now");
9898
});
9999

100+
it("parses queue options and modes", () => {
101+
const res = extractQueueDirective(
102+
"please /queue steer+backlog debounce:2s cap:5 drop:summarize now",
103+
);
104+
expect(res.hasDirective).toBe(true);
105+
expect(res.queueMode).toBe("steer-backlog");
106+
expect(res.debounceMs).toBe(2000);
107+
expect(res.cap).toBe(5);
108+
expect(res.dropPolicy).toBe("summarize");
109+
expect(res.cleaned).toBe("please now");
110+
});
111+
100112
it("extracts reply_to_current tag", () => {
101113
const res = extractReplyToTag("ok [[reply_to_current]]", "msg-1");
102114
expect(res.replyToId).toBe("msg-1");
@@ -276,6 +288,43 @@ describe("directive parsing", () => {
276288
});
277289
});
278290

291+
it("persists queue options when directive is standalone", async () => {
292+
await withTempHome(async (home) => {
293+
vi.mocked(runEmbeddedPiAgent).mockReset();
294+
const storePath = path.join(home, "sessions.json");
295+
296+
const res = await getReplyFromConfig(
297+
{
298+
Body: "/queue collect debounce:2s cap:5 drop:old",
299+
From: "+1222",
300+
To: "+1222",
301+
},
302+
{},
303+
{
304+
agent: {
305+
model: "anthropic/claude-opus-4-5",
306+
workspace: path.join(home, "clawd"),
307+
},
308+
whatsapp: { allowFrom: ["*"] },
309+
session: { store: storePath },
310+
},
311+
);
312+
313+
const text = Array.isArray(res) ? res[0]?.text : res?.text;
314+
expect(text).toMatch(/^⚙️ Queue mode set to collect\./);
315+
expect(text).toMatch(/Queue debounce set to 2000ms/);
316+
expect(text).toMatch(/Queue cap set to 5/);
317+
expect(text).toMatch(/Queue drop set to old/);
318+
const store = loadSessionStore(storePath);
319+
const entry = Object.values(store)[0];
320+
expect(entry?.queueMode).toBe("collect");
321+
expect(entry?.queueDebounceMs).toBe(2000);
322+
expect(entry?.queueCap).toBe(5);
323+
expect(entry?.queueDrop).toBe("old");
324+
expect(runEmbeddedPiAgent).not.toHaveBeenCalled();
325+
});
326+
});
327+
279328
it("resets queue mode to default", async () => {
280329
await withTempHome(async (home) => {
281330
vi.mocked(runEmbeddedPiAgent).mockReset();
@@ -312,6 +361,9 @@ describe("directive parsing", () => {
312361
const store = loadSessionStore(storePath);
313362
const entry = Object.values(store)[0];
314363
expect(entry?.queueMode).toBeUndefined();
364+
expect(entry?.queueDebounceMs).toBeUndefined();
365+
expect(entry?.queueCap).toBeUndefined();
366+
expect(entry?.queueDrop).toBeUndefined();
315367
expect(runEmbeddedPiAgent).not.toHaveBeenCalled();
316368
});
317369
});

0 commit comments

Comments
 (0)