@phren/agent 0.3.19 → 0.3.21
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/dist/agent-loop/index.js +185 -47
- package/dist/agent-loop/stream.js +74 -66
- package/dist/agent-loop/types.js +2 -3
- package/dist/attach-images.js +57 -0
- package/dist/bin.js +7 -2
- package/dist/commands/info.js +7 -3
- package/dist/commands/model.js +55 -13
- package/dist/commands/session.js +79 -18
- package/dist/commands.js +24 -3
- package/dist/config.js +89 -10
- package/dist/context/clear-tool-results.js +43 -0
- package/dist/context/compactor.js +13 -3
- package/dist/context/pruner.js +29 -11
- package/dist/context/usage.js +26 -0
- package/dist/cost.js +68 -10
- package/dist/headless.js +47 -1
- package/dist/index.js +335 -127
- package/dist/mcp-client.js +38 -1
- package/dist/mcp-prompts.js +94 -0
- package/dist/memory/chat.js +70 -0
- package/dist/memory/context.js +1 -1
- package/dist/model-overrides.js +17 -0
- package/dist/models-list.js +73 -0
- package/dist/models.js +42 -15
- package/dist/multi/child-entry.js +18 -28
- package/dist/multi/ipc-hooks.js +34 -0
- package/dist/multi/spawner.js +49 -3
- package/dist/permissions/allowlist.js +19 -4
- package/dist/permissions/checker.js +37 -20
- package/dist/permissions/kernel-sandbox.js +32 -10
- package/dist/permissions/rules.js +173 -0
- package/dist/permissions/shell-classify.js +173 -0
- package/dist/plan.js +6 -2
- package/dist/providers/anthropic.js +119 -75
- package/dist/providers/codex.js +10 -9
- package/dist/providers/ollama.js +5 -2
- package/dist/providers/openai-compat.js +219 -92
- package/dist/providers/openrouter.js +12 -7
- package/dist/providers/resolve.js +47 -1
- package/dist/providers/retry.js +20 -7
- package/dist/providers/types.js +31 -0
- package/dist/repl.js +17 -1
- package/dist/session/persist.js +9 -4
- package/dist/session/preview.js +99 -0
- package/dist/settings.js +14 -0
- package/dist/structured-output.js +85 -0
- package/dist/system-prompt.js +117 -14
- package/dist/tools/apply-patch.js +15 -1
- package/dist/tools/edit-file.js +8 -1
- package/dist/tools/file-state.js +88 -0
- package/dist/tools/glob.js +53 -35
- package/dist/tools/grep.js +139 -53
- package/dist/tools/lint-test.js +24 -0
- package/dist/tools/read-file.js +2 -0
- package/dist/tools/registry.js +8 -1
- package/dist/tools/search-support.js +141 -0
- package/dist/tools/shell.js +266 -41
- package/dist/tools/spawn-agent.js +15 -3
- package/dist/tools/syntax-check.js +116 -0
- package/dist/tools/write-file.js +11 -3
- package/dist/tui/components/App.js +5 -4
- package/dist/tui/components/Banner.js +1 -1
- package/dist/tui/components/ListPicker.js +32 -0
- package/dist/tui/ink-entry.js +79 -15
- package/dist/user-hooks.js +26 -4
- package/package.json +3 -2
- package/dist/memory/context-flush.js +0 -24
package/dist/session/persist.js
CHANGED
|
@@ -113,6 +113,8 @@ export function listEventLogs(phrenPath, opts = {}) {
|
|
|
113
113
|
const { header, events } = loadEventLog(file);
|
|
114
114
|
if (opts.project && header.project !== opts.project)
|
|
115
115
|
continue;
|
|
116
|
+
if (opts.cwd && header.cwd !== opts.cwd)
|
|
117
|
+
continue;
|
|
116
118
|
const log = SessionLog.restore(header, events, () => { });
|
|
117
119
|
const msgs = log.getMessages();
|
|
118
120
|
if (msgs.length === 0)
|
|
@@ -158,10 +160,11 @@ export function findEventLogById(phrenPath, idPrefix) {
|
|
|
158
160
|
return path.join(dir, entries[0]);
|
|
159
161
|
}
|
|
160
162
|
/**
|
|
161
|
-
* Newest event log for resume, optionally filtered by project
|
|
163
|
+
* Newest event log for resume, optionally filtered by project or by the
|
|
164
|
+
* directory it ran in (sessions kept without a store have no project).
|
|
162
165
|
* @returns the file path, or null when none exist.
|
|
163
166
|
*/
|
|
164
|
-
export function findLatestEventLog(phrenPath, project) {
|
|
167
|
+
export function findLatestEventLog(phrenPath, project, cwd) {
|
|
165
168
|
const dir = sessionsDir(phrenPath);
|
|
166
169
|
let entries;
|
|
167
170
|
try {
|
|
@@ -174,9 +177,11 @@ export function findLatestEventLog(phrenPath, project) {
|
|
|
174
177
|
for (const entry of entries) {
|
|
175
178
|
const file = path.join(dir, entry);
|
|
176
179
|
try {
|
|
177
|
-
if (project) {
|
|
180
|
+
if (project || cwd) {
|
|
178
181
|
const { header } = loadEventLog(file);
|
|
179
|
-
if (header.project !== project)
|
|
182
|
+
if (project && header.project !== project)
|
|
183
|
+
continue;
|
|
184
|
+
if (cwd && header.cwd !== cwd)
|
|
180
185
|
continue;
|
|
181
186
|
}
|
|
182
187
|
candidates.push({ file, mtimeMs: fs.statSync(file).mtimeMs });
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The live reply sidecar: `<event log>.preview.json` holds the assistant text
|
|
3
|
+
* block the model is streaming right now, as `{turnStartedAt, text}` (the
|
|
4
|
+
* shape OpenCode's plugin writes). The event log only gets whole messages;
|
|
5
|
+
* Phren Hook reads this file to stream a reply to the phone while it is
|
|
6
|
+
* still being written. Removed once the message lands in the log.
|
|
7
|
+
*/
|
|
8
|
+
import * as fs from "fs";
|
|
9
|
+
import * as path from "path";
|
|
10
|
+
import { sessionsDir } from "@phren/cli/session/utils";
|
|
11
|
+
import { eventLogPath } from "./persist.js";
|
|
12
|
+
/** At most this often on disk; the Hook reads it twice a second. */
|
|
13
|
+
export const PREVIEW_WRITE_MS = 100;
|
|
14
|
+
const MAX_TEXT = 32_768;
|
|
15
|
+
/**
|
|
16
|
+
* A sidecar untouched this long belongs to a process that died mid-reply
|
|
17
|
+
* (a live one rewrites it while streaming and removes it when done).
|
|
18
|
+
*/
|
|
19
|
+
export const STALE_PREVIEW_MS = 30 * 60_000;
|
|
20
|
+
/**
|
|
21
|
+
* Remove sidecars and their staging files that a killed agent left behind.
|
|
22
|
+
* Best effort; run when an agent session starts or resumes.
|
|
23
|
+
*/
|
|
24
|
+
export function removeStalePreviews(phrenPath, maxAgeMs = STALE_PREVIEW_MS, now = Date.now()) {
|
|
25
|
+
const dir = sessionsDir(phrenPath);
|
|
26
|
+
let entries;
|
|
27
|
+
try {
|
|
28
|
+
entries = fs.readdirSync(dir);
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return 0;
|
|
32
|
+
}
|
|
33
|
+
let removed = 0;
|
|
34
|
+
for (const name of entries) {
|
|
35
|
+
if (!/\.preview\.json(?:\.\d+\.tmp)?$/.test(name))
|
|
36
|
+
continue;
|
|
37
|
+
const file = path.join(dir, name);
|
|
38
|
+
try {
|
|
39
|
+
if (now - fs.statSync(file).mtimeMs < maxAgeMs)
|
|
40
|
+
continue;
|
|
41
|
+
fs.rmSync(file, { force: true });
|
|
42
|
+
removed++;
|
|
43
|
+
}
|
|
44
|
+
catch { /* gone already */ }
|
|
45
|
+
}
|
|
46
|
+
return removed;
|
|
47
|
+
}
|
|
48
|
+
export function previewPath(phrenPath, sessionId) {
|
|
49
|
+
return `${eventLogPath(phrenPath, sessionId)}.preview.json`;
|
|
50
|
+
}
|
|
51
|
+
export function livePreview(file, now = Date.now) {
|
|
52
|
+
let turnStartedAt = "";
|
|
53
|
+
let text = "";
|
|
54
|
+
let writtenAt = -Infinity;
|
|
55
|
+
let timer;
|
|
56
|
+
const cancel = () => { if (timer) {
|
|
57
|
+
clearTimeout(timer);
|
|
58
|
+
timer = undefined;
|
|
59
|
+
} };
|
|
60
|
+
const write = () => {
|
|
61
|
+
cancel();
|
|
62
|
+
if (!turnStartedAt || !text)
|
|
63
|
+
return;
|
|
64
|
+
writtenAt = now();
|
|
65
|
+
const staging = `${file}.${process.pid}.tmp`;
|
|
66
|
+
try {
|
|
67
|
+
fs.writeFileSync(staging, JSON.stringify({ turnStartedAt, text }), { mode: 0o600 });
|
|
68
|
+
fs.renameSync(staging, file);
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
try {
|
|
72
|
+
fs.rmSync(staging, { force: true });
|
|
73
|
+
}
|
|
74
|
+
catch { /* best effort */ }
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
return {
|
|
78
|
+
// A new response (or a retried one) shows its first words at once.
|
|
79
|
+
start(at) { cancel(); turnStartedAt = at; text = ""; writtenAt = -Infinity; },
|
|
80
|
+
append(delta) {
|
|
81
|
+
if (!turnStartedAt || text.length >= MAX_TEXT)
|
|
82
|
+
return;
|
|
83
|
+
text = (text + delta).slice(0, MAX_TEXT);
|
|
84
|
+
const wait = writtenAt + PREVIEW_WRITE_MS - now();
|
|
85
|
+
if (wait <= 0)
|
|
86
|
+
write();
|
|
87
|
+
else if (!timer)
|
|
88
|
+
timer = setTimeout(write, wait);
|
|
89
|
+
},
|
|
90
|
+
clear() {
|
|
91
|
+
cancel();
|
|
92
|
+
text = "";
|
|
93
|
+
try {
|
|
94
|
+
fs.rmSync(file, { force: true });
|
|
95
|
+
}
|
|
96
|
+
catch { /* best effort */ }
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
}
|
package/dist/settings.js
CHANGED
|
@@ -40,6 +40,20 @@ export function loadPermissionMode() {
|
|
|
40
40
|
}
|
|
41
41
|
return undefined;
|
|
42
42
|
}
|
|
43
|
+
/** Projects whose own MCP config (`.mcp.json`) the user chose to load. */
|
|
44
|
+
export function isMcpProjectTrusted(projectRoot) {
|
|
45
|
+
const list = readSettings().trustedMcpProjects;
|
|
46
|
+
return Array.isArray(list) && list.includes(path.resolve(projectRoot));
|
|
47
|
+
}
|
|
48
|
+
export function trustMcpProject(projectRoot) {
|
|
49
|
+
const data = readSettings();
|
|
50
|
+
const list = Array.isArray(data.trustedMcpProjects) ? data.trustedMcpProjects.filter((p) => typeof p === "string") : [];
|
|
51
|
+
const root = path.resolve(projectRoot);
|
|
52
|
+
if (!list.includes(root))
|
|
53
|
+
list.push(root);
|
|
54
|
+
data.trustedMcpProjects = list;
|
|
55
|
+
writeSettings(data);
|
|
56
|
+
}
|
|
43
57
|
export function loadTheme() {
|
|
44
58
|
const name = readSettings().theme;
|
|
45
59
|
return typeof name === "string" && name ? name : undefined;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `--json-schema`: a headless run that ends with a JSON value matching the
|
|
3
|
+
* caller's schema, in the result's `structured_output`, as Claude Code's
|
|
4
|
+
* option does.
|
|
5
|
+
*
|
|
6
|
+
* Provider-agnostic: once the task is done, one more request (the session's
|
|
7
|
+
* history, the same tools so Anthropic accepts the tool history, no new tool
|
|
8
|
+
* calls asked for) tells the model to answer with JSON only. The answer is
|
|
9
|
+
* parsed and validated; on a mismatch the model gets the validation errors
|
|
10
|
+
* and tries again, twice at most. The exchange stays out of the session log.
|
|
11
|
+
*/
|
|
12
|
+
import * as fs from "fs";
|
|
13
|
+
import { Ajv } from "ajv";
|
|
14
|
+
import { recordTokenUsage } from "./cost.js";
|
|
15
|
+
const MAX_ATTEMPTS = 3;
|
|
16
|
+
/** Read `--json-schema`: inline JSON, or a path to a JSON file. Throws a user-facing error. */
|
|
17
|
+
export function loadJsonSchema(raw) {
|
|
18
|
+
const text = raw.trim().startsWith("{") ? raw : (() => {
|
|
19
|
+
try {
|
|
20
|
+
return fs.readFileSync(raw, "utf-8");
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
throw new Error(`--json-schema: "${raw}" is neither a JSON object nor a readable file.`);
|
|
24
|
+
}
|
|
25
|
+
})();
|
|
26
|
+
let schema;
|
|
27
|
+
try {
|
|
28
|
+
schema = JSON.parse(text);
|
|
29
|
+
}
|
|
30
|
+
catch (err) {
|
|
31
|
+
throw new Error(`--json-schema is not valid JSON: ${err instanceof Error ? err.message : String(err)}`);
|
|
32
|
+
}
|
|
33
|
+
if (!schema || typeof schema !== "object" || Array.isArray(schema))
|
|
34
|
+
throw new Error("--json-schema must be a JSON Schema object.");
|
|
35
|
+
try {
|
|
36
|
+
const validate = new Ajv({ allErrors: true, strict: false }).compile(schema);
|
|
37
|
+
return { schema: schema, validate };
|
|
38
|
+
}
|
|
39
|
+
catch (err) {
|
|
40
|
+
throw new Error(`--json-schema is not a usable JSON Schema: ${err instanceof Error ? err.message : String(err)}`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/** The JSON in a model's reply: the whole reply, or the first fenced block. */
|
|
44
|
+
export function extractJson(text) {
|
|
45
|
+
const fenced = /```(?:json)?\s*\n([\s\S]*?)```/.exec(text);
|
|
46
|
+
return JSON.parse((fenced ? fenced[1] : text).trim());
|
|
47
|
+
}
|
|
48
|
+
function instruction(schema) {
|
|
49
|
+
return [
|
|
50
|
+
"The task is done. Now reply with ONLY a JSON value that matches this JSON Schema: no prose, no code fence, no tool calls.",
|
|
51
|
+
"",
|
|
52
|
+
JSON.stringify(schema, null, 2),
|
|
53
|
+
].join("\n");
|
|
54
|
+
}
|
|
55
|
+
export async function produceStructuredOutput(provider, systemPrompt, messages, tools, json, opts = {}) {
|
|
56
|
+
const exchange = [...messages, { role: "user", content: instruction(json.schema) }];
|
|
57
|
+
let problem = "";
|
|
58
|
+
for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt++) {
|
|
59
|
+
if (opts.signal?.aborted)
|
|
60
|
+
return { error: "cancelled" };
|
|
61
|
+
const response = await provider.chat(systemPrompt, exchange, tools, opts.signal);
|
|
62
|
+
if (response.usage && opts.costTracker)
|
|
63
|
+
recordTokenUsage(opts.costTracker, response.usage);
|
|
64
|
+
const text = response.content
|
|
65
|
+
.filter((b) => b.type === "text")
|
|
66
|
+
.map((b) => b.text)
|
|
67
|
+
.join("\n");
|
|
68
|
+
let value;
|
|
69
|
+
try {
|
|
70
|
+
value = extractJson(text);
|
|
71
|
+
}
|
|
72
|
+
catch (err) {
|
|
73
|
+
problem = `not JSON (${err instanceof Error ? err.message : String(err)})`;
|
|
74
|
+
}
|
|
75
|
+
if (!problem) {
|
|
76
|
+
if (json.validate(value))
|
|
77
|
+
return { value };
|
|
78
|
+
problem = (json.validate.errors ?? []).map((e) => `${e.instancePath || "(root)"} ${e.message}`).join("; ");
|
|
79
|
+
}
|
|
80
|
+
exchange.push({ role: "assistant", content: text || "(no text)" }, { role: "user", content: `That doesn't match the schema: ${problem}. Reply again with only the JSON value.` });
|
|
81
|
+
if (attempt < MAX_ATTEMPTS - 1)
|
|
82
|
+
problem = "";
|
|
83
|
+
}
|
|
84
|
+
return { error: `no valid structured output after ${MAX_ATTEMPTS} attempts: ${problem}` };
|
|
85
|
+
}
|
package/dist/system-prompt.js
CHANGED
|
@@ -1,4 +1,117 @@
|
|
|
1
|
-
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
import * as os from "node:os";
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
function git(cwd, args) {
|
|
5
|
+
try {
|
|
6
|
+
return execFileSync("git", args, { cwd, timeout: 2000, stdio: ["ignore", "pipe", "ignore"], encoding: "utf8" }).trim();
|
|
7
|
+
}
|
|
8
|
+
catch {
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* The environment block: cwd, platform, shell, date, git state. Nothing in it
|
|
14
|
+
* changes per turn (the date, not the time), so the prompt caches.
|
|
15
|
+
*/
|
|
16
|
+
export function buildEnvironmentBlock(cwd = process.cwd(), permissionMode) {
|
|
17
|
+
const now = new Date();
|
|
18
|
+
const pad = (n) => String(n).padStart(2, "0");
|
|
19
|
+
const date = `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())}`;
|
|
20
|
+
const shell = process.env.SHELL ? path.basename(process.env.SHELL) : process.env.COMSPEC ? path.basename(process.env.COMSPEC) : "unknown";
|
|
21
|
+
const lines = [
|
|
22
|
+
`- Working directory: ${cwd}`,
|
|
23
|
+
`- Platform: ${os.type()} ${os.release()} (${process.platform}, ${os.arch()})`,
|
|
24
|
+
`- Shell: ${shell}`,
|
|
25
|
+
`- Today's date: ${date}`,
|
|
26
|
+
];
|
|
27
|
+
if (git(cwd, ["rev-parse", "--is-inside-work-tree"]) === "true") {
|
|
28
|
+
const branch = git(cwd, ["symbolic-ref", "--short", "-q", "HEAD"]);
|
|
29
|
+
const detached = branch ? null : git(cwd, ["rev-parse", "--short", "HEAD"]);
|
|
30
|
+
lines.push(`- Git repository: yes, ${branch ? `branch ${branch}` : detached ? `detached HEAD at ${detached}` : "no commits yet"}`);
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
lines.push("- Git repository: no");
|
|
34
|
+
}
|
|
35
|
+
if (permissionMode)
|
|
36
|
+
lines.push(`- Permission mode: ${permissionMode}`);
|
|
37
|
+
return `## Environment\n${lines.join("\n")}`;
|
|
38
|
+
}
|
|
39
|
+
const TOOL_GROUPS = [
|
|
40
|
+
["Files and editing", ["read_file", "write_file", "edit_file", "multi_edit", "apply_patch", "read_image"]],
|
|
41
|
+
["Search", ["glob", "grep"]],
|
|
42
|
+
["Shell", ["shell", "task_output", "task_stop"]],
|
|
43
|
+
["Git", ["git_status", "git_diff", "git_commit"]],
|
|
44
|
+
["Web", ["web_search", "web_fetch"]],
|
|
45
|
+
["Memory", ["phren_search", "phren_add_finding", "phren_get_tasks", "phren_complete_task", "phren_add_task", "run_skill"]],
|
|
46
|
+
["Planning and agents", ["update_plan", "spawn_agent", "send_message_to_agent", "list_agents", "list_mcp_resources", "read_mcp_resource"]],
|
|
47
|
+
];
|
|
48
|
+
const EDIT_HINTS = [
|
|
49
|
+
["edit_file", "`edit_file` for a small change"],
|
|
50
|
+
["multi_edit", "`multi_edit` for several changes in one file"],
|
|
51
|
+
["apply_patch", "`apply_patch` for changes across files"],
|
|
52
|
+
["write_file", "`write_file` for new files"],
|
|
53
|
+
];
|
|
54
|
+
/** The tools section, from the names actually registered. MCP tools are counted per server, not listed. */
|
|
55
|
+
export function buildToolSection(toolNames, mcpServers = []) {
|
|
56
|
+
const registered = new Set(toolNames);
|
|
57
|
+
const lines = [];
|
|
58
|
+
const known = new Set();
|
|
59
|
+
for (const [label, names] of TOOL_GROUPS) {
|
|
60
|
+
for (const n of names)
|
|
61
|
+
known.add(n);
|
|
62
|
+
const present = names.filter((n) => registered.has(n));
|
|
63
|
+
if (present.length > 0)
|
|
64
|
+
lines.push(`- ${label}: ${present.map((n) => `\`${n}\``).join(", ")}`);
|
|
65
|
+
}
|
|
66
|
+
const servers = [...mcpServers].sort((a, b) => b.length - a.length);
|
|
67
|
+
const counts = new Map();
|
|
68
|
+
const other = [];
|
|
69
|
+
for (const n of toolNames) {
|
|
70
|
+
if (known.has(n))
|
|
71
|
+
continue;
|
|
72
|
+
if (!n.startsWith("mcp_")) {
|
|
73
|
+
other.push(n);
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
const rest = n.slice(4);
|
|
77
|
+
const server = servers.find((s) => rest.startsWith(`${s}_`)) ?? rest.split("_")[0];
|
|
78
|
+
counts.set(server, (counts.get(server) ?? 0) + 1);
|
|
79
|
+
}
|
|
80
|
+
if (other.length > 0)
|
|
81
|
+
lines.push(`- Other: ${other.map((n) => `\`${n}\``).join(", ")}`);
|
|
82
|
+
if (counts.size > 0) {
|
|
83
|
+
const summary = [...counts].map(([s, c]) => `${s} (${c})`).join(", ");
|
|
84
|
+
lines.push(`- MCP: ${summary}. Tools are named \`mcp_<server>_<tool>\`.`);
|
|
85
|
+
}
|
|
86
|
+
const hints = EDIT_HINTS.filter(([n]) => registered.has(n)).map(([, text]) => text);
|
|
87
|
+
if (hints.length > 1)
|
|
88
|
+
lines.push("", `Edit tools: ${hints.join(", ")}.`);
|
|
89
|
+
return `## Tools You Have\n${lines.join("\n")}`;
|
|
90
|
+
}
|
|
91
|
+
function workflowSteps(has) {
|
|
92
|
+
const steps = [];
|
|
93
|
+
if (has("phren_search"))
|
|
94
|
+
steps.push("**Search memory first** — `phren_search` for relevant past findings before starting work.");
|
|
95
|
+
const reads = ["glob", "grep", "read_file"].filter(has);
|
|
96
|
+
if (reads.length > 0)
|
|
97
|
+
steps.push(`**Read before writing** — ${reads.map((n) => `\`${n}\``).join(", ")} to find and understand code.`);
|
|
98
|
+
if (has("edit_file") && has("write_file"))
|
|
99
|
+
steps.push("**Make changes** — `edit_file` for surgical edits, `write_file` for new files only.");
|
|
100
|
+
else if (has("edit_file"))
|
|
101
|
+
steps.push("**Make changes** — `edit_file` for surgical edits.");
|
|
102
|
+
else if (has("write_file"))
|
|
103
|
+
steps.push("**Make changes** — `write_file`.");
|
|
104
|
+
const verify = [has("shell") && "`shell` to run tests/linters", has("git_diff") && "`git_diff` to review changes"].filter(Boolean);
|
|
105
|
+
if (verify.length > 0)
|
|
106
|
+
steps.push(`**Verify** — ${verify.join(", ")}.`);
|
|
107
|
+
if (has("phren_add_finding"))
|
|
108
|
+
steps.push("**Save learnings** — `phren_add_finding` for non-obvious discoveries (bugs, architecture decisions, gotchas). Skip obvious stuff.");
|
|
109
|
+
steps.push("**Report concisely** — what changed and why. No fluff.");
|
|
110
|
+
return steps.map((s, i) => `${i + 1}. ${s}`);
|
|
111
|
+
}
|
|
112
|
+
export function buildSystemPrompt(phrenContext, priorSummary, providerInfo, customCommands, options = {}) {
|
|
113
|
+
const registered = options.toolNames ? new Set(options.toolNames) : null;
|
|
114
|
+
const has = (name) => !registered || registered.has(name);
|
|
2
115
|
const modelNote = providerInfo ? ` You are running on ${providerInfo.name}${providerInfo.model ? ` (model: ${providerInfo.model})` : ""}.` : "";
|
|
3
116
|
const parts = [
|
|
4
117
|
`You are phren-agent, an autonomous coding agent with persistent memory.${modelNote}`,
|
|
@@ -9,21 +122,11 @@ export function buildSystemPrompt(phrenContext, priorSummary, providerInfo, cust
|
|
|
9
122
|
"You have persistent memory via phren. Past decisions, discovered patterns, and project context are searchable across sessions. Use this to avoid repeating mistakes.",
|
|
10
123
|
"",
|
|
11
124
|
"## Workflow",
|
|
12
|
-
|
|
13
|
-
"2. **Read before writing** — `glob` to find files, `grep` to locate symbols, `read_file` to understand code.",
|
|
14
|
-
"3. **Make changes** — `edit_file` for surgical edits, `write_file` for new files only.",
|
|
15
|
-
"4. **Verify** — `shell` to run tests/linters, `git_diff` to review changes.",
|
|
16
|
-
"5. **Save learnings** — `phren_add_finding` for non-obvious discoveries (bugs, architecture decisions, gotchas). Skip obvious stuff.",
|
|
17
|
-
"6. **Report concisely** — what changed and why. No fluff.",
|
|
125
|
+
...workflowSteps(has),
|
|
18
126
|
"",
|
|
19
|
-
|
|
20
|
-
"- File I/O: `read_file`, `write_file`, `edit_file`",
|
|
21
|
-
"- Search: `glob`, `grep`, `web_search`, `web_fetch`",
|
|
22
|
-
"- System: `shell` (run commands, cd, build, test)",
|
|
23
|
-
"- Git: `git_status`, `git_diff`, `git_commit`",
|
|
24
|
-
"- Memory: `phren_search`, `phren_add_finding`, `phren_get_tasks`, `phren_complete_task`, `phren_add_task`",
|
|
25
|
-
"- Planning: `update_plan` — keep a short visible todo list for multi-step work",
|
|
127
|
+
options.environment ?? buildEnvironmentBlock(process.cwd(), options.permissionMode),
|
|
26
128
|
"",
|
|
129
|
+
...(options.toolNames ? [buildToolSection(options.toolNames, options.mcpServers), ""] : []),
|
|
27
130
|
"## Important",
|
|
28
131
|
"- Be direct and concise. Lead with the answer, not the reasoning.",
|
|
29
132
|
"- Call multiple tools in parallel when they're independent.",
|
|
@@ -24,6 +24,8 @@ import * as fs from "fs";
|
|
|
24
24
|
import * as path from "path";
|
|
25
25
|
import { checkSensitivePath, validatePath } from "../permissions/sandbox.js";
|
|
26
26
|
import { describeNotFound } from "./edit-engine.js";
|
|
27
|
+
import { forgetFileState, recordFileState, staleFileError } from "./file-state.js";
|
|
28
|
+
import { syntaxCheckNote } from "./syntax-check.js";
|
|
27
29
|
const BEGIN = "*** Begin Patch";
|
|
28
30
|
const END = "*** End Patch";
|
|
29
31
|
const ADD = "*** Add File: ";
|
|
@@ -332,6 +334,12 @@ export const applyPatchTool = {
|
|
|
332
334
|
if (!sandbox.ok)
|
|
333
335
|
return { output: `Path outside sandbox: ${sandbox.error}`, is_error: true };
|
|
334
336
|
}
|
|
337
|
+
for (const op of ops) {
|
|
338
|
+
// Add File over an existing file is a whole-file overwrite.
|
|
339
|
+
const stale = staleFileError(path.resolve(cwd, op.path), { requireRead: op.kind === "add" });
|
|
340
|
+
if (stale)
|
|
341
|
+
throw new PatchError(stale);
|
|
342
|
+
}
|
|
335
343
|
writes = planPatch(ops, cwd);
|
|
336
344
|
}
|
|
337
345
|
catch (err) {
|
|
@@ -343,13 +351,19 @@ export const applyPatchTool = {
|
|
|
343
351
|
if (w.content === null) {
|
|
344
352
|
if (fs.existsSync(abs))
|
|
345
353
|
fs.rmSync(abs);
|
|
354
|
+
forgetFileState(abs);
|
|
346
355
|
}
|
|
347
356
|
else {
|
|
348
357
|
fs.mkdirSync(path.dirname(abs), { recursive: true });
|
|
349
358
|
fs.writeFileSync(abs, w.content);
|
|
359
|
+
recordFileState(abs);
|
|
350
360
|
}
|
|
351
361
|
}
|
|
352
362
|
const summary = writes.map((w) => `${w.status} ${w.path}`).join("\n");
|
|
353
|
-
|
|
363
|
+
const notes = writes.flatMap((w) => {
|
|
364
|
+
const note = w.content === null ? null : syntaxCheckNote(w.path, w.before, w.content);
|
|
365
|
+
return note ? [note] : [];
|
|
366
|
+
});
|
|
367
|
+
return { output: `Success. Updated the following files:\n${summary}${notes.length > 0 ? `\n\n${notes.join("\n\n")}` : ""}` };
|
|
354
368
|
},
|
|
355
369
|
};
|
package/dist/tools/edit-file.js
CHANGED
|
@@ -3,6 +3,8 @@ import * as path from "path";
|
|
|
3
3
|
import { encodeDiffPayload } from "../multi/diff-renderer.js";
|
|
4
4
|
import { checkSensitivePath, validatePath } from "../permissions/sandbox.js";
|
|
5
5
|
import { applyEdits, snippetAround } from "./edit-engine.js";
|
|
6
|
+
import { recordFileState, staleFileError } from "./file-state.js";
|
|
7
|
+
import { syntaxCheckNote } from "./syntax-check.js";
|
|
6
8
|
/** Shared path checks and read for the edit tools. */
|
|
7
9
|
function openForEdit(filePath) {
|
|
8
10
|
if (typeof filePath !== "string" || filePath === "") {
|
|
@@ -20,6 +22,9 @@ function openForEdit(filePath) {
|
|
|
20
22
|
if (fs.statSync(filePath).isDirectory()) {
|
|
21
23
|
return { ok: false, result: { output: `${filePath} is a directory, not a file.`, is_error: true } };
|
|
22
24
|
}
|
|
25
|
+
const stale = staleFileError(filePath);
|
|
26
|
+
if (stale)
|
|
27
|
+
return { ok: false, result: { output: stale, is_error: true } };
|
|
23
28
|
return { ok: true, filePath, content: fs.readFileSync(filePath, "utf-8") };
|
|
24
29
|
}
|
|
25
30
|
function runEdits(filePath, edits) {
|
|
@@ -30,13 +35,15 @@ function runEdits(filePath, edits) {
|
|
|
30
35
|
if (!outcome.ok)
|
|
31
36
|
return { output: `${opened.filePath}: ${outcome.error}`, is_error: true };
|
|
32
37
|
fs.writeFileSync(opened.filePath, outcome.content);
|
|
38
|
+
recordFileState(opened.filePath);
|
|
33
39
|
const count = outcome.replacements === 1 ? "1 replacement" : `${outcome.replacements} replacements`;
|
|
34
40
|
const note = outcome.note ? ` (${outcome.note})` : "";
|
|
35
41
|
// The model sees a short numbered excerpt of the result; the diff payload
|
|
36
42
|
// after DIFF_MARKER is for the TUI and is stripped before the model sees it.
|
|
37
43
|
const snippet = snippetAround(outcome.content.replace(/\r\n/g, "\n"), outcome.firstLine, outcome.lastLine);
|
|
44
|
+
const syntax = syntaxCheckNote(opened.filePath, opened.content, outcome.content);
|
|
38
45
|
return {
|
|
39
|
-
output: `Edited ${opened.filePath}: ${count}${note}.\n${snippet}${encodeDiffPayload(opened.filePath, opened.content, outcome.content)}`,
|
|
46
|
+
output: `Edited ${opened.filePath}: ${count}${note}.\n${snippet}${syntax ? `\n\n${syntax}` : ""}${encodeDiffPayload(opened.filePath, opened.content, outcome.content)}`,
|
|
40
47
|
};
|
|
41
48
|
}
|
|
42
49
|
export const editFileTool = {
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the model has seen of each file, so the write tools can refuse to work
|
|
3
|
+
* from a stale picture.
|
|
4
|
+
*
|
|
5
|
+
* read_file records a file's state; a successful edit, write or patch records
|
|
6
|
+
* the state it left behind. Before writing, the tools check the file on disk
|
|
7
|
+
* against that record:
|
|
8
|
+
*
|
|
9
|
+
* - changed since the model last saw it (the user, a formatter or a shell
|
|
10
|
+
* command touched it): refused, so a whole-file write can't silently drop
|
|
11
|
+
* the other change and an edit is not planned against old text;
|
|
12
|
+
* - an existing file the model never read: write_file refuses to overwrite
|
|
13
|
+
* it. Edits and patch hunks are allowed, because their old text has to
|
|
14
|
+
* match the file exactly, which already grounds them in its contents.
|
|
15
|
+
*
|
|
16
|
+
* One record per process, which is one session: subagents are separate
|
|
17
|
+
* processes with their own records, so a child that changes a file the
|
|
18
|
+
* parent has read makes the parent read it again.
|
|
19
|
+
*/
|
|
20
|
+
import { createHash } from "crypto";
|
|
21
|
+
import * as fs from "fs";
|
|
22
|
+
import * as path from "path";
|
|
23
|
+
const records = new Map();
|
|
24
|
+
function key(filePath) {
|
|
25
|
+
const abs = path.resolve(filePath);
|
|
26
|
+
try {
|
|
27
|
+
return fs.realpathSync(abs);
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return abs;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function hashOf(content) {
|
|
34
|
+
return createHash("sha256").update(content).digest("hex");
|
|
35
|
+
}
|
|
36
|
+
/** Remember the file as it is on disk now (after a read or a write). */
|
|
37
|
+
export function recordFileState(filePath) {
|
|
38
|
+
try {
|
|
39
|
+
const stat = fs.statSync(filePath);
|
|
40
|
+
if (!stat.isFile())
|
|
41
|
+
return;
|
|
42
|
+
records.set(key(filePath), { mtimeMs: stat.mtimeMs, size: stat.size, hash: hashOf(fs.readFileSync(filePath)) });
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
records.delete(key(filePath));
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/** Forget a file the tools deleted. */
|
|
49
|
+
export function forgetFileState(filePath) {
|
|
50
|
+
records.delete(key(filePath));
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Why the file must not be written now, or null when it may be. `requireRead`
|
|
54
|
+
* is for whole-file overwrites.
|
|
55
|
+
*/
|
|
56
|
+
export function staleFileError(filePath, opts = {}) {
|
|
57
|
+
if (process.env.PHREN_AGENT_FILE_GUARD === "off")
|
|
58
|
+
return null;
|
|
59
|
+
let stat;
|
|
60
|
+
try {
|
|
61
|
+
stat = fs.statSync(filePath);
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
if (!stat.isFile())
|
|
67
|
+
return null;
|
|
68
|
+
const k = key(filePath);
|
|
69
|
+
const seen = records.get(k);
|
|
70
|
+
if (!seen) {
|
|
71
|
+
return opts.requireRead
|
|
72
|
+
? `${filePath} already exists and has not been read in this session. Read it with read_file first; to change part of it, use edit_file.`
|
|
73
|
+
: null;
|
|
74
|
+
}
|
|
75
|
+
if (stat.mtimeMs === seen.mtimeMs && stat.size === seen.size)
|
|
76
|
+
return null;
|
|
77
|
+
// Touched but maybe not changed (a formatter that found nothing to do).
|
|
78
|
+
const hash = hashOf(fs.readFileSync(filePath));
|
|
79
|
+
if (hash === seen.hash) {
|
|
80
|
+
records.set(k, { mtimeMs: stat.mtimeMs, size: stat.size, hash });
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
return `${filePath} changed on disk since you last read or wrote it (by the user, a formatter or a command). Read it again with read_file before changing it; nothing was written.`;
|
|
84
|
+
}
|
|
85
|
+
/** Test seam: forget everything. */
|
|
86
|
+
export function resetFileState() {
|
|
87
|
+
records.clear();
|
|
88
|
+
}
|
package/dist/tools/glob.js
CHANGED
|
@@ -1,6 +1,37 @@
|
|
|
1
|
-
import * as fs from "fs";
|
|
2
|
-
import * as path from "path";
|
|
3
1
|
import { validatePath } from "../permissions/sandbox.js";
|
|
2
|
+
import { ripgrepPath, runRipgrep, walkTree } from "./search-support.js";
|
|
3
|
+
const MAX_RESULTS = 500;
|
|
4
|
+
const MAX_WALK_FILES = 10000;
|
|
5
|
+
/** Stop reading `rg --files` here; the notice then says "more than". */
|
|
6
|
+
const MAX_RG_FILES = 100000;
|
|
7
|
+
/** Every file under `root` that ripgrep lists (.gitignore applied, hidden files included, not .git or node_modules), or null without ripgrep. */
|
|
8
|
+
async function listWithRipgrep(root, pattern, signal) {
|
|
9
|
+
const rg = ripgrepPath();
|
|
10
|
+
if (!rg)
|
|
11
|
+
return null;
|
|
12
|
+
const matches = [];
|
|
13
|
+
let total = 0;
|
|
14
|
+
let capped = false;
|
|
15
|
+
try {
|
|
16
|
+
await runRipgrep(rg, ["--files", "--hidden", "--no-require-git", "--glob", "!.git", "--glob", "!node_modules", "--sort", "path"], root, (line) => {
|
|
17
|
+
const rel = line.replace(/^\.[\\/]/, "");
|
|
18
|
+
if (!matchGlob(pattern, rel))
|
|
19
|
+
return false;
|
|
20
|
+
total++;
|
|
21
|
+
if (matches.length < MAX_RESULTS)
|
|
22
|
+
matches.push(rel);
|
|
23
|
+
if (total >= MAX_RG_FILES) {
|
|
24
|
+
capped = true;
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
return false;
|
|
28
|
+
}, signal);
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
return { matches, total, capped };
|
|
34
|
+
}
|
|
4
35
|
/** Simple glob matching without external dependencies. Supports * and ** patterns. */
|
|
5
36
|
function matchGlob(pattern, filePath) {
|
|
6
37
|
// Normalize path separators
|
|
@@ -34,34 +65,9 @@ function matchGlob(pattern, filePath) {
|
|
|
34
65
|
}
|
|
35
66
|
return new RegExp(`^${regex}$`).test(f);
|
|
36
67
|
}
|
|
37
|
-
function walkDir(dir, base, results, maxResults) {
|
|
38
|
-
if (results.length >= maxResults)
|
|
39
|
-
return;
|
|
40
|
-
let entries;
|
|
41
|
-
try {
|
|
42
|
-
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
43
|
-
}
|
|
44
|
-
catch {
|
|
45
|
-
return;
|
|
46
|
-
}
|
|
47
|
-
for (const entry of entries) {
|
|
48
|
-
if (results.length >= maxResults)
|
|
49
|
-
return;
|
|
50
|
-
if (entry.name.startsWith(".") || entry.name === "node_modules")
|
|
51
|
-
continue;
|
|
52
|
-
const full = path.join(dir, entry.name);
|
|
53
|
-
const rel = path.relative(base, full);
|
|
54
|
-
if (entry.isDirectory()) {
|
|
55
|
-
walkDir(full, base, results, maxResults);
|
|
56
|
-
}
|
|
57
|
-
else {
|
|
58
|
-
results.push(rel);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
68
|
export const globTool = {
|
|
63
69
|
name: "glob",
|
|
64
|
-
description: "Find files by glob pattern. Use to discover project structure, locate files by extension or name. Examples: '**/*.ts', 'src/**/*.test.js', '**/config.*'.
|
|
70
|
+
description: "Find files by glob pattern. Use to discover project structure, locate files by extension or name. Examples: '**/*.ts', 'src/**/*.test.js', '**/config.*'. Honours .gitignore, includes hidden files such as .github (not .git).",
|
|
65
71
|
input_schema: {
|
|
66
72
|
type: "object",
|
|
67
73
|
properties: {
|
|
@@ -70,20 +76,32 @@ export const globTool = {
|
|
|
70
76
|
},
|
|
71
77
|
required: ["pattern"],
|
|
72
78
|
},
|
|
73
|
-
async execute(input) {
|
|
79
|
+
async execute(input, signal) {
|
|
74
80
|
const pattern = input.pattern;
|
|
75
81
|
const searchPath = input.path || process.cwd();
|
|
76
|
-
const maxResults = 500;
|
|
77
82
|
// Defense-in-depth: validate search path against sandbox
|
|
78
83
|
const pathResult = validatePath(searchPath, process.cwd(), []);
|
|
79
84
|
if (!pathResult.ok) {
|
|
80
85
|
return { output: `Path outside sandbox: ${pathResult.error}`, is_error: true };
|
|
81
86
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
const
|
|
87
|
+
let matches;
|
|
88
|
+
let note = "";
|
|
89
|
+
const listed = await listWithRipgrep(pathResult.resolved, pattern, signal);
|
|
90
|
+
if (listed) {
|
|
91
|
+
matches = listed.matches;
|
|
92
|
+
if (listed.total > matches.length)
|
|
93
|
+
note = `showing the first ${matches.length} of ${listed.capped ? "more than " : ""}${listed.total} matches; narrow the pattern or path`;
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
const walked = walkTree(searchPath, MAX_WALK_FILES, (rel) => matchGlob(pattern, rel));
|
|
97
|
+
matches = walked.files.slice(0, MAX_RESULTS);
|
|
98
|
+
if (walked.truncated)
|
|
99
|
+
note = `searched the first ${MAX_WALK_FILES} files; narrow the path or pattern`;
|
|
100
|
+
else if (walked.files.length > MAX_RESULTS)
|
|
101
|
+
note = `showing the first ${MAX_RESULTS} of ${walked.files.length} matches; narrow the pattern or path`;
|
|
102
|
+
}
|
|
85
103
|
if (matches.length === 0)
|
|
86
|
-
return { output: "No files found." };
|
|
87
|
-
return { output: matches.join("\n") };
|
|
104
|
+
return { output: note ? `No files found (${note}).` : "No files found." };
|
|
105
|
+
return { output: note ? `${matches.join("\n")}\n\n(${note})` : matches.join("\n") };
|
|
88
106
|
},
|
|
89
107
|
};
|