refactor: harden plugin install flow and main DM route pinning

This commit is contained in:
Peter Steinberger
2026-03-02 21:22:32 +00:00
parent af637deed1
commit b782ecb7eb
22 changed files with 737 additions and 269 deletions

View File

@@ -103,4 +103,32 @@ describe("recordInboundSession", () => {
}),
);
});
it("skips last-route updates when main DM owner pin mismatches sender", async () => {
const { recordInboundSession } = await import("./session.js");
const onSkip = vi.fn();
await recordInboundSession({
storePath: "/tmp/openclaw-session-store.json",
sessionKey: "agent:main:telegram:1234:thread:42",
ctx,
updateLastRoute: {
sessionKey: "agent:main:main",
channel: "telegram",
to: "telegram:1234",
mainDmOwnerPin: {
ownerRecipient: "1234",
senderRecipient: "9999",
onSkip,
},
},
onRecordError: vi.fn(),
});
expect(updateLastRouteMock).not.toHaveBeenCalled();
expect(onSkip).toHaveBeenCalledWith({
ownerRecipient: "1234",
senderRecipient: "9999",
});
});
});

View File

@@ -16,8 +16,28 @@ export type InboundLastRouteUpdate = {
to: string;
accountId?: string;
threadId?: string | number;
mainDmOwnerPin?: {
ownerRecipient: string;
senderRecipient: string;
onSkip?: (params: { ownerRecipient: string; senderRecipient: string }) => void;
};
};
function shouldSkipPinnedMainDmRouteUpdate(
pin: InboundLastRouteUpdate["mainDmOwnerPin"] | undefined,
): boolean {
if (!pin) {
return false;
}
const owner = pin.ownerRecipient.trim().toLowerCase();
const sender = pin.senderRecipient.trim().toLowerCase();
if (!owner || !sender || owner === sender) {
return false;
}
pin.onSkip?.({ ownerRecipient: pin.ownerRecipient, senderRecipient: pin.senderRecipient });
return true;
}
export async function recordInboundSession(params: {
storePath: string;
sessionKey: string;
@@ -41,6 +61,9 @@ export async function recordInboundSession(params: {
if (!update) {
return;
}
if (shouldSkipPinnedMainDmRouteUpdate(update.mainDmOwnerPin)) {
return;
}
const targetSessionKey = normalizeSessionStoreKey(update.sessionKey);
await updateLastRoute({
storePath,