Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions packages/agentctx/src/cli/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
import { existsSync } from "node:fs";
import { parseArgs } from "node:util";
import { SEARCH_LIMIT_DEFAULT, SEARCH_LIMIT_MAX } from "../mcp/tools.js";
import { openDatabase } from "../storage/db.js";
import { resolveProjectId } from "../storage/namespace.js";
import { searchRecords } from "../storage/search.js";
Expand All @@ -18,10 +19,7 @@ export const SEARCH_USAGE = `Usage: agentctx search <query> [options]

Options:
--type <type> restrict to one record type (${RECORD_TYPES.join(", ")})
--limit <n> maximum results (default 10, max 50)`;

const LIMIT_DEFAULT = 10;
const LIMIT_MAX = 50;
--limit <n> maximum results (default ${SEARCH_LIMIT_DEFAULT}, max ${SEARCH_LIMIT_MAX})`;

export async function runSearch(env: CliEnv, args: string[]): Promise<number> {
if (args.includes("--help")) {
Expand Down Expand Up @@ -53,11 +51,11 @@ export async function runSearch(env: CliEnv, args: string[]): Promise<number> {
}
type = values.type as RecordType;
}
let limit = LIMIT_DEFAULT;
let limit = SEARCH_LIMIT_DEFAULT;
if (values.limit !== undefined) {
const parsed = Number(values.limit);
if (!Number.isInteger(parsed) || parsed < 1 || parsed > LIMIT_MAX) {
env.io.err(`agentctx search: --limit must be an integer between 1 and ${LIMIT_MAX}`);
if (!Number.isInteger(parsed) || parsed < 1 || parsed > SEARCH_LIMIT_MAX) {
env.io.err(`agentctx search: --limit must be an integer between 1 and ${SEARCH_LIMIT_MAX}`);
return 1;
}
limit = parsed;
Expand Down
29 changes: 29 additions & 0 deletions packages/agentctx/test/cli/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,35 @@ describe("agentctx config", () => {
});
});

describe("agentctx search", () => {
it("rejects limits above the shared MCP/SPEC cap", async () => {
await main(["init", "--no-profile", "--no-mcp"], t.env);

expect(await main(["search", "caching", "--limit", "16"], t.env)).toBe(1);
expect(t.stderr.join("\n")).toContain("between 1 and 15");
});

it("accepts the documented max limit", async () => {
await main(["init", "--no-profile", "--no-mcp"], t.env);
const projectId = resolveProjectId(t.env.cwd);

const db = openDatabase(t.env.dbPath);
for (let i = 0; i < 20; i++) {
insertRecord(db, {
projectId,
type: "decision",
title: `Decision ${i}`,
body: `caching detail ${i}`,
source: "cli",
});
}
db.close();

expect(await main(["search", "caching", "--limit", "15"], t.env)).toBe(0);
expect(t.stdout.filter((line) => line.includes("Decision "))).toHaveLength(15);
});
});

describe("agentctx reset", () => {
it("deletes only the current project's records, after confirmation", async () => {
await main(["init", "--no-profile", "--no-mcp"], t.env);
Expand Down
Loading