refactor(session): centralize thread reset detection

Co-authored-by: Austin Mudd <austinm911@gmail.com>
This commit is contained in:
Peter Steinberger
2026-01-18 06:54:55 +00:00
parent b5ddf08763
commit f86b24c511
6 changed files with 201 additions and 41 deletions

View File

@@ -202,6 +202,36 @@ describe("initSessionState reset policy", () => {
}
});
it("treats sessions as stale before the daily reset when updated before yesterday's boundary", async () => {
vi.useFakeTimers();
vi.setSystemTime(new Date(2026, 0, 18, 3, 0, 0));
try {
const root = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-reset-daily-edge-"));
const storePath = path.join(root, "sessions.json");
const sessionKey = "agent:main:whatsapp:dm:S-edge";
const existingSessionId = "daily-edge-session";
await saveSessionStore(storePath, {
[sessionKey]: {
sessionId: existingSessionId,
updatedAt: new Date(2026, 0, 17, 3, 30, 0).getTime(),
},
});
const cfg = { session: { store: storePath } } as ClawdbotConfig;
const result = await initSessionState({
ctx: { Body: "hello", SessionKey: sessionKey },
cfg,
commandAuthorized: true,
});
expect(result.isNewSession).toBe(true);
expect(result.sessionId).not.toBe(existingSessionId);
} finally {
vi.useRealTimers();
}
});
it("expires sessions when idle timeout wins over daily reset", async () => {
vi.useFakeTimers();
vi.setSystemTime(new Date(2026, 0, 18, 5, 30, 0));
@@ -273,6 +303,76 @@ describe("initSessionState reset policy", () => {
}
});
it("detects thread sessions without thread key suffix", async () => {
vi.useFakeTimers();
vi.setSystemTime(new Date(2026, 0, 18, 5, 0, 0));
try {
const root = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-reset-thread-nosuffix-"));
const storePath = path.join(root, "sessions.json");
const sessionKey = "agent:main:discord:channel:C1";
const existingSessionId = "thread-nosuffix";
await saveSessionStore(storePath, {
[sessionKey]: {
sessionId: existingSessionId,
updatedAt: new Date(2026, 0, 18, 3, 0, 0).getTime(),
},
});
const cfg = {
session: {
store: storePath,
resetByType: { thread: { mode: "idle", idleMinutes: 180 } },
},
} as ClawdbotConfig;
const result = await initSessionState({
ctx: { Body: "reply", SessionKey: sessionKey, ThreadLabel: "Discord thread" },
cfg,
commandAuthorized: true,
});
expect(result.isNewSession).toBe(false);
expect(result.sessionId).toBe(existingSessionId);
} finally {
vi.useRealTimers();
}
});
it("defaults to daily resets when only resetByType is configured", async () => {
vi.useFakeTimers();
vi.setSystemTime(new Date(2026, 0, 18, 5, 0, 0));
try {
const root = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-reset-type-default-"));
const storePath = path.join(root, "sessions.json");
const sessionKey = "agent:main:whatsapp:dm:S4";
const existingSessionId = "type-default-session";
await saveSessionStore(storePath, {
[sessionKey]: {
sessionId: existingSessionId,
updatedAt: new Date(2026, 0, 18, 3, 0, 0).getTime(),
},
});
const cfg = {
session: {
store: storePath,
resetByType: { thread: { mode: "idle", idleMinutes: 60 } },
},
} as ClawdbotConfig;
const result = await initSessionState({
ctx: { Body: "hello", SessionKey: sessionKey },
cfg,
commandAuthorized: true,
});
expect(result.isNewSession).toBe(true);
expect(result.sessionId).not.toBe(existingSessionId);
} finally {
vi.useRealTimers();
}
});
it("keeps legacy idleMinutes behavior without reset config", async () => {
vi.useFakeTimers();
vi.setSystemTime(new Date(2026, 0, 18, 5, 0, 0));

View File

@@ -9,9 +9,9 @@ import {
DEFAULT_RESET_TRIGGERS,
deriveSessionMetaPatch,
evaluateSessionFreshness,
isThreadSessionKey,
type GroupKeyResolution,
loadSessionStore,
resolveThreadFlag,
resolveSessionResetPolicy,
resolveSessionResetType,
resolveGroupSessionKey,
@@ -173,12 +173,13 @@ export async function initSessionState(params: {
const entry = sessionStore[sessionKey];
const previousSessionEntry = resetTriggered && entry ? { ...entry } : undefined;
const now = Date.now();
const isThread =
ctx.MessageThreadId != null ||
Boolean(ctx.ThreadLabel?.trim()) ||
Boolean(ctx.ThreadStarterBody?.trim()) ||
Boolean(ctx.ParentSessionKey?.trim()) ||
isThreadSessionKey(sessionKey);
const isThread = resolveThreadFlag({
sessionKey,
messageThreadId: ctx.MessageThreadId,
threadLabel: ctx.ThreadLabel,
threadStarterBody: ctx.ThreadStarterBody,
parentSessionKey: ctx.ParentSessionKey,
});
const resetType = resolveSessionResetType({ sessionKey, isGroup, isThread });
const resetPolicy = resolveSessionResetPolicy({ sessionCfg, resetType });
const freshEntry = entry