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

推荐订阅源

博客园_首页
量子位
D
DataBreaches.Net
博客园 - 司徒正美
J
Java Code Geeks
博客园 - 【当耐特】
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
aimingoo的专栏
aimingoo的专栏
B
Blog
The Cloudflare Blog
D
Docker
I
InfoQ
爱范儿
爱范儿
MongoDB | Blog
MongoDB | Blog
腾讯CDC
月光博客
月光博客
Hugging Face - Blog
Hugging Face - Blog
Microsoft Azure Blog
Microsoft Azure Blog
Vercel News
Vercel News
阮一峰的网络日志
阮一峰的网络日志
小众软件
小众软件
S
SegmentFault 最新的问题
GbyAI
GbyAI
有赞技术团队
有赞技术团队

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
fix(memory): catch up stale sessions on startup (#82341) ...
giodl73-repo · 2026-05-17 · via Recent Commits to openclaw:main

@@ -0,0 +1,196 @@

1+

import fs from "node:fs/promises";

2+

import os from "node:os";

3+

import path from "node:path";

4+

import type { DatabaseSync } from "node:sqlite";

5+

import {

6+

resolveSessionTranscriptsDirForAgent,

7+

type OpenClawConfig,

8+

type ResolvedMemorySearchConfig,

9+

} from "openclaw/plugin-sdk/memory-core-host-engine-foundation";

10+

import type {

11+

MemorySource,

12+

MemorySyncProgressUpdate,

13+

} from "openclaw/plugin-sdk/memory-core-host-engine-storage";

14+

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

15+

import { MemoryManagerSyncOps } from "./manager-sync-ops.js";

16+17+

type MemoryIndexEntry = {

18+

path: string;

19+

absPath: string;

20+

mtimeMs: number;

21+

size: number;

22+

hash: string;

23+

content?: string;

24+

};

25+26+

type SyncParams = {

27+

reason?: string;

28+

force?: boolean;

29+

sessionFiles?: string[];

30+

progress?: (update: MemorySyncProgressUpdate) => void;

31+

};

32+33+

type SourceStateRow = { path: string; hash: string; mtime: number; size: number };

34+35+

class SessionStartupCatchupHarness extends MemoryManagerSyncOps {

36+

protected readonly cfg = {} as OpenClawConfig;

37+

protected readonly agentId = "main";

38+

protected readonly workspaceDir = "/tmp/openclaw-test-workspace";

39+

protected readonly settings = {

40+

sync: {

41+

sessions: {

42+

deltaBytes: 100_000,

43+

deltaMessages: 50,

44+

postCompactionForce: true,

45+

},

46+

},

47+

} as ResolvedMemorySearchConfig;

48+

protected readonly batch = {

49+

enabled: false,

50+

wait: false,

51+

concurrency: 1,

52+

pollIntervalMs: 0,

53+

timeoutMs: 0,

54+

};

55+

protected readonly vector = { enabled: false, available: false };

56+

protected readonly cache = { enabled: false };

57+

protected db: DatabaseSync;

58+59+

readonly syncCalls: SyncParams[] = [];

60+61+

constructor(sourceRows: SourceStateRow[]) {

62+

super();

63+

this.sources.add("sessions");

64+

this.db = {

65+

prepare: () => ({

66+

all: () => sourceRows,

67+

get: () => undefined,

68+

run: () => undefined,

69+

}),

70+

} as unknown as DatabaseSync;

71+

}

72+73+

async catchUp(): Promise<string[]> {

74+

return await this.runSessionStartupCatchup();

75+

}

76+77+

async markStartupDirtyFiles(): Promise<string[]> {

78+

return await this.markSessionStartupCatchupDirtyFiles();

79+

}

80+81+

getDirtySessionFiles(): string[] {

82+

return Array.from(this.sessionsDirtyFiles);

83+

}

84+85+

isSessionsDirty(): boolean {

86+

return this.sessionsDirty;

87+

}

88+89+

protected computeProviderKey(): string {

90+

return "test";

91+

}

92+93+

protected async sync(params?: SyncParams): Promise<void> {

94+

this.syncCalls.push(params ?? {});

95+

}

96+97+

protected async withTimeout<T>(

98+

promise: Promise<T>,

99+

_timeoutMs: number,

100+

_message: string,

101+

): Promise<T> {

102+

return await promise;

103+

}

104+105+

protected getIndexConcurrency(): number {

106+

return 1;

107+

}

108+109+

protected pruneEmbeddingCacheIfNeeded(): void {}

110+111+

protected async indexFile(

112+

_entry: MemoryIndexEntry,

113+

_options: { source: MemorySource; content?: string },

114+

): Promise<void> {}

115+

}

116+117+

describe("session startup catch-up", () => {

118+

let stateDir = "";

119+120+

beforeEach(async () => {

121+

stateDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-session-startup-"));

122+

vi.stubEnv("OPENCLAW_STATE_DIR", stateDir);

123+

});

124+125+

afterEach(async () => {

126+

vi.unstubAllEnvs();

127+

await fs.rm(stateDir, { recursive: true, force: true });

128+

});

129+130+

async function writeSessionFile(

131+

name: string,

132+

): Promise<{ filePath: string; size: number; mtimeMs: number }> {

133+

const sessionsDir = resolveSessionTranscriptsDirForAgent("main");

134+

await fs.mkdir(sessionsDir, { recursive: true });

135+

const filePath = path.join(sessionsDir, name);

136+

await fs.writeFile(

137+

filePath,

138+

JSON.stringify({ type: "message", message: { role: "user", content: "startup catchup" } }) +

139+

"\n",

140+

"utf-8",

141+

);

142+

const stat = await fs.stat(filePath);

143+

return { filePath, size: stat.size, mtimeMs: stat.mtimeMs };

144+

}

145+146+

it("marks stale indexed session files dirty and schedules catch-up sync", async () => {

147+

const session = await writeSessionFile("thread.jsonl");

148+

const harness = new SessionStartupCatchupHarness([

149+

{

150+

path: "sessions/main/thread.jsonl",

151+

hash: "old-hash",

152+

mtime: session.mtimeMs - 1000,

153+

size: session.size,

154+

},

155+

]);

156+157+

await expect(harness.catchUp()).resolves.toEqual([session.filePath]);

158+

expect(harness.getDirtySessionFiles()).toEqual([session.filePath]);

159+

expect(harness.isSessionsDirty()).toBe(true);

160+

expect(harness.syncCalls).toEqual([{ reason: "session-startup-catchup" }]);

161+

});

162+163+

it("can mark startup catch-up files without scheduling background sync", async () => {

164+

const session = await writeSessionFile("thread.jsonl");

165+

const harness = new SessionStartupCatchupHarness([

166+

{

167+

path: "sessions/main/thread.jsonl",

168+

hash: "old-hash",

169+

mtime: session.mtimeMs - 1000,

170+

size: session.size,

171+

},

172+

]);

173+174+

await expect(harness.markStartupDirtyFiles()).resolves.toEqual([session.filePath]);

175+

expect(harness.getDirtySessionFiles()).toEqual([session.filePath]);

176+

expect(harness.isSessionsDirty()).toBe(true);

177+

expect(harness.syncCalls).toEqual([]);

178+

});

179+180+

it("leaves unchanged indexed session files clean", async () => {

181+

const session = await writeSessionFile("thread.jsonl");

182+

const harness = new SessionStartupCatchupHarness([

183+

{

184+

path: "sessions/main/thread.jsonl",

185+

hash: "current-hash",

186+

mtime: session.mtimeMs,

187+

size: session.size,

188+

},

189+

]);

190+191+

await expect(harness.catchUp()).resolves.toEqual([]);

192+

expect(harness.getDirtySessionFiles()).toEqual([]);

193+

expect(harness.isSessionsDirty()).toBe(false);

194+

expect(harness.syncCalls).toEqual([]);

195+

});

196+

});