惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
云风的 BLOG
云风的 BLOG
Microsoft Azure Blog
Microsoft Azure Blog
腾讯CDC
T
The Blog of Author Tim Ferriss
P
Proofpoint News Feed
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
小众软件
小众软件
美团技术团队
Martin Fowler
Martin Fowler
爱范儿
爱范儿
有赞技术团队
有赞技术团队
博客园 - 【当耐特】
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Microsoft Security Blog
Microsoft Security Blog
宝玉的分享
宝玉的分享
J
Java Code Geeks
B
Blog
V
V2EX
Stack Overflow Blog
Stack Overflow Blog
B
Blog RSS Feed
博客园 - Franky

Recent Commits to openclaw:main

test: merge chat side-result checks · openclaw/openclaw@ddd2c2a test: merge cron history checks · openclaw/openclaw@f7eb746 test: merge responsive navigation shell checks · openclaw/openclaw@c2e4b47 docs(changelog): add codex oauth fixes · openclaw/openclaw@628e6cd test: merge navigation routing cases · openclaw/openclaw@5d8cecb Tests: mock channel registry bundled fallback · openclaw/openclaw@2b08233 Secrets: avoid broad web search discovery for single plugin config · openclaw/openclaw@a464f59 test: merge config view browser checks · openclaw/openclaw@20cf511 fix(status): align oauth health with runtime · openclaw/openclaw@eed7116 feat: add macOS screen snapshots for monitor preview (#67954) thanks … · openclaw/openclaw@f377db1 fix: report shared auth scopes in hello-ok (#67810) thanks @BunsDev · openclaw/openclaw@0b6c39b Auto-reply: avoid eager bundled route fallback · openclaw/openclaw@3ea1bf4 Tests: narrow session binding contract setup · openclaw/openclaw@54e4e16 fix(macOS): enable undo/redo in webchat composer text input (#34962) · openclaw/openclaw@00951dc Tests: speed up channel setup promotion · openclaw/openclaw@82b529a Docs: refresh agent instructions · openclaw/openclaw@5775fe2 fix(auth): serialize OAuth refresh across agents to fix #26322 (#67876) · openclaw/openclaw@8e79080 test: allow ollama public surface boundary test · openclaw/openclaw@7d4f1a6 Docs: add test performance guardrails · openclaw/openclaw@89706d3 Tests: restore context-engine usage proof · openclaw/openclaw@e4c4f95 Tests: slim context engine runtime coverage · openclaw/openclaw@74c198f ci: retry failed custom checkouts · openclaw/openclaw@0ee5baf test: trim duplicate provider auth onboarding cases · openclaw/openclaw@1ffc02e matrix: fix sessions_spawn --thread subagent session spawning (#67643) · openclaw/openclaw@1ce2596 test: reduce auth choice fixture churn · openclaw/openclaw@857b9cd test: mock health status config boundaries · openclaw/openclaw@9d5ab4a test: mock onboard config io boundary · openclaw/openclaw@299694d test: mock legacy state plugin boundaries · openclaw/openclaw@2713089 test: mock channel install boundaries · openclaw/openclaw@b945248 test: mock doctor preview channel boundaries · openclaw/openclaw@b1a3ad4
refactor: move plugin state slices to sqlite · openclaw/o...
steipete · 2026-06-01 · via Recent Commits to openclaw:main
1+

import crypto from "node:crypto";

2+

import fs from "node:fs/promises";

3+

import path from "node:path";

4+

import type { PluginDoctorStateMigration } from "openclaw/plugin-sdk/runtime-doctor";

5+6+

type ActiveMemoryToggleEntry = {

7+

sessionKey: string;

8+

disabled: boolean;

9+

updatedAt: number;

10+

};

11+12+

const TOGGLE_STATE_FILE = "session-toggles.json";

13+

const SESSION_TOGGLES_NAMESPACE = "session-toggles";

14+

const MAX_TOGGLE_ENTRIES = 10_000;

15+16+

function resolveToggleStatePath(stateDir: string): string {

17+

return path.join(stateDir, "plugins", "active-memory", TOGGLE_STATE_FILE);

18+

}

19+20+

function activeMemoryToggleKey(sessionKey: string): string {

21+

return crypto.createHash("sha256").update(sessionKey, "utf8").digest("hex");

22+

}

23+24+

async function fileExists(filePath: string): Promise<boolean> {

25+

try {

26+

const stat = await fs.stat(filePath);

27+

return stat.isFile();

28+

} catch {

29+

return false;

30+

}

31+

}

32+33+

async function readLegacyToggleEntries(filePath: string): Promise<ActiveMemoryToggleEntry[]> {

34+

try {

35+

const parsed = JSON.parse(await fs.readFile(filePath, "utf8")) as unknown;

36+

if (!parsed || typeof parsed !== "object") {

37+

return [];

38+

}

39+

const sessions = (parsed as { sessions?: unknown }).sessions;

40+

if (!sessions || typeof sessions !== "object" || Array.isArray(sessions)) {

41+

return [];

42+

}

43+

const entries: ActiveMemoryToggleEntry[] = [];

44+

for (const [sessionKey, value] of Object.entries(sessions)) {

45+

if (!sessionKey.trim() || !value || typeof value !== "object" || Array.isArray(value)) {

46+

continue;

47+

}

48+

if ((value as { disabled?: unknown }).disabled !== true) {

49+

continue;

50+

}

51+

const updatedAt =

52+

typeof (value as { updatedAt?: unknown }).updatedAt === "number"

53+

? (value as { updatedAt: number }).updatedAt

54+

: Date.now();

55+

entries.push({ sessionKey, disabled: true, updatedAt });

56+

}

57+

return entries;

58+

} catch {

59+

return [];

60+

}

61+

}

62+63+

async function archiveLegacySource(params: {

64+

filePath: string;

65+

label: string;

66+

changes: string[];

67+

warnings: string[];

68+

}): Promise<void> {

69+

const archivedPath = `${params.filePath}.migrated`;

70+

if (await fileExists(archivedPath)) {

71+

params.warnings.push(

72+

`Left migrated ${params.label} source in place because ${archivedPath} already exists`,

73+

);

74+

return;

75+

}

76+

try {

77+

await fs.rename(params.filePath, archivedPath);

78+

params.changes.push(`Archived ${params.label} legacy source -> ${archivedPath}`);

79+

} catch (err) {

80+

params.warnings.push(`Failed archiving ${params.label} legacy source: ${String(err)}`);

81+

}

82+

}

83+84+

export const stateMigrations: PluginDoctorStateMigration[] = [

85+

{

86+

id: "active-memory-session-toggles-json-to-plugin-state",

87+

label: "Active Memory session toggles",

88+

async detectLegacyState(params) {

89+

const filePath = resolveToggleStatePath(params.stateDir);

90+

const entries = await readLegacyToggleEntries(filePath);

91+

if (entries.length === 0) {

92+

return null;

93+

}

94+

return {

95+

preview: [

96+

`- Active Memory session toggles: ${entries.length} ${entries.length === 1 ? "entry" : "entries"} -> plugin state (${SESSION_TOGGLES_NAMESPACE})`,

97+

],

98+

};

99+

},

100+

async migrateLegacyState(params) {

101+

const changes: string[] = [];

102+

const warnings: string[] = [];

103+

const filePath = resolveToggleStatePath(params.stateDir);

104+

const entries = await readLegacyToggleEntries(filePath);

105+

if (entries.length === 0) {

106+

return { changes, warnings };

107+

}

108+

const store = params.context.openPluginStateKeyedStore<ActiveMemoryToggleEntry>({

109+

namespace: SESSION_TOGGLES_NAMESPACE,

110+

maxEntries: MAX_TOGGLE_ENTRIES,

111+

});

112+

const existingKeys = new Set((await store.entries()).map((entry) => entry.key));

113+

const missingEntries = entries.filter(

114+

(entry) => !existingKeys.has(activeMemoryToggleKey(entry.sessionKey)),

115+

);

116+

if (missingEntries.length > MAX_TOGGLE_ENTRIES - existingKeys.size) {

117+

warnings.push(

118+

`Skipped Active Memory session toggle migration because plugin state has room for ${MAX_TOGGLE_ENTRIES - existingKeys.size} of ${missingEntries.length} missing entries; left legacy source in place`,

119+

);

120+

return { changes, warnings };

121+

}

122+

let imported = 0;

123+

for (const entry of entries) {

124+

const key = activeMemoryToggleKey(entry.sessionKey);

125+

if (existingKeys.has(key)) {

126+

continue;

127+

}

128+

await store.register(key, entry);

129+

existingKeys.add(key);

130+

imported++;

131+

}

132+

if (imported > 0) {

133+

changes.push(

134+

`Migrated ${imported} Active Memory session toggle ${imported === 1 ? "entry" : "entries"} -> plugin state`,

135+

);

136+

}

137+

await archiveLegacySource({

138+

filePath,

139+

label: "Active Memory session toggles",

140+

changes,

141+

warnings,

142+

});

143+

return { changes, warnings };

144+

},

145+

},

146+

];