mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 10:11:24 +00:00
agents: reduce prompt token bloat from exec and context (#16539)
Merged via /review-pr -> /prepare-pr -> /merge-pr.
Prepared head SHA: 8e1635fa3f
Co-authored-by: CharlieGreenman <8540141+CharlieGreenman@users.noreply.github.com>
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Reviewed-by: @gumadeiras
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { buildBootstrapContextFiles, DEFAULT_BOOTSTRAP_MAX_CHARS } from "./pi-embedded-helpers.js";
|
||||
import {
|
||||
buildBootstrapContextFiles,
|
||||
DEFAULT_BOOTSTRAP_MAX_CHARS,
|
||||
DEFAULT_BOOTSTRAP_TOTAL_MAX_CHARS,
|
||||
} from "./pi-embedded-helpers.js";
|
||||
import { DEFAULT_AGENTS_FILENAME } from "./workspace.js";
|
||||
|
||||
const makeFile = (overrides: Partial<WorkspaceBootstrapFile>): WorkspaceBootstrapFile => ({
|
||||
@@ -50,4 +54,49 @@ describe("buildBootstrapContextFiles", () => {
|
||||
expect(result?.content).toBe(long);
|
||||
expect(result?.content).not.toContain("[...truncated, read AGENTS.md for full content...]");
|
||||
});
|
||||
|
||||
it("caps total injected bootstrap characters across files", () => {
|
||||
const files = [
|
||||
makeFile({ name: "AGENTS.md", content: "a".repeat(10_000) }),
|
||||
makeFile({ name: "SOUL.md", path: "/tmp/SOUL.md", content: "b".repeat(10_000) }),
|
||||
makeFile({ name: "USER.md", path: "/tmp/USER.md", content: "c".repeat(10_000) }),
|
||||
];
|
||||
const result = buildBootstrapContextFiles(files);
|
||||
const totalChars = result.reduce((sum, entry) => sum + entry.content.length, 0);
|
||||
expect(totalChars).toBeLessThanOrEqual(DEFAULT_BOOTSTRAP_TOTAL_MAX_CHARS);
|
||||
expect(result).toHaveLength(3);
|
||||
expect(result[2]?.content).toContain("[...truncated, read USER.md for full content...]");
|
||||
});
|
||||
|
||||
it("enforces strict total cap even when truncation markers are present", () => {
|
||||
const files = [
|
||||
makeFile({ name: "AGENTS.md", content: "a".repeat(1_000) }),
|
||||
makeFile({ name: "SOUL.md", path: "/tmp/SOUL.md", content: "b".repeat(1_000) }),
|
||||
];
|
||||
const result = buildBootstrapContextFiles(files, {
|
||||
maxChars: 100,
|
||||
totalMaxChars: 150,
|
||||
});
|
||||
const totalChars = result.reduce((sum, entry) => sum + entry.content.length, 0);
|
||||
expect(totalChars).toBeLessThanOrEqual(150);
|
||||
});
|
||||
|
||||
it("skips bootstrap injection when remaining total budget is too small", () => {
|
||||
const files = [makeFile({ name: "AGENTS.md", content: "a".repeat(1_000) })];
|
||||
const result = buildBootstrapContextFiles(files, {
|
||||
maxChars: 200,
|
||||
totalMaxChars: 40,
|
||||
});
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
it("keeps missing markers under small total budgets", () => {
|
||||
const files = [makeFile({ missing: true, content: undefined })];
|
||||
const result = buildBootstrapContextFiles(files, {
|
||||
totalMaxChars: 20,
|
||||
});
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0]?.content.length).toBeLessThanOrEqual(20);
|
||||
expect(result[0]?.content.startsWith("[MISSING]")).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user