From 1192e3ccf9178a2caa563a17fa7a3d240d6f52cd Mon Sep 17 00:00:00 2001 From: Rai Butera Date: Thu, 12 Mar 2026 07:46:31 +0000 Subject: [PATCH] test: harden sentinel edge cases (empty channel, leaked state, non-discord external) Pre-empt edge case regressions flagged in review: - empty string persistedLastChannel treated as absent (no mismatch risk) - sentinel value leaked into persistedLastChannel returns undefined (corrupted state guard) - non-discord external channels (telegram etc.) correctly preserve persistedLastTo --- src/auto-reply/reply/session-delivery.test.ts | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/auto-reply/reply/session-delivery.test.ts b/src/auto-reply/reply/session-delivery.test.ts index 34f063ce365..6b4caaa555e 100644 --- a/src/auto-reply/reply/session-delivery.test.ts +++ b/src/auto-reply/reply/session-delivery.test.ts @@ -105,6 +105,46 @@ describe("INTER_SESSION_CHANNEL sentinel routing", () => { ).toBe("channel:receiver-discord-channel"); }); + it("returns undefined from resolveLastToRaw when persistedLastChannel is empty string", () => { + // Empty string is not an external routing channel — treat same as absent. + // Returning stale persistedLastTo would risk a channel/to mismatch. + expect( + resolveLastToRaw({ + originatingChannelRaw: INTER_SESSION_CHANNEL, + persistedLastChannel: "", + persistedLastTo: "channel:some-target", + sessionKey: "agent:navi:main", + }), + ).toBeUndefined(); + }); + + it("returns undefined from resolveLastToRaw when persistedLastChannel is the sentinel itself (leaked state)", () => { + // Guard against corrupted persisted state where persistedLastChannel was + // accidentally set to "inter_session". The sentinel is not a deliverable + // channel and should never be treated as an established external route. + expect( + resolveLastToRaw({ + originatingChannelRaw: INTER_SESSION_CHANNEL, + persistedLastChannel: INTER_SESSION_CHANNEL, + persistedLastTo: "channel:some-target", + sessionKey: "agent:navi:main", + }), + ).toBeUndefined(); + }); + + it("preserves persistedLastTo for non-discord external channels (e.g. telegram)", () => { + // The sentinel path should work for any external channel, not just discord. + expect( + resolveLastToRaw({ + originatingChannelRaw: INTER_SESSION_CHANNEL, + originatingToRaw: "channel:sender-telegram", + persistedLastChannel: "telegram", + persistedLastTo: "user:987654321", + sessionKey: "agent:navi:main", + }), + ).toBe("user:987654321"); + }); + it("does not treat a real deliverable channel named 'inter_session' as the sentinel (Codex P2 guard)", () => { // isInterSessionChannel guards against plugin channel collision: // if a real channel plugin registers with id="inter_session", it must not