+21

![opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com>](/assets/img/avatar_default.png)



![opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>](/assets/img/avatar_default.png)



James Long
Brendan Allan
Kit Langton
opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>
Affan Ali
affanali2k3
Frank
opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com>
𝓛𝓲𝓽𝓽𝓵𝓮 𝓕𝓻𝓪𝓷𝓴
Aiden Cline
Jay V
Dax Raad
Aarav Sareen
OpeOginni
Luke Parker
Ben Guthrie
Dax
Filip
Max Anderson
Brendan Allan
Jack
Shoubhit Dash
Dustin Deus
starptech
Aiden Cline
usrnk1
Jay
runvip
opencode
Julian Coy
Vladimir Glafirov
8c94e9005f
Co-authored-by: Brendan Allan <14191578+Brendonovich@users.noreply.github.com> Co-authored-by: Kit Langton <kit.langton@gmail.com> Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com> Co-authored-by: Affan Ali <93028901+affanali2k3@users.noreply.github.com> Co-authored-by: affanali2k3 <affanalikhanxx@gmail.com> Co-authored-by: Frank <frank@anoma.ly> Co-authored-by: opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com> Co-authored-by: 𝓛𝓲𝓽𝓽𝓵𝓮 𝓕𝓻𝓪𝓷𝓴 <little-frank@opencord.local> Co-authored-by: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Co-authored-by: Jay V <air@live.ca> Co-authored-by: Dax Raad <d@ironbay.co> Co-authored-by: Aarav Sareen <96787824+arvsrn@users.noreply.github.com> Co-authored-by: OpeOginni <107570612+OpeOginni@users.noreply.github.com> Co-authored-by: Luke Parker <10430890+Hona@users.noreply.github.com> Co-authored-by: Ben Guthrie <benjee.012@gmail.com> Co-authored-by: Dax <mail@thdxr.com> Co-authored-by: Filip <34747899+neriousy@users.noreply.github.com> Co-authored-by: Max Anderson <max.a.anderson95@gmail.com> Co-authored-by: Brendan Allan <git@brendonovich.dev> Co-authored-by: Jack <jack@anoma.ly> Co-authored-by: Shoubhit Dash <shoubhit2005@gmail.com> Co-authored-by: Dustin Deus <deusdustin@gmail.com> Co-authored-by: starptech <starptech@starptechs-MBP.fritz.box> Co-authored-by: Aiden Cline <aidenpcline@gmail.com> Co-authored-by: usrnk1 <7547651+usrnk1@users.noreply.github.com> Co-authored-by: Jay <53023+jayair@users.noreply.github.com> Co-authored-by: runvip <164729189+runvip@users.noreply.github.com> Co-authored-by: opencode <opencode@sst.dev> Co-authored-by: Julian Coy <julian@ex-machina.co> Co-authored-by: Vladimir Glafirov <vglafirov@gitlab.com>
190 lines
6.6 KiB
TypeScript
190 lines
6.6 KiB
TypeScript
/**
|
|
* Reproducer for snapshot race condition with instant tool execution.
|
|
*
|
|
* When the mock LLM returns a tool call response instantly, the AI SDK
|
|
* processes the tool call and executes the tool (e.g. apply_patch) before
|
|
* the processor's start-step handler can capture a pre-tool snapshot.
|
|
* Both the "before" and "after" snapshots end up with the same git tree
|
|
* hash, so computeDiff returns empty and the session summary shows 0 files.
|
|
*
|
|
* This is a real bug: the snapshot system assumes it can capture state
|
|
* before tools run by hooking into start-step, but the AI SDK executes
|
|
* tools internally during multi-step processing before emitting events.
|
|
*/
|
|
import { expect } from "bun:test"
|
|
import { Effect, Layer } from "effect"
|
|
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
|
|
import fs from "fs/promises"
|
|
import path from "path"
|
|
import { Session } from "@/session/session"
|
|
import { SessionPrompt } from "../../src/session/prompt"
|
|
import { SessionSummary } from "../../src/session/summary"
|
|
import { MessageV2 } from "../../src/session/message-v2"
|
|
import { SessionV1 } from "@opencode-ai/core/v1/session"
|
|
import { Database } from "@opencode-ai/core/database/database"
|
|
import { SessionProjector } from "@opencode-ai/core/session/projector"
|
|
import { provideTmpdirServer } from "../fixture/fixture"
|
|
import { testEffect } from "../lib/effect"
|
|
import { TestLLMServer } from "../lib/llm-server"
|
|
|
|
import { LSP } from "@/lsp/lsp"
|
|
import { MCP } from "../../src/mcp"
|
|
import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
|
|
import { RuntimeFlags } from "@/effect/runtime-flags"
|
|
|
|
const mcp = Layer.succeed(
|
|
MCP.Service,
|
|
MCP.Service.of({
|
|
status: () => Effect.succeed({}),
|
|
clients: () => Effect.succeed({}),
|
|
instructions: () => Effect.succeed([]),
|
|
tools: () => Effect.succeed({}),
|
|
prompts: () => Effect.succeed({}),
|
|
resources: () => Effect.succeed({}),
|
|
resourceTemplates: () => Effect.succeed({}),
|
|
add: () => Effect.succeed({ status: { status: "disabled" as const } }),
|
|
connect: () => Effect.void,
|
|
disconnect: () => Effect.void,
|
|
getPrompt: () => Effect.succeed(undefined),
|
|
readResource: () => Effect.succeed(undefined),
|
|
startAuth: () => Effect.die("unexpected MCP auth"),
|
|
authenticate: () => Effect.die("unexpected MCP auth"),
|
|
finishAuth: () => Effect.die("unexpected MCP auth"),
|
|
removeAuth: () => Effect.void,
|
|
supportsOAuth: () => Effect.succeed(false),
|
|
hasStoredTokens: () => Effect.succeed(false),
|
|
getAuthStatus: () => Effect.succeed("not_authenticated" as const),
|
|
}),
|
|
)
|
|
|
|
const lsp = Layer.succeed(
|
|
LSP.Service,
|
|
LSP.Service.of({
|
|
init: () => Effect.void,
|
|
status: () => Effect.succeed([]),
|
|
hasClients: () => Effect.succeed(false),
|
|
touchFile: () => Effect.void,
|
|
diagnostics: () => Effect.succeed({}),
|
|
hover: () => Effect.succeed(undefined),
|
|
definition: () => Effect.succeed([]),
|
|
references: () => Effect.succeed([]),
|
|
implementation: () => Effect.succeed([]),
|
|
documentSymbol: () => Effect.succeed([]),
|
|
workspaceSymbol: () => Effect.succeed([]),
|
|
prepareCallHierarchy: () => Effect.succeed([]),
|
|
incomingCalls: () => Effect.succeed([]),
|
|
outgoingCalls: () => Effect.succeed([]),
|
|
}),
|
|
)
|
|
|
|
const root = LayerNode.group([
|
|
SessionPrompt.node,
|
|
Session.node,
|
|
SessionProjector.node,
|
|
SessionSummary.node,
|
|
Database.node,
|
|
CrossSpawnSpawner.node,
|
|
LayerNode.make({ service: TestLLMServer, layer: TestLLMServer.layer, deps: [] }),
|
|
])
|
|
const it = testEffect(
|
|
LayerNode.compile(root, [
|
|
[MCP.node, mcp],
|
|
[LSP.node, lsp],
|
|
[RuntimeFlags.node, RuntimeFlags.layer({ experimentalEventSystem: true })],
|
|
]),
|
|
)
|
|
|
|
const providerCfg = (url: string) => ({
|
|
provider: {
|
|
test: {
|
|
name: "Test",
|
|
id: "test",
|
|
env: [],
|
|
npm: "@ai-sdk/openai-compatible",
|
|
models: {
|
|
"test-model": {
|
|
id: "test-model",
|
|
name: "Test Model",
|
|
attachment: false,
|
|
reasoning: false,
|
|
temperature: false,
|
|
tool_call: true,
|
|
release_date: "2025-01-01",
|
|
limit: { context: 100000, output: 10000 },
|
|
cost: { input: 0, output: 0 },
|
|
options: {},
|
|
},
|
|
},
|
|
options: {
|
|
apiKey: "test-key",
|
|
baseURL: url,
|
|
},
|
|
},
|
|
},
|
|
})
|
|
|
|
it.live("tool execution produces non-empty session diff (snapshot race)", () =>
|
|
provideTmpdirServer(
|
|
Effect.fnUntraced(function* ({ dir, llm }) {
|
|
const prompt = yield* SessionPrompt.Service
|
|
const sessions = yield* Session.Service
|
|
const summary = yield* SessionSummary.Service
|
|
|
|
const session = yield* sessions.create({
|
|
title: "snapshot race test",
|
|
permission: [{ permission: "*", pattern: "*", action: "allow" }],
|
|
})
|
|
|
|
// Use bash tool (always registered) to create a file
|
|
const command = `echo 'snapshot race test content' > ${path.join(dir, "race-test.txt")}`
|
|
yield* llm.toolMatch((hit) => JSON.stringify(hit.body).includes("create the file"), "bash", {
|
|
command,
|
|
})
|
|
yield* llm.textMatch((hit) => JSON.stringify(hit.body).includes("bash"), "done")
|
|
|
|
// Seed user message
|
|
yield* prompt.prompt({
|
|
sessionID: session.id,
|
|
agent: "build",
|
|
noReply: true,
|
|
parts: [{ type: "text", text: "create the file" }],
|
|
})
|
|
|
|
// Run the agent loop
|
|
const result = yield* prompt.loop({ sessionID: session.id })
|
|
expect(result.info.role).toBe("assistant")
|
|
|
|
// Verify the file was created
|
|
const filePath = path.join(dir, "race-test.txt")
|
|
const fileExists = yield* Effect.promise(() =>
|
|
fs
|
|
.access(filePath)
|
|
.then(() => true)
|
|
.catch(() => false),
|
|
)
|
|
expect(fileExists).toBe(true)
|
|
|
|
// Verify the tool call completed (in the first assistant message)
|
|
const allMsgs = yield* MessageV2.filterCompactedEffect(session.id)
|
|
const user = allMsgs.find(
|
|
(msg): msg is SessionV1.WithParts & { info: SessionV1.User } => msg.info.role === "user",
|
|
)
|
|
const tool = allMsgs
|
|
.flatMap((m) => m.parts)
|
|
.find((p): p is SessionV1.ToolPart => p.type === "tool" && p.tool === "bash")
|
|
expect(tool?.state.status).toBe("completed")
|
|
if (!user) throw new Error("Expected user message")
|
|
|
|
// Poll for the turn diff — summarize() is fire-and-forget.
|
|
let diff: Array<{ file?: string }> = []
|
|
for (let i = 0; i < 50; i++) {
|
|
diff = yield* summary.diff({ sessionID: session.id, messageID: user.info.id })
|
|
if (diff.length > 0) break
|
|
yield* Effect.sleep("100 millis")
|
|
}
|
|
expect(diff.length).toBeGreaterThan(0)
|
|
}),
|
|
{ git: true, config: providerCfg },
|
|
),
|
|
)
|