fix: recover telegram sends from stale thread ids

This commit is contained in:
Ayaan Zaidi
2026-02-09 08:35:53 +05:30
committed by Ayaan Zaidi
parent 5ac1be9cb6
commit d7bd68ff24
5 changed files with 343 additions and 67 deletions
+45
View File
@@ -176,6 +176,51 @@ describe("sessions", () => {
});
});
it("updateLastRoute clears threadId when deliveryContext explicitly omits it", async () => {
const mainSessionKey = "agent:main:main";
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-sessions-"));
const storePath = path.join(dir, "sessions.json");
await fs.writeFile(
storePath,
JSON.stringify(
{
[mainSessionKey]: {
sessionId: "sess-1",
updatedAt: 123,
deliveryContext: {
channel: "telegram",
to: "222",
threadId: "42",
},
lastChannel: "telegram",
lastTo: "222",
lastThreadId: "42",
},
},
null,
2,
),
"utf-8",
);
await updateLastRoute({
storePath,
sessionKey: mainSessionKey,
deliveryContext: {
channel: "telegram",
to: "222",
threadId: undefined,
},
});
const store = loadSessionStore(storePath);
expect(store[mainSessionKey]?.deliveryContext).toEqual({
channel: "telegram",
to: "222",
});
expect(store[mainSessionKey]?.lastThreadId).toBeUndefined();
});
it("updateLastRoute records origin + group metadata when ctx is provided", async () => {
const sessionKey = "agent:main:whatsapp:group:123@g.us";
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-sessions-"));
+18 -1
View File
@@ -86,6 +86,15 @@ function normalizeSessionEntryDelivery(entry: SessionEntry): SessionEntry {
};
}
function removeThreadFromDeliveryContext(context?: DeliveryContext): DeliveryContext | undefined {
if (!context || context.threadId == null) {
return context;
}
const next: DeliveryContext = { ...context };
delete next.threadId;
return next;
}
function normalizeSessionStore(store: Record<string, SessionEntry>): void {
for (const [key, entry] of Object.entries(store)) {
if (!entry) {
@@ -430,7 +439,15 @@ export async function updateLastRoute(params: {
threadId,
});
const mergedInput = mergeDeliveryContext(explicitContext, inlineContext);
const merged = mergeDeliveryContext(mergedInput, deliveryContextFromSession(existing));
const explicitDeliveryContext = params.deliveryContext;
const clearThreadFromFallback =
explicitDeliveryContext != null &&
Object.prototype.hasOwnProperty.call(explicitDeliveryContext, "threadId") &&
explicitDeliveryContext.threadId == null;
const fallbackContext = clearThreadFromFallback
? removeThreadFromDeliveryContext(deliveryContextFromSession(existing))
: deliveryContextFromSession(existing);
const merged = mergeDeliveryContext(mergedInput, fallbackContext);
const normalized = normalizeSessionDeliveryFields({
deliveryContext: {
channel: merged?.channel,