Compare commits

..

1 Commits

Author SHA1 Message Date
Aiden Cline c9640058a8 test(core): reproduce interrupted tool ID reuse 2026-08-07 22:09:56 +00:00
6 changed files with 58 additions and 138 deletions
@@ -139,7 +139,6 @@ test("resolves directory autocomplete from the current browser root", async () =
directories.push(input.location?.directory ?? "")
return Promise.resolve({ data: [] })
},
list: () => Promise.resolve({ data: [] }),
},
},
} as unknown as Parameters<typeof createDirectorySearch>[0]["sdk"]
@@ -153,67 +152,6 @@ test("resolves directory autocomplete from the current browser root", async () =
expect(directories).toEqual(["/repo", "/repo/src"])
})
test("keeps indexed directory results for servers that support empty search", async () => {
const sdk = {
api: {
file: {
find: () => Promise.resolve({ data: [{ path: "projects/", type: "directory" }] }),
list: () => Promise.reject(new Error("listing should not run when search returns results")),
},
},
} as unknown as Parameters<typeof createDirectorySearch>[0]["sdk"]
const search = createDirectorySearch({ sdk, home: () => "/home/luke", base: () => "/home/luke" })
expect(await search("")).toEqual(["/home/luke/projects"])
})
test("lists the default directory when empty search is unsupported", async () => {
const calls: string[] = []
const directories = Array.from({ length: 60 }, (_, index) => ({
path: `project-${index}/`,
type: "directory" as const,
}))
const sdk = {
api: {
file: {
find: () => Promise.resolve({ data: [] }),
list: (input: { location?: { directory?: string } }) => {
calls.push(input.location?.directory ?? "")
return Promise.resolve({
data: [...directories, { path: "README.md", type: "file" }],
})
},
},
},
} as unknown as Parameters<typeof createDirectorySearch>[0]["sdk"]
const search = createDirectorySearch({ sdk, home: () => "/home/luke", base: () => "/home/luke" })
const results = await search("")
expect(results).toHaveLength(60)
expect(results.at(-1)).toBe("/home/luke/project-59")
expect(calls).toEqual(["/home/luke"])
})
test("matches the default directory listing when typed search is unsupported", async () => {
const sdk = {
api: {
file: {
find: () => Promise.resolve({ data: [] }),
list: () =>
Promise.resolve({
data: [
{ path: "Documents/", type: "directory" },
{ path: "Downloads/", type: "directory" },
],
}),
},
},
} as unknown as Parameters<typeof createDirectorySearch>[0]["sdk"]
const search = createDirectorySearch({ sdk, home: () => "/home/luke", base: () => "/home/luke" })
expect(await search("documents")).toEqual(["/home/luke/Documents"])
})
test("searches from an absolute root without a default base", async () => {
const directories: string[] = []
const sdk = {
@@ -379,14 +379,7 @@ export function createDirectorySearch(args: { sdk: ServerSDK; base: () => string
.then((result) => result.data.map((entry) => entry.path))
.catch(() => [])
if (!active()) return []
if (results.length) {
return results.map((path) => joinPickerPath(input.directory, path)).slice(0, 50)
}
const fallback = query
? await match(input.directory, query, 50)
: (await directories(input.directory)).map((item) => item.absolute)
if (!active()) return []
return fallback
return results.map((path) => joinPickerPath(input.directory, path)).slice(0, 50)
}
const segments = query.replace(/^\/+/, "").split("/")
const head = segments.slice(0, -1).filter((part) => part && part !== ".")
+56
View File
@@ -1769,6 +1769,62 @@ describe("SessionRunnerLLM", () => {
}),
)
it.effect("replays interrupted provider-local tool call IDs uniquely", () =>
Effect.gen(function* () {
yield* setup
const session = yield* SessionV2.Service
yield* session.prompt({ sessionID, prompt: Prompt.make({ text: "Echo twice" }), resume: false })
requests.length = 0
executions.length = 0
const firstGate = yield* Deferred.make<void>()
const secondGate = yield* Deferred.make<void>()
toolExecutionGate = firstGate
responses = [
[
LLMEvent.stepStart({ index: 0 }),
LLMEvent.toolCall({ id: "tool_0", name: "echo", input: { text: "first" } }),
LLMEvent.stepFinish({ index: 0, reason: "tool-calls" }),
LLMEvent.finish({ reason: "tool-calls" }),
],
[
LLMEvent.stepStart({ index: 0 }),
LLMEvent.toolCall({ id: "tool_0", name: "echo", input: { text: "second" } }),
LLMEvent.stepFinish({ index: 0, reason: "tool-calls" }),
LLMEvent.finish({ reason: "tool-calls" }),
],
]
const run = yield* session.resume(sessionID).pipe(Effect.forkChild)
while (executions.length < 1) yield* Effect.yieldNow
toolExecutionGate = secondGate
yield* Deferred.succeed(firstGate, undefined)
while (executions.length < 2) yield* Effect.yieldNow
yield* session.interrupt(sessionID)
expect(yield* Fiber.await(run)).toMatchObject({ _tag: "Failure" })
toolExecutionGate = undefined
expect(yield* session.context(sessionID)).toMatchObject([
{ type: "user", text: "Echo twice" },
{ type: "assistant", content: [{ type: "tool", id: "tool_0", state: { status: "completed" } }] },
{ type: "assistant", content: [{ type: "tool", id: "tool_0", state: { status: "error" } }] },
])
requests.length = 0
responses = undefined
response = []
yield* session.resume(sessionID)
const callIDs = requests[0]!.messages.flatMap((message) =>
message.role === "assistant"
? message.content.filter((part) => part.type === "tool-call").map((part) => part.id)
: [],
)
expect(callIDs).toHaveLength(2)
expect(new Set(callIDs).size).toBe(callIDs.length)
}),
)
it.effect("joins concurrent resume calls into one active provider run", () =>
Effect.gen(function* () {
yield* setup
@@ -431,7 +431,6 @@ function unsupportedParts(msgs: ModelMessage[], model: Provider.Model): ModelMes
const modality = mimeToModality(mime)
if (!modality) return part
if (model.capabilities.input[modality]) return part
if ((modality === "image" || modality === "pdf") && model.capabilities.attachment) return part
const name = filename ? `"${filename}"` : modality
return {
@@ -145,10 +145,6 @@ export const toModelMessagesEffect = Effect.fnUntraced(function* (
// Only apply this workaround if the model actually supports that media input -
// otherwise unsupportedParts() will turn it into a user-visible error.
const supportsMediaInToolResult = (attachment: { mime: string }) => {
if (attachment.mime.startsWith("image/") && !model.capabilities.attachment && !model.capabilities.input.image)
return false
if (attachment.mime === "application/pdf" && !model.capabilities.attachment && !model.capabilities.input.pdf)
return false
if (model.api.npm === "@ai-sdk/anthropic") return true
if (model.api.npm === "@ai-sdk/openai") return true
if (model.api.npm === "@ai-sdk/amazon-bedrock/mantle") return true
@@ -322,16 +322,6 @@ describe("session.message-v2.toModelMessage", () => {
test("converts assistant tool completion into tool-call + tool-result messages with attachments", async () => {
const userID = "m-user"
const assistantID = "m-assistant"
const imageModel: Provider.Model = {
...model,
capabilities: {
...model.capabilities,
input: {
...model.capabilities.input,
image: true,
},
},
}
const input: SessionV1.WithParts[] = [
{
@@ -381,7 +371,7 @@ describe("session.message-v2.toModelMessage", () => {
},
]
expect(await MessageV2.toModelMessages(input, imageModel)).toStrictEqual([
expect(await MessageV2.toModelMessages(input, model)).toStrictEqual([
{
role: "user",
content: [{ type: "text", text: "run tool" }],
@@ -419,58 +409,6 @@ describe("session.message-v2.toModelMessage", () => {
],
},
])
const unsupported = await MessageV2.toModelMessages(input, model)
expect(unsupported).toMatchObject([
{ role: "user" },
{ role: "assistant" },
{
role: "tool",
content: [{ output: { type: "text", value: "ok" } }],
},
{
role: "user",
content: [
{ type: "text", text: MessageV2.SYNTHETIC_ATTACHMENT_PROMPT },
{ type: "file", mediaType: "image/png" },
],
},
])
expect(ProviderTransform.message(unsupported, model, {})).toMatchObject([
{ role: "user" },
{ role: "assistant" },
{ role: "tool" },
{
role: "user",
content: [
{ type: "text", text: MessageV2.SYNTHETIC_ATTACHMENT_PROMPT },
{
type: "text",
text: 'ERROR: Cannot read "attachment.png" (this model does not support image input). Inform the user.',
},
],
},
])
const legacyModel: Provider.Model = {
...model,
api: { ...model.api, npm: "@ai-sdk/openai-compatible" },
capabilities: { ...model.capabilities, attachment: true },
}
expect(
ProviderTransform.message(await MessageV2.toModelMessages(input, legacyModel), legacyModel, {}),
).toMatchObject([
{ role: "user" },
{ role: "assistant" },
{ role: "tool" },
{
role: "user",
content: [
{ type: "text", text: MessageV2.SYNTHETIC_ATTACHMENT_PROMPT },
{ type: "file", mediaType: "image/png" },
],
},
])
})
test("preserves jpeg tool-result media for anthropic models", async () => {