fix: strip repeated heartbeat ok tails

This commit is contained in:
Peter Steinberger
2025-12-23 03:12:24 +01:00
parent 4af08b1606
commit 56245d5646
3 changed files with 33 additions and 3 deletions

View File

@@ -134,6 +134,29 @@ describe("heartbeat helpers", () => {
});
});
it("strips repeated OK tails after heartbeat token", () => {
expect(stripHeartbeatToken("HEARTBEAT_OK_OK_OK")).toEqual({
shouldSkip: true,
text: "",
});
expect(stripHeartbeatToken("HEARTBEAT_OK_OK")).toEqual({
shouldSkip: true,
text: "",
});
expect(stripHeartbeatToken("HEARTBEAT_OK _OK")).toEqual({
shouldSkip: true,
text: "",
});
expect(stripHeartbeatToken("HEARTBEAT_OK OK")).toEqual({
shouldSkip: true,
text: "",
});
expect(stripHeartbeatToken("ALERT HEARTBEAT_OK_OK")).toEqual({
shouldSkip: false,
text: "ALERT",
});
});
it("resolves heartbeat minutes with default and overrides", () => {
const cfgBase: ClawdisConfig = {
inbound: {},

View File

@@ -203,10 +203,16 @@ export function stripHeartbeatToken(raw?: string) {
const trimmed = raw.trim();
if (!trimmed) return { shouldSkip: true, text: "" };
if (trimmed === HEARTBEAT_TOKEN) return { shouldSkip: true, text: "" };
const withoutToken = trimmed.replaceAll(HEARTBEAT_TOKEN, "").trim();
const hadToken = trimmed.includes(HEARTBEAT_TOKEN);
let withoutToken = trimmed.replaceAll(HEARTBEAT_TOKEN, "").trim();
if (hadToken && withoutToken) {
// LLMs sometimes echo malformed HEARTBEAT_OK_OK... tails; strip trailing OK runs to avoid spam.
withoutToken = withoutToken.replace(/[\s_]*OK(?:[\s_]*OK)*$/gi, "").trim();
}
const shouldSkip = withoutToken.length === 0;
return {
shouldSkip: withoutToken.length === 0,
text: withoutToken || trimmed,
shouldSkip,
text: shouldSkip ? "" : withoutToken || trimmed,
};
}