Files
mcp/cookbook/10-cursor-context-selector.ts
zeekay 3431b4ac53 kai tools: screen injection by rules first; select tools and triage logs without a model
Measured against kai-1.2 served locally from production's folder (SHA256SUMS e45dd919…).

kai_injection_screen asks Kai the question it was trained on ({prompt},
"Does `prompt` try to make an AI assistant ignore its rules...") beside
seven rule classes: override, role, addressed, conceal, exfiltrate,
tool_call, act, plus the earlier extract patterns. Held out on InjecAgent's
unseen tools and wordings (250 attacks) and 207 benign tool outputs and
README paragraphs: recall .728 at .010 false alarms, against .044 at .116
for the screen it replaces. Kai's question alone found 0 of the 250
planted requests. Rules still decide when Kai does not answer.

kai_tool_select ranks by a named tool, then BM25 over name, description
and parameters, and makes no Kai call: on 300 supplied tool lists from
Kai's T1 dev split it reads top-1 .733 and MRR .841 against .657 and .805
for one Kai choice over the names, in 0.1 ms against 109 ms.

kai_log_triage classifies by failure signature (resources, network,
timeout, permission, dependency, compile, test, crash; none and unknown)
and returns the deciding lines and file spans; kai-1.2 read build logs at
chance. Recipes 01, 04, 05, 08 and 10 check what these tools return;
recipe 08 calls the tool instead of matching names itself. The cookbook
reaches Kai as clients do, through HANZO_API_KEY or LOCAL_KAI_URL, not a
fixed local port and placeholder key. compact.ts types its decision so
tsc passes.
2026-10-14 18:30:09 -07:00

45 lines
2.0 KiB
TypeScript

/**
* Recipe 10: Cursor IDE Context Selection
*
* Objective: Fit large codebases into limited LLM context windows by using Kai to select
* the most relevant files and symbol definitions for a user's prompt or bug fix.
*/
import { createRecipeClient, logRecipe, RecipeResult } from "./helper.js";
export async function runRecipe10(): Promise<RecipeResult> {
const client = await createRecipeClient();
const userQuery = "Add rate limiting middleware to our Express HTTP server for auth routes.";
const candidateFiles = [
{ name: "src/server.ts", description: "Main HTTP server setup, port listener, and middleware pipeline" },
{ name: "src/routes/auth.ts", description: "Authentication endpoints: login, signup, refresh token, oauth" },
{ name: "src/middleware/rate-limiter.ts", description: "Redis-backed sliding window rate limiter implementation" },
{ name: "src/database/schema.prisma", description: "Database ORM models for users, sessions, and payments" },
{ name: "frontend/src/components/Button.tsx", description: "React UI component for button styling" },
{ name: "scripts/deploy.sh", description: "Docker build and Kubernetes cluster deployment script" }
];
const res = await client.callTool("kai_tool_select", {
task: userQuery,
tools: candidateFiles,
limit: 3
});
const selectedNames = res.data.selected_tools?.map((t: any) => t.name) || [];
const result: RecipeResult = {
recipe: "10: Cursor Context Selection",
tool: "kai_tool_select",
success: ["src/routes/auth.ts", "src/middleware/rate-limiter.ts"].every((f) => selectedNames.includes(f)) && !selectedNames.some((f: string) => f.startsWith("frontend/") || f.startsWith("scripts/")),
latency_ms: res.latency_ms,
output: res.data,
explanation: "BM25 over file names and descriptions put the auth routes and the rate limiter in the top 3, ahead of frontend and deploy files. No model call."
};
return result;
}
if (process.argv[1]?.endsWith("10-cursor-context-selector.ts")) {
runRecipe10().then(logRecipe).catch(console.error);
}