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

推荐订阅源

Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
月光博客
月光博客
MyScale Blog
MyScale Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
爱范儿
爱范儿
P
Proofpoint News Feed
人人都是产品经理
人人都是产品经理
Last Week in AI
Last Week in AI
罗磊的独立博客
G
Google Developers Blog
Y
Y Combinator Blog
博客园 - 【当耐特】
WordPress大学
WordPress大学
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
J
Java Code Geeks
酷 壳 – CoolShell
酷 壳 – CoolShell
V
Visual Studio Blog
美团技术团队
宝玉的分享
宝玉的分享
Jina AI
Jina AI
小众软件
小众软件
T
Tailwind CSS Blog
A
About on SuperTechFans

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
Preserve AGENTS.md policy during bootstrap truncation (#8...
galiniliev · 2026-05-20 · via Recent Commits to openclaw:main

@@ -96,6 +96,11 @@ const MIN_BOOTSTRAP_FILE_BUDGET_CHARS = 64;

9696

const BOOTSTRAP_HEAD_RATIO = 0.75;

9797

const BOOTSTRAP_TAIL_RATIO = 0.25;

9898

const MIN_BOOTSTRAP_TRIMMED_CONTENT_CHARS = 16;

99+

const AGENTS_BOOTSTRAP_FILENAME = "AGENTS.md";

100+

const AGENTS_POLICY_DIGEST_RATIO = 0.35;

101+

const AGENTS_POLICY_HEAD_RATIO = 0.45;

102+

const AGENTS_POLICY_TAIL_RATIO = 0.15;

103+

const AGENTS_POLICY_DIGEST_MAX_LINE_CHARS = 240;

99104100105

type TrimBootstrapResult = {

101106

content: string;

@@ -104,6 +109,11 @@ type TrimBootstrapResult = {

104109

originalLength: number;

105110

};

106111112+

type PolicyDigest = {

113+

text: string;

114+

omittedLines: number;

115+

};

116+107117

export function resolveBootstrapMaxChars(cfg?: OpenClawConfig, agentId?: string | null): number {

108118

const raw =

109119

cfg && agentId

@@ -141,6 +151,120 @@ export function resolveBootstrapPromptTruncationWarningMode(

141151

return DEFAULT_BOOTSTRAP_PROMPT_TRUNCATION_WARNING_MODE;

142152

}

143153154+

function isAgentsBootstrapFile(fileName: string): boolean {

155+

return fileName.toLowerCase() === AGENTS_BOOTSTRAP_FILENAME.toLowerCase();

156+

}

157+158+

function isPolicyDigestCandidate(line: string): boolean {

159+

if (/^(?:#{1,6}|\s*[-*+]|\s*\d+[.)])\s+\S/u.test(line)) {

160+

return true;

161+

}

162+

return /\b(?:AGENTS\.md|scoped|required|must|never|do not|before subtree|read scoped|owner|security|secret|credential|test|validation|command|commit|push|github|pr)\b/iu.test(

163+

line,

164+

);

165+

}

166+167+

function normalizePolicyDigestLine(line: string): string {

168+

const normalized = line.trim().replace(/\s+/gu, " ");

169+

if (normalized.length <= AGENTS_POLICY_DIGEST_MAX_LINE_CHARS) {

170+

return normalized;

171+

}

172+

return `${truncateUtf16Safe(normalized, AGENTS_POLICY_DIGEST_MAX_LINE_CHARS - 1)}…`;

173+

}

174+175+

function buildAgentsPolicyDigest(content: string, budget: number): PolicyDigest {

176+

if (budget <= 0) {

177+

return { text: "", omittedLines: 0 };

178+

}

179+180+

const candidates = content

181+

.split(/\r?\n/u)

182+

.map((line, index) => ({ index, line: normalizePolicyDigestLine(line) }))

183+

.filter(({ line }) => line.length > 0 && isPolicyDigestCandidate(line));

184+

const highPriorityPattern =

185+

/\b(?:AGENTS\.md|scoped|required|must|never|do not|before subtree|read scoped|security|secret|credential)\b/iu;

186+

const selected = new Set<number>();

187+

let used = 0;

188+

const trySelect = (candidate: { index: number; line: string }) => {

189+

const separatorChars = selected.size > 0 ? 1 : 0;

190+

if (used + separatorChars + candidate.line.length > budget) {

191+

return;

192+

}

193+

selected.add(candidate.index);

194+

used += separatorChars + candidate.line.length;

195+

};

196+197+

for (const candidate of candidates) {

198+

if (highPriorityPattern.test(candidate.line)) {

199+

trySelect(candidate);

200+

}

201+

}

202+

for (const candidate of candidates) {

203+

if (!selected.has(candidate.index)) {

204+

trySelect(candidate);

205+

}

206+

}

207+208+

const lines = candidates

209+

.filter((candidate) => selected.has(candidate.index))

210+

.toSorted((a, b) => a.index - b.index)

211+

.map((candidate) => candidate.line);

212+

return {

213+

text: lines.join("\n"),

214+

omittedLines: Math.max(0, candidates.length - lines.length),

215+

};

216+

}

217+218+

function trimAgentsBootstrapContent(content: string, maxChars: number): TrimBootstrapResult {

219+

const trimmed = content.trimEnd();

220+

if (trimmed.length <= maxChars) {

221+

return {

222+

content: trimmed,

223+

truncated: false,

224+

maxChars,

225+

originalLength: trimmed.length,

226+

};

227+

}

228+229+

let headChars = Math.floor(maxChars * AGENTS_POLICY_HEAD_RATIO);

230+

let tailChars = Math.floor(maxChars * AGENTS_POLICY_TAIL_RATIO);

231+

let digestBudget = Math.floor(maxChars * AGENTS_POLICY_DIGEST_RATIO);

232+

let digest = buildAgentsPolicyDigest(trimmed, digestBudget);

233+

const render = () =>

234+

[

235+

trimmed.slice(0, headChars),

236+

`[...truncated, read ${AGENTS_BOOTSTRAP_FILENAME} for full content...]`,

237+

digest.text ? "[Policy digest from AGENTS.md]" : "",

238+

digest.text,

239+

digest.omittedLines > 0 ? `[...${digest.omittedLines} more policy lines omitted...]` : "",

240+

`…(truncated ${AGENTS_BOOTSTRAP_FILENAME}: kept ${headChars}+policy ${digest.text.length}+${tailChars} chars of ${trimmed.length})…`,

241+

tailChars > 0 ? trimmed.slice(-tailChars) : "",

242+

]

243+

.filter((part) => part.length > 0)

244+

.join("\n");

245+246+

let rendered = render();

247+

while (rendered.length > maxChars && (tailChars > 0 || headChars > 1 || digestBudget > 0)) {

248+

const overflow = rendered.length - maxChars;

249+

if (tailChars > 0) {

250+

tailChars = Math.max(0, tailChars - overflow);

251+

} else if (headChars > 1) {

252+

headChars = Math.max(1, headChars - overflow);

253+

} else {

254+

digestBudget = Math.max(0, digestBudget - overflow);

255+

digest = buildAgentsPolicyDigest(trimmed, digestBudget);

256+

}

257+

rendered = render();

258+

}

259+260+

return {

261+

content: rendered.length > maxChars ? truncateUtf16Safe(rendered, maxChars) : rendered,

262+

truncated: true,

263+

maxChars,

264+

originalLength: trimmed.length,

265+

};

266+

}

267+144268

function trimBootstrapContent(

145269

content: string,

146270

fileName: string,

@@ -155,6 +279,9 @@ function trimBootstrapContent(

155279

originalLength: trimmed.length,

156280

};

157281

}

282+

if (isAgentsBootstrapFile(fileName)) {

283+

return trimAgentsBootstrapContent(content, maxChars);

284+

}

158285159286

const markerTemplate = (headChars: number, tailChars: number) =>

160287

[