-
Notifications
You must be signed in to change notification settings - Fork 10.8k
fix(coding-agent): speed up external editor launch #6903
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
303d14c
fix(coding-agent): speed up external editor launch
christianklotz 150adca
refactor(coding-agent): clarify external editor handling
christianklotz 6677e34
refactor(coding-agent): simplify external editor result
christianklotz 9336a59
refactor(coding-agent): model external editor lifecycle
christianklotz 38c6a5c
fix(coding-agent): rename external editor test fake
christianklotz 3e6aa2f
fix(coding-agent): secure external editor temp files
christianklotz f8fd223
fix(coding-agent): simplify external editor temp path
christianklotz 5c6bb44
fix(coding-agent): restore system temp editor files
christianklotz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
packages/coding-agent/src/modes/interactive/external-editor.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { spawn } from "node:child_process"; | ||
| import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
|
|
||
| export interface ExternalEditorOptions { | ||
| command: string; | ||
| content: string; | ||
| } | ||
|
|
||
| export type ExternalEditorResult = { status: "complete"; content: string } | { status: "failed" }; | ||
|
|
||
| export async function editInExternalEditor(options: ExternalEditorOptions): Promise<ExternalEditorResult> { | ||
| const directory = mkdtempSync(join(tmpdir(), "pi-editor-")); | ||
| const filePath = join(directory, "prompt.md"); | ||
| try { | ||
| writeFileSync(filePath, options.content, "utf-8"); | ||
| const [editor, ...editorArgs] = options.command.split(" "); | ||
| process.stdout.write(`Launching external editor: ${options.command}\nPi will resume when the editor exits.\n`); | ||
|
|
||
| // Do not use spawnSync here. On Windows, synchronous child_process calls can keep | ||
| // Node/libuv's console input read active after the parent pauses stdin, racing | ||
| // vim/nvim for the console input buffer until Ctrl+C cancels the pending read. | ||
| const exitCode = await new Promise<number | null>((resolve) => { | ||
| const child = spawn(editor, [...editorArgs, filePath], { | ||
| stdio: "inherit", | ||
| shell: process.platform === "win32", | ||
| }); | ||
| child.on("error", () => resolve(null)); | ||
| child.on("close", (code) => resolve(code)); | ||
| }); | ||
|
|
||
| if (exitCode !== 0) { | ||
| return { status: "failed" }; | ||
| } | ||
|
|
||
| return { status: "complete", content: readFileSync(filePath, "utf-8").replace(/\n$/, "") }; | ||
| } finally { | ||
| try { | ||
| rmSync(directory, { recursive: true, force: true }); | ||
| } catch { | ||
| // Cleanup is best effort. | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs"; | ||
| import { tmpdir } from "node:os"; | ||
| import { basename, dirname, join } from "node:path"; | ||
| import { fileURLToPath } from "node:url"; | ||
| import { describe, expect, it } from "vitest"; | ||
| import { type ExternalEditorResult, editInExternalEditor } from "../src/modes/interactive/external-editor.ts"; | ||
|
|
||
| const editorFixturePath = fileURLToPath(new URL("./fixtures/fake-external-editor.mjs", import.meta.url)); | ||
|
|
||
| interface EditorCapture { | ||
| filePath: string; | ||
| content: string; | ||
| entries: string[]; | ||
| directoryMode: number; | ||
| } | ||
|
|
||
| async function runExternalEditor(fixtureFlag?: "--fail" | "--empty"): Promise<{ | ||
| result: ExternalEditorResult; | ||
| capture: EditorCapture; | ||
| }> { | ||
| const testDirectory = mkdtempSync(join(tmpdir(), "pi-external-editor-test-")); | ||
| const capturePath = join(testDirectory, "capture.json"); | ||
| try { | ||
| const result = await editInExternalEditor({ | ||
| command: `${process.execPath} ${editorFixturePath} ${capturePath}${fixtureFlag ? ` ${fixtureFlag}` : ""}`, | ||
| content: "original", | ||
| }); | ||
| const capture = JSON.parse(readFileSync(capturePath, "utf-8")) as EditorCapture; | ||
| return { result, capture }; | ||
| } finally { | ||
| rmSync(testDirectory, { recursive: true, force: true }); | ||
| } | ||
| } | ||
|
|
||
| describe("editInExternalEditor", () => { | ||
| it("edits a prompt inside a private temporary directory", async () => { | ||
| const { result, capture } = await runExternalEditor(); | ||
| const directory = dirname(capture.filePath); | ||
|
|
||
| expect(result).toEqual({ status: "complete", content: "edited" }); | ||
| expect(dirname(directory)).toBe(tmpdir()); | ||
| expect(basename(directory)).toMatch(/^pi-editor-.+$/); | ||
| expect(basename(capture.filePath)).toBe("prompt.md"); | ||
| expect(capture.entries).toEqual(["prompt.md"]); | ||
| expect(capture.content).toBe("original"); | ||
| if (process.platform !== "win32") { | ||
| expect(capture.directoryMode & 0o077).toBe(0); | ||
| } | ||
| expect(existsSync(directory)).toBe(false); | ||
| }); | ||
|
|
||
| it("keeps the original content when the editor exits unsuccessfully", async () => { | ||
| const { result, capture } = await runExternalEditor("--fail"); | ||
|
|
||
| expect(result).toEqual({ status: "failed" }); | ||
| expect(existsSync(dirname(capture.filePath))).toBe(false); | ||
| }); | ||
| it("returns empty content when the editor clears the prompt", async () => { | ||
| const { result } = await runExternalEditor("--empty"); | ||
|
|
||
| expect(result).toEqual({ status: "complete", content: "" }); | ||
| }); | ||
| }); |
25 changes: 25 additions & 0 deletions
25
packages/coding-agent/test/fixtures/fake-external-editor.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import { readdirSync, readFileSync, statSync, writeFileSync } from "node:fs"; | ||
| import { dirname } from "node:path"; | ||
|
|
||
| const capturePath = process.argv[2]; | ||
| const filePath = process.argv.at(-1); | ||
| if (!capturePath || !filePath) { | ||
| process.exit(1); | ||
| } | ||
|
|
||
| const directory = dirname(filePath); | ||
| writeFileSync( | ||
| capturePath, | ||
| JSON.stringify({ | ||
| filePath, | ||
| content: readFileSync(filePath, "utf-8"), | ||
| entries: readdirSync(directory), | ||
| directoryMode: statSync(directory).mode & 0o777, | ||
| }), | ||
| "utf-8", | ||
| ); | ||
|
|
||
| if (process.argv.includes("--fail")) { | ||
| process.exit(1); | ||
| } | ||
| writeFileSync(filePath, process.argv.includes("--empty") ? "" : "edited\n", "utf-8"); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.