adding-an-agent-tool
Step-by-step reference for adding a new agent tool to KlennyCode — where the schema, dispatch case, approval preview, tool-name registries, ledger paths, barrel export and tests must all be touched, plus the prompt-caching rule that forbids state-driven tool gating. Read this before adding, removing or re-gating any tool.
- License 许可
- MIT
- 最近更新
- 2026-10-24
- Stars
- ★ 2
- Forks
- 1
01 前言 Front Matter
- name
- adding-an-agent-tool
- description
- Step-by-step reference for adding a new agent tool to KlennyCode — where the schema, dispatch case, approval preview, tool-name registries, ledger paths, barrel export and tests must all be touched, plus the prompt-caching rule that forbids state-driven tool gating. Read this before adding, removing or re-gating any tool.
03 SKILL.md 正文
Anchors here are symbol names and grep patterns, never line numbers — an earlier version of this reference lived as a root-level markdown file full of line numbers and every one of them had rotted (
executeToolhad moved ~500 lines,resolveWorkspacePath~120). Grep for the symbol.
The files a new tool touches
| Step | File | What goes there |
|---|---|---|
| 1 | agent/shared/types.ts |
add the name to the ToolName union, plus any family arrays that apply |
| 2 | agent/src/main/agent/tools/<module>.ts |
the implementation, returning ToolResultPayload |
| 3 | agent/src/main/agent/tools/index.ts |
re-export from the barrel |
| 4 | agent/src/main/agent/tools/definitions.ts |
the JSON schema in getToolDefinitions()'s all[] |
| 5 | agent/src/main/agent/orchestrator/loop.ts |
a case in dispatchTool(), and an approval entry in executeTool() if it mutates |
| 6 | agent/src/main/agent/orchestrator/approval-previews.ts |
a branch in previewMutatingTool() if it mutates |
| 7 | agent/src/main/agent/orchestrator/ledger.ts |
WRITE_TOOLS + collectPathsFromArgs if it creates files |
| 8 | agent/shared/ipc.ts, agent/src/main/ipc.ts, agent/src/preload/index.ts |
only if the renderer needs to call it |
| 9 | agent/tests/ |
a test file; barrel tests already exist and will need updating |
| 10 | README.md / KLENNY.md |
if user-visible behavior or architecture changed |
1. Register the name
ToolName in agent/shared/types.ts is the single source of truth. Below it sit the family
arrays that drive gating and behavior — decide membership deliberately for each:
CODING_ONLY_TOOLS— needs a real project workspace; excluded from Assistant tabs. Currentlyrun_command,read_terminal,codebase_search,parallel_write.ASSISTANT_TOOLS— the entire allow-set for Assistant tabs (file tools included, scoped todocumentsDirectory). If a tool should work there, it must be listed here explicitly.MUTATING_TOOLS— used by reasoning-effort scoring, not by the approval gate.ALWAYS_BLOCKED_TOOLS— hard-blocked pending human approval regardless of approval mode, and refused outright inside subagents (the Pawprint tools).BROWSER_TOOLS,DOCX_TOOLS,GMAIL_READ_TOOLS,GMAIL_SEND_TOOLS,DISCORD_TOOLS— the option-dependent families.
ASSISTANT_TOOLS' doc comment notes the Assistant system prompt spells the same names out in
prose, not generated from the array — keep both in sync by hand.
2. Implement it
Put it in the module that fits (file-ops.ts, search.ts, web.ts, shell.ts, browser.ts,
image.ts, …) or a new one. Always return ToolResultPayload:
| { ok: true; summary: string; data?: Record<string, unknown> }
| { ok: false; summary: string; error: string; data?: Record<string, unknown> }
summary is user-visible; error is a machine-readable code; data is what the model reads.
Paths and the mutation sandbox
const abs = resolveWorkspacePath(args.path, root) // tools/file-ops.ts
if (!assertMutationAllowed(abs, root)) { // main/workspace.ts
return { ok: false, summary: 'Path outside workspace', error: 'sandbox' }
}
assertMutationAllowed RETURNS false — it does not throw. A bare call inside a try block is
a silent sandbox escape (this actually shipped in parallel_write's Phase 1). Only
resolveWorkspacePath throws, and only for a structurally invalid path; it does no .. stripping,
so the boolean branch is the only thing between a relative path and an escape.
Allowed mutation roots: the open workspace, the Assistant documentsDirectory, and always
~/.klenny plus Electron's userData (alwaysAllowedMutationRoots()).
Other implementation invariants
- Respect EOL:
detectEol/toLf/fromLf(tools/eol.ts) — CRLF is the primary dev platform. - For edits, honor
fileReadCachestaleness (mtimeMsmismatch →error: 'stale'). - A diff is a bounded preview, never raw content. Use
makeDiff/joinDiffs(tools/diff.ts) andreadTextForDiff()rather thanreadFile+ diff — an unbounded diff once wrote a 61 MB session file and froze the app. - If the model must see an image, the key is exactly
data.dataUrl(lifted out byloop.ts); any other key silently becomes truncated base64 garbage. - Wrap anything that can hang (Playwright, network) in a deadline race, as
browser.tsdoes.
3. Barrel export
Add it to agent/src/main/agent/tools/index.ts. agent/tests/tools-index-barrel.test.ts asserts
the barrel re-exports every implementation function — extend it.
4. Schema in definitions.ts
Append to the all[] array inside getToolDefinitions(mode, restrictTo, codebaseSearchAvailable, hasWorkspace, isAssistant, gating):
{
type: 'function',
function: {
name: 'write_file',
description: 'Write or overwrite a file.',
parameters: {
type: 'object',
properties: { path: { type: 'string' }, content: { type: 'string' } },
required: ['path', 'content']
}
}
}
Schemas are documentation only — there is no ajv/zod validation at dispatch. Real enforcement
is in the implementation, and models do send JSON-string-encoded arrays where a native array was
declared, so coerce defensively (see normalizeFilesArg in file-ops.ts and
coerce-array-arg.test.ts).
The description is the model's only instruction manual for the tool; spend words on routing rules and failure modes, not just a label.
⛔ The gating rule you must not break
getToolDefinitions()'s return value is the request's tools array, and providers serialize
it ahead of the system prompt. A cached block is keyed on its entire preceding prefix, so a
tools array that changes mid-conversation invalidates every cache breakpoint, including the
fixed system one.
Never add a gate driven by per-turn conversation state. This was a real, measured bug:
update_checklist used to be hidden until TabSession.activeChecklist existed, so the first
create_checklist call grew the array and re-wrote the whole prefix at full price
(cachedTokens=0, cacheWriteTokens=53456) while the system message's own fingerprint was
byte-identical. Every legitimate gate derives from settings, tab kind or subagent type — all fixed
for a conversation's life. Prefer always offering the tool and returning a clean error
(no_active_checklist) over hiding it.
Gates at the end of the function, all filter-style, all legitimate: hasWorkspace,
codebaseSearchAvailable, gating.imageGenerationAvailable, docx/Gmail/Discord connection +
automationPermissions + *AvailableInCoding, and gating.browserAutomationAvailable. Add new
optional fields to ToolGatingOptions, which defaults every field closed so a caller that
forgets one never over-shares.
agent/tests/tools-cache-stability.test.ts pins this; it has positive controls, so don't "fix" a
failure by blinding it.
5. Dispatch and approval in loop.ts
dispatchTool() is a plain switch (name) ending in default: { ok: false, error: 'unknown_tool' }:
case 'edit_file':
return editFileTool(args as { path: string; old_string: string; new_string: string; replace_all?: boolean }, fileRoot)
fileRoot is the sandbox root (Assistant documentsDirectory, else undefined = open workspace).
Extra capabilities arrive as injected dispatch objects (imageGen, parallelWrite, toolCallId)
rather than globals — follow that pattern instead of reaching for module state.
executeTool() runs the gates in order before dispatch:
- Assistant-tab guard —
tab.kind === 'assistant' && CODING_ONLY_TOOLS.includes(name)is refused server-side, becausegetWorkspace()is a process-global singleton and a hallucinated or stale call would otherwise execute against whatever project another window has open. The client-side allow-list is not sufficient on its own. ALWAYS_BLOCKED_TOOLS— always prompts; refused withunsupported_in_subagentin a subagent.- The standard mutating list —
['write_file', 'edit_file', 'multi_edit', 'multi_write', 'delete_file', 'write_docx', 'edit_docx', 'generate_image', 'run_command']:
const needsApproval = approvalMode === 'manual' || (approvalMode === 'command' && name === 'run_command')
if (needsApproval) {
const preview = await previewMutatingTool(name, args, fileRoot)
const action = approvalManager.buildPendingFromTool(tab.id, tc.id, name as PendingActionKind, preview.title, preview.extra)
emit({ type: 'pending_action', tabId: tab.id, action })
const decision = await approvalManager.waitForDecision(action.id)
emit({ type: 'pending_action_resolved', tabId: tab.id, actionId: action.id })
if (decision === 'reject') return { payload: { ok: false, summary: 'User rejected action', error: 'rejected' }, status: 'rejected' }
} else {
const ws = getWorkspace()
if (ws) await approvalManager.createCheckpoint(ws)
}
Adding a name here also means adding it to PendingActionKind (grep it in agent/shared/types.ts)
and handling the card in ApprovalCard.tsx. Note the else branch: skipping approval still takes
a checkpoint — don't drop it.
A tool whose content doesn't exist yet must not be listed here. parallel_write is
deliberately absent and instead requests approval from inside the tool, once per job, via an
injected approve() callback; listing it would queue a second, contentless card. browser has its
own independent policy gate ('off' | 'ask' | 'auto') keyed on whether the action is mutating.
Subagent exclusion is a runtime check, not an allow-list one. A subagent type may declare
tools: 'all', so allow-lists alone leak; parallel_write checks unattended inside its dispatch
case. Subagent runs also force approvalMode: 'auto', so anything needing a human must refuse
outright there.
6. Approval preview
previewMutatingTool(name, args, root) returns { title, extra }:
if (name === 'write_file') {
const { content: oldContent } = await readTextForDiff(resolveWorkspacePath(path, root))
return { title: `Write ${path}`, extra: { filePath: path, diff: makeDiff(oldContent, String(args.content), path) } }
}
This path ships over IPC to the renderer, so it needs the same bounded-diff discipline as the tool
itself — it once had the identical unbounded-diff bug. Never let a preview throw; degrade to
{ title, extra: { filePath } } with no diff.
7. Ledger (only if the tool creates files)
The fabrication guard's C3 check flags a claimed-created file that no write targeted. Add the name
to WRITE_TOOLS in orchestrator/ledger.ts and teach collectPathsFromArgs your argument shape
(including alias keys and map forms, as multi_write needs) — otherwise a file genuinely written
by your tool gets hard-flagged as fabricated.
8. IPC (only if the renderer calls it)
Four coordinated edits, all in agent/shared/ipc.ts + agent/src/main/ipc.ts +
agent/src/preload/index.ts:
- A channel constant in
IPC, namednamespace:action('memory:write','chat:sendMessage'). - A method on the
KlennyApiinterface. ipcMain.handle(IPC.x, …)in main.ipcRenderer.invoke(IPC.x, …)in the preload bridge (or anon-listener returning an unsubscribe function, for streaming events).
9. Tests
bun test (Bun, not npm/yarn). Existing suites you will likely need to update: tools.test.ts,
tools-index-barrel.test.ts, tools-cache-stability.test.ts, global-mutation-sandbox.test.ts,
ledger.test.ts, and diff-safety.test.ts if the tool produces diffs. Cover at minimum: the happy
path, an out-of-sandbox path (asserting nothing was written), and a malformed-arguments shape.
Checklist
ToolName+ family arrays inagent/shared/types.ts- Implementation returning
ToolResultPayload, with theassertMutationAllowedreturn-value branch if it mutates - Barrel export in
tools/index.ts - Schema in
getToolDefinitions()'sall[]— no per-turn-state gate dispatchTool()case; approval entry +PendingActionKindif mutating; runtimeunattendedrefusal if it needs a humanpreviewMutatingTool()branch with a bounded diffWRITE_TOOLS+collectPathsFromArgsif it creates files- IPC channel / handler / preload /
KlennyApiif the UI needs it - Tests in
agent/tests/ README.mdandKLENNY.mdif documented behavior changed
04 同仓库更多 Skills
prompt-caching-safety
Before adding anything to the system prompt or the start of the message array in this codebase, check whether it breaks Anthropic/Qwen-style explicit prompt caching — a real regression that shipped once (see project memory "System prompt caching regression..."). Use this whenever touching buildSystemPrompt, toORMessages, applyCacheControl, or anything that runs on every agent turn.
placeholder-unused
placeholder
caching-live-verification
MANDATORY for any change touching prompt caching (openrouter/caching.ts, cacheDiag.ts, client.ts request building, the tools array, toORMessages, the trailing time note, compaction): write a throwaway script that runs a REAL live OpenRouter request ladder, run it, confirm read-back, then delete the script. Unit tests cannot observe upstream cache behavior.