@useavalon/caelence-agent 0.2.2-canary.2 → 0.2.2-canary.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@useavalon/caelence-agent",
3
- "version": "0.2.2-canary.2",
3
+ "version": "0.2.2-canary.3",
4
4
  "description": "Caelence agent: Strands turn loop, local tools, skills, evals, and a themable terminal UI.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -57,4 +57,46 @@ describe("exec approval", () => {
57
57
  await gate.request({ callId: "1", toolName: "exec", input: { command: "sudo ls" } }),
58
58
  ).toBe(false);
59
59
  });
60
+
61
+ test("prompt policy asks before ordinary file writes", async () => {
62
+ const asked: string[] = [];
63
+ const alwaysAllow = new Set<string>();
64
+ const gate = createExecApprovalGate({
65
+ policy: "prompt",
66
+ alwaysAllow,
67
+ ask: async (req) => {
68
+ asked.push(`${req.toolName}:${String(req.input.path)}`);
69
+ return true;
70
+ },
71
+ });
72
+ expect(
73
+ await gate.request({
74
+ callId: "1",
75
+ toolName: "write_file",
76
+ input: { path: "README.md", content: "x" },
77
+ }),
78
+ ).toBe(true);
79
+ expect(asked).toEqual(["write_file:README.md"]);
80
+ alwaysAllow.add("write_file");
81
+ alwaysAllow.add("edit_file");
82
+ expect(
83
+ await gate.request({
84
+ callId: "2",
85
+ toolName: "edit_file",
86
+ input: { path: "README.md", old: "x", new: "y" },
87
+ }),
88
+ ).toBe(true);
89
+ expect(asked).toEqual(["write_file:README.md"]);
90
+ });
91
+
92
+ test("deny and auto still allow file writes", async () => {
93
+ const deny = createExecApprovalGate({ policy: "deny" });
94
+ const auto = createExecApprovalGate({ policy: "auto" });
95
+ expect(
96
+ await deny.request({ callId: "1", toolName: "write_file", input: { path: "a.ts" } }),
97
+ ).toBe(true);
98
+ expect(
99
+ await auto.request({ callId: "2", toolName: "edit_file", input: { path: "a.ts" } }),
100
+ ).toBe(true);
101
+ });
60
102
  });
@@ -16,6 +16,10 @@ export function isRiskyToolCall(toolName: string): boolean {
16
16
  return toolName === "exec" || toolName === "git_commit";
17
17
  }
18
18
 
19
+ export function isFileWriteTool(toolName: string): boolean {
20
+ return toolName === "edit_file" || toolName === "write_file";
21
+ }
22
+
19
23
  /** Host-side denylist — local exec is the machine, not a sandbox. */
20
24
  export const DESTRUCTIVE_COMMAND_PATTERNS: readonly RegExp[] = [
21
25
  /\bsudo\b/,
@@ -55,10 +59,28 @@ export interface ExecApprovalGateDeps {
55
59
  alwaysAllow?: Set<string>;
56
60
  }
57
61
 
62
+ async function promptAsk(deps: ExecApprovalGateDeps, req: ApprovalRequest): Promise<boolean> {
63
+ deps.emit?.({
64
+ kind: "approval_request",
65
+ callId: req.callId,
66
+ toolName: req.toolName,
67
+ input: req.input,
68
+ });
69
+ if (!deps.ask) return false;
70
+ return deps.ask(req);
71
+ }
72
+
58
73
  export function createExecApprovalGate(deps: ExecApprovalGateDeps): ApprovalGate {
59
74
  const always = deps.alwaysAllow ?? new Set<string>();
60
75
  return {
61
76
  async request(req) {
77
+ if (isFileWriteTool(req.toolName)) {
78
+ if (always.has("write_file") || always.has("edit_file") || always.has(req.toolName)) {
79
+ return true;
80
+ }
81
+ if (deps.policy === "prompt" && deps.ask) return promptAsk(deps, req);
82
+ return true;
83
+ }
62
84
  if (!isRiskyToolCall(req.toolName)) return true;
63
85
  if (always.has(req.toolName)) return true;
64
86
  if (deps.policy === "deny") return false;
@@ -66,14 +88,7 @@ export function createExecApprovalGate(deps: ExecApprovalGateDeps): ApprovalGate
66
88
  if (deps.policy === "auto") {
67
89
  return !isLikelyDestructive(command);
68
90
  }
69
- deps.emit?.({
70
- kind: "approval_request",
71
- callId: req.callId,
72
- toolName: req.toolName,
73
- input: req.input,
74
- });
75
- if (!deps.ask) return false;
76
- return deps.ask(req);
91
+ return promptAsk(deps, req);
77
92
  },
78
93
  };
79
94
  }
package/src/core/mode.ts CHANGED
@@ -55,6 +55,7 @@ export function modePrompt(mode: AgentMode): string {
55
55
  return [
56
56
  "## Mode: agent",
57
57
  "You may edit files, run approved exec, and commit when the user asked for the change.",
58
+ "If they asked to change a file, call write_file or edit_file. Do not claim the write until the tool succeeds.",
58
59
  "Use todo_write for multi-step work. Prefer git_status / git_diff / git_log over exec for git.",
59
60
  ].join("\n");
60
61
  }
@@ -2,7 +2,7 @@ import { noopObservability } from "../observability/noop.ts";
2
2
  import { sanitize } from "../observability/sanitize.ts";
3
3
  import type { Observability } from "../observability/types.ts";
4
4
  import type { ApprovalGate } from "./approval.ts";
5
- import { isRiskyToolCall } from "./approval.ts";
5
+ import { isFileWriteTool, isRiskyToolCall } from "./approval.ts";
6
6
  import { errorMessage, isAbortError } from "./errors.ts";
7
7
  import type { AgentEventEmitter, MainModelProvider } from "./events.ts";
8
8
  import type { HookRunner } from "./hooks.ts";
@@ -359,7 +359,7 @@ async function invokeGuardedBody(
359
359
  result = errorResult(errorText);
360
360
  }
361
361
  let approved = true;
362
- if (!result && isRiskyToolCall(def.name)) {
362
+ if (!result && (isRiskyToolCall(def.name) || isFileWriteTool(def.name))) {
363
363
  approved = await approval.request({ callId, toolName: def.name, input });
364
364
  }
365
365
  if (result) {
@@ -439,6 +439,7 @@ export function buildSystemPrompt(input: {
439
439
  "You are a coding agent in this project.",
440
440
  `Project root: ${input.cwd}.`,
441
441
  "Prefer small, targeted edits. Use edit_file for snippets, write_file for new files.",
442
+ "If the user asked to change a file, call write_file or edit_file before you reply. Do not say you wrote, edited, or saved a file unless that tool returned success.",
442
443
  "Use read_file, glob, and grep to inspect the repo. Do not use exec to read or search files.",
443
444
  "Use git_status, git_diff, and git_log for git. git_commit requires approval and does not push.",
444
445
  "Read skills with read_skill when a task matches a skill name. Spawn a subagent with task for a focused job.",
package/src/runtime.ts CHANGED
@@ -6,6 +6,7 @@ import {
6
6
  type ApprovalRequest,
7
7
  createExecApprovalGate,
8
8
  type ExecApprovalPolicy,
9
+ isFileWriteTool,
9
10
  } from "./core/approval.ts";
10
11
  import type { AgentEventEmitter, MainModelProvider } from "./core/events.ts";
11
12
  import { createHookRunner } from "./core/hooks.ts";
@@ -295,6 +296,10 @@ export async function createHarness(options: CreateHarnessOptions): Promise<Harn
295
296
  },
296
297
  allowRiskyAlways(toolName) {
297
298
  alwaysAllow.add(toolName);
299
+ if (isFileWriteTool(toolName)) {
300
+ alwaysAllow.add("write_file");
301
+ alwaysAllow.add("edit_file");
302
+ }
298
303
  },
299
304
  async compact() {
300
305
  if (!runtime.activeSessionId) return false;