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

推荐订阅源

V
Visual Studio Blog
Y
Y Combinator Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Hugging Face - Blog
Hugging Face - Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
L
LangChain Blog
美团技术团队
N
Netflix TechBlog - Medium
量子位
酷 壳 – CoolShell
酷 壳 – CoolShell
B
Blog
博客园 - 司徒正美
爱范儿
爱范儿
D
DataBreaches.Net
月光博客
月光博客
U
Unit 42
B
Blog RSS Feed
Engineering at Meta
Engineering at Meta
Apple Machine Learning Research
Apple Machine Learning Research
Jina AI
Jina AI
MongoDB | Blog
MongoDB | Blog
腾讯CDC

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: dedupe session storage indexing · openclaw/open...
vincentkoc · 2026-05-29 · via Recent Commits to openclaw:main
1-

import type {

2-

FileSystem,

3-

JsonlSessionMetadata,

4-

LeafEntry,

5-

SessionStorage,

6-

SessionTreeEntry,

7-

} from "../types.js";

1+

import type { FileSystem, JsonlSessionMetadata, SessionTreeEntry } from "../types.js";

82

import { SessionError, toError } from "../types.js";

93

import { getFileSystemResultOrThrow } from "./repo-utils.js";

10-

import { uuidv7 } from "./uuid.js";

4+

import { BaseSessionStorage, leafIdAfterEntry } from "./storage-base.js";

115126

type JsonlSessionStorageFileSystem = Pick<

137

FileSystem,

@@ -23,36 +17,6 @@ interface SessionHeader {

2317

parentSession?: string;

2418

}

251926-

function updateLabelCache(labelsById: Map<string, string>, entry: SessionTreeEntry): void {

27-

if (entry.type !== "label") {

28-

return;

29-

}

30-

const label = entry.label?.trim();

31-

if (label) {

32-

labelsById.set(entry.targetId, label);

33-

} else {

34-

labelsById.delete(entry.targetId);

35-

}

36-

}

37-38-

function buildLabelsById(entries: SessionTreeEntry[]): Map<string, string> {

39-

const labelsById = new Map<string, string>();

40-

for (const entry of entries) {

41-

updateLabelCache(labelsById, entry);

42-

}

43-

return labelsById;

44-

}

45-46-

function generateEntryId(byId: { has(id: string): boolean }): string {

47-

for (let i = 0; i < 100; i++) {

48-

const id = uuidv7().slice(0, 8);

49-

if (!byId.has(id)) {

50-

return id;

51-

}

52-

}

53-

return uuidv7();

54-

}

55-5620

function isRecord(value: unknown): value is Record<string, unknown> {

5721

return typeof value === "object" && value !== null;

5822

}

@@ -144,10 +108,6 @@ function parseEntryLine(line: string, filePath: string, lineNumber: number): Ses

144108

return parsed as unknown as SessionTreeEntry;

145109

}

146110147-

function leafIdAfterEntry(entry: SessionTreeEntry): string | null {

148-

return entry.type === "leaf" ? entry.targetId : entry.id;

149-

}

150-151111

function headerToSessionMetadata(header: SessionHeader, path: string): JsonlSessionMetadata {

152112

return {

153113

id: header.id,

@@ -201,14 +161,9 @@ async function loadJsonlStorage(

201161

return { header, entries, leafId };

202162

}

203163204-

export class JsonlSessionStorage implements SessionStorage<JsonlSessionMetadata> {

164+

export class JsonlSessionStorage extends BaseSessionStorage<JsonlSessionMetadata> {

205165

private readonly fs: JsonlSessionStorageFileSystem;

206166

private readonly filePath: string;

207-

private readonly metadata: JsonlSessionMetadata;

208-

private entries: SessionTreeEntry[];

209-

private byId: Map<string, SessionTreeEntry>;

210-

private labelsById: Map<string, string>;

211-

private currentLeafId: string | null;

212167213168

private constructor(

214169

fs: JsonlSessionStorageFileSystem,

@@ -217,13 +172,9 @@ export class JsonlSessionStorage implements SessionStorage<JsonlSessionMetadata>

217172

entries: SessionTreeEntry[],

218173

leafId: string | null,

219174

) {

175+

super(headerToSessionMetadata(header, filePath), entries, leafId);

220176

this.fs = fs;

221177

this.filePath = filePath;

222-

this.metadata = headerToSessionMetadata(header, this.filePath);

223-

this.entries = entries;

224-

this.byId = new Map(entries.map((entry) => [entry.id, entry]));

225-

this.labelsById = buildLabelsById(entries);

226-

this.currentLeafId = leafId;

227178

}

228179229180

static async open(

@@ -258,92 +209,20 @@ export class JsonlSessionStorage implements SessionStorage<JsonlSessionMetadata>

258209

return new JsonlSessionStorage(fs, filePath, header, [], null);

259210

}

260211261-

async getMetadata(): Promise<JsonlSessionMetadata> {

262-

return this.metadata;

263-

}

264-265-

async getLeafId(): Promise<string | null> {

266-

if (this.currentLeafId !== null && !this.byId.has(this.currentLeafId)) {

267-

throw new SessionError("invalid_session", `Entry ${this.currentLeafId} not found`);

268-

}

269-

return this.currentLeafId;

270-

}

271-272-

async setLeafId(leafId: string | null): Promise<void> {

273-

if (leafId !== null && !this.byId.has(leafId)) {

274-

throw new SessionError("not_found", `Entry ${leafId} not found`);

275-

}

276-

const entry: LeafEntry = {

277-

type: "leaf",

278-

id: generateEntryId(this.byId),

279-

parentId: this.currentLeafId,

280-

timestamp: new Date().toISOString(),

281-

targetId: leafId,

282-

};

212+

override async setLeafId(leafId: string | null): Promise<void> {

213+

const entry = this.createLeafEntry(leafId);

283214

getFileSystemResultOrThrow(

284215

await this.fs.appendFile(this.filePath, `${JSON.stringify(entry)}\n`),

285216

`Failed to append session leaf ${entry.id}`,

286217

);

287-

this.entries.push(entry);

288-

this.byId.set(entry.id, entry);

289-

this.currentLeafId = leafId;

290-

}

291-292-

async createEntryId(): Promise<string> {

293-

return generateEntryId(this.byId);

218+

this.recordEntry(entry);

294219

}

295220296-

async appendEntry(entry: SessionTreeEntry): Promise<void> {

221+

override async appendEntry(entry: SessionTreeEntry): Promise<void> {

297222

getFileSystemResultOrThrow(

298223

await this.fs.appendFile(this.filePath, `${JSON.stringify(entry)}\n`),

299224

`Failed to append session entry ${entry.id}`,

300225

);

301-

this.entries.push(entry);

302-

this.byId.set(entry.id, entry);

303-

updateLabelCache(this.labelsById, entry);

304-

this.currentLeafId = leafIdAfterEntry(entry);

305-

}

306-307-

async getEntry(id: string): Promise<SessionTreeEntry | undefined> {

308-

return this.byId.get(id);

309-

}

310-311-

async findEntries<TType extends SessionTreeEntry["type"]>(

312-

type: TType,

313-

): Promise<Array<Extract<SessionTreeEntry, { type: TType }>>> {

314-

return this.entries.filter(

315-

(entry): entry is Extract<SessionTreeEntry, { type: TType }> => entry.type === type,

316-

);

317-

}

318-319-

async getLabel(id: string): Promise<string | undefined> {

320-

return this.labelsById.get(id);

321-

}

322-323-

async getPathToRoot(leafId: string | null): Promise<SessionTreeEntry[]> {

324-

if (leafId === null) {

325-

return [];

326-

}

327-

const path: SessionTreeEntry[] = [];

328-

let current = this.byId.get(leafId);

329-

if (!current) {

330-

throw new SessionError("not_found", `Entry ${leafId} not found`);

331-

}

332-

while (current) {

333-

path.unshift(current);

334-

if (!current.parentId) {

335-

break;

336-

}

337-

const parent = this.byId.get(current.parentId);

338-

if (!parent) {

339-

throw new SessionError("invalid_session", `Entry ${current.parentId} not found`);

340-

}

341-

current = parent;

342-

}

343-

return path;

344-

}

345-346-

async getEntries(): Promise<SessionTreeEntry[]> {

347-

return [...this.entries];

226+

this.recordEntry(entry);

348227

}

349228

}