Skip to content

Commit e87fb68

Browse files
steipetePeter Steinberger
andauthored
fix(channels): preserve thread participation expiry across restarts (#118630)
Co-authored-by: Peter Steinberger <[email protected]>
1 parent 5b7272f commit e87fb68

4 files changed

Lines changed: 38 additions & 6 deletions

File tree

‎extensions/mattermost/src/mattermost/thread-participation.test.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ describe("mattermost thread participation", () => {
4949
afterEach(() => {
5050
threadParticipationMemory.clear();
5151
resetPluginStateStoreForTests();
52+
vi.restoreAllMocks();
5253
});
5354

5455
it("remembers a thread the bot replied in", async () => {
@@ -85,10 +86,13 @@ describe("mattermost thread participation", () => {
8586
).resolves.toBe(false);
8687
});
8788

88-
it("recovers participation from the persistent store after the in-memory cache is lost", async () => {
89+
it("restores participation after a restart without extending its original expiry", async () => {
90+
const repliedAt = 1_711_406_400_000;
91+
const now = vi.spyOn(Date, "now").mockReturnValue(repliedAt);
8992
recordMattermostThreadParticipation("acct", "chan", "root-1");
9093
await flush();
91-
// Simulate a restart: in-memory cache cleared, persistent SQLite store intact.
94+
now.mockReturnValue(repliedAt + 7 * 24 * 60 * 60 * 1000 - 1000);
95+
// Simulate a restart near expiry: memory is lost, but the SQLite row is still valid.
9296
threadParticipationMemory.clear();
9397
await expect(
9498
hasMattermostThreadParticipationWithPersistence({
@@ -97,6 +101,15 @@ describe("mattermost thread participation", () => {
97101
threadRootId: "root-1",
98102
}),
99103
).resolves.toBe(true);
104+
105+
now.mockReturnValue(repliedAt + 7 * 24 * 60 * 60 * 1000 + 1000);
106+
await expect(
107+
hasMattermostThreadParticipationWithPersistence({
108+
accountId: "acct",
109+
channelId: "chan",
110+
threadRootId: "root-1",
111+
}),
112+
).resolves.toBe(false);
100113
});
101114

102115
it("degrades to in-memory only when the persistent store fails", async () => {

‎extensions/mattermost/src/mattermost/thread-participation.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ const threadParticipation = createPersistentDedupeCache<MattermostThreadParticip
3737
"thread-participation-state",
3838
"Mattermost persistent thread participation state failed",
3939
),
40+
// Restoring participation must not extend its original mention-bypass window.
41+
readTimestamp: ({ repliedAt }) => repliedAt,
4042
},
4143
});
4244

‎extensions/slack/src/sent-thread-cache.test.ts‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,14 @@ describe("slack sent-thread-cache", () => {
9393
expect(hasSlackThreadParticipation("A1", "C123", "1700000000.005000")).toBe(true);
9494
});
9595

96-
it("writes and reads persistent thread participation when runtime state is available", async () => {
96+
it("restores persistent thread participation without extending its original expiry", async () => {
97+
const repliedAt = 1_711_406_400_000;
98+
const ttlMs = 24 * 60 * 60 * 1000;
99+
const now = vi.spyOn(Date, "now").mockReturnValue(repliedAt);
97100
const register = vi.fn().mockResolvedValue(undefined);
98-
const lookup = vi.fn().mockResolvedValue({ repliedAt: 123 });
101+
const lookup = vi
102+
.fn()
103+
.mockImplementation(async () => (Date.now() < repliedAt + ttlMs ? { repliedAt } : undefined));
99104
const openKeyedStore = vi.fn(() => ({
100105
register,
101106
lookup,
@@ -109,14 +114,14 @@ describe("slack sent-thread-cache", () => {
109114
logging: { getChildLogger: () => ({ warn: vi.fn() }) },
110115
} as never);
111116

112-
vi.spyOn(Date, "now").mockReturnValue(1_711_406_400_000);
113117
recordSlackThreadParticipation("A1", "C123", "1700000000.000002");
114118

115119
await vi.waitFor(() => expect(register).toHaveBeenCalledTimes(1));
116120
expect(register).toHaveBeenCalledWith("A1:C123:1700000000.000002", {
117-
repliedAt: 1_711_406_400_000,
121+
repliedAt,
118122
});
119123

124+
now.mockReturnValue(repliedAt + ttlMs - 1000);
120125
clearSlackThreadParticipationCache();
121126
await expect(
122127
hasSlackThreadParticipationWithPersistence({
@@ -137,6 +142,16 @@ describe("slack sent-thread-cache", () => {
137142
}),
138143
).resolves.toBe(true);
139144
expect(lookup).not.toHaveBeenCalled();
145+
146+
now.mockReturnValue(repliedAt + ttlMs + 1000);
147+
await expect(
148+
hasSlackThreadParticipationWithPersistence({
149+
accountId: "A1",
150+
channelId: "C123",
151+
threadTs: "1700000000.000002",
152+
}),
153+
).resolves.toBe(false);
154+
expect(lookup).toHaveBeenCalledWith("A1:C123:1700000000.000002");
140155
});
141156

142157
it("falls back to in-memory thread participation when persistent state cannot open", async () => {

‎extensions/slack/src/sent-thread-cache.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ const threadParticipation = createPersistentDedupeCache<SlackThreadParticipation
3737
"thread-participation-state",
3838
"Slack persistent thread participation state failed",
3939
),
40+
// Restoring participation must not extend its original mention-bypass window.
41+
readTimestamp: ({ repliedAt }) => repliedAt,
4042
},
4143
});
4244

0 commit comments

Comments
 (0)