Skip to content

Commit 22bdda2

Browse files
committed
chore(deadcode): share nostr state account ids
1 parent 2755112 commit 22bdda2

5 files changed

Lines changed: 66 additions & 22 deletions

File tree

‎extensions/nostr/doctor-contract-api.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,33 @@ describe("nostr doctor state migration", () => {
102102
lastPublishResults: { "wss://relay.example": "ok" },
103103
});
104104
});
105+
106+
it("preserves legacy account key bytes when importing state files", async () => {
107+
const nostrDir = path.join(stateDir, "nostr");
108+
const busPath = path.join(nostrDir, "bus-state-Team.A.json");
109+
await fs.mkdir(nostrDir, { recursive: true });
110+
await fs.writeFile(
111+
busPath,
112+
JSON.stringify({
113+
version: 1,
114+
lastProcessedAt: 1700,
115+
gatewayStartedAt: 1600,
116+
}),
117+
);
118+
119+
const context = createDoctorContext(env);
120+
await stateMigrations[0].migrateLegacyState({
121+
config: {},
122+
env,
123+
stateDir,
124+
oauthDir: path.join(stateDir, "oauth"),
125+
context,
126+
});
127+
128+
const store = context.openPluginStateKeyedStore({ namespace: "bus-state", maxEntries: 256 });
129+
await expect(store.lookup("Team.A")).resolves.toMatchObject({
130+
lastProcessedAt: 1700,
131+
});
132+
await expect(store.lookup("team-a")).resolves.toBeUndefined();
133+
});
105134
});

‎extensions/nostr/doctor-contract-api.ts‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { Dirent } from "node:fs";
33
import fs from "node:fs/promises";
44
import path from "node:path";
55
import type { PluginDoctorStateMigration } from "openclaw/plugin-sdk/runtime-doctor";
6+
import { normalizeNostrStateAccountId } from "./src/state-account-id.js";
67

78
type NostrBusState = {
89
version: 2;
@@ -22,14 +23,6 @@ const BUS_STATE_NAMESPACE = "bus-state";
2223
const PROFILE_STATE_NAMESPACE = "profile-state";
2324
const MAX_NOSTR_STATE_ENTRIES = 256;
2425

25-
function normalizeAccountId(accountId?: string): string {
26-
const trimmed = accountId?.trim();
27-
if (!trimmed) {
28-
return "default";
29-
}
30-
return trimmed.replace(/[^a-z0-9._-]+/gi, "_");
31-
}
32-
3326
function finiteNumberOrNull(value: unknown): number | null {
3427
return typeof value === "number" && Number.isFinite(value) ? value : null;
3528
}
@@ -114,7 +107,7 @@ async function listLegacyFiles(params: {
114107
continue;
115108
}
116109
const rawAccountId = entry.name.slice(params.prefix.length, -suffix.length);
117-
const accountId = normalizeAccountId(rawAccountId);
110+
const accountId = normalizeNostrStateAccountId(rawAccountId);
118111
const filePath = path.join(dir, entry.name);
119112
try {
120113
const value = params.parse(await readJsonFile(filePath));

‎extensions/nostr/src/nostr-state-store.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,21 @@ describe("nostr bus state store", () => {
9797
expect(stateB?.lastProcessedAt).toBe(2000);
9898
});
9999
});
100+
101+
it("preserves legacy account key bytes for state lookup", async () => {
102+
await withTempStateDir(async () => {
103+
await writeNostrBusState({
104+
accountId: " Team.A ",
105+
lastProcessedAt: 1234,
106+
gatewayStartedAt: 1200,
107+
});
108+
109+
await expect(readNostrBusState({ accountId: "Team.A" })).resolves.toMatchObject({
110+
lastProcessedAt: 1234,
111+
});
112+
await expect(readNostrBusState({ accountId: "team-a" })).resolves.toBeNull();
113+
});
114+
});
100115
});
101116

102117
describe("nostr profile state store", () => {

‎extensions/nostr/src/nostr-state-store.ts‎

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Nostr plugin module implements nostr state store behavior.
22
import { getNostrRuntime } from "./runtime.js";
3+
import { normalizeNostrStateAccountId } from "./state-account-id.js";
34

45
const STORE_VERSION = 2;
56
const PROFILE_STATE_VERSION = 1;
@@ -25,14 +26,6 @@ type NostrProfileState = {
2526
lastPublishResults: Record<string, "ok" | "failed" | "timeout"> | null;
2627
};
2728

28-
function normalizeAccountId(accountId?: string): string {
29-
const trimmed = accountId?.trim();
30-
if (!trimmed) {
31-
return "default";
32-
}
33-
return trimmed.replace(/[^a-z0-9._-]+/gi, "_");
34-
}
35-
3629
function openNostrBusStateStore(env?: NodeJS.ProcessEnv) {
3730
return getNostrRuntime().state.openKeyedStore<NostrBusState>({
3831
namespace: "bus-state",
@@ -54,7 +47,9 @@ export async function readNostrBusState(params: {
5447
env?: NodeJS.ProcessEnv;
5548
}): Promise<NostrBusState | null> {
5649
return (
57-
(await openNostrBusStateStore(params.env).lookup(normalizeAccountId(params.accountId))) ?? null
50+
(await openNostrBusStateStore(params.env).lookup(
51+
normalizeNostrStateAccountId(params.accountId),
52+
)) ?? null
5853
);
5954
}
6055

@@ -71,7 +66,10 @@ export async function writeNostrBusState(params: {
7166
gatewayStartedAt: params.gatewayStartedAt,
7267
recentEventIds: (params.recentEventIds ?? []).filter((x): x is string => typeof x === "string"),
7368
};
74-
await openNostrBusStateStore(params.env).register(normalizeAccountId(params.accountId), payload);
69+
await openNostrBusStateStore(params.env).register(
70+
normalizeNostrStateAccountId(params.accountId),
71+
payload,
72+
);
7573
}
7674

7775
/**
@@ -107,8 +105,9 @@ export async function readNostrProfileState(params: {
107105
env?: NodeJS.ProcessEnv;
108106
}): Promise<NostrProfileState | null> {
109107
return (
110-
(await openNostrProfileStateStore(params.env).lookup(normalizeAccountId(params.accountId))) ??
111-
null
108+
(await openNostrProfileStateStore(params.env).lookup(
109+
normalizeNostrStateAccountId(params.accountId),
110+
)) ?? null
112111
);
113112
}
114113

@@ -126,7 +125,7 @@ export async function writeNostrProfileState(params: {
126125
lastPublishResults: params.lastPublishResults,
127126
};
128127
await openNostrProfileStateStore(params.env).register(
129-
normalizeAccountId(params.accountId),
128+
normalizeNostrStateAccountId(params.accountId),
130129
payload,
131130
);
132131
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Nostr state stores keep legacy account key bytes; do not use the newer SDK normalizer here.
2+
export function normalizeNostrStateAccountId(accountId?: string): string {
3+
const trimmed = accountId?.trim();
4+
if (!trimmed) {
5+
return "default";
6+
}
7+
return trimmed.replace(/[^a-z0-9._-]+/gi, "_");
8+
}

0 commit comments

Comments
 (0)