Skip to content

Commit 7163b10

Browse files
committed
feat(ai): search unsaved buffers, and load the resize tool up front
Two measured changes and one wording correction, all from the A/B runs recorded in phoenix-pro/unshipped/ai-panel-efficiency-notes.md. - searchEditorBuffers: regex search over the UNSAVED open files only. Those are the only files where the model's Grep is wrong — Grep reads disk, and disk is stale for a buffer the user has edited but not saved. Read/Edit are already buffer-safe (the agent flushes first); search was the one gap. The response names the files it covered and tells the model to Grep the rest, and when nothing is unsaved it says Grep is authoritative and does no work. Deliberately not a project scan: that would run on the UI thread and re-do what ripgrep already does off-thread. - resizeLivePreview is alwaysLoad. Responsive checks are a core use case and it cost two ToolSearch round-trips every time: same fix, 3 fewer turns, ~10s faster per responsive task. - Context line names which tools the "unsaved" note applies to. "Unsaved, so stale on disk" made the model refuse Edit and drive DocumentManager by hand through execJsInEditor — no edit card, no undo, 4x the cost. Now: "Read and Edit see the unsaved text as normal; Grep does not, so use searchEditorBuffers to search these."
1 parent eb080ed commit 7163b10

3 files changed

Lines changed: 60 additions & 3 deletions

File tree

‎src-node/claude-code-agent.js‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,11 @@ function _buildEditorContextLine(ctx) {
329329
if (ctx.activeFile) {
330330
parts.push("the user is editing " + ctx.activeFile + ".");
331331
if (ctx.unsaved) {
332-
parts.push("Unsaved, so stale on disk: " + ctx.unsaved + ".");
332+
// Read and Edit are buffer-safe here (the agent flushes the buffer
333+
// first); only Grep sees stale disk. Saying "stale on disk" without that
334+
// steered the model off Edit onto the editor API — no edit card, no undo.
335+
parts.push("Unsaved (Read and Edit see the unsaved text as normal; Grep does not, so " +
336+
"use searchEditorBuffers to search these): " + ctx.unsaved + ".");
333337
}
334338
}
335339
if (ctx.livePreviewFile) {
@@ -1421,6 +1425,7 @@ async function _runQuery(requestId, prompt, projectPath, model, signal, locale,
14211425
"WebFetch", "WebSearch",
14221426
"EnterPlanMode", "ExitPlanMode",
14231427
"mcp__phoenix-editor__getEditorState",
1428+
"mcp__phoenix-editor__searchEditorBuffers",
14241429
"mcp__phoenix-editor__takeScreenshot",
14251430
"mcp__phoenix-editor__execJsInLivePreview",
14261431
"mcp__phoenix-editor__execJsInEditor",
@@ -1442,6 +1447,7 @@ async function _runQuery(requestId, prompt, projectPath, model, signal, locale,
14421447
" files to answer questions. Do not modify files.",
14431448
tools: ["Read", "Glob", "Grep",
14441449
"mcp__phoenix-editor__getEditorState",
1450+
"mcp__phoenix-editor__searchEditorBuffers",
14451451
"mcp__phoenix-editor__takeScreenshot",
14461452
"mcp__phoenix-editor__execJsInLivePreview",
14471453
"mcp__phoenix-editor__editorDocs"]
@@ -1454,6 +1460,7 @@ async function _runQuery(requestId, prompt, projectPath, model, signal, locale,
14541460
" only for new files.",
14551461
tools: ["Read", "Edit", "Write", "Glob", "Grep",
14561462
"mcp__phoenix-editor__getEditorState",
1463+
"mcp__phoenix-editor__searchEditorBuffers",
14571464
"mcp__phoenix-editor__takeScreenshot",
14581465
"mcp__phoenix-editor__execJsInLivePreview",
14591466
"mcp__phoenix-editor__execJsInEditor",

‎src-node/mcp-editor-tools.js‎

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ const EXEC_PEER_TIMEOUT_MS = {
5454
getEditorState: 5000,
5555
takeScreenshot: 15000,
5656
controlEditor: 5000,
57-
resizeLivePreview: 5000
57+
resizeLivePreview: 5000,
58+
searchEditorBuffers: 3000
5859
};
5960

6061
// Floor for caller-provided timeouts (e.g. execJsInLivePreview's
@@ -161,6 +162,52 @@ function createEditorMcpServer(sdkModule, nodeConnector, clarificationAccessors)
161162
}
162163
);
163164

165+
const searchEditorBuffersTool = sdkModule.tool(
166+
"searchEditorBuffers",
167+
"Regex search over the UNSAVED open files only — the ones the editor-state line at the top of " +
168+
"the prompt lists as unsaved. Those are the only files where Grep is wrong: Grep reads disk, and " +
169+
"disk is stale for a buffer the user has edited but not saved. Use Grep for everything else; it " +
170+
"is faster and covers the whole project. Only call this when the editor-state line names unsaved " +
171+
"files. Returns matches {file, line, text}, searchedFiles (what this actually covered) and truncated.",
172+
{
173+
pattern: z.string().describe("Regex (default) or literal text to find"),
174+
isRegex: z.boolean().optional().describe("false to match the pattern literally. Default true"),
175+
caseSensitive: z.boolean().optional().describe("Default false"),
176+
fileGlob: z.string().optional().describe("Limit to matching files, e.g. *.css"),
177+
maxResults: z.number().optional().describe("Cap on matches returned. Default 50, max 200")
178+
},
179+
async function (args) {
180+
let result;
181+
try {
182+
const found = await _execPeerWithTimeout(nodeConnector, "searchEditorBuffers",
183+
args || {}, "searchEditorBuffers");
184+
let text;
185+
if (found && found.error) {
186+
text = JSON.stringify(found);
187+
} else if (!found || !found.searchedFiles || !found.searchedFiles.length) {
188+
text = "No unsaved files, so nothing in the editor differs from disk. Use Grep — " +
189+
"it is authoritative for the whole project right now.";
190+
} else {
191+
text = JSON.stringify(found) +
192+
"\n\nThis searched ONLY the unsaved files in searchedFiles. Every other file " +
193+
"matches disk — use Grep for the rest of the project.";
194+
}
195+
result = { content: [{ type: "text", text: text }] };
196+
} catch (err) {
197+
result = {
198+
content: [{ type: "text", text: "Error searching unsaved files: " + err.message }],
199+
isError: true
200+
};
201+
}
202+
return _maybeAppendHint(result, hasClarification);
203+
},
204+
{
205+
annotations: { readOnlyHint: true },
206+
alwaysLoad: true,
207+
searchHint: "search the unsaved editor buffers, where Grep would see stale disk content"
208+
}
209+
);
210+
164211
const takeScreenshotTool = sdkModule.tool(
165212
"takeScreenshot",
166213
"Take a screenshot of the Phoenix Code editor application window (or a region within it). " +
@@ -382,6 +429,7 @@ function createEditorMcpServer(sdkModule, nodeConnector, clarificationAccessors)
382429
},
383430
{
384431
annotations: { readOnlyHint: true },
432+
alwaysLoad: true,
385433
searchHint: "resize the user's live preview browser viewport to check a responsive layout"
386434
}
387435
);
@@ -646,7 +694,7 @@ function createEditorMcpServer(sdkModule, nodeConnector, clarificationAccessors)
646694

647695
return sdkModule.createSdkMcpServer({
648696
name: "phoenix-editor",
649-
tools: [getEditorStateTool, takeScreenshotTool, execJsInLivePreviewTool,
697+
tools: [getEditorStateTool, searchEditorBuffersTool, takeScreenshotTool, execJsInLivePreviewTool,
650698
execJsInEditorTool, editorPreferencesTool, editorDocsTool,
651699
controlEditorTool, resizeLivePreviewTool, waitTool, getUserClarificationTool]
652700
});

‎src/nls/root/strings.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2659,6 +2659,8 @@ define({
26592659
"AI_CHAT_TOOL_SCREENSHOT_LIVE_PREVIEW": "live preview",
26602660
"AI_CHAT_TOOL_SCREENSHOT_FULL_EDITOR": "the full editor",
26612661
"AI_CHAT_TOOL_LIVE_PREVIEW_JS": "Inspecting preview",
2662+
"AI_CHAT_TOOL_SEARCH_UNSAVED": "Search unsaved files",
2663+
"AI_CHAT_TOOL_SEARCH_UNSAVED_FOR": "Search unsaved files: {0}",
26622664
"AI_CHAT_TOOL_EDITOR_JS": "Inspecting editor",
26632665
"AI_CHAT_TOOL_EDITOR_PREFERENCES": "Editor preferences",
26642666
"AI_CHAT_TOOL_EDITOR_DOCS": "Editor docs",

0 commit comments

Comments
 (0)