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

推荐订阅源

J
Java Code Geeks
腾讯CDC
博客园 - 聂微东
爱范儿
爱范儿
罗磊的独立博客
P
Proofpoint News Feed
博客园 - Franky
博客园 - 三生石上(FineUI控件)
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
酷 壳 – CoolShell
酷 壳 – CoolShell
Jina AI
Jina AI
Blog — PlanetScale
Blog — PlanetScale
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 司徒正美
美团技术团队
MongoDB | Blog
MongoDB | Blog
WordPress大学
WordPress大学
A
About on SuperTechFans
I
InfoQ
博客园_首页
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
H
Help Net Security
Microsoft Azure Blog
Microsoft Azure Blog
G
Google Developers Blog

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
perf(agents): cache subagent registry reads · openclaw/op...
steipete · 2026-04-29 · via Recent Commits to openclaw:main

@@ -1,3 +1,4 @@

1+

import fs from "node:fs";

12

import os from "node:os";

23

import path from "node:path";

34

import { resolveStateDir } from "../config/paths.js";

@@ -21,16 +22,48 @@ type PersistedSubagentRegistryV2 = {

2122

type PersistedSubagentRegistry = PersistedSubagentRegistryV1 | PersistedSubagentRegistryV2;

22232324

const REGISTRY_VERSION = 2 as const;

25+

const MAX_SUBAGENT_REGISTRY_READ_CACHE_ENTRIES = 32;

24262527

type PersistedSubagentRunRecord = SubagentRunRecord;

262829+

type RegistryCacheEntry = {

30+

signature: string;

31+

runs: Map<string, SubagentRunRecord>;

32+

};

33+2734

type LegacySubagentRunRecord = PersistedSubagentRunRecord & {

2835

announceCompletedAt?: unknown;

2936

announceHandled?: unknown;

3037

requesterChannel?: unknown;

3138

requesterAccountId?: unknown;

3239

};

334041+

const registryReadCache = new Map<string, RegistryCacheEntry>();

42+43+

function cloneSubagentRunRecord(entry: SubagentRunRecord): SubagentRunRecord {

44+

return structuredClone(entry);

45+

}

46+47+

function cloneSubagentRunMap(runs: Map<string, SubagentRunRecord>): Map<string, SubagentRunRecord> {

48+

return new Map([...runs].map(([runId, entry]) => [runId, cloneSubagentRunRecord(entry)]));

49+

}

50+51+

function setCachedRegistryRead(

52+

pathname: string,

53+

signature: string,

54+

runs: Map<string, SubagentRunRecord>,

55+

): void {

56+

registryReadCache.delete(pathname);

57+

registryReadCache.set(pathname, { signature, runs: cloneSubagentRunMap(runs) });

58+

if (registryReadCache.size <= MAX_SUBAGENT_REGISTRY_READ_CACHE_ENTRIES) {

59+

return;

60+

}

61+

const oldestKey = registryReadCache.keys().next().value;

62+

if (typeof oldestKey === "string") {

63+

registryReadCache.delete(oldestKey);

64+

}

65+

}

66+3467

function resolveSubagentStateDir(env: NodeJS.ProcessEnv = process.env): string {

3568

const explicit = env.OPENCLAW_STATE_DIR?.trim();

3669

if (explicit) {

@@ -48,16 +81,30 @@ export function resolveSubagentRegistryPath(): string {

48814982

export function loadSubagentRegistryFromDisk(): Map<string, SubagentRunRecord> {

5083

const pathname = resolveSubagentRegistryPath();

84+

const signature = statRegistryFileSignature(pathname);

85+

if (signature === null) {

86+

registryReadCache.delete(pathname);

87+

return new Map();

88+

}

89+

const cached = registryReadCache.get(pathname);

90+

if (cached?.signature === signature) {

91+

registryReadCache.delete(pathname);

92+

registryReadCache.set(pathname, cached);

93+

return cloneSubagentRunMap(cached.runs);

94+

}

5195

const raw = loadJsonFile(pathname);

5296

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

97+

setCachedRegistryRead(pathname, signature, new Map());

5398

return new Map();

5499

}

55100

const record = raw as Partial<PersistedSubagentRegistry>;

56101

if (record.version !== 1 && record.version !== 2) {

102+

setCachedRegistryRead(pathname, signature, new Map());

57103

return new Map();

58104

}

59105

const runsRaw = record.runs;

60106

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

107+

setCachedRegistryRead(pathname, signature, new Map());

61108

return new Map();

62109

}

63110

const out = new Map<string, SubagentRunRecord>();

@@ -123,6 +170,8 @@ export function loadSubagentRegistryFromDisk(): Map<string, SubagentRunRecord> {

123170

} catch {

124171

// ignore migration write failures

125172

}

173+

} else {

174+

setCachedRegistryRead(pathname, signature, out);

126175

}

127176

return out;

128177

}

@@ -138,4 +187,25 @@ export function saveSubagentRegistryToDisk(runs: Map<string, SubagentRunRecord>)

138187

runs: serialized,

139188

};

140189

saveJsonFile(pathname, out);

190+

const signature = statRegistryFileSignature(pathname);

191+

if (signature === null) {

192+

registryReadCache.delete(pathname);

193+

} else {

194+

setCachedRegistryRead(pathname, signature, runs);

195+

}

196+

}

197+198+

function statRegistryFileSignature(pathname: string): string | null {

199+

try {

200+

const stat = fs.statSync(pathname, { bigint: true });

201+

if (!stat.isFile()) {

202+

return null;

203+

}

204+

return `${stat.dev}:${stat.ino}:${stat.size}:${stat.mtimeNs}:${stat.ctimeNs}`;

205+

} catch (error) {

206+

if ((error as NodeJS.ErrnoException).code === "ENOENT") {

207+

return null;

208+

}

209+

throw error;

210+

}

141211

}