Skip to content

Open-source AI coworkers that each get a computer of their own: a browser, files and tools, with every action decided before it happens and recorded after. Bring any AG-UI agent.

Keep a Bot's saved group reply when handing it on fails (#713)

Omkar Chebale38506ac9d864
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b93bb76a..6ee23a31 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -56,6 +56,14 @@ the sentence a malformed range already got. A rule like this saved earlier match
- An existing Windows clone checks text files out with LF only after
`git rm -r --cached . && git reset --hard` on a clean tree.
+### A Bot's saved reply in a group is no longer replaced by a later error
+
+In a group conversation, a Bot's reply was saved, then handed on: to Activity, to any consent
+cards, and to the Bots it named. A fault in that hand-on, such as the audit trail being
+unreachable, wrote the error's text over the saved reply and marked it failed, and a retry did not
+bring the reply back. The reply now stays as saved, and the fault is logged as
+`group-turn-after-reply-error`.
+
### The egress filter reaches an IPv6 upstream proxy and asks it for IPv6 hosts correctly
An upstream proxy configured at an IPv6 address, such as `http://[fd00::1]:3128`, could not be
diff --git a/server/src/channels/group.ts b/server/src/channels/group.ts
index 7c8de49f..4ea6187a 100644
--- a/server/src/channels/group.ts
+++ b/server/src/channels/group.ts
@@ -798,6 +798,12 @@ export function createGroupConversations(deps: {
// Partial text, written at most this often, so the transcript shows the reply as it arrives.
let latest: string | null = null;
let writing: Promise<void> | null = null;
+ // How far past the save this turn got, so a fault after it finishes what never started without
+ // repeating what did: consents are collected once, and a relay that began may have sent hops.
+ let saved = false;
+ let savedReply = "";
+ let consentsPosted = false;
+ let relayStarted = false;
let lastWrite = 0;
const every = deps.progressEveryMs ?? 400;
const flush = () => {
@@ -868,10 +874,53 @@ export function createGroupConversations(deps: {
return;
}
await deps.store.finish(id, result.replyText, "completed");
+ saved = true;
+ savedReply = result.replyText;
await deps.activity?.(turn, agentId, result.replyText, `group:${id}`);
+ consentsPosted = true;
await postConsents(turn, agentId, threadId, id, consents);
+ relayStarted = true;
await relay(turn, bot, id, result.replyText, bots, runId);
} catch (error) {
+ // The reply is already saved: a fault handing it on is not this Bot's answer failing, and
+ // writing it over the row would replace a good reply with an error that no retry repairs.
+ if (saved) {
+ console.error(
+ JSON.stringify({
+ type: "group-turn-after-reply-error",
+ error: error instanceof Error ? error.message : String(error),
+ context: { channelId: turn.channelId, agentId, rowId: id },
+ timestamp: new Date().toISOString(),
+ }),
+ );
+ const unfinished = async (step: string, run: () => Promise<void>) => {
+ try {
+ await run();
+ } catch (stepError) {
+ console.error(
+ JSON.stringify({
+ type: "group-turn-after-reply-error",
+ step,
+ error:
+ stepError instanceof Error
+ ? stepError.message
+ : String(stepError),
+ context: { channelId: turn.channelId, agentId, rowId: id },
+ timestamp: new Date().toISOString(),
+ }),
+ );
+ }
+ };
+ if (!consentsPosted)
+ await unfinished("consents", () =>
+ postConsents(turn, agentId, threadId, id, consents),
+ );
+ if (!relayStarted)
+ await unfinished("relay", () =>
+ relay(turn, bot, id, savedReply, bots, runId),
+ );
+ return;
+ }
await writing;
await postConsents(turn, agentId, threadId, id, consents);
if (error instanceof HeadlessToolSuspension)
diff --git a/server/tests/group-conversations.integration.test.ts b/server/tests/group-conversations.integration.test.ts
index 57359e16..7eba9fb1 100644
--- a/server/tests/group-conversations.integration.test.ts
+++ b/server/tests/group-conversations.integration.test.ts
@@ -91,10 +91,12 @@ function service(options: {
>;
maxDepth?: number;
granted?: boolean;
+ auditFails?: boolean;
privateShare?: Parameters<typeof createGroupConversations>[0]["privateShare"];
listenForConsent?: Parameters<
typeof createGroupConversations
>[0]["listenForConsent"];
+ activity?: Parameters<typeof createGroupConversations>[0]["activity"];
}) {
const calls: Parameters<TurnRunner>[0][] = [];
const audit: AuditEventInput[] = [];
@@ -122,7 +124,12 @@ function service(options: {
},
caps: { maxDepth: options.maxDepth ?? 1, maxPerRun: 2 },
mayAddress: async () => options.granted ?? true,
- auditStore: { insert: async (event) => void audit.push(event) },
+ auditStore: {
+ insert: async (event) => {
+ if (options.auditFails) throw new Error("audit store unavailable");
+ audit.push(event);
+ },
+ },
createChannel: async (ownerUserId, agentIds) => {
const channel = await channelStore.create(
{ id: ownerUserId, role: "user" },
@@ -136,6 +143,7 @@ function service(options: {
...(options.listenForConsent
? { listenForConsent: options.listenForConsent }
: {}),
+ ...(options.activity ? { activity: options.activity } : {}),
});
return { conversations, calls, audit };
}
@@ -316,6 +324,66 @@ describe("group conversations in PostgreSQL", () => {
).filter((row) => row.key.startsWith(prefix) && row.key.includes(">")),
).toEqual([]);
});
+ test("a fault after a reply is saved still posts its consent cards and hands it on", async () => {
+ const owner = await person();
+ const ada = await bot(owner, "Ada");
+ const grace = await bot(owner, "Grace");
+ const channel = await channelStore.create(owner, [ada, grace]);
+ createdChannels.push(channel.id);
+ const { conversations, calls } = service({
+ replies: { [ada]: () => "@Grace over to you", [grace]: () => "ok" },
+ // Only Ada's saved reply fails to reach the activity feed; the person's message still does.
+ activity: async (_turn, agentId) => {
+ if (agentId === ada) throw new Error("activity feed unavailable");
+ },
+ listenForConsent: (_ownerUserId, agentId) => () =>
+ agentId === ada
+ ? [{ botId: ada, serverId: "gmail", message: "Ada needs your mail." }]
+ : [],
+ });
+ await conversations.send(owner.id, channel.id, {
+ id: `${prefix}-activity-fault`,
+ text: "go",
+ agentId: ada,
+ });
+ await drain(conversations);
+ const { messages } = await conversations.list(owner.id, channel.id);
+ expect(
+ messages.find((row) => row.agentId === ada && !row.consent),
+ ).toMatchObject({
+ status: "completed",
+ text: "@Grace over to you",
+ });
+ expect(messages.some((row) => row.consent?.serverId === "gmail")).toBe(
+ true,
+ );
+ expect(calls.some((call) => call.agentId === grace)).toBe(true);
+ });
+
+ test("a fault handing a saved reply on does not write over the reply", async () => {
+ const owner = await person();
+ const ada = await bot(owner, "Ada");
+ const grace = await bot(owner, "Grace");
+ const channel = await channelStore.create(owner, [ada, grace]);
+ createdChannels.push(channel.id);
+ const { conversations } = service({
+ auditFails: true,
+ replies: { [ada]: () => "@Grace over to you", [grace]: () => "ok" },
+ });
+ await conversations.send(owner.id, channel.id, {
+ id: `${prefix}-relay-fault`,
+ text: "go",
+ agentId: ada,
+ });
+ await drain(conversations);
+ const reply = (
+ await conversations.list(owner.id, channel.id)
+ ).messages.find((row) => row.agentId === ada);
+ expect(reply).toMatchObject({
+ status: "completed",
+ text: "@Grace over to you",
+ });
+ });
test("Bots answer in the order chosen, or in the order a message names them", async () => {
const owner = await person();
const ada = await bot(owner, "Ada");