refactor(tool): Tool.Def.execute returns Effect, rename defineEffect → define (#21961)

This commit is contained in:
Kit Langton
2026-04-10 22:36:02 -04:00
committed by GitHub
parent f99812443c
commit c5fb6281f0
39 changed files with 674 additions and 721 deletions

View File

@@ -7,6 +7,10 @@ import { Instance } from "../../src/project/instance"
import { tmpdir } from "../fixture/fixture"
import { FileTime } from "../../src/file/time"
import { LSP } from "../../src/lsp"
import { AppFileSystem } from "../../src/filesystem"
import { Format } from "../../src/format"
import { Bus } from "../../src/bus"
import { BusEvent } from "../../src/bus/bus-event"
import { SessionID, MessageID } from "../../src/session/schema"
const ctx = {
@@ -17,7 +21,7 @@ const ctx = {
abort: AbortSignal.any([]),
messages: [],
metadata: () => {},
ask: async () => {},
ask: () => Effect.void,
}
afterEach(async () => {
@@ -29,7 +33,9 @@ async function touch(file: string, time: number) {
await fs.utimes(file, date, date)
}
const runtime = ManagedRuntime.make(Layer.mergeAll(LSP.defaultLayer, FileTime.defaultLayer))
const runtime = ManagedRuntime.make(
Layer.mergeAll(LSP.defaultLayer, FileTime.defaultLayer, AppFileSystem.defaultLayer, Format.defaultLayer, Bus.layer),
)
afterAll(async () => {
await runtime.dispose()
@@ -43,6 +49,12 @@ const resolve = () =>
}),
)
const readFileTime = (sessionID: SessionID, filepath: string) =>
runtime.runPromise(FileTime.Service.use((ft) => ft.read(sessionID, filepath)))
const subscribeBus = <D extends BusEvent.Definition>(def: D, callback: () => unknown) =>
runtime.runPromise(Bus.Service.use((bus) => bus.subscribeCallback(def, callback)))
describe("tool.edit", () => {
describe("creating new files", () => {
test("creates new file when oldString is empty", async () => {
@@ -53,14 +65,14 @@ describe("tool.edit", () => {
directory: tmp.path,
fn: async () => {
const edit = await resolve()
const result = await edit.execute(
const result = await Effect.runPromise(edit.execute(
{
filePath: filepath,
oldString: "",
newString: "new content",
},
ctx,
)
))
expect(result.metadata.diff).toContain("new content")
@@ -78,14 +90,14 @@ describe("tool.edit", () => {
directory: tmp.path,
fn: async () => {
const edit = await resolve()
await edit.execute(
await Effect.runPromise(edit.execute(
{
filePath: filepath,
oldString: "",
newString: "nested file",
},
ctx,
)
))
const content = await fs.readFile(filepath, "utf-8")
expect(content).toBe("nested file")
@@ -100,22 +112,20 @@ describe("tool.edit", () => {
await Instance.provide({
directory: tmp.path,
fn: async () => {
const { Bus } = await import("../../src/bus")
const { File } = await import("../../src/file")
const { FileWatcher } = await import("../../src/file/watcher")
const events: string[] = []
const unsubUpdated = Bus.subscribe(FileWatcher.Event.Updated, () => events.push("updated"))
const unsubUpdated = await subscribeBus(FileWatcher.Event.Updated, () => events.push("updated"))
const edit = await resolve()
await edit.execute(
await Effect.runPromise(edit.execute(
{
filePath: filepath,
oldString: "",
newString: "content",
},
ctx,
)
))
expect(events).toContain("updated")
unsubUpdated()
@@ -133,17 +143,17 @@ describe("tool.edit", () => {
await Instance.provide({
directory: tmp.path,
fn: async () => {
await FileTime.read(ctx.sessionID, filepath)
await readFileTime(ctx.sessionID, filepath)
const edit = await resolve()
const result = await edit.execute(
const result = await Effect.runPromise(edit.execute(
{
filePath: filepath,
oldString: "old content",
newString: "new content",
},
ctx,
)
))
expect(result.output).toContain("Edit applied successfully")
@@ -160,18 +170,18 @@ describe("tool.edit", () => {
await Instance.provide({
directory: tmp.path,
fn: async () => {
await FileTime.read(ctx.sessionID, filepath)
await readFileTime(ctx.sessionID, filepath)
const edit = await resolve()
await expect(
edit.execute(
Effect.runPromise(edit.execute(
{
filePath: filepath,
oldString: "old",
newString: "new",
},
ctx,
),
)),
).rejects.toThrow("not found")
},
})
@@ -187,14 +197,14 @@ describe("tool.edit", () => {
fn: async () => {
const edit = await resolve()
await expect(
edit.execute(
Effect.runPromise(edit.execute(
{
filePath: filepath,
oldString: "same",
newString: "same",
},
ctx,
),
)),
).rejects.toThrow("identical")
},
})
@@ -208,18 +218,18 @@ describe("tool.edit", () => {
await Instance.provide({
directory: tmp.path,
fn: async () => {
await FileTime.read(ctx.sessionID, filepath)
await readFileTime(ctx.sessionID, filepath)
const edit = await resolve()
await expect(
edit.execute(
Effect.runPromise(edit.execute(
{
filePath: filepath,
oldString: "not in file",
newString: "replacement",
},
ctx,
),
)),
).rejects.toThrow()
},
})
@@ -235,14 +245,14 @@ describe("tool.edit", () => {
fn: async () => {
const edit = await resolve()
await expect(
edit.execute(
Effect.runPromise(edit.execute(
{
filePath: filepath,
oldString: "content",
newString: "modified",
},
ctx,
),
)),
).rejects.toThrow("You must read file")
},
})
@@ -258,7 +268,7 @@ describe("tool.edit", () => {
directory: tmp.path,
fn: async () => {
// Read first
await FileTime.read(ctx.sessionID, filepath)
await readFileTime(ctx.sessionID, filepath)
// Simulate external modification
await fs.writeFile(filepath, "modified externally", "utf-8")
@@ -267,14 +277,14 @@ describe("tool.edit", () => {
// Try to edit with the new content
const edit = await resolve()
await expect(
edit.execute(
Effect.runPromise(edit.execute(
{
filePath: filepath,
oldString: "modified externally",
newString: "edited",
},
ctx,
),
)),
).rejects.toThrow("modified since it was last read")
},
})
@@ -288,10 +298,10 @@ describe("tool.edit", () => {
await Instance.provide({
directory: tmp.path,
fn: async () => {
await FileTime.read(ctx.sessionID, filepath)
await readFileTime(ctx.sessionID, filepath)
const edit = await resolve()
await edit.execute(
await Effect.runPromise(edit.execute(
{
filePath: filepath,
oldString: "foo",
@@ -299,7 +309,7 @@ describe("tool.edit", () => {
replaceAll: true,
},
ctx,
)
))
const content = await fs.readFile(filepath, "utf-8")
expect(content).toBe("qux bar qux baz qux")
@@ -315,23 +325,22 @@ describe("tool.edit", () => {
await Instance.provide({
directory: tmp.path,
fn: async () => {
await FileTime.read(ctx.sessionID, filepath)
await readFileTime(ctx.sessionID, filepath)
const { Bus } = await import("../../src/bus")
const { FileWatcher } = await import("../../src/file/watcher")
const events: string[] = []
const unsubUpdated = Bus.subscribe(FileWatcher.Event.Updated, () => events.push("updated"))
const unsubUpdated = await subscribeBus(FileWatcher.Event.Updated, () => events.push("updated"))
const edit = await resolve()
await edit.execute(
await Effect.runPromise(edit.execute(
{
filePath: filepath,
oldString: "original",
newString: "modified",
},
ctx,
)
))
expect(events).toContain("updated")
unsubUpdated()
@@ -349,17 +358,17 @@ describe("tool.edit", () => {
await Instance.provide({
directory: tmp.path,
fn: async () => {
await FileTime.read(ctx.sessionID, filepath)
await readFileTime(ctx.sessionID, filepath)
const edit = await resolve()
await edit.execute(
await Effect.runPromise(edit.execute(
{
filePath: filepath,
oldString: "line2",
newString: "new line 2\nextra line",
},
ctx,
)
))
const content = await fs.readFile(filepath, "utf-8")
expect(content).toBe("line1\nnew line 2\nextra line\nline3")
@@ -375,17 +384,17 @@ describe("tool.edit", () => {
await Instance.provide({
directory: tmp.path,
fn: async () => {
await FileTime.read(ctx.sessionID, filepath)
await readFileTime(ctx.sessionID, filepath)
const edit = await resolve()
await edit.execute(
await Effect.runPromise(edit.execute(
{
filePath: filepath,
oldString: "old",
newString: "new",
},
ctx,
)
))
const content = await fs.readFile(filepath, "utf-8")
expect(content).toBe("line1\r\nnew\r\nline3")
@@ -403,14 +412,14 @@ describe("tool.edit", () => {
fn: async () => {
const edit = await resolve()
await expect(
edit.execute(
Effect.runPromise(edit.execute(
{
filePath: filepath,
oldString: "",
newString: "",
},
ctx,
),
)),
).rejects.toThrow("identical")
},
})
@@ -424,18 +433,18 @@ describe("tool.edit", () => {
await Instance.provide({
directory: tmp.path,
fn: async () => {
await FileTime.read(ctx.sessionID, dirpath)
await readFileTime(ctx.sessionID, dirpath)
const edit = await resolve()
await expect(
edit.execute(
Effect.runPromise(edit.execute(
{
filePath: dirpath,
oldString: "old",
newString: "new",
},
ctx,
),
)),
).rejects.toThrow("directory")
},
})
@@ -449,17 +458,17 @@ describe("tool.edit", () => {
await Instance.provide({
directory: tmp.path,
fn: async () => {
await FileTime.read(ctx.sessionID, filepath)
await readFileTime(ctx.sessionID, filepath)
const edit = await resolve()
const result = await edit.execute(
const result = await Effect.runPromise(edit.execute(
{
filePath: filepath,
oldString: "line2",
newString: "new line a\nnew line b",
},
ctx,
)
))
expect(result.metadata.filediff).toBeDefined()
expect(result.metadata.filediff.file).toBe(filepath)
@@ -520,8 +529,8 @@ describe("tool.edit", () => {
fn: async () => {
const edit = await resolve()
const filePath = path.join(tmp.path, "test.txt")
await FileTime.read(ctx.sessionID, filePath)
await edit.execute(
await readFileTime(ctx.sessionID, filePath)
await Effect.runPromise(edit.execute(
{
filePath,
oldString: input.oldString,
@@ -529,7 +538,7 @@ describe("tool.edit", () => {
replaceAll: input.replaceAll,
},
ctx,
)
))
return await Bun.file(filePath).text()
},
})
@@ -661,31 +670,31 @@ describe("tool.edit", () => {
await Instance.provide({
directory: tmp.path,
fn: async () => {
await FileTime.read(ctx.sessionID, filepath)
await readFileTime(ctx.sessionID, filepath)
const edit = await resolve()
// Two concurrent edits
const promise1 = edit.execute(
const promise1 = Effect.runPromise(edit.execute(
{
filePath: filepath,
oldString: "0",
newString: "1",
},
ctx,
)
))
// Need to read again since FileTime tracks per-session
await FileTime.read(ctx.sessionID, filepath)
await readFileTime(ctx.sessionID, filepath)
const promise2 = edit.execute(
const promise2 = Effect.runPromise(edit.execute(
{
filePath: filepath,
oldString: "0",
newString: "2",
},
ctx,
)
))
// Both should complete without error (though one might fail due to content mismatch)
const results = await Promise.allSettled([promise1, promise2])