refactor: remove history limit alias

This commit is contained in:
Peter Steinberger
2026-05-03 14:30:48 +01:00
parent 928c70fb6b
commit db5f96cdc1
9 changed files with 149 additions and 306 deletions

View File

@@ -1,60 +0,0 @@
import { describe, expect, it } from "vitest";
import type { OpenClawConfig } from "../config/config.js";
import { getDmHistoryLimitFromSessionKey } from "./pi-embedded-runner/history.js";
describe("getDmHistoryLimitFromSessionKey", () => {
it("falls back to provider default when per-DM not set", () => {
const config = {
channels: {
telegram: {
dmHistoryLimit: 15,
dms: { "456": { historyLimit: 5 } },
},
},
} as OpenClawConfig;
expect(getDmHistoryLimitFromSessionKey("telegram:dm:123", config)).toBe(15);
});
it("returns per-DM override for agent-prefixed keys", () => {
const config = {
channels: {
telegram: {
dmHistoryLimit: 20,
dms: { "789": { historyLimit: 3 } },
},
},
} as OpenClawConfig;
expect(getDmHistoryLimitFromSessionKey("agent:main:telegram:dm:789", config)).toBe(3);
});
it("handles userId with colons (e.g., email)", () => {
const config = {
channels: {
msteams: {
dmHistoryLimit: 10,
dms: { "user@example.com": { historyLimit: 7 } },
},
},
} as OpenClawConfig;
expect(getDmHistoryLimitFromSessionKey("msteams:dm:user@example.com", config)).toBe(7);
});
it("returns undefined when per-DM historyLimit is not set", () => {
const config = {
channels: {
telegram: {
dms: { "123": {} },
},
},
} as OpenClawConfig;
expect(getDmHistoryLimitFromSessionKey("telegram:dm:123", config)).toBeUndefined();
});
it("returns 0 when per-DM historyLimit is explicitly 0 (unlimited)", () => {
const config = {
channels: {
telegram: {
dmHistoryLimit: 15,
dms: { "123": { historyLimit: 0 } },
},
},
} as OpenClawConfig;
expect(getDmHistoryLimitFromSessionKey("telegram:dm:123", config)).toBe(0);
});
});

View File

@@ -1,198 +0,0 @@
import { describe, expect, it } from "vitest";
import type { OpenClawConfig } from "../config/config.js";
import { getDmHistoryLimitFromSessionKey } from "./pi-embedded-runner/history.js";
describe("getDmHistoryLimitFromSessionKey", () => {
it("returns undefined when sessionKey is undefined", () => {
expect(getDmHistoryLimitFromSessionKey(undefined, {})).toBeUndefined();
});
it("returns undefined when config is undefined", () => {
expect(getDmHistoryLimitFromSessionKey("telegram:dm:123", undefined)).toBeUndefined();
});
it("returns dmHistoryLimit for telegram provider", () => {
const config = {
channels: { telegram: { dmHistoryLimit: 15 } },
} as OpenClawConfig;
expect(getDmHistoryLimitFromSessionKey("telegram:dm:123", config)).toBe(15);
});
it("returns dmHistoryLimit for whatsapp provider", () => {
const config = {
channels: { whatsapp: { dmHistoryLimit: 20 } },
} as OpenClawConfig;
expect(getDmHistoryLimitFromSessionKey("whatsapp:dm:123", config)).toBe(20);
});
it("returns dmHistoryLimit for agent-prefixed session keys", () => {
const config = {
channels: { telegram: { dmHistoryLimit: 10 } },
} as OpenClawConfig;
expect(getDmHistoryLimitFromSessionKey("agent:main:telegram:dm:123", config)).toBe(10);
});
it("strips thread suffix from dm session keys", () => {
const config = {
channels: { telegram: { dmHistoryLimit: 10, dms: { "123": { historyLimit: 7 } } } },
} as OpenClawConfig;
expect(getDmHistoryLimitFromSessionKey("agent:main:telegram:dm:123:thread:999", config)).toBe(
7,
);
expect(getDmHistoryLimitFromSessionKey("agent:main:telegram:dm:123:topic:555", config)).toBe(7);
expect(getDmHistoryLimitFromSessionKey("telegram:dm:123:thread:999", config)).toBe(7);
});
it("keeps non-numeric thread markers in dm ids", () => {
const config = {
channels: {
telegram: { dms: { "user:thread:abc": { historyLimit: 9 } } },
},
} as OpenClawConfig;
expect(getDmHistoryLimitFromSessionKey("agent:main:telegram:dm:user:thread:abc", config)).toBe(
9,
);
});
it("returns historyLimit for channel session kinds when configured", () => {
const config = {
channels: {
slack: { historyLimit: 10, dmHistoryLimit: 15 },
discord: { historyLimit: 8 },
},
} as OpenClawConfig;
expect(getDmHistoryLimitFromSessionKey("agent:beta:slack:channel:c1", config)).toBe(10);
expect(getDmHistoryLimitFromSessionKey("discord:channel:123456", config)).toBe(8);
});
it("returns undefined for non-dm/channel/group session kinds", () => {
const config = {
channels: {
telegram: { dmHistoryLimit: 15, historyLimit: 10 },
},
} as OpenClawConfig;
// "slash" is not dm, channel, or group
expect(getDmHistoryLimitFromSessionKey("telegram:slash:123", config)).toBeUndefined();
});
it("returns undefined for unknown provider", () => {
const config = {
channels: { telegram: { dmHistoryLimit: 15 } },
} as OpenClawConfig;
expect(getDmHistoryLimitFromSessionKey("unknown:dm:123", config)).toBeUndefined();
});
it("returns undefined when provider config has no dmHistoryLimit", () => {
const config = { channels: { telegram: {} } } as OpenClawConfig;
expect(getDmHistoryLimitFromSessionKey("telegram:dm:123", config)).toBeUndefined();
});
it("handles all supported providers", () => {
const providers = [
"telegram",
"whatsapp",
"discord",
"slack",
"signal",
"imessage",
"msteams",
"nextcloud-talk",
] as const;
for (const provider of providers) {
const config = {
channels: { [provider]: { dmHistoryLimit: 5 } },
} as OpenClawConfig;
expect(getDmHistoryLimitFromSessionKey(`${provider}:dm:123`, config)).toBe(5);
}
});
it("handles per-DM overrides for all supported providers", () => {
const providers = [
"telegram",
"whatsapp",
"discord",
"slack",
"signal",
"imessage",
"msteams",
"nextcloud-talk",
] as const;
for (const provider of providers) {
// Test per-DM override takes precedence
const configWithOverride = {
channels: {
[provider]: {
dmHistoryLimit: 20,
dms: { user123: { historyLimit: 7 } },
},
},
} as OpenClawConfig;
expect(getDmHistoryLimitFromSessionKey(`${provider}:dm:user123`, configWithOverride)).toBe(7);
// Test fallback to provider default when user not in dms
expect(getDmHistoryLimitFromSessionKey(`${provider}:dm:otheruser`, configWithOverride)).toBe(
20,
);
// Test with agent-prefixed key
expect(
getDmHistoryLimitFromSessionKey(`agent:main:${provider}:dm:user123`, configWithOverride),
).toBe(7);
}
});
it("returns per-DM override when set", () => {
const config = {
channels: {
telegram: {
dmHistoryLimit: 15,
dms: { "123": { historyLimit: 5 } },
},
},
} as OpenClawConfig;
expect(getDmHistoryLimitFromSessionKey("telegram:dm:123", config)).toBe(5);
});
it("returns historyLimit for channel sessions for all providers", () => {
const providers = [
"telegram",
"whatsapp",
"discord",
"slack",
"signal",
"imessage",
"msteams",
"nextcloud-talk",
] as const;
for (const provider of providers) {
const config = {
channels: { [provider]: { historyLimit: 12 } },
} as OpenClawConfig;
expect(getDmHistoryLimitFromSessionKey(`${provider}:channel:123`, config)).toBe(12);
expect(getDmHistoryLimitFromSessionKey(`agent:main:${provider}:channel:456`, config)).toBe(
12,
);
}
});
it("returns historyLimit for group sessions", () => {
const config = {
channels: {
discord: { historyLimit: 15 },
slack: { historyLimit: 10 },
},
} as OpenClawConfig;
expect(getDmHistoryLimitFromSessionKey("discord:group:123", config)).toBe(15);
expect(getDmHistoryLimitFromSessionKey("agent:main:slack:group:abc", config)).toBe(10);
});
it("returns undefined for channel sessions when historyLimit is not configured", () => {
const config = {
channels: {
discord: { dmHistoryLimit: 10 }, // only dmHistoryLimit, no historyLimit
},
} as OpenClawConfig;
expect(getDmHistoryLimitFromSessionKey("discord:channel:123", config)).toBeUndefined();
});
describe("backward compatibility", () => {
it("accepts both legacy :dm: and new :direct: session keys", () => {
const config = {
channels: { telegram: { dmHistoryLimit: 10 } },
} as OpenClawConfig;
// Legacy format with :dm:
expect(getDmHistoryLimitFromSessionKey("telegram:dm:123", config)).toBe(10);
expect(getDmHistoryLimitFromSessionKey("agent:main:telegram:dm:123", config)).toBe(10);
// New format with :direct:
expect(getDmHistoryLimitFromSessionKey("telegram:direct:123", config)).toBe(10);
expect(getDmHistoryLimitFromSessionKey("agent:main:telegram:direct:123", config)).toBe(10);
});
});
});

View File

@@ -1,31 +0,0 @@
import { describe, expect, it } from "vitest";
import type { OpenClawConfig } from "../config/config.js";
import { getDmHistoryLimitFromSessionKey } from "./pi-embedded-runner/history.js";
describe("getDmHistoryLimitFromSessionKey", () => {
it("keeps backward compatibility for dm/direct session kinds", () => {
const config = {
channels: { telegram: { dmHistoryLimit: 10 } },
} as OpenClawConfig;
expect(getDmHistoryLimitFromSessionKey("telegram:dm:123", config)).toBe(10);
expect(getDmHistoryLimitFromSessionKey("telegram:direct:123", config)).toBe(10);
});
it("returns historyLimit for channel and group session kinds", () => {
const config = {
channels: { discord: { historyLimit: 12, dmHistoryLimit: 5 } },
} as OpenClawConfig;
expect(getDmHistoryLimitFromSessionKey("discord:channel:123", config)).toBe(12);
expect(getDmHistoryLimitFromSessionKey("discord:group:456", config)).toBe(12);
});
it("returns undefined for unsupported session kinds", () => {
const config = {
channels: { discord: { historyLimit: 12, dmHistoryLimit: 5 } },
} as OpenClawConfig;
expect(getDmHistoryLimitFromSessionKey("discord:slash:123", config)).toBeUndefined();
});
});

View File

@@ -488,7 +488,7 @@ export async function loadCompactHooksHarness(): Promise<{
}));
vi.doMock("./history.js", () => ({
getDmHistoryLimitFromSessionKey: vi.fn(() => undefined),
getHistoryLimitFromSessionKey: vi.fn(() => undefined),
limitHistoryTurns: vi.fn((msgs: unknown[]) => msgs.slice(0, 2)),
}));

View File

@@ -131,7 +131,7 @@ import {
import { applyFinalEffectiveToolPolicy } from "./effective-tool-policy.js";
import { buildEmbeddedExtensionFactories } from "./extensions.js";
import { applyExtraParamsToAgent } from "./extra-params.js";
import { getDmHistoryLimitFromSessionKey, limitHistoryTurns } from "./history.js";
import { getHistoryLimitFromSessionKey, limitHistoryTurns } from "./history.js";
import { log } from "./logger.js";
import { hardenManualCompactionBoundary } from "./manual-compaction-boundary.js";
import { buildEmbeddedMessageActionDiscoveryInput } from "./message-action-discovery-input.js";
@@ -1096,7 +1096,7 @@ async function compactEmbeddedPiSessionDirectOnce(
const originalMessages = session.messages.slice();
const truncated = limitHistoryTurns(
session.messages,
getDmHistoryLimitFromSessionKey(params.sessionKey, params.config),
getHistoryLimitFromSessionKey(params.sessionKey, params.config),
);
// Re-run tool_use/tool_result pairing repair after truncation, since
// limitHistoryTurns can orphan tool_result blocks by removing the

View File

@@ -1,4 +1,5 @@
import { describe, expect, it } from "vitest";
import type { OpenClawConfig } from "../../config/config.js";
import { getHistoryLimitFromSessionKey } from "./history.js";
describe("getHistoryLimitFromSessionKey", () => {
@@ -13,4 +14,141 @@ describe("getHistoryLimitFromSessionKey", () => {
}),
).toBe(17);
});
it("returns undefined when sessionKey or config is undefined", () => {
expect(getHistoryLimitFromSessionKey(undefined, {})).toBeUndefined();
expect(getHistoryLimitFromSessionKey("telegram:dm:123", undefined)).toBeUndefined();
});
it("returns dmHistoryLimit for direct message sessions", () => {
const config = {
channels: {
telegram: { dmHistoryLimit: 15 },
whatsapp: { dmHistoryLimit: 20 },
},
} as OpenClawConfig;
expect(getHistoryLimitFromSessionKey("telegram:dm:123", config)).toBe(15);
expect(getHistoryLimitFromSessionKey("whatsapp:dm:123", config)).toBe(20);
expect(getHistoryLimitFromSessionKey("agent:main:telegram:dm:123", config)).toBe(15);
});
it("keeps backward compatibility for dm and direct session kinds", () => {
const config = {
channels: { telegram: { dmHistoryLimit: 10 } },
} as OpenClawConfig;
expect(getHistoryLimitFromSessionKey("telegram:dm:123", config)).toBe(10);
expect(getHistoryLimitFromSessionKey("agent:main:telegram:dm:123", config)).toBe(10);
expect(getHistoryLimitFromSessionKey("telegram:direct:123", config)).toBe(10);
expect(getHistoryLimitFromSessionKey("agent:main:telegram:direct:123", config)).toBe(10);
});
it("strips numeric thread and topic suffixes from direct message session keys", () => {
const config = {
channels: { telegram: { dmHistoryLimit: 10, dms: { "123": { historyLimit: 7 } } } },
} as OpenClawConfig;
expect(getHistoryLimitFromSessionKey("agent:main:telegram:dm:123:thread:999", config)).toBe(7);
expect(getHistoryLimitFromSessionKey("agent:main:telegram:dm:123:topic:555", config)).toBe(7);
expect(getHistoryLimitFromSessionKey("telegram:dm:123:thread:999", config)).toBe(7);
});
it("keeps non-numeric thread markers in direct message ids", () => {
const config = {
channels: {
telegram: { dms: { "user:thread:abc": { historyLimit: 9 } } },
},
} as OpenClawConfig;
expect(getHistoryLimitFromSessionKey("agent:main:telegram:dm:user:thread:abc", config)).toBe(9);
});
it("uses per-DM overrides before provider defaults", () => {
const config = {
channels: {
telegram: {
dmHistoryLimit: 15,
dms: {
"123": { historyLimit: 5 },
"456": {},
"789": { historyLimit: 0 },
},
},
},
} as OpenClawConfig;
expect(getHistoryLimitFromSessionKey("telegram:dm:123", config)).toBe(5);
expect(getHistoryLimitFromSessionKey("telegram:dm:456", config)).toBe(15);
expect(getHistoryLimitFromSessionKey("telegram:dm:789", config)).toBe(0);
expect(getHistoryLimitFromSessionKey("telegram:dm:other", config)).toBe(15);
});
it("returns per-DM overrides for agent-prefixed keys and colon-containing ids", () => {
const config = {
channels: {
telegram: {
dmHistoryLimit: 20,
dms: { "789": { historyLimit: 3 } },
},
msteams: {
dmHistoryLimit: 10,
dms: { "user@example.com": { historyLimit: 7 } },
},
},
} as OpenClawConfig;
expect(getHistoryLimitFromSessionKey("agent:main:telegram:dm:789", config)).toBe(3);
expect(getHistoryLimitFromSessionKey("msteams:dm:user@example.com", config)).toBe(7);
});
it("returns historyLimit for channel and group sessions", () => {
const config = {
channels: {
slack: { historyLimit: 10, dmHistoryLimit: 15 },
discord: { historyLimit: 8 },
},
} as OpenClawConfig;
expect(getHistoryLimitFromSessionKey("agent:beta:slack:channel:c1", config)).toBe(10);
expect(getHistoryLimitFromSessionKey("discord:channel:123456", config)).toBe(8);
expect(getHistoryLimitFromSessionKey("discord:group:123", config)).toBe(8);
});
it("returns undefined for unsupported session kinds, unknown providers, and missing limits", () => {
const config = {
channels: {
telegram: { historyLimit: 10 },
discord: { dmHistoryLimit: 10 },
},
} as OpenClawConfig;
expect(getHistoryLimitFromSessionKey("telegram:slash:123", config)).toBeUndefined();
expect(getHistoryLimitFromSessionKey("unknown:dm:123", config)).toBeUndefined();
expect(getHistoryLimitFromSessionKey("discord:channel:123", config)).toBeUndefined();
expect(getHistoryLimitFromSessionKey("telegram:dm:123", config)).toBeUndefined();
});
it("handles supported provider ids for DM and channel history limits", () => {
const providers = [
"telegram",
"whatsapp",
"discord",
"slack",
"signal",
"imessage",
"msteams",
"nextcloud-talk",
] as const;
for (const provider of providers) {
const config = {
channels: { [provider]: { dmHistoryLimit: 5, historyLimit: 12 } },
} as OpenClawConfig;
expect(getHistoryLimitFromSessionKey(`${provider}:dm:123`, config)).toBe(5);
expect(getHistoryLimitFromSessionKey(`${provider}:channel:123`, config)).toBe(12);
expect(getHistoryLimitFromSessionKey(`agent:main:${provider}:channel:456`, config)).toBe(12);
}
});
});

View File

@@ -116,9 +116,3 @@ export function getHistoryLimitFromSessionKey(
return undefined;
}
/**
* @deprecated Use getHistoryLimitFromSessionKey instead.
* Alias for backward compatibility.
*/
export const getDmHistoryLimitFromSessionKey = getHistoryLimitFromSessionKey;

View File

@@ -77,7 +77,7 @@ type AttemptSpawnWorkspaceHoisted = {
getGlobalHookRunnerMock: Mock<() => unknown>;
initializeGlobalHookRunnerMock: UnknownMock;
runContextEngineMaintenanceMock: AsyncContextEngineMaintenanceMock;
getDmHistoryLimitFromSessionKeyMock: Mock<
getHistoryLimitFromSessionKeyMock: Mock<
(sessionKey: string | undefined, config: unknown) => number | undefined
>;
limitHistoryTurnsMock: Mock<<T>(messages: T, limit: number | undefined) => T>;
@@ -148,7 +148,7 @@ const hoisted = vi.hoisted((): AttemptSpawnWorkspaceHoisted => {
const getGlobalHookRunnerMock = vi.fn<() => unknown>(() => undefined);
const initializeGlobalHookRunnerMock = vi.fn();
const runContextEngineMaintenanceMock = vi.fn(async (_params?: unknown) => undefined);
const getDmHistoryLimitFromSessionKeyMock = vi.fn<
const getHistoryLimitFromSessionKeyMock = vi.fn<
(sessionKey: string | undefined, config: unknown) => number | undefined
>(() => undefined);
const limitHistoryTurnsMock = vi.fn<<T>(messages: T, limit: number | undefined) => T>(
@@ -187,7 +187,7 @@ const hoisted = vi.hoisted((): AttemptSpawnWorkspaceHoisted => {
getGlobalHookRunnerMock,
initializeGlobalHookRunnerMock,
runContextEngineMaintenanceMock,
getDmHistoryLimitFromSessionKeyMock,
getHistoryLimitFromSessionKeyMock,
limitHistoryTurnsMock,
preemptiveCompactionCalls,
sessionManager,
@@ -597,8 +597,8 @@ vi.mock("../compaction-safety-timeout.js", () => ({
}));
vi.mock("../history.js", () => ({
getDmHistoryLimitFromSessionKey: (sessionKey: string | undefined, config: unknown) =>
hoisted.getDmHistoryLimitFromSessionKeyMock(sessionKey, config),
getHistoryLimitFromSessionKey: (sessionKey: string | undefined, config: unknown) =>
hoisted.getHistoryLimitFromSessionKeyMock(sessionKey, config),
limitHistoryTurns: (messages: unknown, limit: number | undefined) =>
hoisted.limitHistoryTurnsMock(messages, limit),
}));
@@ -805,7 +805,7 @@ export function resetEmbeddedAttemptHarness(
hoisted.supportsModelToolsMock.mockReset().mockReturnValue(true);
hoisted.getGlobalHookRunnerMock.mockReset().mockReturnValue(undefined);
hoisted.runContextEngineMaintenanceMock.mockReset().mockResolvedValue(undefined);
hoisted.getDmHistoryLimitFromSessionKeyMock.mockReset().mockReturnValue(undefined);
hoisted.getHistoryLimitFromSessionKeyMock.mockReset().mockReturnValue(undefined);
hoisted.limitHistoryTurnsMock.mockReset().mockImplementation((messages) => messages);
hoisted.preemptiveCompactionCalls.length = 0;
hoisted.sessionManager.getLeafEntry.mockReset().mockReturnValue(null);

View File

@@ -179,7 +179,7 @@ import {
resolvePreparedExtraParams,
} from "../extra-params.js";
import { prepareGooglePromptCacheStreamFn } from "../google-prompt-cache.js";
import { getDmHistoryLimitFromSessionKey, limitHistoryTurns } from "../history.js";
import { getHistoryLimitFromSessionKey, limitHistoryTurns } from "../history.js";
import { log } from "../logger.js";
import { buildEmbeddedMessageActionDiscoveryInput } from "../message-action-discovery-input.js";
import {
@@ -2214,7 +2214,7 @@ export async function runEmbeddedAttempt(
);
const truncated = limitHistoryTurns(
heartbeatFiltered,
getDmHistoryLimitFromSessionKey(params.sessionKey, params.config),
getHistoryLimitFromSessionKey(params.sessionKey, params.config),
);
// Re-run tool_use/tool_result pairing repair after truncation, since
// limitHistoryTurns can orphan tool_result blocks by removing the