Skip to content

Commit 0d41316

Browse files
committed
fix(state): preserve ttl for plugin-state imports
1 parent 9297c20 commit 0d41316

4 files changed

Lines changed: 27 additions & 22 deletions

File tree

‎extensions/telegram/src/state-migrations.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
resolveTelegramMessageDispatchLegacyPath,
1818
TELEGRAM_MESSAGE_DISPATCH_DEDUPE_MAX_ENTRIES,
1919
TELEGRAM_MESSAGE_DISPATCH_DEDUPE_NAMESPACE_PREFIX,
20+
TELEGRAM_MESSAGE_DISPATCH_DEDUPE_TTL_MS,
2021
} from "./message-dispatch-dedupe.js";
2122
import { detectTelegramLegacyStateMigrations } from "./state-migrations.js";
2223
import {
@@ -494,6 +495,7 @@ describe("telegram state migrations", () => {
494495
const targetStore = createPluginStateSyncKeyedStoreForTests("telegram", {
495496
namespace: dispatchNamespace,
496497
maxEntries: TELEGRAM_MESSAGE_DISPATCH_DEDUPE_MAX_ENTRIES,
498+
defaultTtlMs: TELEGRAM_MESSAGE_DISPATCH_DEDUPE_TTL_MS,
497499
env,
498500
});
499501
for (const entry of entries) {

‎extensions/telegram/src/state-migrations.ts‎

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ function hasCurrentMessageDispatchDedupeTargets(params: {
170170
const store = createPluginStateSyncKeyedStore<PersistentDedupeEntry>("telegram", {
171171
namespace: params.namespace,
172172
maxEntries: TELEGRAM_MESSAGE_DISPATCH_DEDUPE_MAX_ENTRIES,
173+
defaultTtlMs: TELEGRAM_MESSAGE_DISPATCH_DEDUPE_TTL_MS,
173174
env: params.env,
174175
});
175176
const existingByKey = new Map(store.entries().map((entry) => [entry.key, entry.value]));
@@ -436,7 +437,7 @@ function detectTelegramMessageDispatchLegacyStateMigration(params: {
436437
namespace: accountId,
437438
}),
438439
}));
439-
const jsonPlans = sources.flatMap((source) => {
440+
const jsonPlans: ChannelLegacyStateMigrationPlan[] = sources.flatMap((source) => {
440441
const sourcePath = source.sourcePath;
441442
if (!fileExists(sourcePath)) {
442443
return [];
@@ -449,6 +450,7 @@ function detectTelegramMessageDispatchLegacyStateMigration(params: {
449450
pluginId: "telegram",
450451
namespace,
451452
maxEntries: TELEGRAM_MESSAGE_DISPATCH_DEDUPE_MAX_ENTRIES,
453+
defaultTtlMs: TELEGRAM_MESSAGE_DISPATCH_DEDUPE_TTL_MS,
452454
scopeKey: "",
453455
cleanupSource: "rename",
454456
preview: `- Telegram message dispatch dedupe: ${sourcePath} → plugin state (${namespace})`,
@@ -476,27 +478,26 @@ function detectTelegramMessageDispatchLegacyStateMigration(params: {
476478
) {
477479
return jsonPlans;
478480
}
479-
return [
480-
...jsonPlans,
481-
{
482-
kind: "plugin-state-import",
483-
label: "Telegram message dispatch dedupe",
484-
sourcePath: `plugin state:${TELEGRAM_MESSAGE_DISPATCH_LEGACY_BUCKET_NAMESPACE}:${accountId}`,
485-
targetPath: `plugin state:${namespace}`,
486-
pluginId: "telegram",
487-
namespace,
488-
maxEntries: TELEGRAM_MESSAGE_DISPATCH_DEDUPE_MAX_ENTRIES,
489-
scopeKey: "",
490-
preview: `- Telegram message dispatch dedupe: plugin state (${TELEGRAM_MESSAGE_DISPATCH_LEGACY_BUCKET_NAMESPACE}) → plugin state (${namespace})`,
491-
shouldReplaceExistingEntry: ({ existingValue, incomingValue }) =>
492-
shouldReplacePersistentDedupeEntry({ existingValue, incomingValue }),
493-
readEntries: () =>
494-
listTelegramLegacyMessageDispatchPluginStateEntries({
495-
accountId,
496-
env,
497-
}),
498-
},
499-
];
481+
const pluginStatePlan: ChannelLegacyStateMigrationPlan = {
482+
kind: "plugin-state-import",
483+
label: "Telegram message dispatch dedupe",
484+
sourcePath: `plugin state:${TELEGRAM_MESSAGE_DISPATCH_LEGACY_BUCKET_NAMESPACE}:${accountId}`,
485+
targetPath: `plugin state:${namespace}`,
486+
pluginId: "telegram",
487+
namespace,
488+
maxEntries: TELEGRAM_MESSAGE_DISPATCH_DEDUPE_MAX_ENTRIES,
489+
defaultTtlMs: TELEGRAM_MESSAGE_DISPATCH_DEDUPE_TTL_MS,
490+
scopeKey: "",
491+
preview: `- Telegram message dispatch dedupe: plugin state (${TELEGRAM_MESSAGE_DISPATCH_LEGACY_BUCKET_NAMESPACE}) → plugin state (${namespace})`,
492+
shouldReplaceExistingEntry: ({ existingValue, incomingValue }) =>
493+
shouldReplacePersistentDedupeEntry({ existingValue, incomingValue }),
494+
readEntries: () =>
495+
listTelegramLegacyMessageDispatchPluginStateEntries({
496+
accountId,
497+
env,
498+
}),
499+
};
500+
return [...jsonPlans, pluginStatePlan];
500501
});
501502
}
502503

‎src/channels/plugins/legacy-state-migration.types.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export type ChannelLegacyStateMigrationPlan =
1313
pluginId: string;
1414
namespace: string;
1515
maxEntries: number;
16+
defaultTtlMs?: number;
1617
scopeKey: string;
1718
stateDir?: string;
1819
cleanupSource?: "rename";

‎src/infra/state-migrations.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,6 +1581,7 @@ async function runLegacyMigrationPlans(
15811581
const store = createPluginStateKeyedStore<unknown>(plan.pluginId, {
15821582
namespace: plan.namespace,
15831583
maxEntries: plan.maxEntries,
1584+
...(plan.defaultTtlMs != null ? { defaultTtlMs: plan.defaultTtlMs } : {}),
15841585
});
15851586
try {
15861587
storeEntries = await store.entries();

0 commit comments

Comments
 (0)