mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 12:41:23 +00:00
refactor(agents): dedupe lifecycle send assertions and stable payload stringify
This commit is contained in:
@@ -129,12 +129,18 @@ function stableStringify(value: unknown): string {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (Array.isArray(value)) {
|
if (Array.isArray(value)) {
|
||||||
return `[${value.map((entry) => stableStringify(entry)).join(",")}]`;
|
const serializedEntries: string[] = [];
|
||||||
|
for (const entry of value) {
|
||||||
|
serializedEntries.push(stableStringify(entry));
|
||||||
|
}
|
||||||
|
return `[${serializedEntries.join(",")}]`;
|
||||||
}
|
}
|
||||||
const record = value as Record<string, unknown>;
|
const record = value as Record<string, unknown>;
|
||||||
const keys = Object.keys(record).toSorted();
|
const serializedFields: string[] = [];
|
||||||
const entries = keys.map((key) => `${JSON.stringify(key)}:${stableStringify(record[key])}`);
|
for (const key of Object.keys(record).toSorted()) {
|
||||||
return `{${entries.join(",")}}`;
|
serializedFields.push(`${JSON.stringify(key)}:${stableStringify(record[key])}`);
|
||||||
|
}
|
||||||
|
return `{${serializedFields.join(",")}}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function digest(value: unknown): string {
|
function digest(value: unknown): string {
|
||||||
|
|||||||
@@ -135,6 +135,21 @@ const waitFor = async (predicate: () => boolean, timeoutMs = 2000) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function expectSingleCompletionSend(
|
||||||
|
calls: GatewayRequest[],
|
||||||
|
expected: { sessionKey: string; channel: string; to: string; message: string },
|
||||||
|
) {
|
||||||
|
const sendCalls = calls.filter((call) => call.method === "send");
|
||||||
|
expect(sendCalls).toHaveLength(1);
|
||||||
|
const send = sendCalls[0]?.params as
|
||||||
|
| { sessionKey?: string; channel?: string; to?: string; message?: string }
|
||||||
|
| undefined;
|
||||||
|
expect(send?.sessionKey).toBe(expected.sessionKey);
|
||||||
|
expect(send?.channel).toBe(expected.channel);
|
||||||
|
expect(send?.to).toBe(expected.to);
|
||||||
|
expect(send?.message).toBe(expected.message);
|
||||||
|
}
|
||||||
|
|
||||||
describe("openclaw-tools: subagents (sessions_spawn lifecycle)", () => {
|
describe("openclaw-tools: subagents (sessions_spawn lifecycle)", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
resetSessionsSpawnConfigOverride();
|
resetSessionsSpawnConfigOverride();
|
||||||
@@ -204,15 +219,12 @@ describe("openclaw-tools: subagents (sessions_spawn lifecycle)", () => {
|
|||||||
expect(first?.lane).toBe("subagent");
|
expect(first?.lane).toBe("subagent");
|
||||||
|
|
||||||
// Direct send should route completion to the requester channel/session.
|
// Direct send should route completion to the requester channel/session.
|
||||||
const sendCalls = ctx.calls.filter((c) => c.method === "send");
|
expectSingleCompletionSend(ctx.calls, {
|
||||||
expect(sendCalls).toHaveLength(1);
|
sessionKey: "agent:main:main",
|
||||||
const send = sendCalls[0]?.params as
|
channel: "whatsapp",
|
||||||
| { sessionKey?: string; channel?: string; to?: string; message?: string }
|
to: "+123",
|
||||||
| undefined;
|
message: "✅ Subagent main finished\n\ndone",
|
||||||
expect(send?.sessionKey).toBe("agent:main:main");
|
});
|
||||||
expect(send?.channel).toBe("whatsapp");
|
|
||||||
expect(send?.to).toBe("+123");
|
|
||||||
expect(send?.message).toBe("✅ Subagent main finished\n\ndone");
|
|
||||||
expect(child.sessionKey?.startsWith("agent:main:subagent:")).toBe(true);
|
expect(child.sessionKey?.startsWith("agent:main:subagent:")).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -289,15 +301,12 @@ describe("openclaw-tools: subagents (sessions_spawn lifecycle)", () => {
|
|||||||
expect(first?.sessionKey?.startsWith("agent:main:subagent:")).toBe(true);
|
expect(first?.sessionKey?.startsWith("agent:main:subagent:")).toBe(true);
|
||||||
expect(child.sessionKey?.startsWith("agent:main:subagent:")).toBe(true);
|
expect(child.sessionKey?.startsWith("agent:main:subagent:")).toBe(true);
|
||||||
|
|
||||||
const sendCalls = ctx.calls.filter((c) => c.method === "send");
|
expectSingleCompletionSend(ctx.calls, {
|
||||||
expect(sendCalls).toHaveLength(1);
|
sessionKey: "agent:main:discord:group:req",
|
||||||
const send = sendCalls[0]?.params as
|
channel: "discord",
|
||||||
| { sessionKey?: string; channel?: string; to?: string; message?: string }
|
to: "discord:dm:u123",
|
||||||
| undefined;
|
message: "✅ Subagent main finished",
|
||||||
expect(send?.sessionKey).toBe("agent:main:discord:group:req");
|
});
|
||||||
expect(send?.channel).toBe("discord");
|
|
||||||
expect(send?.to).toBe("discord:dm:u123");
|
|
||||||
expect(send?.message).toBe("✅ Subagent main finished");
|
|
||||||
|
|
||||||
expect(deletedKey?.startsWith("agent:main:subagent:")).toBe(true);
|
expect(deletedKey?.startsWith("agent:main:subagent:")).toBe(true);
|
||||||
});
|
});
|
||||||
@@ -356,15 +365,12 @@ describe("openclaw-tools: subagents (sessions_spawn lifecycle)", () => {
|
|||||||
const first = agentCalls[0]?.params as { lane?: string } | undefined;
|
const first = agentCalls[0]?.params as { lane?: string } | undefined;
|
||||||
expect(first?.lane).toBe("subagent");
|
expect(first?.lane).toBe("subagent");
|
||||||
|
|
||||||
const sendCalls = ctx.calls.filter((c) => c.method === "send");
|
expectSingleCompletionSend(ctx.calls, {
|
||||||
expect(sendCalls).toHaveLength(1);
|
sessionKey: "agent:main:discord:group:req",
|
||||||
const send = sendCalls[0]?.params as
|
channel: "discord",
|
||||||
| { sessionKey?: string; channel?: string; to?: string; message?: string }
|
to: "discord:dm:u123",
|
||||||
| undefined;
|
message: "✅ Subagent main finished\n\ndone",
|
||||||
expect(send?.sessionKey).toBe("agent:main:discord:group:req");
|
});
|
||||||
expect(send?.channel).toBe("discord");
|
|
||||||
expect(send?.to).toBe("discord:dm:u123");
|
|
||||||
expect(send?.message).toBe("✅ Subagent main finished\n\ndone");
|
|
||||||
|
|
||||||
// Session should be deleted
|
// Session should be deleted
|
||||||
expect(deletedKey?.startsWith("agent:main:subagent:")).toBe(true);
|
expect(deletedKey?.startsWith("agent:main:subagent:")).toBe(true);
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import type { AssistantMessage } from "@mariozechner/pi-ai";
|
import type { AssistantMessage } from "@mariozechner/pi-ai";
|
||||||
import type { OpenClawConfig } from "../../config/config.js";
|
import type { OpenClawConfig } from "../../config/config.js";
|
||||||
import { formatSandboxToolPolicyBlockedMessage } from "../sandbox.js";
|
import { formatSandboxToolPolicyBlockedMessage } from "../sandbox.js";
|
||||||
|
import { stableStringify } from "../stable-stringify.js";
|
||||||
import type { FailoverReason } from "./types.js";
|
import type { FailoverReason } from "./types.js";
|
||||||
|
|
||||||
export function formatBillingErrorMessage(provider?: string): string {
|
export function formatBillingErrorMessage(provider?: string): string {
|
||||||
@@ -309,19 +310,6 @@ function parseApiErrorPayload(raw: string): ErrorPayload | null {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function stableStringify(value: unknown): string {
|
|
||||||
if (!value || typeof value !== "object") {
|
|
||||||
return JSON.stringify(value) ?? "null";
|
|
||||||
}
|
|
||||||
if (Array.isArray(value)) {
|
|
||||||
return `[${value.map((entry) => stableStringify(entry)).join(",")}]`;
|
|
||||||
}
|
|
||||||
const record = value as Record<string, unknown>;
|
|
||||||
const keys = Object.keys(record).toSorted();
|
|
||||||
const entries = keys.map((key) => `${JSON.stringify(key)}:${stableStringify(record[key])}`);
|
|
||||||
return `{${entries.join(",")}}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getApiErrorPayloadFingerprint(raw?: string): string | null {
|
export function getApiErrorPayloadFingerprint(raw?: string): string | null {
|
||||||
if (!raw) {
|
if (!raw) {
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
12
src/agents/stable-stringify.ts
Normal file
12
src/agents/stable-stringify.ts
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
export function stableStringify(value: unknown): string {
|
||||||
|
if (value === null || typeof value !== "object") {
|
||||||
|
return JSON.stringify(value) ?? "null";
|
||||||
|
}
|
||||||
|
if (Array.isArray(value)) {
|
||||||
|
return `[${value.map((entry) => stableStringify(entry)).join(",")}]`;
|
||||||
|
}
|
||||||
|
const record = value as Record<string, unknown>;
|
||||||
|
const keys = Object.keys(record).toSorted();
|
||||||
|
const entries = keys.map((key) => `${JSON.stringify(key)}:${stableStringify(record[key])}`);
|
||||||
|
return `{${entries.join(",")}}`;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user