mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-29 10:02:04 +00:00
test(auto-reply): isolate fallback selection coverage
This commit is contained in:
@@ -88,12 +88,19 @@ vi.mock("../heartbeat.js", () => ({
|
||||
}));
|
||||
|
||||
vi.mock("./agent-runner-utils.js", () => ({
|
||||
buildEmbeddedRunExecutionParams: (params: { provider: string; model: string }) => ({
|
||||
buildEmbeddedRunExecutionParams: (params: {
|
||||
provider: string;
|
||||
model: string;
|
||||
run: { provider?: string; authProfileId?: string; authProfileIdSource?: "auto" | "user" };
|
||||
}) => ({
|
||||
embeddedContext: {},
|
||||
senderContext: {},
|
||||
runBaseParams: {
|
||||
provider: params.provider,
|
||||
model: params.model,
|
||||
authProfileId: params.provider === params.run.provider ? params.run.authProfileId : undefined,
|
||||
authProfileIdSource:
|
||||
params.provider === params.run.provider ? params.run.authProfileIdSource : undefined,
|
||||
},
|
||||
}),
|
||||
resolveModelFallbackOptions: vi.fn(() => ({})),
|
||||
@@ -111,6 +118,10 @@ async function getRunAgentTurnWithFallback() {
|
||||
return (await import("./agent-runner-execution.js")).runAgentTurnWithFallback;
|
||||
}
|
||||
|
||||
async function getApplyFallbackCandidateSelectionToEntry() {
|
||||
return (await import("./agent-runner-execution.js")).applyFallbackCandidateSelectionToEntry;
|
||||
}
|
||||
|
||||
type FallbackRunnerParams = {
|
||||
run: (provider: string, model: string) => Promise<unknown>;
|
||||
};
|
||||
@@ -1294,4 +1305,105 @@ describe("runAgentTurnWithFallback", () => {
|
||||
expect(sessionStore.main.providerOverride).toBe("zai");
|
||||
expect(sessionStore.main.modelOverride).toBe("glm-5");
|
||||
});
|
||||
|
||||
it("drops authProfileId when fallback switches providers", async () => {
|
||||
state.runWithModelFallbackMock.mockImplementation(
|
||||
async (params: { run: (provider: string, model: string) => Promise<unknown> }) => ({
|
||||
result: await params.run("openai-codex", "gpt-5.4"),
|
||||
provider: "openai-codex",
|
||||
model: "gpt-5.4",
|
||||
attempts: [],
|
||||
}),
|
||||
);
|
||||
state.runEmbeddedPiAgentMock.mockResolvedValue({
|
||||
payloads: [{ text: "ok" }],
|
||||
meta: {},
|
||||
});
|
||||
|
||||
const followupRun = createFollowupRun();
|
||||
followupRun.run.provider = "anthropic";
|
||||
followupRun.run.model = "claude-opus";
|
||||
followupRun.run.authProfileId = "anthropic:openclaw";
|
||||
followupRun.run.authProfileIdSource = "user";
|
||||
|
||||
const sessionEntry: SessionEntry = {
|
||||
sessionId: "session",
|
||||
updatedAt: Date.now(),
|
||||
totalTokens: 1,
|
||||
compactionCount: 0,
|
||||
};
|
||||
const sessionStore = { main: sessionEntry };
|
||||
|
||||
const runAgentTurnWithFallback = await getRunAgentTurnWithFallback();
|
||||
const result = await runAgentTurnWithFallback({
|
||||
commandBody: "hello",
|
||||
followupRun,
|
||||
sessionCtx: {
|
||||
Provider: "telegram",
|
||||
MessageSid: "msg",
|
||||
} as unknown as TemplateContext,
|
||||
opts: {},
|
||||
typingSignals: createMockTypingSignaler(),
|
||||
blockReplyPipeline: null,
|
||||
blockStreamingEnabled: false,
|
||||
resolvedBlockStreamingBreak: "message_end",
|
||||
applyReplyToMode: (payload) => payload,
|
||||
shouldEmitToolResult: () => true,
|
||||
shouldEmitToolOutput: () => false,
|
||||
pendingToolTasks: new Set(),
|
||||
resetSessionAfterCompactionFailure: async () => false,
|
||||
resetSessionAfterRoleOrderingConflict: async () => false,
|
||||
isHeartbeat: false,
|
||||
sessionKey: "main",
|
||||
getActiveSessionEntry: () => sessionEntry,
|
||||
activeSessionStore: sessionStore,
|
||||
resolvedVerboseLevel: "off",
|
||||
});
|
||||
|
||||
expect(result.kind).toBe("success");
|
||||
expect(state.runEmbeddedPiAgentMock).toHaveBeenCalledTimes(1);
|
||||
expect(state.runEmbeddedPiAgentMock.mock.calls[0]?.[0]).toMatchObject({
|
||||
provider: "openai-codex",
|
||||
model: "gpt-5.4",
|
||||
authProfileId: undefined,
|
||||
authProfileIdSource: undefined,
|
||||
});
|
||||
expect(sessionEntry.providerOverride).toBe("openai-codex");
|
||||
expect(sessionEntry.modelOverride).toBe("gpt-5.4");
|
||||
expect(sessionEntry.authProfileOverride).toBeUndefined();
|
||||
expect(sessionEntry.authProfileOverrideSource).toBeUndefined();
|
||||
expect(sessionStore.main.authProfileOverride).toBeUndefined();
|
||||
});
|
||||
|
||||
it("keeps same-provider auth profile when fallback only changes model", async () => {
|
||||
const applyFallbackCandidateSelectionToEntry = await getApplyFallbackCandidateSelectionToEntry();
|
||||
const entry = {
|
||||
sessionId: "session",
|
||||
updatedAt: 1,
|
||||
authProfileOverride: "anthropic:openclaw",
|
||||
authProfileOverrideSource: "user" as const,
|
||||
} as SessionEntry;
|
||||
|
||||
const { updated } = applyFallbackCandidateSelectionToEntry({
|
||||
entry,
|
||||
run: {
|
||||
provider: "anthropic",
|
||||
model: "claude-opus",
|
||||
authProfileId: "anthropic:openclaw",
|
||||
authProfileIdSource: "user",
|
||||
} as FollowupRun["run"],
|
||||
provider: "anthropic",
|
||||
model: "claude-sonnet",
|
||||
now: 123,
|
||||
});
|
||||
|
||||
expect(updated).toBe(true);
|
||||
expect(entry).toMatchObject({
|
||||
updatedAt: 123,
|
||||
providerOverride: "anthropic",
|
||||
modelOverride: "claude-sonnet",
|
||||
authProfileOverride: "anthropic:openclaw",
|
||||
authProfileOverrideSource: "user",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -183,6 +183,29 @@ function buildFallbackSelectionState(params: {
|
||||
};
|
||||
}
|
||||
|
||||
export function applyFallbackCandidateSelectionToEntry(params: {
|
||||
entry: SessionEntry;
|
||||
run: FollowupRun["run"];
|
||||
provider: string;
|
||||
model: string;
|
||||
now?: number;
|
||||
}): { updated: boolean; nextState?: FallbackSelectionState } {
|
||||
if (params.provider === params.run.provider && params.model === params.run.model) {
|
||||
return { updated: false };
|
||||
}
|
||||
const scopedAuthProfile = resolveRunAuthProfile(params.run, params.provider);
|
||||
const nextState = buildFallbackSelectionState({
|
||||
provider: params.provider,
|
||||
model: params.model,
|
||||
authProfileId: scopedAuthProfile.authProfileId,
|
||||
authProfileIdSource: scopedAuthProfile.authProfileIdSource,
|
||||
});
|
||||
return {
|
||||
updated: applyFallbackSelectionState(params.entry, nextState, params.now),
|
||||
nextState,
|
||||
};
|
||||
}
|
||||
|
||||
function applyFallbackSelectionState(
|
||||
entry: SessionEntry,
|
||||
nextState: FallbackSelectionState,
|
||||
@@ -553,14 +576,14 @@ export async function runAgentTurnWithFallback(params: {
|
||||
}
|
||||
|
||||
const previousState = snapshotFallbackSelectionState(activeSessionEntry);
|
||||
const scopedAuthProfile = resolveRunAuthProfile(params.followupRun.run, provider);
|
||||
const nextState = buildFallbackSelectionState({
|
||||
const applied = applyFallbackCandidateSelectionToEntry({
|
||||
entry: activeSessionEntry,
|
||||
run: params.followupRun.run,
|
||||
provider,
|
||||
model,
|
||||
authProfileId: scopedAuthProfile.authProfileId,
|
||||
authProfileIdSource: scopedAuthProfile.authProfileIdSource,
|
||||
});
|
||||
if (!applyFallbackSelectionState(activeSessionEntry, nextState)) {
|
||||
const nextState = applied.nextState;
|
||||
if (!applied.updated || !nextState) {
|
||||
return;
|
||||
}
|
||||
params.activeSessionStore[params.sessionKey] = activeSessionEntry;
|
||||
|
||||
@@ -282,214 +282,6 @@ describe("runReplyAgent onAgentRunStart", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("runReplyAgent authProfileId fallback scoping", () => {
|
||||
it("drops authProfileId when provider changes during fallback", async () => {
|
||||
runWithModelFallbackMock.mockImplementationOnce(
|
||||
async ({ run }: RunWithModelFallbackParams) => ({
|
||||
result: await run("openai-codex", "gpt-5.4"),
|
||||
provider: "openai-codex",
|
||||
model: "gpt-5.4",
|
||||
}),
|
||||
);
|
||||
|
||||
runEmbeddedPiAgentMock.mockResolvedValue({ payloads: [{ text: "ok" }], meta: {} });
|
||||
|
||||
const typing = createMockTypingController();
|
||||
const sessionCtx = {
|
||||
Provider: "telegram",
|
||||
OriginatingTo: "chat",
|
||||
AccountId: "primary",
|
||||
MessageSid: "msg",
|
||||
Surface: "telegram",
|
||||
} as unknown as TemplateContext;
|
||||
|
||||
const resolvedQueue = { mode: "interrupt" } as unknown as QueueSettings;
|
||||
const followupRun = {
|
||||
prompt: "hello",
|
||||
summaryLine: "hello",
|
||||
enqueuedAt: Date.now(),
|
||||
run: {
|
||||
agentId: "main",
|
||||
agentDir: "/tmp/agent",
|
||||
sessionId: "session",
|
||||
sessionKey: "main",
|
||||
messageProvider: "telegram",
|
||||
sessionFile: "/tmp/session.jsonl",
|
||||
workspaceDir: "/tmp",
|
||||
config: createCliBackendTestConfig(),
|
||||
skillsSnapshot: {},
|
||||
provider: "anthropic",
|
||||
model: "claude-opus",
|
||||
authProfileId: "anthropic:openclaw",
|
||||
authProfileIdSource: "manual",
|
||||
thinkLevel: "low",
|
||||
verboseLevel: "off",
|
||||
elevatedLevel: "off",
|
||||
bashElevated: {
|
||||
enabled: false,
|
||||
allowed: false,
|
||||
defaultLevel: "off",
|
||||
},
|
||||
timeoutMs: 5_000,
|
||||
blockReplyBreak: "message_end",
|
||||
},
|
||||
} as unknown as FollowupRun;
|
||||
|
||||
const sessionKey = "main";
|
||||
const sessionEntry: SessionEntry = {
|
||||
sessionId: "session",
|
||||
updatedAt: Date.now(),
|
||||
totalTokens: 1,
|
||||
compactionCount: 0,
|
||||
};
|
||||
|
||||
await runReplyAgent({
|
||||
commandBody: "hello",
|
||||
followupRun,
|
||||
queueKey: sessionKey,
|
||||
resolvedQueue,
|
||||
shouldSteer: false,
|
||||
shouldFollowup: false,
|
||||
isActive: false,
|
||||
isStreaming: false,
|
||||
typing,
|
||||
sessionCtx,
|
||||
sessionEntry,
|
||||
sessionStore: { [sessionKey]: sessionEntry },
|
||||
sessionKey,
|
||||
storePath: undefined,
|
||||
defaultModel: "anthropic/claude-opus-4-6",
|
||||
agentCfgContextTokens: 100_000,
|
||||
resolvedVerboseLevel: "off",
|
||||
isNewSession: false,
|
||||
blockStreamingEnabled: false,
|
||||
resolvedBlockStreamingBreak: "message_end",
|
||||
shouldInjectGroupIntro: false,
|
||||
typingMode: "instant",
|
||||
});
|
||||
|
||||
expect(runEmbeddedPiAgentMock).toHaveBeenCalledTimes(1);
|
||||
const call = runEmbeddedPiAgentMock.mock.calls[0]?.[0] as {
|
||||
authProfileId?: unknown;
|
||||
authProfileIdSource?: unknown;
|
||||
provider?: unknown;
|
||||
};
|
||||
|
||||
expect(call.provider).toBe("openai-codex");
|
||||
expect(call.authProfileId).toBeUndefined();
|
||||
expect(call.authProfileIdSource).toBeUndefined();
|
||||
expect(sessionEntry.providerOverride).toBe("openai-codex");
|
||||
expect(sessionEntry.modelOverride).toBe("gpt-5.4");
|
||||
expect(sessionEntry.authProfileOverride).toBeUndefined();
|
||||
expect(sessionEntry.authProfileOverrideSource).toBeUndefined();
|
||||
});
|
||||
|
||||
it("persists same-provider fallback model while keeping the scoped auth profile", async () => {
|
||||
runWithModelFallbackMock.mockImplementationOnce(
|
||||
async ({ run }: RunWithModelFallbackParams) => ({
|
||||
result: await run("anthropic", "claude-sonnet"),
|
||||
provider: "anthropic",
|
||||
model: "claude-sonnet",
|
||||
}),
|
||||
);
|
||||
|
||||
runEmbeddedPiAgentMock.mockResolvedValue({ payloads: [{ text: "ok" }], meta: {} });
|
||||
|
||||
const typing = createMockTypingController();
|
||||
const sessionCtx = {
|
||||
Provider: "telegram",
|
||||
OriginatingTo: "chat",
|
||||
AccountId: "primary",
|
||||
MessageSid: "msg",
|
||||
Surface: "telegram",
|
||||
} as unknown as TemplateContext;
|
||||
|
||||
const resolvedQueue = { mode: "interrupt" } as unknown as QueueSettings;
|
||||
const followupRun = {
|
||||
prompt: "hello",
|
||||
summaryLine: "hello",
|
||||
enqueuedAt: Date.now(),
|
||||
run: {
|
||||
agentId: "main",
|
||||
agentDir: "/tmp/agent",
|
||||
sessionId: "session",
|
||||
sessionKey: "main",
|
||||
messageProvider: "telegram",
|
||||
sessionFile: "/tmp/session.jsonl",
|
||||
workspaceDir: "/tmp",
|
||||
config: createCliBackendTestConfig(),
|
||||
skillsSnapshot: {},
|
||||
provider: "anthropic",
|
||||
model: "claude-opus",
|
||||
authProfileId: "anthropic:openclaw",
|
||||
authProfileIdSource: "user",
|
||||
thinkLevel: "low",
|
||||
verboseLevel: "off",
|
||||
elevatedLevel: "off",
|
||||
bashElevated: {
|
||||
enabled: false,
|
||||
allowed: false,
|
||||
defaultLevel: "off",
|
||||
},
|
||||
timeoutMs: 5_000,
|
||||
blockReplyBreak: "message_end",
|
||||
},
|
||||
} as unknown as FollowupRun;
|
||||
|
||||
const sessionKey = "main";
|
||||
const sessionEntry: SessionEntry = {
|
||||
sessionId: "session",
|
||||
updatedAt: Date.now(),
|
||||
totalTokens: 1,
|
||||
compactionCount: 0,
|
||||
authProfileOverride: "anthropic:openclaw",
|
||||
authProfileOverrideSource: "user" as const,
|
||||
};
|
||||
|
||||
await runReplyAgent({
|
||||
commandBody: "hello",
|
||||
followupRun,
|
||||
queueKey: sessionKey,
|
||||
resolvedQueue,
|
||||
shouldSteer: false,
|
||||
shouldFollowup: false,
|
||||
isActive: false,
|
||||
isStreaming: false,
|
||||
typing,
|
||||
sessionCtx,
|
||||
sessionEntry,
|
||||
sessionStore: { [sessionKey]: sessionEntry },
|
||||
sessionKey,
|
||||
storePath: undefined,
|
||||
defaultModel: "anthropic/claude-opus-4-6",
|
||||
agentCfgContextTokens: 100_000,
|
||||
resolvedVerboseLevel: "off",
|
||||
isNewSession: false,
|
||||
blockStreamingEnabled: false,
|
||||
resolvedBlockStreamingBreak: "message_end",
|
||||
shouldInjectGroupIntro: false,
|
||||
typingMode: "instant",
|
||||
});
|
||||
|
||||
expect(runEmbeddedPiAgentMock).toHaveBeenCalledTimes(1);
|
||||
const call = runEmbeddedPiAgentMock.mock.calls[0]?.[0] as {
|
||||
authProfileId?: unknown;
|
||||
authProfileIdSource?: unknown;
|
||||
provider?: unknown;
|
||||
model?: unknown;
|
||||
};
|
||||
|
||||
expect(call.provider).toBe("anthropic");
|
||||
expect(call.model).toBe("claude-sonnet");
|
||||
expect(call.authProfileId).toBe("anthropic:openclaw");
|
||||
expect(call.authProfileIdSource).toBe("user");
|
||||
expect(sessionEntry.providerOverride).toBe("anthropic");
|
||||
expect(sessionEntry.modelOverride).toBe("claude-sonnet");
|
||||
expect(sessionEntry.authProfileOverride).toBe("anthropic:openclaw");
|
||||
expect(sessionEntry.authProfileOverrideSource).toBe("user");
|
||||
});
|
||||
});
|
||||
|
||||
describe("runReplyAgent auto-compaction token update", () => {
|
||||
type EmbeddedRunParams = {
|
||||
prompt?: string;
|
||||
|
||||
Reference in New Issue
Block a user