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

推荐订阅源

博客园_首页
博客园 - Franky
大猫的无限游戏
大猫的无限游戏
博客园 - 三生石上(FineUI控件)
量子位
博客园 - 聂微东
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
SegmentFault 最新的问题
Apple Machine Learning Research
Apple Machine Learning Research
爱范儿
爱范儿
V
Visual Studio Blog
雷峰网
雷峰网
T
Tailwind CSS Blog
宝玉的分享
宝玉的分享
Blog — PlanetScale
Blog — PlanetScale
有赞技术团队
有赞技术团队
博客园 - 叶小钗
Microsoft Azure Blog
Microsoft Azure Blog
T
The Blog of Author Tim Ferriss
U
Unit 42
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
小众软件
小众软件
阮一峰的网络日志
阮一峰的网络日志
Y
Y Combinator 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
fix(agents): normalize session tool limits · openclaw/ope...
vincentkoc · 2026-05-28 · via Recent Commits to openclaw:main

File tree

  • src/agents/sessions/tools

Original file line numberDiff line numberDiff line change

@@ -0,0 +1,52 @@

1+

import { describe, expect, it } from "vitest";

2+

import { createFindToolDefinition, type FindOperations } from "./find.js";

3+
4+

function operations(results: string[]): FindOperations {

5+

return {

6+

exists: () => true,

7+

glob: (_pattern, _cwd, options) => results.slice(0, options.limit),

8+

};

9+

}

10+
11+

function textContent(

12+

result: Awaited<ReturnType<ReturnType<typeof createFindToolDefinition>["execute"]>>,

13+

): string {

14+

const first = result.content[0];

15+

return first?.type === "text" ? (first.text ?? "") : "";

16+

}

17+
18+

describe("find tool", () => {

19+

it("clamps non-positive limits before delegating to custom search operations", async () => {

20+

const tool = createFindToolDefinition("/workspace", {

21+

operations: operations(["/workspace/a.ts", "/workspace/b.ts"]),

22+

});

23+
24+

const result = await tool.execute(

25+

"call-1",

26+

{ pattern: "*.ts", limit: -4 },

27+

undefined,

28+

undefined,

29+

{} as never,

30+

);

31+
32+

expect(textContent(result)).toBe("a.ts\n\n[1 results limit reached]");

33+

expect(result.details?.resultLimitReached).toBe(1);

34+

});

35+
36+

it("uses the default limit for non-finite values", async () => {

37+

const tool = createFindToolDefinition("/workspace", {

38+

operations: operations(["/workspace/a.ts", "/workspace/b.ts"]),

39+

});

40+
41+

const result = await tool.execute(

42+

"call-1",

43+

{ pattern: "*.ts", limit: Number.POSITIVE_INFINITY },

44+

undefined,

45+

undefined,

46+

{} as never,

47+

);

48+
49+

expect(textContent(result)).toBe("a.ts\nb.ts");

50+

expect(result.details).toBeUndefined();

51+

});

52+

});

Original file line numberDiff line numberDiff line change

@@ -8,6 +8,7 @@ import { keyHint } from "../../modes/interactive/components/keybinding-hints.js"

88

import type { AgentTool } from "../../runtime/index.js";

99

import { ensureTool } from "../../utils/tools-manager.js";

1010

import type { ToolDefinition, ToolRenderResultOptions } from "../extensions/types.js";

11+

import { normalizePositiveLimit } from "./limits.js";

1112

import { resolveToCwd } from "./path-utils.js";

1213

import { getTextOutput, invalidArgText, shortenPath, str } from "./render-utils.js";

1314

import type { FindToolDetails } from "./tool-contracts.js";

@@ -161,7 +162,7 @@ export function createFindToolDefinition(

161162

void (async () => {

162163

try {

163164

const searchPath = resolveToCwd(searchDir || ".", cwd);

164-

const effectiveLimit = limit ?? DEFAULT_LIMIT;

165+

const effectiveLimit = normalizePositiveLimit(limit, DEFAULT_LIMIT);

165166

const ops = customOps ?? defaultFindOperations;

166167
167168

// If custom operations provide glob(), use that instead of fd.

Original file line numberDiff line numberDiff line change

@@ -8,6 +8,7 @@ import { keyHint } from "../../modes/interactive/components/keybinding-hints.js"

88

import type { AgentTool } from "../../runtime/index.js";

99

import { ensureTool } from "../../utils/tools-manager.js";

1010

import type { ToolDefinition, ToolRenderResultOptions } from "../extensions/types.js";

11+

import { normalizePositiveLimit } from "./limits.js";

1112

import { resolveToCwd } from "./path-utils.js";

1213

import { getTextOutput, invalidArgText, shortenPath, str } from "./render-utils.js";

1314

import type { GrepToolDetails } from "./tool-contracts.js";

@@ -205,7 +206,7 @@ export function createGrepToolDefinition(

205206

}

206207
207208

const contextValue = context && context > 0 ? context : 0;

208-

const effectiveLimit = Math.max(1, limit ?? DEFAULT_LIMIT);

209+

const effectiveLimit = normalizePositiveLimit(limit, DEFAULT_LIMIT);

209210

const formatPath = (filePath: string): string => {

210211

if (isDirectory) {

211212

const relative = path.relative(searchPath, filePath);

Original file line numberDiff line numberDiff line change

@@ -0,0 +1,16 @@

1+

import { describe, expect, it } from "vitest";

2+

import { normalizePositiveLimit } from "./limits.js";

3+
4+

describe("session tool limits", () => {

5+

it.each([

6+

[undefined, 500],

7+

[Number.NaN, 500],

8+

[Number.POSITIVE_INFINITY, 500],

9+

[0, 1],

10+

[-12, 1],

11+

[2.9, 2],

12+

[7, 7],

13+

])("normalizes %s to %s", (input, expected) => {

14+

expect(normalizePositiveLimit(input, 500)).toBe(expected);

15+

});

16+

});

Original file line numberDiff line numberDiff line change

@@ -0,0 +1,6 @@

1+

export function normalizePositiveLimit(value: number | undefined, fallback: number): number {

2+

if (value === undefined || !Number.isFinite(value)) {

3+

return fallback;

4+

}

5+

return Math.max(1, Math.floor(value));

6+

}

Original file line numberDiff line numberDiff line change

@@ -0,0 +1,51 @@

1+

import { describe, expect, it } from "vitest";

2+

import { createLsToolDefinition, type LsOperations } from "./ls.js";

3+
4+

function operations(entries: string[]): LsOperations {

5+

return {

6+

exists: () => true,

7+

stat: (absolutePath) => ({

8+

isDirectory: () => absolutePath === "/workspace" || absolutePath.endsWith("/dir"),

9+

}),

10+

readdir: () => entries,

11+

};

12+

}

13+
14+

function textContent(

15+

result: Awaited<ReturnType<ReturnType<typeof createLsToolDefinition>["execute"]>>,

16+

): string {

17+

const first = result.content[0];

18+

return first?.type === "text" ? (first.text ?? "") : "";

19+

}

20+
21+

describe("ls tool", () => {

22+

it("clamps non-positive limits instead of reporting a non-empty directory as empty", async () => {

23+

const tool = createLsToolDefinition("/workspace", {

24+

operations: operations(["beta.txt", "alpha.txt"]),

25+

});

26+
27+

const result = await tool.execute("call-1", { limit: 0 }, undefined, undefined, {} as never);

28+
29+

expect(textContent(result)).toBe(

30+

"alpha.txt\n\n[1 entries limit reached. Use limit=2 for more]",

31+

);

32+

expect(result.details?.entryLimitReached).toBe(1);

33+

});

34+
35+

it("uses the default limit for non-finite values", async () => {

36+

const tool = createLsToolDefinition("/workspace", {

37+

operations: operations(["beta.txt", "alpha.txt"]),

38+

});

39+
40+

const result = await tool.execute(

41+

"call-1",

42+

{ limit: Number.NaN },

43+

undefined,

44+

undefined,

45+

{} as never,

46+

);

47+
48+

expect(textContent(result)).toBe("alpha.txt\nbeta.txt");

49+

expect(result.details).toBeUndefined();

50+

});

51+

});

Original file line numberDiff line numberDiff line change

@@ -5,6 +5,7 @@ import { Type } from "typebox";

55

import { keyHint } from "../../modes/interactive/components/keybinding-hints.js";

66

import type { AgentTool } from "../../runtime/index.js";

77

import type { ToolDefinition, ToolRenderResultOptions } from "../extensions/types.js";

8+

import { normalizePositiveLimit } from "./limits.js";

89

import { resolveToCwd } from "./path-utils.js";

910

import { getTextOutput, invalidArgText, shortenPath, str } from "./render-utils.js";

1011

import type { LsToolDetails } from "./tool-contracts.js";

@@ -134,7 +135,7 @@ export function createLsToolDefinition(

134135

void (async () => {

135136

try {

136137

const dirPath = resolveToCwd(path || ".", cwd);

137-

const effectiveLimit = limit ?? DEFAULT_LIMIT;

138+

const effectiveLimit = normalizePositiveLimit(limit, DEFAULT_LIMIT);

138139
139140

// Check if path exists.

140141

if (!(await ops.exists(dirPath))) {