Compare commits

...
Author SHA1 Message Date
LukeParkerDev 9be4ebe348 Merge branch 'v2' into worktree-ui
Adopt the location-based worktree API from #47358 and update the project menu e2e to the outline-worktree icon.
2026-09-07 11:03:17 +10:00
Luke Parker 5e3100a46a fix(desktop): persist renderer state in SQLite instead of electron-store (#47695) 2026-09-07 10:51:46 +10:00
Luke Parker 69b30acfd4 chore: stop trusting tree-sitter install scripts (#47696) 2026-09-07 00:09:44 +00:00
Luke Parker ecb3c88601 fix(app): give worktree creation a setup-length request deadline (#47694) 2026-09-06 23:44:45 +00:00
opencode-agent[bot]andHona a5ebf5bf2f fix(www): regenerate OpenAPI documents (#47693)
Co-authored-by: Hona <10430890+Hona@users.noreply.github.com>
2026-09-06 23:26:56 +00:00
Luke Parker f334377c5b fix(util): bound opencode.log by trimming its head in place (#47676) 2026-09-07 09:16:09 +10:00
Luke Parker 873d437c12 fix(desktop): emit preload as .cjs so it loads under --no-sandbox (#47691) 2026-09-07 09:14:38 +10:00
Aiden Cline 13286e883e fix(ai): raise typed errors for failed finishes (#47688) 2026-09-06 18:09:49 -05:00
Luke Parker 0d70595338 fix(desktop): export tail of oversized logs in debug bundle (#47672) 2026-09-07 08:47:30 +10:00
Aiden Cline a02415a3eb fix(ai): restrict Bedrock cache checkpoints to Claude (#47660) 2026-09-06 17:00:12 -05:00
Dax beca5e8926 feat(project): allow updating canonical directory (#47642) 2026-09-06 17:52:48 -04:00
Shoubhit Dash e3c89654d1 feat(core): keep web search providers sticky per session (#47334) 2026-09-07 03:09:47 +05:30
Dax 33f48f36c9 docs(www): add Console documentation (#47638) 2026-09-06 12:06:07 -04:00
Dax 58f949d2d0 fix(core): log MCP OAuth and credential lifecycle (#47636) 2026-09-06 11:56:42 -04:00
Kit Langton 2a895b9e03 fix(core): interrupt sessions before inactivity eviction (#47629) 2026-09-06 11:11:42 -04:00
Dax Raad fe506f201d docs: explain supporting v1 and v2 plugins 2026-09-06 11:08:26 -04:00
usrnk1 f5d87ae5a9 Merge branch 'v2' into worktree-ui 2026-09-04 17:20:18 +02:00
usrnk1 be6aba9482 fix(desktop): unify worktree container padding 2026-09-04 17:07:15 +02:00
usrnk1 b74c2cf143 fix(desktop): tune worktree settings spacing 2026-09-04 17:04:33 +02:00
usrnk1 2769d69420 feat(desktop): refine worktree experience 2026-09-04 17:00:16 +02:00
usrnk1 405cd21314 feat(ui): add worktree visual variants 2026-09-04 17:00:08 +02:00
110 changed files with 4692 additions and 976 deletions
+3 -5
View File
@@ -432,6 +432,7 @@
"@types/node": "catalog:",
"@typescript/native-preview": "catalog:",
"app-builder-lib": "26.15.7",
"drizzle-kit": "catalog:",
"drizzle-orm": "catalog:",
"effect": "catalog:",
"electron": "42.10.1",
@@ -1036,12 +1037,9 @@
},
},
"trustedDependencies": [
"esbuild",
"tree-sitter-powershell",
"protobufjs",
"electron",
"web-tree-sitter",
"tree-sitter-bash",
"esbuild",
"protobufjs",
],
"patchedDependencies": {
"@pierre/trees@1.0.0-beta.4": "patches/@pierre%2Ftrees@1.0.0-beta.4.patch",
-4
View File
@@ -146,10 +146,6 @@
"esbuild",
"node-pty",
"protobufjs",
"tree-sitter",
"tree-sitter-bash",
"tree-sitter-powershell",
"web-tree-sitter",
"electron"
],
"overrides": {
@@ -415,10 +415,7 @@ const lowerMessages = Effect.fn("BedrockConverse.lowerMessages")(function* (
// System prompts share the cache-point convention: emit the text block, then
// optionally a positional `cachePoint` marker.
const lowerSystem = (
breakpoints: BedrockCache.Breakpoints,
system: ReadonlyArray<LLMRequest["system"][number]>,
) => {
const lowerSystem = (breakpoints: BedrockCache.Breakpoints, system: ReadonlyArray<LLMRequest["system"][number]>) => {
const content = system
.filter((part) => part.text.length > 0)
.flatMap((part) => textWithCache(breakpoints, part.text, part.cache))
@@ -431,7 +428,7 @@ const fromRequest = Effect.fn("BedrockConverse.fromRequest")(function* (request:
const generation = request.generation
// Bedrock-Claude shares Anthropic's 4-breakpoint cap. Spend the budget in
// tools → system → messages order to favour the highest-impact prefixes.
const breakpoints = BedrockCache.breakpoints()
const breakpoints = BedrockCache.breakpoints(request.model.id)
const toolConfig = (() => {
if (flattened.tools.length === 0) return undefined
return {
+8
View File
@@ -639,6 +639,14 @@ const step = (state: ParserState, event: GeminiEvent) => {
: state.usage,
}
const candidate = event.candidates?.[0]
if (candidate?.finishReason && mapFinishReason(candidate.finishReason, state.hasToolCalls) === "error")
return Effect.fail(
ProviderShared.eventError(
state.route,
`Gemini stopped with ${candidate.finishReason}`,
ProviderShared.encodeJson(event),
),
)
if (!candidate?.content)
return Effect.succeed([
{ ...nextState, finishReason: candidate?.finishReason ?? nextState.finishReason },
+12
View File
@@ -9,6 +9,8 @@ import {
AIError,
InvalidProviderOutputError,
LLMEvent,
ProviderInternalError,
UnknownProviderError,
Usage,
type FinishReasonDetails,
type LLMRequest,
@@ -700,6 +702,16 @@ const step = Effect.fn("MistralChat.step")(function* (state: ParserState, event:
normalized: mapFinishReason(choice.finish_reason),
raw: choice.finish_reason,
}
if (finishReason.normalized === "error") {
const details = {
message: `Mistral Chat stopped with ${finishReason.raw}`,
body: ProviderShared.encodeJson(event),
}
return yield* new AIError({
reason:
finishReason.raw === "network_error" ? new ProviderInternalError(details) : new UnknownProviderError(details),
})
}
const incomplete = finishReason.normalized === "length" || finishReason.normalized === "content-filter"
if (!incomplete && Object.keys(withTools.pendingTools).length > 0)
return yield* ProviderShared.eventError(
@@ -1,6 +1,6 @@
import { Schema } from "effect"
import type { CacheHint } from "../../schema/index.js"
import { newBreakpoints, ttlBucket, type Breakpoints } from "./cache.js"
import { newBreakpoints, ttlBucket } from "./cache.js"
// Bedrock cache markers are positional: emit a `cachePoint` block immediately
// after the content the caller wants treated as a cacheable prefix. Bedrock
@@ -13,24 +13,46 @@ export const CachePointBlock = Schema.Struct({
})
export type CachePointBlock = Schema.Schema.Type<typeof CachePointBlock>
// Callers pass a shared counter through every `block()` call site so the
// four-breakpoint budget is respected across `system`, `messages`, and `tools`.
const LEGACY_CLAUDE = ["anthropic.claude-instant", "anthropic.claude-v1", "anthropic.claude-v2", "anthropic.claude-3-"]
// These legacy Claude releases support explicit caching, but only for five minutes.
const CLAUDE_5M = [
"anthropic.claude-3-5-sonnet-20241022-v2:0",
"anthropic.claude-3-5-haiku-20241022-v1:0",
"anthropic.claude-3-7-sonnet-20250219-v1:0",
"anthropic.claude-sonnet-4-20250514-v1:0",
"anthropic.claude-opus-4-20250514-v1:0",
"anthropic.claude-opus-4-1-20250805-v1:0",
]
// Callers share the four-breakpoint budget across system, messages, and tools.
export const BEDROCK_BREAKPOINT_CAP = 4
export type { Breakpoints } from "./cache.js"
export const breakpoints = () => newBreakpoints(BEDROCK_BREAKPOINT_CAP)
export const breakpoints = (modelID: string) => {
// Substring matching also handles regional prefixes and model-bearing ARNs.
const short = CLAUDE_5M.some((id) => modelID.includes(id))
return {
...newBreakpoints(BEDROCK_BREAKPOINT_CAP),
// Assume modern Claude releases retain caching support; older generations need an explicit exception.
// Other model families use implicit caching where available.
supported: modelID.includes("anthropic.claude-") && (short || !LEGACY_CLAUDE.some((id) => modelID.includes(id))),
ttl1h: !short,
}
}
export type Breakpoints = ReturnType<typeof breakpoints>
const DEFAULT_5M: CachePointBlock = { cachePoint: { type: "default" } }
const DEFAULT_1H: CachePointBlock = { cachePoint: { type: "default", ttl: "1h" } }
export const block = (breakpoints: Breakpoints, cache: CacheHint | undefined): CachePointBlock | undefined => {
if (!breakpoints.supported) return undefined
if (cache?.type !== "ephemeral" && cache?.type !== "persistent") return undefined
if (breakpoints.remaining <= 0) {
breakpoints.dropped += 1
return undefined
}
breakpoints.remaining -= 1
return ttlBucket(cache.ttlSeconds) === "1h" ? DEFAULT_1H : DEFAULT_5M
return breakpoints.ttl1h && ttlBucket(cache.ttlSeconds) === "1h" ? DEFAULT_1H : DEFAULT_5M
}
export * as BedrockCache from "./bedrock-cache.js"
@@ -0,0 +1,119 @@
import { describe, expect } from "bun:test"
import { Effect } from "effect"
import { CacheHint, LLM, Message, ToolCallPart } from "../../src/index.js"
import { AmazonBedrock } from "../../src/providers.js"
import { compileRequest } from "../../src/route/client.js"
import { it } from "../lib/effect.js"
const bedrock = AmazonBedrock.configure({ apiKey: "fixture" })
describe("Bedrock Converse cache policy", () => {
for (const id of [
"deepseek.r1-v1:0",
"meta.llama3-3-70b-instruct-v1:0",
"mistral.mistral-large-2402-v1:0",
"qwen.qwen3-coder-480b-a35b-v1:0",
"openai.gpt-oss-120b-1:0",
"cohere.command-r-v1:0",
"anthropic.claude-instant-v1",
"anthropic.claude-v1",
"anthropic.claude-v2",
"anthropic.claude-v2:1",
"anthropic.claude-3-haiku-20240307-v1:0",
"anthropic.claude-3-sonnet-20240229-v1:0",
"anthropic.claude-3-opus-20240229-v1:0",
"anthropic.claude-3-5-sonnet-20240620-v1:0",
"amazon.nova-lite-v1:0",
"global.amazon.nova-2-lite-v1:0",
"custom-model",
"arn:aws:bedrock:us-east-1:123456789012:application-inference-profile/abc123",
]) {
for (const policy of [undefined, "auto", "none", { tools: true, system: true, messages: { tail: 3 } }] as const) {
it.effect(`omits checkpoints for ${id} (${JSON.stringify(policy)})`, () =>
Effect.gen(function* () {
// Exercise both automatic placement and manual hints at every lowering site.
const cache = policy === "none" ? new CacheHint({ type: "ephemeral", ttlSeconds: 3600 }) : undefined
const prepared = yield* compileRequest(
LLM.request({
model: bedrock.model(id),
cache: policy,
system: [{ type: "text", text: "System prefix", cache }],
tools: [{ name: "lookup", description: "Lookup", inputSchema: { type: "object" }, cache }],
messages: [
Message.user([{ type: "text", text: "Question", cache }]),
Message.system([{ type: "text", text: "Update", cache }]),
Message.assistant([
{ type: "text", text: "Answer", cache },
{ type: "reasoning", text: "Unsigned reasoning", cache },
ToolCallPart.make({ id: "call_1", name: "lookup", input: {} }),
]),
Message.tool({ id: "call_1", name: "lookup", result: "Result", cache }),
],
}),
)
expect(JSON.stringify(prepared.body)).not.toContain("cachePoint")
expect(prepared.body).toMatchObject({
modelId: id,
system: [{ text: "System prefix" }],
toolConfig: { tools: [{ toolSpec: { name: "lookup" } }] },
messages: [
{ role: "user", content: [{ text: "Question" }, { text: "<system-update>\nUpdate\n</system-update>" }] },
{
role: "assistant",
content: [{ text: "Answer" }, { text: "Unsigned reasoning" }, { toolUse: { name: "lookup" } }],
},
{ role: "user", content: [{ toolResult: { content: [{ json: "Result" }] } }] },
],
})
}),
)
}
}
for (const [id, ttl] of [
["anthropic.claude-3-5-sonnet-20241022-v2:0", undefined],
["us.anthropic.claude-3-5-haiku-20241022-v1:0", undefined],
["eu.anthropic.claude-3-7-sonnet-20250219-v1:0", undefined],
["apac.anthropic.claude-sonnet-4-20250514-v1:0", undefined],
["anthropic.claude-opus-4-20250514-v1:0", undefined],
["anthropic.claude-opus-4-1-20250805-v1:0", undefined],
["anthropic.claude-sonnet-4-5-20250929-v1:0", "1h"],
["global.anthropic.claude-sonnet-99", "1h"],
["anthropic.claude-new-family-99", "1h"],
["arn:aws:bedrock:us-east-1::foundation-model/anthropic.claude-sonnet-4-6", "1h"],
["arn:aws:bedrock:us-east-1:123456789012:inference-profile/us.anthropic.claude-sonnet-4-6", "1h"],
] as const) {
for (const ttlSeconds of [undefined, 3600]) {
it.effect(`preserves Claude checkpoints for ${id} (TTL: ${ttlSeconds ?? "default"})`, () =>
Effect.gen(function* () {
const prepared = yield* compileRequest(
LLM.request({
model: bedrock.model(id),
system: [
{ type: "text", text: "Agent" },
{ type: "text", text: "Project" },
],
tools: [{ name: "lookup", description: "Lookup", inputSchema: { type: "object" } }],
prompt: "Question",
cache:
ttlSeconds === undefined ? undefined : { tools: true, system: true, messages: { tail: 1 }, ttlSeconds },
}),
)
const marker = {
cachePoint: ttlSeconds === undefined || ttl === undefined ? { type: "default" } : { type: "default", ttl },
}
expect(prepared.body).toMatchObject({
modelId: id,
toolConfig: { tools: [{ toolSpec: { name: "lookup" } }, marker] },
system: [{ text: "Agent" }, marker, { text: "Project" }, marker],
messages: [{ role: "user", content: [{ text: "Question" }, marker] }],
})
if (ttlSeconds === undefined || ttl === undefined)
expect(JSON.stringify(prepared.body)).not.toContain('"ttl"')
}),
)
}
}
})
@@ -136,7 +136,7 @@ const captureHeaders = (target: LanguageModel) =>
const model = AmazonBedrock.configure({
baseURL: "https://bedrock-runtime.test",
apiKey: "test-bearer",
}).model("anthropic.claude-3-5-sonnet-20240620-v1:0")
}).model("anthropic.claude-sonnet-4-5-20250929-v1:0")
const baseRequest = LLM.request({
id: "req_1",
@@ -155,7 +155,7 @@ describe("Bedrock Converse route", () => {
const prepared = yield* compileRequest(baseRequest)
expect(prepared.body).toEqual({
modelId: "anthropic.claude-3-5-sonnet-20240620-v1:0",
modelId: "anthropic.claude-sonnet-4-5-20250929-v1:0",
system: [{ text: "You are concise." }],
messages: [{ role: "user", content: [{ text: "Say hello." }] }],
inferenceConfig: { maxTokens: 64, temperature: 0 },
+21 -6
View File
@@ -1643,19 +1643,14 @@ describe("Gemini route", () => {
}),
)
it.effect("maps current blocking and invalid-output finish reasons", () =>
it.effect("preserves blocking finishes and rejects invalid-output finish reasons", () =>
Effect.gen(function* () {
const reasons = [
["MODEL_ARMOR", "content-filter"],
["IMAGE_PROHIBITED_CONTENT", "content-filter"],
["IMAGE_RECITATION", "content-filter"],
["LANGUAGE", "content-filter"],
["UNEXPECTED_TOOL_CALL", "error"],
["NO_IMAGE", "error"],
["IMAGE_OTHER", "unknown"],
["TOO_MANY_TOOL_CALLS", "error"],
["MISSING_THOUGHT_SIGNATURE", "error"],
["MALFORMED_RESPONSE", "error"],
] as const
for (const [raw, normalized] of reasons) {
@@ -1666,6 +1661,26 @@ describe("Gemini route", () => {
)
expect(response.finishReason).toEqual({ normalized, raw })
}
for (const raw of [
"MALFORMED_FUNCTION_CALL",
"UNEXPECTED_TOOL_CALL",
"NO_IMAGE",
"TOO_MANY_TOOL_CALLS",
"MISSING_THOUGHT_SIGNATURE",
"MALFORMED_RESPONSE",
]) {
const event = { candidates: [{ finishReason: raw, finishMessage: "Provider detail" }], responseId: "failure" }
const error = yield* LLMClient.generate(request).pipe(
Effect.provide(fixedResponse(sseEvents(event))),
Effect.flip,
)
expect(error).toMatchObject({
_tag: "AI.Error",
reason: { _tag: "InvalidProviderOutput", body: JSON.stringify(event), http: { status: 200 } },
message: `Gemini stopped with ${raw}`,
})
}
}),
)
+16 -1
View File
@@ -600,7 +600,6 @@ describe("Mistral Chat", () => {
["stop", "stop"],
["model_length", "length"],
["tool_calls", "tool-calls"],
["error", "error"],
["future_reason", "unknown"],
] as const) {
const response = yield* LLMClient.generate(request).pipe(
@@ -609,6 +608,22 @@ describe("Mistral Chat", () => {
expect(response.finishReason).toEqual({ normalized, raw })
}
for (const [raw, tag] of [
["error", "UnknownProvider"],
["network_error", "ProviderInternal"],
] as const) {
const event = { ...chunk({}, raw), diagnostics: { trace: "failure" } }
const error = yield* LLMClient.generate(request).pipe(
Effect.provide(fixedResponse(sseEvents(event))),
Effect.flip,
)
expect(error).toMatchObject({
_tag: "AI.Error",
reason: { _tag: tag, body: JSON.stringify(event), http: { status: 200 } },
message: `Mistral Chat stopped with ${raw}`,
})
}
const truncated = yield* LLMClient.generate(request).pipe(
Effect.provide(
fixedResponse(
@@ -53,7 +53,7 @@ for (const direction of ["ltr", "rtl"] as const) {
await expect(trigger).toBeEnabled()
await expect(trigger.locator("use")).toHaveAttribute(
"href",
`#opencode-v2-icon-${workspace ? "workspace-isolated" : "monitor"}`,
`#opencode-v2-icon-${workspace ? "outline-worktree" : "monitor"}`,
)
const background = await trigger.evaluate((element) => getComputedStyle(element).backgroundColor)
await trigger.hover()
@@ -267,7 +267,7 @@ for (const state of ["closed", "unopened"] as const) {
messages.resolve()
await expect(header.getByRole("button", { name: "More options", exact: true })).toBeVisible()
}
await expect(trigger.locator("use")).toHaveAttribute("href", "#opencode-v2-icon-workspace-isolated")
await expect(trigger.locator("use")).toHaveAttribute("href", "#opencode-v2-icon-outline-worktree")
await trigger.click()
await expect(menu.getByRole("menuitem", { name: fixture.project.name, exact: true })).toBeEnabled()
await expect(menu.getByRole("menuitem", { name: directory, exact: true })).toBeDisabled()
+59 -15
View File
@@ -34,6 +34,8 @@ export const ProviderTipSchema = Persistence.struct({
dismissedAt: Schema.Finite,
})
export const WorkspaceTipSchema = ProviderTipSchema
export function NewSessionView(props: {
composer: ComposerModel
project: PromptProjectController
@@ -99,7 +101,15 @@ export function NewSessionView(props: {
</div>
</div>
</div>
<ProviderTip />
<NewSessionTips
workspaceEligible={
!!props.project.selected() &&
props.workspace.bar.visible() &&
props.workspace.selection.value() !== "create" &&
props.workspace.project.managed() === 0
}
onWorkspace={() => select("create")}
/>
</div>
</div>
)
@@ -118,56 +128,90 @@ export function NewSessionStatus(props: { visible: boolean }) {
)
}
function ProviderTip() {
function NewSessionTips(props: { workspaceEligible: boolean; onWorkspace: () => void }) {
const language = useLanguage()
const dialog = useDialog()
const sdk = useWorkspaceLocation()
const providers = useProviders(() => sdk().directory)
const [persistedState, setPersistedState, , persistedReady] = persisted(
const [providerState, setProviderState, , providerReady] = persisted(
Persist.global("new-session.provider-tip"),
ProviderTipSchema,
{ dismissedAt: 0 },
)
const visible = createMemo(
const [workspaceState, setWorkspaceState, , workspaceReady] = persisted(
Persist.global("new-session.workspace-tip"),
WorkspaceTipSchema,
{ dismissedAt: 0 },
)
const workspaceVisible = createMemo(
() =>
props.workspaceEligible &&
workspaceReady() &&
Date.now() - workspaceState.dismissedAt >= providerTipDismissalDuration,
)
const providerVisible = createMemo(
() =>
providers.ready() &&
persistedReady() &&
providerReady() &&
providers.paid().length === 0 &&
Date.now() - persistedState.dismissedAt >= providerTipDismissalDuration,
Date.now() - providerState.dismissedAt >= providerTipDismissalDuration,
)
const tip = createMemo<"workspace" | "provider" | undefined>(() => {
if (providerVisible()) return "provider"
if (workspaceVisible()) return "workspace"
})
const displayed = createMemo<"workspace" | "provider" | undefined>((previous) => tip() ?? previous)
const [ref, setRef] = createSignal<HTMLDivElement>()
const presence = createPresence({
show: visible,
show: () => tip() !== undefined,
element: () => ref() ?? null,
})
const openProviders = () => {
const open = () => {
const current = tip()
if (!current) return
if (current === "workspace") {
setWorkspaceState("dismissedAt", Date.now())
props.onWorkspace()
return
}
void import("@/providers/connect/dialog").then(({ DialogConnectProvider }) => {
void dialog.show(() => <DialogConnectProvider directory={sdk().directory} />)
})
}
const dismiss = () => {
const current = tip()
if (!current) return
if (current === "workspace") {
setWorkspaceState("dismissedAt", Date.now())
return
}
setProviderState("dismissedAt", Date.now())
}
return (
<Show when={presence.present()}>
<div class="pointer-events-none absolute inset-x-0 bottom-4 flex justify-center px-10">
<div
ref={setRef}
data-component="provider-tip"
data-visible={visible()}
class="group/provider-tip pointer-events-auto relative flex h-6 max-w-full items-center transition-[opacity,transform] duration-[250ms] ease-[cubic-bezier(0.215,0.61,0.355,1)] motion-reduce:transition-none"
data-component="new-session-tip"
data-visible={tip() !== undefined}
class="group/new-session-tip pointer-events-auto relative flex h-6 max-w-full items-center transition-[opacity,transform] duration-[250ms] ease-[cubic-bezier(0.215,0.61,0.355,1)] motion-reduce:transition-none"
classList={{ "data-[visible=false]:animate-out fade-out slide-out-to-bottom-4": true }}
>
<button
type="button"
class="flex h-6 min-w-0 items-center rounded-[4px] pl-1.5 text-[13px] leading-text-compact tracking-[-0.04px] text-v2-text-text-faint transition-[background-color,color] duration-150 ease-in-out hover:bg-v2-overlay-simple-overlay-hover hover:text-v2-text-text-muted focus-visible:bg-v2-overlay-simple-overlay-hover focus-visible:text-v2-text-text-muted focus-visible:outline-none"
onClick={openProviders}
onClick={open}
>
<span class="truncate">{language.t("home.providerTip")}</span>
<span class="truncate">
{language.t(displayed() === "workspace" ? "home.workspaceTip" : "home.providerTip")}
</span>
<span class="flex size-6 shrink-0 items-center justify-center" aria-hidden="true">
<Icon name="chevron-down" size="small" class="-rotate-90" />
</span>
</button>
<Tooltip
class="hover-reveal absolute left-full top-0 flex h-6 w-7 items-center justify-end delay-0 duration-0 group-hover/provider-tip:delay-[250ms] group-hover/provider-tip:duration-150 group-hover/provider-tip:opacity-100 focus-within:delay-0 focus-within:duration-0 focus-within:opacity-100"
class="hover-reveal absolute left-full top-0 flex h-6 w-7 items-center justify-end delay-0 duration-0 group-hover/new-session-tip:delay-[250ms] group-hover/new-session-tip:duration-150 group-hover/new-session-tip:opacity-100 focus-within:delay-0 focus-within:duration-0 focus-within:opacity-100"
placement="top"
openDelay={1000}
value={language.t("common.dismiss")}
@@ -176,7 +220,7 @@ function ProviderTip() {
type="button"
class="flex size-6 items-center justify-center rounded-[4px] text-v2-icon-icon-muted transition-[background-color,color] duration-150 ease-in-out hover:bg-v2-overlay-simple-overlay-hover hover:text-v2-icon-icon-base focus-visible:bg-v2-overlay-simple-overlay-hover focus-visible:text-v2-icon-icon-base focus-visible:outline-none"
aria-label={language.t("common.dismiss")}
onClick={() => setPersistedState("dismissedAt", Date.now())}
onClick={dismiss}
>
<Icon name="xmark-small" />
</button>
@@ -1,5 +1,5 @@
import { debounce } from "@solid-primitives/scheduled"
import { createEffect, createMemo, createResource } from "solid-js"
import { createEffect, createMemo, createResource, onCleanup } from "solid-js"
import { createStore } from "solid-js/store"
import { useWorkspaceLocation } from "@/workspaces/location"
import { useServerSDK } from "@/runtime/server/client"
@@ -11,8 +11,8 @@ import { normalizeProjectInfo } from "@/runtime/server/global-sync/utils"
import {
isWorkspaceDirectory,
isWorkspaceSelection,
sameDirectory,
workspaceDefaultSelection,
workspaceDirectories,
workspaceSelectionDestination,
} from "@/workspaces/paths"
@@ -56,6 +56,50 @@ export function createNewSessionWorkspaceController(input: {
const current = projectID ? data.project.get(projectID) : undefined
return current ? normalizeProjectInfo(current) : undefined
})
const worktreeSource = createMemo(
() => {
const project = currentProject()
return project ? { projectID: project.id, directory: project.worktree } : undefined
},
undefined,
{ equals: (a, b) => a?.projectID === b?.projectID && a?.directory === b?.directory },
)
const [worktrees, worktreeActions] = createResource(worktreeSource, async (source) => ({
projectID: source.projectID,
items: await serverSDK.api.worktree
.list({ location: { directory: source.directory } })
.catch(() => (currentProject()?.id === source.projectID ? currentProject()?.worktrees : undefined) ?? []),
}))
onCleanup(
serverSDK.event.listen((event) => {
if (event.type === "worktree.updated") void worktreeActions.refetch()
}),
)
const worktreeItems = createMemo(() => {
const project = currentProject()
if (!project) return []
const loaded = worktrees.latest
return loaded?.projectID === project.id ? loaded.items : project.worktrees
})
const worktreeDirectories = createMemo(() => {
const project = currentProject()
if (!project) return []
const directories = [
...worktreeItems().map((item) => item.directory),
...project.worktrees.map((item) => item.directory),
...(project.sandboxes ?? []),
]
return directories
.filter((directory) => !sameDirectory(project.worktree, directory))
.filter((directory, index, items) => items.findIndex((item) => sameDirectory(item, directory)) === index)
})
const managedWorktrees = createMemo(() => {
const project = currentProject()
if (!project) return 0
return worktreeItems().filter(
(item) => item.strategy !== undefined && !sameDirectory(project.worktree, item.directory),
).length
})
const visible = createMemo(() =>
resolveNewSessionGit({
projectVcs: currentProject()?.vcs,
@@ -66,7 +110,10 @@ export function createNewSessionWorkspaceController(input: {
const project = currentProject()
const worktree = input.selectedWorktree()
if (!project || !worktree) return
return isWorkspaceSelection(project, worktree) ? worktree : undefined
return isWorkspaceSelection(project, worktree) ||
worktreeDirectories().some((item) => sameDirectory(item, worktree))
? worktree
: undefined
})
const fallback = createMemo(() => {
const project = currentProject()
@@ -97,7 +144,7 @@ export function createNewSessionWorkspaceController(input: {
() => undefined,
)
const project = currentProject()
const directories = project ? [project.worktree, ...workspaceDirectories(project)] : [sdk().directory]
const directories = project ? [project.worktree, ...worktreeDirectories()] : [sdk().directory]
directories.forEach((directory) => void data.location.vcs.sync({ directory }).catch(() => undefined))
})
const branch = createMemo(() =>
@@ -122,7 +169,12 @@ export function createNewSessionWorkspaceController(input: {
workspace: createMemo(() => {
const project = currentProject()
const current = value()
return current === "create" || (!!project && isWorkspaceDirectory(project, current))
return (
current === "create" ||
(!!project &&
(isWorkspaceDirectory(project, current) ||
worktreeDirectories().some((item) => sameDirectory(item, current))))
)
}),
reset: () => {
input.setSelectedWorktree(undefined)
@@ -142,10 +194,8 @@ export function createNewSessionWorkspaceController(input: {
},
project: {
root: projectRoot,
workspaces: () => {
const project = currentProject()
return project ? workspaceDirectories(project) : []
},
workspaces: worktreeDirectories,
managed: managedWorktrees,
git: visible,
branches: () => {
const current = data.location.vcs.info({ directory: sdk().directory })?.branch.current
@@ -1,5 +1,6 @@
import { createMemo, For, Show } from "solid-js"
import { createMemo, createSignal, For, Show } from "solid-js"
import { createStore } from "solid-js/store"
import { createResizeObserver } from "@solid-primitives/resize-observer"
import { Menu } from "@opencode-ai/ui/menu"
import { Tooltip } from "@opencode-ai/ui/tooltip"
import { Icon } from "@opencode-ai/ui/icon"
@@ -25,6 +26,9 @@ export function PromptWorkspaceSelector(props: {
let searchInput: HTMLInputElement | undefined
let branchSearchInput: HTMLInputElement | undefined
let focusSearch = false
const branchTruncation = createTruncatedText()
const focusWorktreeSearch = () =>
requestAnimationFrame(() => requestAnimationFrame(() => searchInput?.focus({ preventScroll: true })))
let pending: { type: "select"; value: string } | { type: "create"; branch: string } | { type: "viewAll" } | undefined
const selected = () => (sameDirectory(props.value, props.projectRoot) ? "main" : props.value)
const workspaces = createMemo(() => {
@@ -34,8 +38,8 @@ export function PromptWorkspaceSelector(props: {
})
const icon = () => {
if (selected() === "main") return "monitor"
if (selected() === "create") return "workspace-new"
return "workspace-isolated"
if (selected() === "create") return "plus"
return "outline-worktree"
}
const select = (value: string) => {
pending = { type: "select", value }
@@ -66,13 +70,14 @@ export function PromptWorkspaceSelector(props: {
<>
<span class="hidden select-none opacity-50 sm:inline mx-1">/</span>
<Tooltip
appearance={props.onboarding ? "large" : undefined}
placement="top"
openDelay={800}
value={
props.onboarding ? (
<div class="flex flex-col gap-1 text-start">
<div class="flex items-center gap-1.5 font-[530] text-v2-text-text-base">
<Icon name="workspace-isolated" size="small" class="shrink-0 text-v2-text-text-accent" />
<Icon name="outline-worktree" size="small" class="shrink-0 text-v2-text-text-accent" />
<span>{language.t("workspace.onboarding.title")}</span>
</div>
<span class="font-[440] text-v2-text-text-muted">{language.t("workspace.onboarding.description")}</span>
@@ -89,7 +94,10 @@ export function PromptWorkspaceSelector(props: {
aria-description={language.t("session.new.workspace.trigger.tooltip")}
class="flex h-6 min-w-0 max-w-[203px] items-center gap-1.5 rounded-sm px-1.5 hover:bg-v2-overlay-simple-overlay-hover focus-visible:bg-v2-overlay-simple-overlay-hover focus-visible:outline-none data-[expanded]:bg-v2-overlay-simple-overlay-pressed data-[expanded]:text-v2-text-text-muted"
>
<Icon name={icon()} class="shrink-0 text-v2-icon-icon-muted" />
<Icon
name={icon()}
class={`shrink-0 ${selected() === "main" || selected() === "create" ? "text-v2-icon-icon-muted" : "text-v2-icon-icon-accent"}`}
/>
<span class="min-w-0 truncate">{label()}</span>
<Show when={props.onboarding}>
<span
@@ -109,14 +117,8 @@ export function PromptWorkspaceSelector(props: {
<Tooltip
placement="right"
openDelay={800}
value={
<span class="flex flex-col gap-0.5">
<span>{language.t("session.new.workspace.local")}</span>
<span class="font-[440] text-v2-text-text-muted">
{language.t("session.new.workspace.local.tooltip")}
</span>
</span>
}
value={language.t("session.new.workspace.local.tooltip")}
contentClass="max-w-[140px]"
class="min-w-0 flex-1"
>
<span class="min-w-0 truncate">{language.t("session.new.workspace.local")}</span>
@@ -126,24 +128,22 @@ export function PromptWorkspaceSelector(props: {
</Show>
</Menu.Item>
<Menu.Item onSelect={() => select("create")}>
<Icon name="workspace-new" />
<span class="min-w-0 flex-1 truncate">{language.t("workspace.new")}</span>
<Icon name="plus" />
<Tooltip
placement="right"
openDelay={800}
value={language.t("session.new.workspace.new.tooltip")}
contentClass="max-w-[140px]"
class="min-w-0 flex-1"
>
<span class="min-w-0 truncate">{language.t("workspace.new")}</span>
</Tooltip>
<Show when={selected() === "create"}>
<Icon name="check" size="small" class="shrink-0" />
</Show>
</Menu.Item>
</Menu.Group>
<Show
when={props.workspaces.length > 0}
fallback={
<>
<Menu.Separator class="h-[0.5px]" />
<Menu.Item onSelect={() => (pending = { type: "viewAll" })}>
<span class="min-w-0 flex-1 truncate">{language.t("common.viewAll")}</span>
</Menu.Item>
</>
}
>
<Show when={props.workspaces.length > 0}>
<Menu.Separator class="h-[0.5px]" />
<Menu.Sub
gutter={0}
@@ -156,10 +156,11 @@ export function PromptWorkspaceSelector(props: {
}
if (!focusSearch || props.workspaces.length < 10) return
focusSearch = false
requestAnimationFrame(() => searchInput?.focus())
focusWorktreeSearch()
}}
>
<Menu.SubTrigger
onClick={focusWorktreeSearch}
onKeyDown={(event) => {
if (
event.key === "ArrowRight" ||
@@ -170,7 +171,7 @@ export function PromptWorkspaceSelector(props: {
focusSearch = true
}}
>
<Icon name="workspace-isolated" />
<Icon name="outline-worktree" />
<span class="min-w-0 flex-1 truncate">
{language.t("session.new.workspace.existing").replace(/(…|\.{3})$/, "")}
</span>
@@ -205,7 +206,7 @@ export function PromptWorkspaceSelector(props: {
<For each={workspaces()}>
{(workspace) => (
<Menu.Item onSelect={() => select(workspace)}>
<Icon name="workspace-isolated" />
<Icon name="outline-worktree" />
<span class="min-w-0 flex-1 truncate">{getFilename(workspace)}</span>
<Show when={selected() === workspace}>
<Icon name="check" size="small" class="shrink-0" />
@@ -213,6 +214,11 @@ export function PromptWorkspaceSelector(props: {
</Menu.Item>
)}
</For>
<Show when={search.workspaces.trim() && workspaces().length === 0}>
<div class="px-3 py-4 text-center text-[13px] font-[440] leading-5 text-v2-text-text-muted">
{language.t("session.new.workspace.search.empty")}
</div>
</Show>
<Menu.Separator class="h-[0.5px]" />
<Menu.Item onSelect={() => (pending = { type: "viewAll" })}>
<span class="min-w-0 flex-1 truncate">{language.t("common.viewAll")}</span>
@@ -232,6 +238,7 @@ export function PromptWorkspaceSelector(props: {
<Tooltip
placement="top"
value={language.t("session.new.workspace.fromBranch", { branch: props.branch! })}
disabled={!branchTruncation.truncated()}
class="ms-1 min-w-0 max-w-[220px]"
contentClass="max-w-[calc(100vw-32px)] break-all"
>
@@ -245,7 +252,7 @@ export function PromptWorkspaceSelector(props: {
>
<Menu.Trigger class="flex h-6 min-w-0 max-w-[220px] items-center gap-1.5 rounded-full bg-v2-background-bg-layer-02 px-2.5 text-[13px] font-[440] leading-5 tracking-[-0.04px] text-v2-text-text-faint transition-colors hover:bg-v2-background-bg-layer-03 hover:text-v2-text-text-muted focus-visible:bg-v2-background-bg-layer-03 focus-visible:text-v2-text-text-muted focus-visible:outline-none data-[expanded]:bg-v2-background-bg-layer-03 data-[expanded]:text-v2-text-text-muted">
<Icon name="branch-out" size="small" class="shrink-0 text-v2-icon-icon-muted" />
<span class="min-w-0 truncate">
<span ref={branchTruncation.observe} class="min-w-0 truncate">
{language.t("session.new.workspace.fromBranch", { branch: props.branch! })}
</span>
<Icon name="chevron-down" size="small" class="shrink-0 text-v2-icon-icon-muted" />
@@ -319,6 +326,7 @@ export function PromptWorkspaceSelector(props: {
export function PromptGitStatus(props: { branch?: string; noGit?: boolean; from?: boolean; class?: string }) {
const language = useLanguage()
const truncation = createTruncatedText()
const label = () => {
if (props.noGit) return language.t("session.new.git.none")
if (!props.branch) return undefined
@@ -338,15 +346,27 @@ export function PromptGitStatus(props: { branch?: string; noGit?: boolean; from?
<Tooltip
placement="top"
value={value()}
disabled={!truncation.truncated()}
class={`min-w-0 max-w-[220px] ${props.class ?? ""}`}
contentClass="max-w-[calc(100vw-32px)] break-all"
>
<div class="flex h-6 min-w-0 max-w-[220px] items-center gap-1.5 rounded-full bg-v2-background-bg-layer-02 px-2.5 text-[13px] font-[440] leading-5 tracking-[-0.04px] text-v2-text-text-faint">
<Icon name={icon()} size="small" class="shrink-0 text-v2-icon-icon-muted" />
<span class="min-w-0 truncate">{value()}</span>
<span ref={truncation.observe} class="min-w-0 truncate">
{value()}
</span>
</div>
</Tooltip>
)}
</Show>
)
}
function createTruncatedText() {
const [truncated, setTruncated] = createSignal(false)
return {
truncated,
observe: (element: HTMLSpanElement) =>
createResizeObserver(element, () => setTruncated(element.scrollWidth > element.clientWidth)),
}
}
+27 -11
View File
@@ -659,6 +659,7 @@ export const dict = {
"home.sessions.group.yesterday": "Yesterday",
"home.sessions.group.older": "Older",
"home.providerTip": "Connect to 75+ providers to use other models, including Claude, GPT, Gemini, etc",
"home.workspaceTip": "Start next session in a new workspace to keep changes isolated",
"session.tab.session": "Session",
"session.tab.files": "Files",
@@ -1250,30 +1251,43 @@ export const dict = {
"workspace.new": "New worktree",
"common.viewAll": "View all",
"session.new.workspace.local.tooltip": "Use current checkout",
"session.new.workspace.new.tooltip": "Create isolated checkout",
"session.new.workspace.local.tooltip": "Uses projects current checkout",
"session.new.workspace.new.tooltip": "Creates isolated copy from current checkout",
"session.new.workspace.fromBranch": "from {{branch}}",
"session.new.workspace.createFrom": "Create from branch",
"session.new.workspace.branch.search.placeholder": "Search branches",
"session.new.workspace.trigger.tooltip": "Select where to run session",
"session.new.workspace.search.placeholder": "Search worktrees",
"session.new.workspace.search.empty": "No matching worktrees",
"settings.tab.workspaces": "Worktrees",
"settings.workspaces.description": "Review worktrees and manage disk usage",
"settings.workspaces.filter.all": "All projects",
"settings.workspaces.empty": "No worktrees",
"settings.workspaces.empty": "No worktrees yet",
"settings.workspaces.empty.description": "Worktrees created in OpenCode will appear here",
"settings.workspaces.count.one": "{{count}} worktree",
"settings.workspaces.count.other": "{{count}} worktrees",
"settings.workspaces.sessions.one": "{{count}} session in {{project}}",
"settings.workspaces.sessions.other": "{{count}} sessions in {{project}}",
"settings.workspaces.sessions.filtered.one": "{{count}} session",
"settings.workspaces.sessions.filtered.other": "{{count}} sessions",
"settings.workspaces.lastActiveSession": "Last active session",
"settings.workspaces.deleteAll": "Delete all worktrees",
"settings.workspaces.deleteAll.confirm": "Delete all {{count}} worktrees?",
"settings.workspaces.delete.warning":
"The worktree directory and branch will be permanently removed, including any unmerged changes shown below.",
"settings.workspaces.deleteAll.confirm.one": "This will permanently delete {{count}} selected worktree.",
"settings.workspaces.deleteAll.confirm.other": "This will permanently delete {{count}} selected worktrees.",
"settings.workspaces.deleteWithoutSessions": "Delete worktrees without sessions",
"settings.workspaces.deleteWithoutSessions.confirm.one":
"This will permanently delete {{count}} worktree without linked sessions.",
"settings.workspaces.deleteWithoutSessions.confirm.other":
"This will permanently delete {{count}} worktrees without linked sessions.",
"settings.workspaces.deleteWithoutSessions.warning":
"Worktrees with unmerged changes or active locations will be kept.",
"settings.workspaces.delete.button": "Delete worktrees",
"settings.workspaces.delete.warning": "This permanently deletes the worktree and its branch.",
"settings.workspaces.deleteAll.warning":
"The {{count}} selected worktrees in {{project}} will be permanently removed only if each is clean, inactive, and has no linked sessions.",
"Worktrees with unmerged changes, linked sessions, or active locations will be kept.",
"settings.workspaces.delete.blocked.active": "The active worktree cannot be deleted.",
"settings.workspaces.delete.blocked.linked":
"Linked sessions will remain, but their working directory will be permanently removed.",
"Linked sessions will remain, but their working directory will no longer exist.",
"settings.workspaces.default.title": "Default environment",
"settings.workspaces.default.description": "Choose where new sessions start",
"settings.workspaces.default.lastUsed": "Last used per project",
@@ -1284,9 +1298,10 @@ export const dict = {
"workspace.move.failed": "Failed to move session",
"workspace.lifecycle.creating": "Creating worktree",
"workspace.lifecycle.created": "Worktree created",
"workspace.lifecycle.deleting": "Deleting…",
"workspace.lifecycle.starting": "Starting session",
"workspace.onboarding.title": "Isolate sessions with worktrees",
"workspace.onboarding.description": "Each gets its own checkout, so nothing interferes with your local repository",
"workspace.onboarding.description": "Each gets its own checkout",
"workspace.lifecycle.moving": "Moving to worktree",
"workspace.lifecycle.set": "Worktree set",
"session.summary.title": "Session details",
@@ -1305,9 +1320,10 @@ export const dict = {
"workspace.status.checking": "Checking for unmerged changes…",
"workspace.status.error": "Unable to verify git status.",
"workspace.status.clean": "No unmerged changes detected.",
"workspace.status.dirty": "Unmerged changes detected in this worktree.",
"workspace.status.dirty": "Unmerged changes will be lost.",
"workspace.delete.title": "Delete worktree",
"workspace.delete.confirm": 'Delete worktree "{{name}}"?',
"workspace.delete.confirm": "Delete {{name}}”?",
"workspace.delete.location": "Location",
"workspace.delete.button": "Delete worktree",
"workspace.reset.title": "Reset worktree",
"workspace.reset.confirm": 'Reset worktree "{{name}}"?',
@@ -1,6 +1,6 @@
import { describe, expect, test } from "bun:test"
import { Schema } from "effect"
import { WorkspaceOnboardingSchema, ProviderTipSchema } from "@/new-session/view"
import { WorkspaceOnboardingSchema, ProviderTipSchema, WorkspaceTipSchema } from "@/new-session/view"
import { ModelSelectionSchema } from "@/providers/models/selection"
import { Persistence } from "@/runtime/persistence/schema"
import { FileViewsSchema } from "@/workspaces/files/view-cache"
@@ -12,6 +12,7 @@ describe("persisted consumer schemas", () => {
test("onboarding and provider tip retain defaults and validate stored values", () => {
const onboarding = Schema.decodeUnknownSync(Persistence.withInitial(WorkspaceOnboardingSchema, { used: false }))
const tip = Schema.decodeUnknownSync(Persistence.withInitial(ProviderTipSchema, { dismissedAt: 0 }))
const workspaceTip = Schema.decodeUnknownSync(Persistence.withInitial(WorkspaceTipSchema, { dismissedAt: 0 }))
expect(onboarding({})).toEqual({ used: false })
expect(onboarding({ used: "true" })).toEqual({ used: false })
expect(onboarding({ used: true })).toEqual({ used: true })
@@ -19,6 +20,7 @@ describe("persisted consumer schemas", () => {
expect(tip({ dismissedAt: "yesterday" })).toEqual({ dismissedAt: 0 })
expect(tip({ dismissedAt: Infinity })).toEqual({ dismissedAt: 0 })
expect(tip({ dismissedAt: 123 })).toEqual({ dismissedAt: 123 })
expect(workspaceTip({ dismissedAt: 123 })).toEqual({ dismissedAt: 123 })
})
test("collapse records recover malformed entries without losing valid siblings", () => {
@@ -1,7 +1,13 @@
import { describe, expect, test } from "bun:test"
import { createRequestQueue, isSlowRequest } from "./request-queue"
import { createRequestQueue, isSetupRequest, isSlowRequest } from "./request-queue"
function setup(input?: { limit?: number; slowLimit?: number; stallMs?: number; headersTimeoutMs?: number }) {
function setup(input?: {
limit?: number
slowLimit?: number
stallMs?: number
headersTimeoutMs?: number
setupHeadersTimeoutMs?: number
}) {
const pending: Array<{ url: string; signal: AbortSignal; resolve: () => void }> = []
const logs: Array<{ message: string; data: Record<string, unknown> }> = []
let clock = 0
@@ -10,12 +16,13 @@ function setup(input?: { limit?: number; slowLimit?: number; stallMs?: number; h
slowLimit: input?.slowLimit,
stallMs: input?.stallMs,
headersTimeoutMs: input?.headersTimeoutMs,
setupHeadersTimeoutMs: input?.setupHeadersTimeoutMs,
now: () => clock,
log: (message, data) => logs.push({ message, data }),
fetch: Object.assign(
(resource: RequestInfo | URL) =>
(resource: RequestInfo | URL, init?: RequestInit) =>
new Promise<Response>((resolve, reject) => {
const request = new Request(resource)
const request = new Request(resource, init)
request.signal.addEventListener("abort", () => reject(request.signal.reason), { once: true })
pending.push({ url: request.url, signal: request.signal, resolve: () => resolve(new Response("ok")) })
}),
@@ -43,7 +50,12 @@ describe("createRequestQueue", () => {
test("slow endpoints hold at most their share of slots so small reads go first", async () => {
const input = setup({ limit: 4, slowLimit: 2 })
const paths = ["/api/vcs?location[directory]=%2Fa", "/api/vcs/diff?location[directory]=%2Fa", "/api/worktree", "/api/session/ses_1"]
const paths = [
"/api/vcs?location[directory]=%2Fa",
"/api/vcs/diff?location[directory]=%2Fa",
"/api/worktree",
"/api/session/ses_1",
]
const responses = paths.map((path) => input.queue.fetch(`http://server${path}`))
await input.settle()
const started = () => input.pending.map((item) => new URL(item.url).pathname)
@@ -109,6 +121,26 @@ describe("createRequestQueue", () => {
expect(input.queue.inflight()).toBe(0)
})
test("worktree creation gets the setup deadline while worktree reads keep the normal one", async () => {
const input = setup({ limit: 4, headersTimeoutMs: 10, setupHeadersTimeoutMs: 200 })
const create = input.queue.fetch("http://server/api/worktree?location[directory]=%2Fa", { method: "POST" })
const list = input.queue.fetch("http://server/api/worktree?location[directory]=%2Fa")
const listError = await list.catch((cause: unknown) => cause)
expect((listError as DOMException).name).toBe("TimeoutError")
// Past the normal deadline, the create is still on the wire.
expect(input.pending[0]!.signal.aborted).toBe(false)
input.pending[0]!.resolve()
await expect(create).resolves.toBeInstanceOf(Response)
expect(input.queue.inflight()).toBe(0)
})
test("only worktree creation counts as a setup request", () => {
expect(isSetupRequest("POST", "/api/worktree")).toBe(true)
expect(isSetupRequest("GET", "/api/worktree")).toBe(false)
expect(isSetupRequest("POST", "/api/worktree/refresh")).toBe(false)
expect(isSetupRequest("DELETE", "/api/worktree")).toBe(false)
})
test("caller aborts still reach the underlying request", async () => {
const input = setup({ limit: 1 })
const controller = new AbortController()
@@ -19,16 +19,26 @@ export const requestStallMs = 2_000
// instead of wedging every later API call; the body may still stream for as long as it needs.
export const requestHeadersTimeoutMs = 60_000
// Creating a worktree runs the project's setup script (dependency installs, fetches) before the
// server answers, so it needs a budget measured in minutes rather than seconds. Cutting it off
// kills the script midway and leaves a registered but half-initialised worktree behind.
export const setupRequestHeadersTimeoutMs = 10 * 60_000
export function isSlowRequest(pathname: string) {
return slowRequestPaths.some((path) => pathname === path || pathname.startsWith(`${path}/`))
}
export function isSetupRequest(method: string, pathname: string) {
return method === "POST" && pathname === "/api/worktree"
}
export function createRequestQueue(input: {
fetch: typeof globalThis.fetch
limit?: number
slowLimit?: number
stallMs?: number
headersTimeoutMs?: number
setupHeadersTimeoutMs?: number
log?: (message: string, data: Record<string, unknown>) => void
now?: () => number
}) {
@@ -36,6 +46,7 @@ export function createRequestQueue(input: {
const slowLimit = input.slowLimit ?? requestQueueSlowLimit
const stallMs = input.stallMs ?? requestStallMs
const headersTimeoutMs = input.headersTimeoutMs ?? requestHeadersTimeoutMs
const setupHeadersTimeoutMs = input.setupHeadersTimeoutMs ?? setupRequestHeadersTimeoutMs
// Call the browser fetch unbound; `input.fetch(...)` would make `this` the options object.
const base = input.fetch
const now = input.now ?? Date.now
@@ -101,7 +112,7 @@ export function createRequestQueue(input: {
request.signal.addEventListener("abort", () => controller.abort(request.signal.reason), { once: true })
const timer = setTimeout(
() => controller.abort(new DOMException("Timed out waiting for the server to respond", "TimeoutError")),
headersTimeoutMs,
isSetupRequest(request.method, pathname) ? setupHeadersTimeoutMs : headersTimeoutMs,
)
return base(new Request(request, { signal: controller.signal })).finally(() => {
clearTimeout(timer)
@@ -91,7 +91,7 @@ export function SessionProjectMenu(props: {
when={props.showProjectIcon}
fallback={
<span class={props.workspace ? "text-v2-icon-icon-accent" : "text-v2-icon-icon-muted"}>
<Icon name={props.workspace ? "workspace-isolated" : "monitor"} />
<Icon name={props.workspace ? "outline-worktree" : "monitor"} />
</span>
}
>
@@ -184,7 +184,7 @@ function WorkspaceMoveAction(props: {
: "flex h-[46px] w-full items-center gap-2 rounded-b-[6px] px-3 pe-9 pt-2.5 text-[13px] font-[440] leading-5 tracking-[-0.04px] text-v2-text-text-muted focus-visible:outline-none"
}
>
<Icon name="workspace-new" class="shrink-0 text-v2-icon-icon-muted" />
<Icon name="outline-worktree" class="shrink-0 text-v2-icon-icon-muted" />
<span class="min-w-0 truncate">{language.t("workspace.move.title")}</span>
</SessionWorkspaceMenu>
<button
@@ -256,7 +256,10 @@ export function SessionSummaryPanel(props: {
gutter={props.mobile ? 4 : -22}
class={`${row} hover:bg-v2-overlay-simple-overlay-hover focus-visible:bg-v2-overlay-simple-overlay-hover focus-visible:outline-none data-[expanded]:bg-v2-overlay-simple-overlay-pressed`}
>
<Icon name={props.local ? "monitor" : "workspace-isolated"} class="shrink-0 text-v2-icon-icon-muted" />
<Icon
name={props.local ? "monitor" : "outline-worktree"}
class={`shrink-0 ${props.local ? "text-v2-icon-icon-muted" : "text-v2-icon-icon-accent"}`}
/>
<span dir="auto" class="min-w-0 flex-1 truncate text-start">
{location()}
</span>
@@ -7,7 +7,6 @@ import type { Project } from "@/runtime/server/types"
import { useLanguage } from "@/runtime/i18n/language"
import { useServerSDK } from "@/runtime/server/client"
import { useData } from "@/runtime/server/current"
import { useSettingsDialog } from "@/settings/command"
import { pathKey } from "@/workspaces/path-key"
import { showToast } from "@/shell/notifications/toast"
import { containsDirectory, sameDirectory, workspaceDirectories } from "@/workspaces/paths"
@@ -28,7 +27,6 @@ export function SessionWorkspaceMenu(props: {
const language = useLanguage()
const serverSDK = useServerSDK()
const data = useData()
const openWorkspaces = useSettingsDialog("workspaces")
const [store, setStore] = createStore({ selected: undefined as string | undefined })
const [directories, setDirectories] = createSignal(workspaceDirectories(props.project))
const blocked = () => props.eligible === false || data.session.status(props.sessionID) === "running"
@@ -99,13 +97,13 @@ export function SessionWorkspaceMenu(props: {
</Menu.Item>
</Show>
<Menu.Item disabled={!!store.selected || blocked()} onSelect={() => void move("create")}>
<Icon name="workspace-new" />
<Icon name="plus" />
{language.t("workspace.new")}
</Menu.Item>
<Show when={workspaces().length > 0}>
<Menu.Sub gutter={0} overlap overflowPadding={8}>
<Menu.SubTrigger>
<Icon name="workspace-isolated" />
<Icon name="outline-worktree" />
{language.t("session.new.workspace.existing").replace(/(…|\.{3})$/, "")}
</Menu.SubTrigger>
<Menu.Portal>
@@ -113,7 +111,7 @@ export function SessionWorkspaceMenu(props: {
<For each={workspaces()}>
{(workspace) => (
<Menu.Item disabled={!!store.selected || blocked()} onSelect={() => void move(workspace)}>
<Icon name="workspace-isolated" />
<Icon name="outline-worktree" />
<span class="min-w-0 flex-1 truncate">{getFilename(workspace)}</span>
</Menu.Item>
)}
@@ -123,10 +121,6 @@ export function SessionWorkspaceMenu(props: {
</Menu.Sub>
</Show>
</Menu.Group>
<Menu.Separator class="h-[0.5px] bg-v2-border-border-base" />
<Menu.Item onSelect={() => openWorkspaces()}>
<span class="min-w-0 flex-1 truncate">{language.t("common.viewAll")}</span>
</Menu.Item>
</Menu.Content>
</Menu.Portal>
</Menu>
+101 -13
View File
@@ -1029,25 +1029,92 @@
display: flex;
flex-direction: column;
gap: 0;
padding: 20px;
border-radius: 6px;
background-color: var(--v2-background-bg-base);
padding: 16px;
border-radius: 8px;
background-color: var(--v2-background-bg-layer-01);
box-shadow: inset 0 0 0 0.5px var(--v2-border-border-base);
}
.settings-workspaces-inventory[data-empty="true"] [data-component="settings-list"] {
padding: 0;
background-color: transparent;
box-shadow: none;
}
.settings-workspaces-row-motion {
display: grid;
grid-template-rows: minmax(0, 1fr);
opacity: 1;
transition:
grid-template-rows 150ms ease-out,
opacity 120ms ease-out;
}
.settings-workspaces-row-motion[data-removing="true"] {
grid-template-rows: minmax(0, 0fr);
opacity: 0;
}
.settings-workspaces-row {
display: flex;
min-width: 0;
min-height: 0;
flex-direction: column;
gap: 12px;
overflow: hidden;
transition:
padding-bottom 150ms ease-out,
margin-bottom 150ms ease-out,
border-color 120ms ease-out;
}
.settings-workspaces-row:not(:last-child) {
.settings-workspaces-row-motion:not(:last-child) > .settings-workspaces-row {
padding-bottom: 20px;
margin-bottom: 20px;
border-bottom: 0.5px solid var(--v2-border-border-base);
}
.settings-workspaces-row-motion:has(+ .settings-workspaces-row-motion[data-removing="true"]:last-child)
> .settings-workspaces-row {
padding-bottom: 0;
margin-bottom: 0;
border-color: transparent;
}
.settings-workspaces-empty-motion {
display: grid;
min-height: 0;
grid-template-rows: minmax(0, 0fr);
opacity: 0;
overflow: hidden;
pointer-events: none;
transition:
grid-template-rows 150ms ease-out,
opacity 120ms ease-out;
}
.settings-workspaces-empty-motion[data-visible="true"] {
grid-template-rows: minmax(0, 1fr);
opacity: 1;
pointer-events: auto;
}
.settings-workspaces-empty-motion > .settings-workspaces-empty {
min-height: 0;
overflow: hidden;
}
@media (prefers-reduced-motion: reduce) {
.settings-workspaces-row-motion {
transition: none;
}
.settings-workspaces-row,
.settings-workspaces-empty-motion {
transition: none;
}
}
.settings-workspaces-row-header {
display: flex;
min-width: 0;
@@ -1088,7 +1155,7 @@
color: var(--v2-text-text-base);
font-family: inherit;
font-size: 13px;
font-weight: 530;
font-weight: 440;
line-height: var(--line-height-compact);
letter-spacing: -0.04px;
text-overflow: ellipsis;
@@ -1098,6 +1165,11 @@
cursor: default;
}
.settings-workspaces-path-name {
color: var(--v2-text-text-base);
font-weight: 530;
}
.settings-workspaces-meta {
font-size: 13px;
font-weight: 440;
@@ -1105,6 +1177,10 @@
color: var(--v2-text-text-faint);
}
.settings-workspaces-meta-project {
color: var(--v2-text-text-muted);
}
.settings-workspaces-active,
.settings-workspaces-more {
flex-shrink: 0;
@@ -1114,11 +1190,16 @@
color: var(--v2-text-text-faint);
}
.settings-workspaces-active {
font-size: 13px;
line-height: var(--line-height-compact);
}
.settings-workspaces-sessions {
display: flex;
flex-direction: column;
border: 0.5px solid var(--v2-border-border-base);
border-radius: 4px;
border-radius: 6px;
background-color: var(--v2-background-bg-base);
overflow: hidden;
}
@@ -1129,7 +1210,8 @@
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 10px 12px;
padding-block: 10px;
padding-inline: 12px 16px;
font-size: 13px;
font-weight: 440;
line-height: 16px;
@@ -1149,22 +1231,28 @@
.settings-workspaces-session-time {
flex-shrink: 0;
font-size: 11px;
line-height: 1;
color: var(--v2-text-text-muted);
font-size: 13px;
line-height: var(--line-height-compact);
color: var(--v2-text-text-faint);
}
.settings-workspaces-empty {
display: flex;
align-items: center;
justify-content: center;
padding-block: 48px;
min-width: 0;
flex-direction: column;
align-items: flex-start;
gap: 4px;
font-size: 13px;
font-weight: 440;
line-height: var(--line-height-compact);
color: var(--v2-text-text-muted);
}
.settings-workspaces-empty-title {
color: var(--v2-text-text-base);
font-weight: 530;
}
@media (max-width: 639px) {
.settings-workspaces-header {
padding: 24px 20px 20px;
+21 -4
View File
@@ -1,4 +1,5 @@
import { Component, createEffect, createMemo, For, Show, onMount, startTransition } from "solid-js"
import { createStore } from "solid-js/store"
import { Tabs } from "@opencode-ai/ui/tabs"
import { Icon } from "@opencode-ai/ui/icon"
import { Menu } from "@opencode-ai/ui/menu"
@@ -35,7 +36,7 @@ const sections = [
[
{ value: "servers", icon: "server", label: "status.popover.tab.servers" },
{ value: "projects", icon: "folder", label: "settings.tab.projects" },
{ value: "workspaces", icon: "workspace-isolated", label: "settings.tab.workspaces" },
{ value: "workspaces", icon: "outline-worktree", label: "settings.tab.workspaces" },
],
[
{ value: "providers", icon: "providers", label: "settings.providers.title" },
@@ -54,6 +55,7 @@ export const SettingsScreen: Component = () => {
const servers = useServers()
const tabs = useTabs()
const global = useGlobal()
const [state, setState] = createStore({ worktreeFilterReset: 0 })
let root: HTMLDivElement | undefined
onMount(() => {
@@ -146,7 +148,14 @@ export const SettingsScreen: Component = () => {
</Show>
<For each={group}>
{(section) => (
<Menu.RadioItem value={section.value} closeOnSelect>
<Menu.RadioItem
value={section.value}
closeOnSelect
onSelect={() => {
if (section.value === "workspaces")
setState("worktreeFilterReset", (value) => value + 1)
}}
>
<Icon name={section.icon} />
{language.t(section.label)}
</Menu.RadioItem>
@@ -172,7 +181,12 @@ export const SettingsScreen: Component = () => {
<div class="flex flex-col gap-1 w-full">
<For each={group}>
{(section) => (
<Tabs.Trigger value={section.value}>
<Tabs.Trigger
value={section.value}
onClick={() => {
if (section.value === "workspaces") setState("worktreeFilterReset", (value) => value + 1)
}}
>
<Icon name={section.icon} />
{language.t(section.label)}
</Tabs.Trigger>
@@ -208,7 +222,10 @@ export const SettingsScreen: Component = () => {
</Tabs.Content>
<SettingsServerScope directory={directory()}>
<Tabs.Content value="workspaces" class="settings-panel">
<SettingsWorkspaces activeDirectory={directory()} />
<SettingsWorkspaces
activeDirectory={directory()}
resetProjectFilter={() => state.worktreeFilterReset}
/>
</Tabs.Content>
<Tabs.Content value="providers" class="settings-panel">
<SettingsProviders directory={directory()} onBack={showProviders} />
@@ -1,6 +1,8 @@
import type { Component } from "solid-js"
import { For, Show, createMemo } from "solid-js"
import { For, Show, createEffect, createMemo, createSignal } from "solid-js"
import { createStore } from "solid-js/store"
import { createResizeObserver } from "@solid-primitives/resize-observer"
import { Key } from "@solid-primitives/keyed"
import type { SessionInfo } from "@opencode-ai/client/promise"
import { useQuery } from "@tanstack/solid-query"
import { Button } from "@opencode-ai/ui/button"
@@ -46,7 +48,9 @@ type Workspace = {
project: Project
}
export const SettingsWorkspaces: Component<{ activeDirectory?: string }> = (props) => {
export const SettingsWorkspaces: Component<{ activeDirectory?: string; resetProjectFilter: () => number }> = (
props,
) => {
const dialog = useDialog()
const language = useLanguage()
const serverSDK = useServerSDK()
@@ -56,6 +60,12 @@ export const SettingsWorkspaces: Component<{ activeDirectory?: string }> = (prop
const [store, setStore] = createStore({
project: "all",
transaction: undefined as "confirm" | "running" | undefined,
deleting: [] as string[],
removing: [] as string[],
})
createEffect(() => {
props.resetProjectFilter()
setStore("project", "all")
})
const projectQuery = useQuery(() => ({
@@ -111,6 +121,7 @@ export const SettingsWorkspaces: Component<{ activeDirectory?: string }> = (prop
] as const,
queryFn: () => loadSessions(workspaceDirectories()),
enabled: serverSDK.connection.status() === "connected" && workspaceDirectories().length > 0,
placeholderData: (previous) => previous,
refetchOnMount: "always",
}))
const sessionsByWorkspace = createMemo(() => {
@@ -123,14 +134,29 @@ export const SettingsWorkspaces: Component<{ activeDirectory?: string }> = (prop
)
})
const workspaceSessions = (workspace: Workspace) => sessionsByWorkspace().get(pathKey(workspace.directory)) ?? []
const workspacesWithoutSessions = createMemo(() => {
if (sessionQuery.isPending || sessionQuery.isError) return []
return filtered().filter((workspace) => workspaceSessions(workspace).length === 0)
})
const sessionCount = (workspace: Workspace) => {
if (sessionQuery.isPending) return language.t("session.messages.loading")
if (sessionQuery.isError) return language.t("common.requestFailed")
const count = workspaceSessions(workspace).length
return language.plural("settings.workspaces.sessions", count, {
if (selectedProject() !== "all") return language.plural("settings.workspaces.sessions.filtered", count, { count })
const project = projectName(workspace.project)
const label = language.plural("settings.workspaces.sessions", count, {
count,
project: projectName(workspace.project),
project,
})
const start = label.lastIndexOf(project)
if (start < 0) return label
return (
<>
{label.slice(0, start)}
<span class="settings-workspaces-meta-project">{project}</span>
{label.slice(start + project.length)}
</>
)
}
const lastActive = (workspace: Workspace) => {
const updated = workspaceSessions(workspace)[0]?.time.updated
@@ -158,54 +184,64 @@ export const SettingsWorkspaces: Component<{ activeDirectory?: string }> = (prop
}
const inspectionMessages = (result: WorkspaceDeleteInspection) => {
const messages = [
result.active ? language.t("settings.workspaces.delete.blocked.active") : undefined,
result.linked ? language.t("settings.workspaces.delete.blocked.linked") : undefined,
result.dirty ? language.t("workspace.status.dirty") : undefined,
].filter((message): message is string => message !== undefined)
return messages.length > 0 ? messages : [language.t("workspace.status.clean")]
return messages
}
const blocked = (result: WorkspaceDeleteInspection) => {
showToast({
variant: "error",
title: language.t("workspace.delete.failed.title"),
description: inspectionMessages(result)[0],
description: result.active
? language.t("settings.workspaces.delete.blocked.active")
: inspectionMessages(result)[0],
})
}
const remove = async (workspace: Workspace, force = false, context = captureDeleteContext()) => {
const preflight = await inspect(workspace, context)
if (preflight.result.active || (!force && (preflight.result.linked || preflight.result.dirty))) {
blocked(preflight.result)
return
}
const removed = await context.sdk.api.worktree
.remove({
location: { directory: workspace.project.worktree },
directory: workspace.directory,
force,
})
.then(() => true)
.catch((error) => {
showToast({
variant: "error",
title: language.t("workspace.delete.failed.title"),
description: error instanceof Error ? error.message : language.t("common.requestFailed"),
const key = String(pathKey(workspace.directory))
setStore("deleting", (items) => [...items, key])
try {
const preflight = await inspect(workspace, context)
if (!force && (preflight.result.active || preflight.result.linked || preflight.result.dirty)) {
blocked(preflight.result)
return
}
const removed = await context.sdk.api.worktree
.remove({
location: { directory: workspace.project.worktree },
directory: workspace.directory,
force,
})
.then(() => true)
.catch((error) => {
showToast({
variant: "error",
title: language.t("workspace.delete.failed.title"),
description: error instanceof Error ? error.message : language.t("common.requestFailed"),
})
return false
})
if (!removed) return
setStore("removing", (items) => [...items, key])
await new Promise((resolve) => setTimeout(resolve, 150))
tabs.store.forEach((tab) => {
if (tab.type !== "draft" || tab.server !== context.server) return
const directoryMatches = containsDirectory(workspace.directory, tab.directory)
const worktreeMatches = tab.worktree && containsDirectory(workspace.directory, tab.worktree)
if (!directoryMatches && !worktreeMatches) return
tabs.updateDraft(tab.draftID, {
directory: directoryMatches ? workspace.project.worktree : tab.directory,
worktree: undefined,
})
return false
})
if (!removed) return
tabs.store.forEach((tab) => {
if (tab.type !== "draft" || tab.server !== context.server) return
const directoryMatches = containsDirectory(workspace.directory, tab.directory)
const worktreeMatches = tab.worktree && containsDirectory(workspace.directory, tab.worktree)
if (!directoryMatches && !worktreeMatches) return
tabs.updateDraft(tab.draftID, {
directory: directoryMatches ? workspace.project.worktree : tab.directory,
worktree: undefined,
})
})
clearWorkspaceTerminals(workspace.directory, platform, context.sdk.scope)
await projectQuery.refetch()
clearWorkspaceTerminals(workspace.directory, platform, context.sdk.scope)
await projectQuery.refetch()
} finally {
setStore("deleting", (items) => items.filter((item) => item !== key))
setStore("removing", (items) => items.filter((item) => item !== key))
}
}
let inspectionID = 0
@@ -253,13 +289,30 @@ export const SettingsWorkspaces: Component<{ activeDirectory?: string }> = (prop
if (store.transaction) return
const context = captureDeleteContext()
const inventory = [...filtered()]
const project = projectOptions().find((option) => option.id === selectedProject())?.label ?? selectedProject()
setStore("transaction", "confirm")
void dialog.push(
() => (
<DialogDeleteAllWorkspaces
count={inventory.length}
project={project}
<DialogDeleteWorkspaces
title={language.t("settings.workspaces.deleteAll")}
confirmation={language.plural("settings.workspaces.deleteAll.confirm", inventory.length)}
warning={language.t("settings.workspaces.deleteAll.warning")}
onDelete={() => transact(() => removeAll(inventory, context))}
/>
),
releaseConfirmation,
)
}
const confirmDeleteWithoutSessions = () => {
if (store.transaction || workspacesWithoutSessions().length === 0) return
const context = captureDeleteContext()
const inventory = [...workspacesWithoutSessions()]
setStore("transaction", "confirm")
void dialog.push(
() => (
<DialogDeleteWorkspaces
title={language.t("settings.workspaces.deleteWithoutSessions")}
confirmation={language.plural("settings.workspaces.deleteWithoutSessions.confirm", inventory.length)}
warning={language.t("settings.workspaces.deleteWithoutSessions.warning")}
onDelete={() => transact(() => removeAll(inventory, context))}
/>
),
@@ -271,44 +324,45 @@ export const SettingsWorkspaces: Component<{ activeDirectory?: string }> = (prop
<>
<div class="settings-tab-header settings-workspaces-header">
<div class="settings-tab-header-row">
<h2 class="settings-tab-title">{language.t("settings.tab.workspaces")}</h2>
<div class="flex flex-col gap-1">
<h2 class="settings-tab-title">{language.t("settings.tab.workspaces")}</h2>
<span class="text-11-regular text-v2-text-text-muted">{language.t("settings.workspaces.description")}</span>
</div>
<InlineServerSelect />
</div>
</div>
<div class="settings-tab-body settings-workspaces">
<div class="settings-workspaces-toolbar">
<span class="settings-workspaces-count">
<Show when={!projectQuery.isPending && !projectQuery.isError}>
<Show when={filtered().length > 0}>
<div class="settings-workspaces-toolbar">
<span class="settings-workspaces-count">
{language.plural("settings.workspaces.count", filtered().length)}
</Show>
</span>
<div class="settings-workspaces-toolbar-actions">
<Show when={projects().length > 1}>
<Menu placement="bottom-end" gutter={6}>
<Menu.Trigger class="flex h-6 max-w-48 items-center gap-1 rounded-sm px-2 text-13-medium hover:bg-v2-overlay-simple-overlay-hover focus-visible:bg-v2-overlay-simple-overlay-hover focus-visible:outline-none data-[expanded]:bg-v2-overlay-simple-overlay-pressed">
<span class="min-w-0 truncate">
{projectOptions().find((option) => option.id === selectedProject())?.label}
</span>
<Icon name="chevron-down" size="small" class="shrink-0 text-v2-icon-icon-muted" />
</Menu.Trigger>
<Menu.Portal>
<Menu.Content>
<For each={projectOptions()}>
{(option) => (
<Menu.Item onSelect={() => setStore("project", option.id)}>
<span class="min-w-0 flex-1 truncate">{option.label}</span>
<Show when={selectedProject() === option.id}>
<Icon name="check" size="small" class="shrink-0" />
</Show>
</Menu.Item>
)}
</For>
</Menu.Content>
</Menu.Portal>
</Menu>
</Show>
<Show when={filtered().length > 0}>
</span>
<div class="settings-workspaces-toolbar-actions">
<Show when={projects().length > 1}>
<Menu placement="bottom-end" gutter={6}>
<Menu.Trigger as={Button} size="small" variant="ghost-muted" class="max-w-48">
<span class="min-w-0 truncate">
{projectOptions().find((option) => option.id === selectedProject())?.label}
</span>
<Icon name="chevron-down" size="small" class="shrink-0 text-v2-icon-icon-muted" />
</Menu.Trigger>
<Menu.Portal>
<Menu.Content>
<For each={projectOptions()}>
{(option) => (
<Menu.Item onSelect={() => setStore("project", option.id)}>
<span class="min-w-0 flex-1 truncate">{option.label}</span>
<Show when={selectedProject() === option.id}>
<Icon name="check" size="small" class="shrink-0" />
</Show>
</Menu.Item>
)}
</For>
</Menu.Content>
</Menu.Portal>
</Menu>
</Show>
<Menu placement="bottom-end" gutter={4}>
<Menu.Trigger
as={IconButton}
@@ -321,78 +375,83 @@ export const SettingsWorkspaces: Component<{ activeDirectory?: string }> = (prop
/>
<Menu.Portal>
<Menu.Content>
<Show when={workspacesWithoutSessions().length > 0}>
<Menu.Item onSelect={confirmDeleteWithoutSessions}>
{language.t("settings.workspaces.deleteWithoutSessions")}
</Menu.Item>
<Menu.Separator />
</Show>
<Menu.Item onSelect={confirmDeleteAll}>
<span class="settings-workspaces-delete-all">{language.t("settings.workspaces.deleteAll")}</span>
</Menu.Item>
</Menu.Content>
</Menu.Portal>
</Menu>
</Show>
</div>
</div>
</div>
</Show>
<div class="settings-workspaces-inventory">
<Show
when={filtered().length > 0}
fallback={
<div class="settings-workspaces-inventory" data-empty={filtered().length === 0}>
<SettingsList>
<div class="settings-workspaces-empty-motion" data-visible={filtered().length === 0}>
<div class="settings-workspaces-empty">
{language.t(
projectQuery.isPending
? "common.loading"
: projectQuery.isError
? "common.requestFailed"
: "settings.workspaces.empty",
)}
<Show
when={!projectQuery.isPending && !projectQuery.isError}
fallback={language.t(projectQuery.isPending ? "common.loading" : "common.requestFailed")}
>
<span class="settings-workspaces-empty-title">{language.t("settings.workspaces.empty")}</span>
<span>{language.t("settings.workspaces.empty.description")}</span>
</Show>
</div>
}
>
<SettingsList>
<For each={filtered()}>
{(workspace) => {
const linked = () => workspaceSessions(workspace)
return (
</div>
<Key each={filtered()} by={(workspace) => `${workspace.project.id}:${pathKey(workspace.directory)}`}>
{(workspace) => {
const linked = () => workspaceSessions(workspace())
const key = () => String(pathKey(workspace().directory))
const deleting = () => store.deleting.includes(key())
return (
<div class="settings-workspaces-row-motion" data-removing={store.removing.includes(key())}>
<div class="settings-workspaces-row">
<div class="settings-workspaces-row-header">
<div class="settings-workspaces-copy">
<div class="settings-workspaces-main">
<Tooltip
value={workspace.directory}
placement="top-start"
contentClass="max-w-[calc(100vw-32px)] break-all"
>
<span
tabIndex={0}
dir="ltr"
aria-label={workspace.directory}
class="settings-workspaces-path"
>
{workspace.directory}
</span>
</Tooltip>
<WorkspacePath directory={workspace().directory} />
</div>
<span class="settings-workspaces-meta">{sessionCount(workspace)}</span>
<span class="settings-workspaces-meta">{sessionCount(workspace())}</span>
</div>
<div class="settings-workspaces-row-actions">
<Show when={lastActive(workspace)}>
{(value) => (
<Tooltip value={language.t("settings.workspaces.lastActiveSession")} placement="top-end">
<span tabIndex={0} class="settings-workspaces-active">
{value()}
</span>
</Tooltip>
)}
<Show
when={deleting()}
fallback={
<>
<Show when={lastActive(workspace())}>
{(value) => (
<Tooltip
value={language.t("settings.workspaces.lastActiveSession")}
placement="top-end"
>
<span tabIndex={0} class="settings-workspaces-active">
{value()}
</span>
</Tooltip>
)}
</Show>
<IconButton
type="button"
variant="ghost-muted"
size="small"
aria-label={language.t("workspace.delete.confirm", {
name: getFilename(workspace().directory),
})}
disabled={!!store.transaction}
icon={<Icon name="outline-trash" size="small" />}
onClick={() => confirmDelete(workspace())}
/>
</>
}
>
<span class="settings-workspaces-active">{language.t("workspace.lifecycle.deleting")}</span>
</Show>
<IconButton
type="button"
variant="ghost-muted"
size="small"
aria-label={language.t("workspace.delete.confirm", {
name: getFilename(workspace.directory),
})}
disabled={!!store.transaction}
icon={<Icon name="trash" size="small" />}
onClick={() => confirmDelete(workspace)}
/>
</div>
</div>
<Show when={linked().length > 0}>
@@ -401,7 +460,7 @@ export const SettingsWorkspaces: Component<{ activeDirectory?: string }> = (prop
{(session) => (
<div class="settings-workspaces-session">
<span>{sessionLabel(session)}</span>
<Show when={sessionTime(session)}>
<Show when={linked().length > 1 ? sessionTime(session) : undefined}>
{(time) => <span class="settings-workspaces-session-time">{time()}</span>}
</Show>
</div>
@@ -410,18 +469,48 @@ export const SettingsWorkspaces: Component<{ activeDirectory?: string }> = (prop
</div>
</Show>
</div>
)
}}
</For>
</SettingsList>
</Show>
</div>
)
}}
</Key>
</SettingsList>
</div>
</div>
</>
)
}
function DialogDeleteAllWorkspaces(props: { count: number; project: string; onDelete: () => Promise<void> }) {
function WorkspacePath(props: { directory: string }) {
const [truncated, setTruncated] = createSignal(false)
const name = () => getFilename(props.directory)
return (
<Tooltip
value={props.directory}
placement="top-start"
disabled={!truncated()}
contentClass="max-w-[calc(100vw-32px)] break-all"
>
<span
ref={(element) => createResizeObserver(element, () => setTruncated(element.scrollWidth > element.clientWidth))}
tabIndex={truncated() ? 0 : undefined}
dir="ltr"
aria-label={props.directory}
class="settings-workspaces-path"
>
<span>{props.directory.slice(0, -name().length)}</span>
<span class="settings-workspaces-path-name">{name()}</span>
</span>
</Tooltip>
)
}
function DialogDeleteWorkspaces(props: {
title: string
confirmation: string
warning: string
onDelete: () => Promise<void>
}) {
const dialog = useDialog()
const language = useLanguage()
const remove = () => {
@@ -434,13 +523,12 @@ function DialogDeleteAllWorkspaces(props: { count: number; project: string; onDe
<Dialog fit>
<DialogHeader>
<DialogTitleGroup
title={language.t("settings.workspaces.deleteAll")}
title={props.title}
description={
<>
{language.t("settings.workspaces.deleteAll.confirm", { count: props.count })}
<br />
{language.t("settings.workspaces.deleteAll.warning", { count: props.count, project: props.project })}
</>
<div class="flex flex-col gap-2">
<div>{props.confirmation}</div>
<div>{props.warning}</div>
</div>
}
/>
</DialogHeader>
@@ -449,7 +537,7 @@ function DialogDeleteAllWorkspaces(props: { count: number; project: string; onDe
{language.t("common.cancel")}
</Button>
<Button type="button" variant="danger" onClick={remove}>
{language.t("settings.workspaces.deleteAll")}
{language.t("settings.workspaces.delete.button")}
</Button>
</DialogFooter>
</Dialog>
@@ -472,7 +560,7 @@ function DialogDeleteWorkspace(props: {
staleTime: 0,
}))
const descriptions = () => {
if (status.isPending) return [language.t("workspace.status.checking")]
if (status.isPending) return []
if (status.isError) return [language.t("workspace.status.error")]
if (!status.data) return []
return props.inspectionMessages(status.data.result)
@@ -487,18 +575,20 @@ function DialogDeleteWorkspace(props: {
<Dialog fit>
<DialogHeader>
<DialogTitleGroup
title={language.t("workspace.delete.title")}
title={language.t("workspace.delete.confirm", { name: getFilename(props.workspace.directory) })}
description={
<>
{language.t("workspace.delete.confirm", { name: getFilename(props.workspace.directory) })}
<br />
<code class="max-w-full rounded-[4px] bg-[color-mix(in_oklch,var(--v2-text-text-base)_8%,transparent)] px-1 py-0.5 font-mono text-xs font-medium leading-4 text-v2-text-text-base break-all">
{props.workspace.directory}
</code>
<br />
{language.t("settings.workspaces.delete.warning")}
<div class="flex flex-col gap-2">
<div class="flex flex-col gap-1">
<span class="text-11-regular text-v2-text-text-faint">
{language.t(status.isPending ? "workspace.status.checking" : "workspace.delete.location")}
</span>
<code class="block w-fit max-w-full rounded-[4px] bg-[color-mix(in_oklch,var(--v2-text-text-base)_8%,transparent)] px-1 py-0.5 font-mono text-xs font-medium leading-4 text-v2-text-text-base break-all">
{props.workspace.directory}
</code>
</div>
<div>{language.t("settings.workspaces.delete.warning")}</div>
<For each={descriptions()}>{(description) => <div>{description}</div>}</For>
</>
</div>
}
/>
</DialogHeader>
@@ -506,12 +596,7 @@ function DialogDeleteWorkspace(props: {
<Button type="button" variant="neutral" onClick={() => dialog.close()}>
{language.t("common.cancel")}
</Button>
<Button
type="button"
variant="danger"
disabled={status.isPending || status.isError || status.data?.result.active}
onClick={remove}
>
<Button type="button" variant="danger" disabled={status.isPending || status.isError} onClick={remove}>
{language.t("workspace.delete.button")}
</Button>
</DialogFooter>
+5 -1
View File
@@ -605,7 +605,10 @@ export type SessionLogOutput =
readonly type: "session.execution.interrupted"
readonly durable: { readonly aggregateID: string; readonly seq: Event.Seq; readonly version: Event.Version }
readonly location?: Location.Ref | undefined
readonly data: { readonly sessionID: Session.ID; readonly reason: "user" | "shutdown" | "superseded" }
readonly data: {
readonly sessionID: Session.ID
readonly reason: "user" | "shutdown" | "superseded" | "inactivity"
}
}
| {
readonly id: Event.ID
@@ -1426,6 +1429,7 @@ export type ProjectListOperation<E = never> = () => Effect.Effect<ProjectListOut
export type ProjectUpdateInput = {
readonly projectID: Project.ID
readonly canonical?: AbsolutePath | undefined
readonly name?: string | undefined
readonly icon?: Project.Icon | undefined
readonly commands?: Project.Commands | undefined
@@ -1018,7 +1018,7 @@ const EndpointProjectUpdate = (raw: RawClient["server.project"]) => (input: Proj
preserveEffect<ProjectUpdateOutput>()(
raw["project.update"]({
params: { projectID: input["projectID"] },
payload: { name: input["name"], icon: input["icon"], commands: input["commands"] },
payload: { canonical: input["canonical"], name: input["name"], icon: input["icon"], commands: input["commands"] },
}).pipe(Effect.mapError(mapClientError)),
)
@@ -1377,7 +1377,12 @@ export function make(options: ClientOptions) {
{
method: "PATCH",
path: `/api/project/${encodeURIComponent(input.projectID)}`,
body: { name: input["name"], icon: input["icon"], commands: input["commands"] },
body: {
canonical: input["canonical"],
name: input["name"],
icon: input["icon"],
commands: input["commands"],
},
successStatus: 200,
declaredStatuses: [400, 401, 404],
empty: false,
+10 -1
View File
@@ -710,7 +710,7 @@ export type SessionExecutionInterrupted = {
type: "session.execution.interrupted"
durable: { aggregateID: string; seq: number; version: 1 }
location?: LocationRef
data: { sessionID: string; reason: "user" | "shutdown" | "superseded" }
data: { sessionID: string; reason: "user" | "shutdown" | "superseded" | "inactivity" }
}
export type SessionInstructionsUpdated = {
@@ -4672,17 +4672,26 @@ export type ProjectListOutput = Array<Project>
export type ProjectUpdateInput = {
readonly projectID: { readonly projectID: string }["projectID"]
readonly canonical?: {
readonly canonical?: string
readonly name?: string
readonly icon?: { readonly url?: string; readonly override?: string; readonly color?: string }
readonly commands?: { readonly start?: string }
}["canonical"]
readonly name?: {
readonly canonical?: string
readonly name?: string
readonly icon?: { readonly url?: string; readonly override?: string; readonly color?: string }
readonly commands?: { readonly start?: string }
}["name"]
readonly icon?: {
readonly canonical?: string
readonly name?: string
readonly icon?: { readonly url?: string; readonly override?: string; readonly color?: string }
readonly commands?: { readonly start?: string }
}["icon"]
readonly commands?: {
readonly canonical?: string
readonly name?: string
readonly icon?: { readonly url?: string; readonly override?: string; readonly color?: string }
readonly commands?: { readonly start?: string }
+83 -11
View File
@@ -1,13 +1,14 @@
export * as Credential from "./credential.js"
import { asc, desc, eq } from "drizzle-orm"
import { Context, Effect, Layer, Schema } from "effect"
import { Cause, Context, Effect, Layer, Schema } from "effect"
import { Credential } from "@opencode-ai/schema/credential"
import { Integration } from "@opencode-ai/schema/integration"
import { Database } from "./database/database.js"
import { Bus } from "./bus.js"
import { makeGlobalNode } from "@opencode-ai/util/effect/app-node"
import { CredentialTable } from "./credential/sql.js"
import { ErrorSummary } from "./util/error-summary.js"
export const ID = Credential.ID
export type ID = Credential.ID
@@ -123,7 +124,21 @@ const layer = Layer.effect(
.run()
}),
)
.pipe(Effect.orDie)
.pipe(
Effect.onError((cause) =>
Effect.logError("credential create failed", {
credentialID: credential.id,
integrationID: credential.integrationID,
errors: ErrorSummary.from(Cause.squash(cause)),
}),
),
Effect.orDie,
)
yield* Effect.logInfo("credential created", {
credentialID: credential.id,
integrationID: credential.integrationID,
type: credential.value.type,
})
yield* bus.publish(Event.Updated, {}, { global: true })
yield* bus.publish(
Event.Switched,
@@ -154,8 +169,19 @@ const layer = Layer.effect(
return credential.integration_id
}),
)
.pipe(Effect.orDie)
if (integrationID) yield* bus.publish(Event.Switched, { integrationID, credentialID: id }, { global: true })
.pipe(
Effect.onError((cause) =>
Effect.logError("credential activate failed", {
credentialID: id,
errors: ErrorSummary.from(Cause.squash(cause)),
}),
),
Effect.orDie,
)
if (integrationID) {
yield* Effect.logInfo("credential activated", { integrationID, credentialID: id })
yield* bus.publish(Event.Switched, { integrationID, credentialID: id }, { global: true })
}
}),
update: Effect.fn("Credential.update")(function* (id, updates) {
if (updates.label === undefined && updates.value === undefined) return
@@ -164,15 +190,46 @@ const layer = Layer.effect(
.from(CredentialTable)
.where(eq(CredentialTable.id, id))
.get()
.pipe(Effect.orDie)
if (!credential?.integrationID) return
.pipe(
Effect.onError((cause) =>
Effect.logError("credential update lookup failed", {
credentialID: id,
errors: ErrorSummary.from(Cause.squash(cause)),
}),
),
Effect.orDie,
)
if (!credential?.integrationID) {
yield* Effect.logWarning("credential update skipped", { credentialID: id, reason: "credential_missing" })
return
}
if (updates.label === credential.label && updates.value === undefined) return
yield* db
const updated = yield* db
.update(CredentialTable)
.set({ label: updates.label, value: updates.value })
.where(eq(CredentialTable.id, id))
.run()
.pipe(Effect.orDie)
.returning({ id: CredentialTable.id })
.get()
.pipe(
Effect.onError((cause) =>
Effect.logError("credential update failed", {
credentialID: id,
integrationID: credential.integrationID,
errors: ErrorSummary.from(Cause.squash(cause)),
}),
),
Effect.orDie,
)
if (!updated) {
yield* Effect.logWarning("credential update skipped", { credentialID: id, reason: "credential_removed" })
return
}
yield* Effect.logInfo("credential updated", {
credentialID: id,
integrationID: credential.integrationID,
valueChanged: updates.value !== undefined,
labelChanged: updates.label !== undefined && updates.label !== credential.label,
})
if (updates.label !== undefined && updates.label !== credential.label)
yield* bus.publish(Event.Updated, {}, { global: true })
}),
@@ -191,7 +248,8 @@ const layer = Layer.effect(
.get()
: undefined
yield* tx.delete(CredentialTable).where(eq(CredentialTable.id, id)).run()
if (!credential.integration_id || active?.id !== id) return { switched: false as const }
if (!credential.integration_id || active?.id !== id)
return { switched: false as const, integrationID: credential.integration_id }
const replacement = yield* tx
.select({ id: CredentialTable.id })
.from(CredentialTable)
@@ -217,8 +275,22 @@ const layer = Layer.effect(
}
}),
)
.pipe(Effect.orDie)
.pipe(
Effect.onError((cause) =>
Effect.logError("credential remove failed", {
credentialID: id,
errors: ErrorSummary.from(Cause.squash(cause)),
}),
),
Effect.orDie,
)
if (!removed) return
yield* Effect.logInfo("credential removed", {
credentialID: id,
integrationID: removed.integrationID,
active: removed.switched,
...(removed.switched ? { replacementID: removed.credentialID } : {}),
})
yield* bus.publish(Event.Updated, {}, { global: true })
if (removed.switched)
yield* bus.publish(
+37 -15
View File
@@ -5,6 +5,8 @@ import { Bus } from "./bus.js"
import { Location } from "./location.js"
import { LocationServiceMap } from "./location-service-map.js"
import { SessionEvent } from "./session/event.js"
import { SessionExecution } from "./session/execution.js"
import { SessionStore } from "./session/store.js"
import { makeGlobalNode } from "@opencode-ai/util/effect/app-node"
const isSessionEvent = Schema.is(SessionEvent.Durable)
@@ -18,9 +20,11 @@ export function layer(options: { readonly timeToLive?: Duration.Input; readonly
const clock = yield* Clock.Clock
const bus = yield* Bus.Service
const locations = yield* LocationServiceMap.Service
const execution = yield* SessionExecution.Service
const sessions = yield* SessionStore.Service
const timeToLive = Duration.toMillis(options.timeToLive ?? "60 minutes")
const entries = new Map<string, { readonly ref: Location.Ref; expiresAt: number }>()
const key = (ref: Location.Ref) => `${ref.directory}\0${ref.workspaceID ?? ""}`
const key = (ref: Location.Ref) => `${LocationServiceMap.canonical(ref).directory}\0${ref.workspaceID ?? ""}`
const touch = (ref: Location.Ref) =>
Effect.sync(() => {
entries.set(key(ref), { ref, expiresAt: clock.currentTimeMillisUnsafe() + timeToLive })
@@ -39,26 +43,44 @@ export function layer(options: { readonly timeToLive?: Duration.Input; readonly
yield* Effect.sleep(options.sweepInterval ?? "1 minute")
const refs = Array.from(yield* RcMap.keys(locations.rcMap))
const cached = new Set(refs.map(key))
yield* Effect.forEach(
refs,
(ref) => (entries.has(key(ref)) ? Effect.void : touch(ref)),
{ discard: true },
)
yield* Effect.forEach(refs, (ref) => (entries.has(key(ref)) ? Effect.void : touch(ref)), { discard: true })
for (const id of entries.keys()) {
if (!cached.has(id)) entries.delete(id)
}
const now = clock.currentTimeMillisUnsafe()
const expired = Array.from(entries.values()).filter((entry) => entry.expiresAt <= now)
if (expired.length === 0) return
const active = yield* Effect.forEach(yield* execution.active, (sessionID) => sessions.get(sessionID))
yield* Effect.forEach(
expired,
(entry) => {
entries.delete(key(entry.ref))
return Effect.logInfo("location services evicted", {
directory: entry.ref.directory,
workspaceID: entry.ref.workspaceID,
}).pipe(Effect.andThen(locations.invalidate(entry.ref)))
},
{ discard: true },
(entry) =>
Effect.gen(function* () {
const owners = active.flatMap((session) =>
session && key(session.location) === key(entry.ref) ? [session] : [],
)
// Invalidation only detaches the cache entry; borrowers retain the old
// graph. Stop its executions and settle tool cleanup before detaching it.
yield* Effect.forEach(
owners,
(session) => execution.interrupt(session.id, { reason: "inactivity", awaitSettlement: true }),
{
discard: true,
concurrency: "unbounded",
},
)
const remaining = yield* Effect.forEach(yield* execution.active, (sessionID) => sessions.get(sessionID))
// New work admitted during cleanup may now own the cached graph.
if (remaining.some((session) => session && key(session.location) === key(entry.ref))) {
yield* touch(entry.ref)
return
}
entries.delete(key(entry.ref))
yield* Effect.logInfo("location services evicted", {
directory: entry.ref.directory,
workspaceID: entry.ref.workspaceID,
}).pipe(Effect.andThen(locations.invalidate(entry.ref)))
}),
{ discard: true, concurrency: "unbounded" },
)
}).pipe(Effect.forever, Effect.forkScoped)
@@ -70,5 +92,5 @@ export function layer(options: { readonly timeToLive?: Duration.Input; readonly
export const node = makeGlobalNode({
service: Service,
layer: layer(),
deps: [Bus.node, LocationServiceMap.node],
deps: [Bus.node, LocationServiceMap.node, SessionExecution.node, SessionStore.node],
})
+3
View File
@@ -231,6 +231,8 @@ export const connect = Effect.fnUntraced(function* (
}
if (!URL.canParse(config.url))
return yield* new ConnectError({ server, message: `Invalid MCP URL for "${server}"` })
const { McpOAuth } = yield* Effect.promise(() => import("./oauth.js"))
const fetch = yield* McpOAuth.loggedFetch({ server, directory })
// Prefer raw tools for our Code Mode without changing the configured URL used for OAuth identity.
const url = new URL(config.url)
const addedCodemode = config.codemode !== false && !url.searchParams.has("codemode")
@@ -240,6 +242,7 @@ export const connect = Effect.fnUntraced(function* (
new StreamableHTTPClientTransport(url, {
requestInit: config.headers ? { headers: config.headers } : undefined,
authProvider,
fetch,
}),
)
+49 -8
View File
@@ -259,27 +259,41 @@ export const layer = (options?: Options) =>
const { McpOAuth } = yield* Effect.promise(() => import("./oauth.js"))
const remote = entry.config
const oauth = remote.oauth || undefined
const run = Effect.runPromiseWith(yield* Effect.context())
const base = {
redirectUrl: oauth?.redirect_uri ?? "http://127.0.0.1/callback",
scope: oauth?.scope,
client: oauth?.client_id ? { id: oauth.client_id, secret: oauth.client_secret } : undefined,
// No browser during connect: an auth-gated server surfaces needs_auth instead of opening a browser.
onRedirect: () => {},
onRedirect: () => run(Effect.logInfo("mcp oauth authorization required")),
}
const found = (yield* credentials.list(entry.integrationID)).at(-1)
if (!found || found.value.type !== "oauth")
if (!found || found.value.type !== "oauth") {
// No stored credential yet: an empty in-memory store still lets the SDK run the auth handshake, which
// ends in UnauthorizedError -> needs_auth. Returning no provider instead would let the transport throw
// a raw HTTP error, hiding the auth requirement behind a generic failed status. Anonymous servers are
// unaffected: tokens() returns undefined, so no auth header is sent and the SDK never calls auth().
yield* Effect.logInfo("mcp oauth credential unavailable", {
integrationID: entry.integrationID,
reason: found ? "not_oauth" : "missing",
})
return McpOAuth.provider({ ...base, store: McpOAuth.memoryStore() })
}
const credentialID = found.id
const methodID = found.value.methodID
const fields = { credentialID, integrationID: entry.integrationID }
yield* Effect.logInfo("mcp oauth credential loaded", {
...fields,
hasRefreshToken: Boolean(found.value.refresh),
hasClientInformation: Boolean(McpOAuth.clientFromCredential(found.value)),
expiresAt: found.value.expires,
expired: found.value.expires !== 0 && found.value.expires <= Date.now(),
})
// Tracks the refresh token this provider last presented, so invalidate can tell whether the SDK
// rejected the currently-stored credential or a snapshot another connection has already rotated past.
let presented = found.value.refresh
const readOAuthCredential = async () => {
const stored = await Effect.runPromise(credentials.get(credentialID))
const stored = await run(credentials.get(credentialID))
return stored?.value.type === "oauth" ? stored.value : undefined
}
return McpOAuth.provider({
@@ -290,10 +304,25 @@ export const layer = (options?: Options) =>
// strand every connection in needs_auth until a manual re-auth. Credential deletion notifies all locations;
// reconnects remain serialized by the server lock.
invalidate: async (scope) => {
if (scope === "verifier" || scope === "discovery") return
if (scope === "verifier" || scope === "discovery") {
await run(
Effect.logDebug("mcp oauth invalidation skipped", { ...fields, scope, reason: "not_credentials" }),
)
return
}
const oauth = await readOAuthCredential()
if (!oauth || oauth.refresh !== presented) return
await Effect.runPromise(credentials.remove(credentialID))
if (!oauth || oauth.refresh !== presented) {
await run(
Effect.logInfo("mcp oauth invalidation skipped", {
...fields,
scope,
reason: oauth ? "token_rotated" : "credential_missing",
}),
)
return
}
await run(Effect.logWarning("mcp oauth credential invalidation requested", { ...fields, scope }))
await run(credentials.remove(credentialID))
},
// Always read the latest stored tokens instead of caching at connect time: with refresh-token rotation,
// a cached snapshot goes stale the moment another connection refreshes, and re-presenting the consumed
@@ -314,7 +343,16 @@ export const layer = (options?: Options) =>
client: previous ? McpOAuth.clientFromCredential(previous) : undefined,
})
presented = value.refresh
await Effect.runPromise(credentials.update(credentialID, { value }))
await run(
Effect.logInfo("mcp oauth tokens received", {
...fields,
credentialPresent: Boolean(previous),
refreshRotated: Boolean(previous && previous.refresh !== value.refresh),
hasRefreshToken: Boolean(value.refresh),
expiresAt: value.expires,
}),
)
await run(credentials.update(credentialID, { value }))
},
clientInformation: async () => {
const oauth = await readOAuthCredential()
@@ -560,7 +598,10 @@ export const layer = (options?: Options) =>
: { status: "failed", error: error instanceof Error ? error.message : String(error) }
yield* Effect.logWarning("mcp connect failed", { server: name, status: entry.status })
yield* bus.publish(McpEvent.StatusChanged, { server: name })
}).pipe(Effect.ensuring(entry.startup.open))
}).pipe(
Effect.ensuring(entry.startup.open),
Effect.annotateLogs({ server: name, directory: location.directory, connectionID: crypto.randomUUID() }),
)
const stopServer = Effect.fnUntraced(function* (name: ServerName, entry: ServerEntry) {
const scope = entry.scope
+90 -9
View File
@@ -1,12 +1,63 @@
export * as McpOAuth from "./oauth.js"
import { auth, type OAuthClientProvider } from "@modelcontextprotocol/sdk/client/auth.js"
import { auth, parseErrorResponse, type OAuthClientProvider } from "@modelcontextprotocol/sdk/client/auth.js"
import type { OAuthClientInformationMixed, OAuthTokens } from "@modelcontextprotocol/sdk/shared/auth.js"
import { Deferred, Effect } from "effect"
import type { FetchLike } from "@modelcontextprotocol/sdk/shared/transport.js"
import { Cause, Deferred, Effect } from "effect"
import { Credential } from "@opencode-ai/schema/credential"
import { ConfigMCP } from "@opencode-ai/schema/config/mcp"
import { OauthCallbackPage } from "../oauth/page.js"
import type { Integration } from "../integration.js"
import { ErrorSummary } from "../util/error-summary.js"
/** Observe OAuth failures before the SDK handles them by invalidating credentials or redirecting. */
export const loggedFetch = (fields: { readonly server: string; readonly directory?: string }) =>
Effect.gen(function* () {
const run = Effect.runPromiseWith(yield* Effect.context())
const request: FetchLike = (url, init) => {
const grant = init?.body instanceof URLSearchParams ? init.body.get("grant_type") : undefined
const operation = grant === "refresh_token" ? "refresh" : grant === "authorization_code" ? "exchange" : undefined
const started = Date.now()
return run(
Effect.gen(function* () {
if (operation) yield* Effect.logInfo("mcp oauth request started")
const response = yield* Effect.tryPromise({ try: () => fetch(url, init), catch: (error) => error })
const result = { status: response.status, durationMs: Date.now() - started }
if (operation && !response.ok) {
// Only retain the SDK's standard error code. Descriptions and raw bodies can echo credentials.
const error = yield* Effect.tryPromise(async () => parseErrorResponse(await response.clone().text())).pipe(
Effect.map((error) => error.errorCode),
Effect.orElseSucceed(() => "unreadable_response"),
)
yield* Effect.logWarning("mcp oauth request rejected", { ...result, error })
}
if (operation && response.ok) {
yield* Effect.logInfo("mcp oauth request succeeded", result)
}
if (!operation && (response.status === 401 || response.status === 403)) {
yield* Effect.logWarning("mcp http authentication rejected", result)
}
return response
}).pipe(
Effect.onError((cause) => {
if (init?.signal?.aborted) return Effect.logDebug("mcp http request aborted")
return Effect.logWarning("mcp http request failed", {
errors: ErrorSummary.from(Cause.squash(cause)),
durationMs: Date.now() - started,
})
}),
Effect.annotateLogs({
...fields,
requestID: crypto.randomUUID(),
origin: new URL(url).origin,
method: init?.method ?? "GET",
...(operation ? { operation } : {}),
}),
),
)
}
return request
})
/** Persists the OAuth artifacts for one MCP server session: DCR client info, PKCE verifier, and tokens. */
export interface Store {
@@ -145,6 +196,12 @@ export const authorize = (input: {
readonly methodID: Integration.MethodID
}) =>
Effect.gen(function* () {
const fields = { server: input.name, methodID: input.methodID, oauthAttemptID: crypto.randomUUID() }
const context = yield* Effect.context()
const run = Effect.runPromiseWith(context)
const runFork = Effect.runForkWith(context)
const fetchFn = yield* loggedFetch({ server: input.name }).pipe(Effect.annotateLogs(fields))
yield* Effect.logInfo("mcp oauth authorization started", fields)
const oauth = input.config.oauth || undefined
const store = memoryStore()
const code = yield* Deferred.make<string, Error>()
@@ -160,19 +217,20 @@ export const authorize = (input: {
response.writeHead(404).end("Not found")
return
}
const fail = (reason: string) => {
const fail = (reason: string, failure: string) => {
runFork(Effect.logWarning("mcp oauth callback rejected", { ...fields, reason: failure }))
Effect.runFork(Deferred.fail(code, new Error(reason)))
response
.writeHead(400, { "Content-Type": "text/html" })
.end(OauthCallbackPage.error(reason, { provider: input.name }))
}
const error = url.searchParams.get("error_description") ?? url.searchParams.get("error")
if (error) return fail(error)
if (error) return fail(error, "authorization_error")
// Reject a redirect whose state does not match what we issued: this is the CSRF defense the
// state parameter exists for, so an attacker can't inject their own authorization code.
if (url.searchParams.get("state") !== state) return fail("OAuth state mismatch")
if (url.searchParams.get("state") !== state) return fail("OAuth state mismatch", "state_mismatch")
const value = url.searchParams.get("code")
if (!value) return fail("Missing authorization code")
if (!value) return fail("Missing authorization code", "missing_code")
Effect.runFork(Deferred.succeed(code, value))
response.writeHead(200, { "Content-Type": "text/html" }).end(OauthCallbackPage.success({ provider: input.name }))
})
@@ -202,6 +260,7 @@ export const authorize = (input: {
client: oauth?.client_id ? { id: oauth.client_id, secret: oauth.client_secret } : undefined,
onRedirect: (url) => {
authorizationUrl = url
return run(Effect.logInfo("mcp oauth awaiting authorization", fields))
},
store,
})
@@ -210,11 +269,16 @@ export const authorize = (input: {
const tokens = yield* Effect.promise(() => store.tokens())
if (!tokens) return yield* Effect.fail(new Error(`MCP server "${input.name}" did not return OAuth tokens`))
const client = yield* Effect.promise(() => store.clientInformation())
yield* Effect.logInfo("mcp oauth authorization completed", {
...fields,
hasRefreshToken: Boolean(tokens.refresh_token),
expiresIn: tokens.expires_in,
})
return toCredential({ methodID: input.methodID, serverUrl: input.config.url, tokens, client })
})
yield* Effect.tryPromise({
try: () => auth(oauthProvider, { serverUrl: input.config.url, scope: oauth?.scope }),
try: () => auth(oauthProvider, { serverUrl: input.config.url, scope: oauth?.scope, fetchFn }),
catch: (error) => (error instanceof Error ? error : new Error(String(error))),
})
@@ -229,11 +293,28 @@ export const authorize = (input: {
Effect.flatMap((value) =>
Effect.tryPromise({
try: () =>
auth(oauthProvider, { serverUrl: input.config.url, authorizationCode: value, scope: oauth?.scope }),
auth(oauthProvider, {
serverUrl: input.config.url,
authorizationCode: value,
scope: oauth?.scope,
fetchFn,
}),
catch: (error) => (error instanceof Error ? error : new Error(String(error))),
}),
),
Effect.flatMap(() => finalize),
Effect.onError((cause) =>
Effect.logWarning("mcp oauth authorization failed", { errors: ErrorSummary.from(Cause.squash(cause)) }),
),
Effect.annotateLogs(fields),
),
}
})
}).pipe(
Effect.onError((cause) =>
Effect.logWarning("mcp oauth authorization setup failed", {
server: input.name,
methodID: input.methodID,
errors: ErrorSummary.from(Cause.squash(cause)),
}),
),
)
+2 -1
View File
@@ -52,7 +52,7 @@ export const call = <F extends Schema.Struct.Fields, R extends Schema.Struct.Fie
}),
)
return yield* Effect.gen(function* () {
const response = yield* HttpClient.filterStatusOk(http).execute(request)
const response = yield* HttpClient.withScope(HttpClient.filterStatusOk(http)).execute(request)
const body = yield* collectBoundedResponseBody(
response,
MAX_RESPONSE_BYTES,
@@ -60,6 +60,7 @@ export const call = <F extends Schema.Struct.Fields, R extends Schema.Struct.Fie
)
return yield* parseResponse(body.toString("utf8"), schema.output)
}).pipe(
Effect.scoped,
Effect.timeoutOrElse({
duration: Duration.seconds(25),
orElse: () => Effect.fail(new Error(`${tool} request timed out`)),
+2 -1
View File
@@ -63,10 +63,11 @@ export const Plugin = define<HttpClient.HttpClient | Scope.Scope>({
max_results: 8,
}),
)
const response = yield* HttpClient.filterStatusOk(http)
const response = yield* HttpClient.withScope(HttpClient.filterStatusOk(http))
.execute(request)
.pipe(
Effect.flatMap(HttpClientResponse.schemaBodyJson(SearchResponse)),
Effect.scoped,
Effect.timeoutOrElse({
duration: Duration.seconds(25),
orElse: () => Effect.fail(new Error("Tavily web search request timed out")),
+1
View File
@@ -212,6 +212,7 @@ const layer = Layer.effect(
const row = yield* db
.update(ProjectTable)
.set({
worktree: input.canonical,
name: input.name === undefined ? undefined : input.name || null,
icon_url_override: input.icon?.override === undefined ? undefined : input.icon.override || null,
icon_color: input.icon?.color === undefined ? undefined : input.icon.color || null,
+14 -7
View File
@@ -28,9 +28,17 @@ export interface Interface {
* Interrupt active work owned by this process. Idle interruption is a no-op. Resolves once
* the interruption is accepted; cleanup settles asynchronously in the execution fiber.
* Returns whether an active execution was interrupted. Compose with `awaitIdle` when
* settlement matters.
* settlement matters. `awaitSettlement` waits only for the interrupted execution,
* rather than fresh work admitted during its cleanup.
*/
readonly interrupt: (sessionID: SessionSchema.ID, options?: { readonly continue?: boolean }) => Effect.Effect<boolean>
readonly interrupt: (
sessionID: SessionSchema.ID,
options?: {
readonly continue?: boolean
readonly reason?: "user" | "inactivity"
readonly awaitSettlement?: boolean
},
) => Effect.Effect<boolean>
/** Resolves once this process owns no active execution for the Session. Returns immediately when idle and never starts work. */
readonly awaitIdle: (sessionID: SessionSchema.ID) => Effect.Effect<void>
}
@@ -38,7 +46,7 @@ export interface Interface {
/** Routes execution from a Session ID to its selected instance's runner. */
export class Service extends Context.Service<Service, Interface>()("@opencode/SessionExecution") {}
type InterruptReason = "user" | "shutdown"
type InterruptReason = "user" | "shutdown" | "inactivity"
export function terminal(exit: Exit.Exit<void, SessionRunner.RunError>, reason?: InterruptReason) {
if (Exit.isSuccess(exit)) return { type: "succeeded" as const }
@@ -120,9 +128,8 @@ export const layer = Layer.effect(
return
}
if (outcome.type === "interrupted") {
// A user cancel releases the claim: the turn must not resurrect at the next
// boot. Shutdown interruption keeps it for restart continuity.
if (outcome.reason === "user") yield* jobs.cancel(sessionID)
// Deliberate stops release the claim; shutdown keeps it for restart continuity.
if (outcome.reason !== "shutdown") yield* jobs.cancel(sessionID)
yield* bus.publish(
SessionEvent.Execution.Interrupted,
{ sessionID, reason: outcome.reason },
@@ -147,7 +154,7 @@ export const layer = Layer.effect(
isActive: coordinator.isActive,
interrupt: (sessionID, options) =>
Effect.gen(function* () {
const interrupted = yield* coordinator.interrupt(sessionID, "user")
const interrupted = yield* coordinator.interrupt(sessionID, options?.reason ?? "user", options)
if (!options?.continue) return interrupted
// Resume steering input and between-turn control work from the interrupted
// intent. Queued next-turn prompts stay parked: a steer-scoped drain never
+25 -3
View File
@@ -17,9 +17,14 @@ export interface Coordinator<Key, E, Reason = never> {
* Stops the active execution and clears its doorbell. No-op when idle. Resolves once the
* interruption is accepted, not when cleanup settles: the execution fiber finishes its
* finalizers and settled hook on its own time. Returns whether an active execution was
* interrupted. Compose with `awaitIdle` for settlement.
* interrupted. `awaitSettlement` waits for this execution's cleanup and settled hook,
* without following fresh work admitted during cleanup. `awaitIdle` follows successors too.
*/
readonly interrupt: (key: Key, reason?: Reason) => Effect.Effect<boolean>
readonly interrupt: (
key: Key,
reason?: Reason,
options?: { readonly awaitSettlement?: boolean },
) => Effect.Effect<boolean>
/** Resolves once no execution is active for the key. Returns immediately when already idle and never starts work. */
readonly awaitIdle: (key: Key) => Effect.Effect<void>
}
@@ -170,5 +175,22 @@ export const make = <Key, E, Reason = never>(options: {
return Deferred.await(execution.done).pipe(Effect.ignoreCause, Effect.andThen(awaitIdle(key)))
})
return { active: Effect.sync(() => new Set(executions.keys())), isActive, run, wake, interrupt, awaitIdle }
return {
active: Effect.sync(() => new Set(executions.keys())),
isActive,
run,
wake,
interrupt: (key, reason, options) =>
Effect.suspend(() => {
const execution = executions.get(key)
return interrupt(key, reason).pipe(
Effect.tap(() =>
options?.awaitSettlement && execution
? Deferred.await(execution.done).pipe(Effect.ignoreCause)
: Effect.void,
),
)
}),
awaitIdle,
}
})
+104 -120
View File
@@ -11,6 +11,10 @@ import { WebSearch } from "../../websearch.js"
export const name = "websearch"
export const NO_RESULTS = "No search results found. Please try a different query."
const providerSelectionLock = Semaphore.makeUnsafe(1)
const httpErrors = new Map([
[429, "Web search rate limited (HTTP 429)"],
[401, "Web search authentication failed (HTTP 401)"],
])
export const description = `Search the web using the user's selected search integration. Use this for current information beyond knowledge cutoff.
@@ -50,106 +54,100 @@ export const Plugin = {
agent: context.agent,
source: { type: "tool", messageID: context.messageID, id: context.id },
})
const search = (): Effect.Effect<Effect.Success<ReturnType<typeof ctx.websearch.query>>, unknown> =>
websearch.default().pipe(
Effect.flatMap((provider) => {
if (!provider) return ctx.websearch.query(input)
return context
.progress({ provider: provider.id })
.pipe(Effect.andThen(ctx.websearch.query({ ...input, providerID: provider.id })))
}),
Effect.catch((error) => {
if (!Schema.is(WebSearch.ProviderRequiredError)(error)) return Effect.fail(error)
return providerSelectionLock
.withPermit(
Effect.gen(function* () {
if (yield* websearch.default()) return
const providers = (yield* ctx.websearch.providers()).data
const defaultProvider = providers[0]
if (!defaultProvider) return yield* new WebSearch.ProviderRequiredError()
const response = yield* forms.ask({
sessionID: context.sessionID,
title: "Web Search",
metadata: { kind: "websearch.provider" },
fields: [
{
key: "choice",
description: "Allow OpenCode to search the web for up-to-date information?",
type: "string",
required: true,
custom: false,
options: [
{
value: "allow",
label: `Allow search via ${providers.map((provider) => provider.name).join(", ")}`,
},
{
value: "choose",
label: "Choose another provider",
},
{ value: "disable", label: "Disable web search" },
],
},
],
})
if (response.status === "cancelled")
return yield* Effect.fail(new Error("Web search cancelled"))
if (response.answer.choice === "disable") {
yield* websearch.select(false)
return yield* new WebSearch.DisabledError()
}
const selection =
response.answer.choice === "choose"
? yield* forms.ask({
sessionID: context.sessionID,
title: "Choose a web search provider",
metadata: { kind: "websearch.provider" },
fields: [
{
key: "provider",
description: "Choose a provider for web search.",
type: "string",
required: true,
custom: false,
options: providers.map((provider) => ({
value: provider.id,
label: provider.name,
})),
},
],
})
: undefined
if (selection?.status === "cancelled")
return yield* Effect.fail(new Error("Web search cancelled"))
const providerID = selection?.answer.provider ?? "random"
if (
typeof providerID !== "string" ||
(providerID !== "random" && !providers.some((provider) => provider.id === providerID))
)
return yield* new WebSearch.ProviderRequiredError()
yield* websearch.select(providerID === "random" ? "random" : WebSearch.ID.make(providerID))
if (providerID !== "random") return WebSearch.ID.make(providerID)
return providers[Math.floor(Math.random() * providers.length)]?.id
}),
)
.pipe(
Effect.timeoutOrElse({
duration: "1 minute",
orElse: () => Effect.fail(new Error("Web search cancelled")),
}),
Effect.flatMap((providerID) => {
if (!providerID) return Effect.suspend(search)
return context
.progress({ provider: providerID })
.pipe(Effect.andThen(ctx.websearch.query({ ...input, providerID })))
}),
)
}),
const search = (providerID?: WebSearch.ID) =>
websearch.query(
{ ...input, providerID },
{
sessionID: context.sessionID,
onProvider: (provider) => context.progress({ provider: provider.id }),
},
)
const result = yield* search()
const result = yield* search().pipe(
Effect.catchTag("WebSearch.ProviderRequired", () => {
return providerSelectionLock
.withPermit(
Effect.gen(function* () {
if (yield* websearch.default()) return
const providers = (yield* ctx.websearch.providers()).data
const defaultProvider = providers[0]
if (!defaultProvider) return yield* new WebSearch.ProviderRequiredError()
const response = yield* forms.ask({
sessionID: context.sessionID,
title: "Web Search",
metadata: { kind: "websearch.provider" },
fields: [
{
key: "choice",
description: "Allow OpenCode to search the web for up-to-date information?",
type: "string",
required: true,
custom: false,
options: [
{
value: "allow",
label: `Allow search via ${providers.map((provider) => provider.name).join(", ")}`,
},
{
value: "choose",
label: "Choose another provider",
},
{ value: "disable", label: "Disable web search" },
],
},
],
})
if (response.status === "cancelled")
return yield* Effect.fail(new Error("Web search cancelled"))
if (response.answer.choice === "disable") {
yield* websearch.select(false)
return yield* new WebSearch.DisabledError()
}
const selection =
response.answer.choice === "choose"
? yield* forms.ask({
sessionID: context.sessionID,
title: "Choose a web search provider",
metadata: { kind: "websearch.provider" },
fields: [
{
key: "provider",
description: "Choose a provider for web search.",
type: "string",
required: true,
custom: false,
options: providers.map((provider) => ({
value: provider.id,
label: provider.name,
})),
},
],
})
: undefined
if (selection?.status === "cancelled")
return yield* Effect.fail(new Error("Web search cancelled"))
const providerID = selection?.answer.provider ?? "random"
if (providerID === "random") {
yield* websearch.select("random")
return
}
const provider = providers.find((provider) => provider.id === providerID)
if (!provider) return yield* new WebSearch.ProviderRequiredError()
yield* websearch.select(provider.id)
return provider.id
}),
)
.pipe(
Effect.timeoutOrElse({
duration: "1 minute",
orElse: () => Effect.fail(new Error("Web search cancelled")),
}),
Effect.flatMap(search),
)
}),
)
const output = {
provider: result.data.providerID,
results: result.data.results,
provider: result.providerID,
results: result.results,
}
const content = output.results.length
? output.results
@@ -168,28 +166,14 @@ export const Plugin = {
const fallback = `Unable to search the web for ${input.query}`
if (!Schema.is(WebSearch.RequestError)(error)) return new ToolFailure({ message: fallback, error })
const status = HttpClientError.isHttpClientError(error.cause) ? error.cause.response?.status : undefined
switch (status) {
case 429:
return new ToolFailure({
message: "Web search rate limited (HTTP 429)",
error,
metadata: { provider: error.providerID },
})
case 401:
return new ToolFailure({
message: "Web search authentication failed (HTTP 401)",
error,
metadata: { provider: error.providerID },
})
case undefined:
return new ToolFailure({ message: fallback, error, metadata: { provider: error.providerID } })
default:
return new ToolFailure({
message: `Web search request failed (HTTP ${status})`,
error,
metadata: { provider: error.providerID },
})
}
return new ToolFailure({
message:
status === undefined
? fallback
: (httpErrors.get(status) ?? `Web search request failed (HTTP ${status})`),
error,
metadata: { provider: error.providerID },
})
}),
),
}),
+31
View File
@@ -0,0 +1,31 @@
export * as ErrorSummary from "./error-summary.js"
import { Option, Schema } from "effect"
const decode = Schema.decodeUnknownOption(
Schema.Struct({
name: Schema.optional(Schema.String),
_tag: Schema.optional(Schema.String),
code: Schema.optional(Schema.Union([Schema.String, Schema.Number])),
errno: Schema.optional(Schema.Number),
cause: Schema.optional(Schema.Unknown),
}),
)
/** Error messages, stacks and SQL parameters may contain credentials. Retain only diagnostic classifications. */
export function from(error: unknown) {
const errors: { type: string; code?: string | number; errno?: number }[] = []
const seen = new Set<unknown>()
while (error && !seen.has(error) && errors.length < 8) {
seen.add(error)
const result = decode(error)
if (Option.isNone(result)) break
errors.push({
type: result.value._tag ?? (error instanceof Error ? error.name : result.value.name) ?? "unknown",
code: result.value.code,
errno: result.value.errno,
})
error = error instanceof Error ? error.cause : result.value.cause
}
return errors
}
+97 -23
View File
@@ -1,7 +1,10 @@
export * as WebSearch from "./websearch.js"
import { WebSearch } from "@opencode-ai/schema/websearch"
import { Context, Effect, Layer, Option, Schema } from "effect"
import type { Session } from "@opencode-ai/schema/session"
import { SessionEvent } from "@opencode-ai/schema/session-event"
import { Clock, Context, Effect, Layer, Option, Schema, Stream } from "effect"
import { HttpClientError } from "effect/unstable/http"
import { makeLocationNode } from "@opencode-ai/util/effect/app-node"
import { Bus } from "./bus.js"
import { KV } from "./kv.js"
@@ -55,7 +58,13 @@ export interface Interface extends State.Transformable<Editor> {
readonly providers: () => Effect.Effect<readonly Provider[]>
readonly default: () => Effect.Effect<Provider | undefined, DisabledError>
readonly select: (selection: Selection) => Effect.Effect<void>
readonly query: (input: Input) => Effect.Effect<Response, Error>
readonly query: (
input: Input,
options?: {
readonly sessionID?: Session.ID
readonly onProvider?: (provider: Provider) => Effect.Effect<void>
},
) => Effect.Effect<Response, Error>
}
export class Service extends Context.Service<Service, Interface>()("@opencode/WebSearch") {}
@@ -79,6 +88,17 @@ const layer = Layer.effect(
const bus = yield* Bus.Service
const kv = yield* KV.Service
const decodeResults = Schema.decodeUnknownEffect(Schema.Array(Result))
const cooldowns = new Map<ID, { until: number; error: RequestError }>()
const preferred = new Map<Session.ID | undefined, { provider?: ID }>()
yield* Effect.addFinalizer(() => Effect.sync(() => preferred.clear()))
yield* bus.subscribe([SessionEvent.Deleted, SessionEvent.Moved]).pipe(
Stream.runForEach((event) =>
Effect.sync(() => {
preferred.delete(event.data.sessionID)
}),
),
Effect.forkScoped({ startImmediately: true }),
)
const state = State.create<Data, Editor>({
initial: () => ({ providers: new Map() }),
editor: (editor) => ({
@@ -96,26 +116,46 @@ const layer = Layer.effect(
return provider ? Effect.succeed(provider) : Effect.fail(new ProviderNotFoundError({ providerID }))
}
const defaultProvider = Effect.fn("WebSearch.default")(function* () {
const selection = Effect.fn("WebSearch.selection")(function* () {
const data = state.get()
const stored = data.selection === undefined ? yield* kv.get(ProviderKey) : undefined
if (
data.selection === false ||
data.selection === "random" ||
(data.selection && data.providers.has(data.selection))
)
return data.selection
const stored = yield* kv.get(ProviderKey)
const decoded = Schema.decodeUnknownOption(Selection)(stored)
if (stored !== undefined && Option.isNone(decoded)) yield* kv.remove(ProviderKey)
const selection = data.selection ?? Option.getOrUndefined(decoded)
if (selection === false) return yield* new DisabledError()
if (selection === "random") {
const providers = Array.from(data.providers.values())
return providers[Math.floor(Math.random() * providers.length)]
}
return selection ? data.providers.get(selection) : undefined
return Option.getOrUndefined(decoded)
})
const resolve = Effect.fn("WebSearch.resolve")(function* (input: Input) {
const randomProvider = (now: number, affinity: { provider?: ID }, attempted?: Set<ID>) => {
const providers = state.get().providers
if (input.providerID) return yield* requireProvider(providers, input.providerID)
const provider = yield* defaultProvider()
if (!provider) return yield* new ProviderRequiredError()
cooldowns.forEach((cooldown, id) => {
if (cooldown.until <= now || !providers.has(id)) cooldowns.delete(id)
})
const current = affinity.provider === undefined ? undefined : providers.get(affinity.provider)
if (current && !cooldowns.has(current.id) && !attempted?.has(current.id)) return current
const available = Array.from(providers.values()).filter(
(provider) => !cooldowns.has(provider.id) && !attempted?.has(provider.id),
)
const provider = available[Math.floor(Math.random() * available.length)]
if (provider) affinity.provider = provider.id
return provider
}
const defaultProvider = Effect.fn("WebSearch.default")(function* (choice: Selection | undefined) {
if (choice === false) return yield* new DisabledError()
if (choice === "random") {
// Inspection must not select a provider or reopen consent when every provider is cooling down.
const active = preferred.get(undefined)?.provider
return (
(active === undefined ? undefined : state.get().providers.get(active)) ??
state.get().providers.values().next().value
)
}
return choice ? state.get().providers.get(choice) : undefined
})
return Service.of({
@@ -128,24 +168,58 @@ const layer = Layer.effect(
})).toSorted((a, b) => a.name.localeCompare(b.name))
}),
default: Effect.fn("WebSearch.defaultInfo")(function* () {
const provider = yield* defaultProvider()
const provider = yield* defaultProvider(yield* selection())
return provider && { id: provider.id, name: provider.name }
}),
select: Effect.fn("WebSearch.select")(function* (selection) {
yield* kv.set(ProviderKey, selection)
}),
query: Effect.fn("WebSearch.query")(function* (input) {
const provider = yield* resolve(input)
const results = yield* provider.execute({ query: input.query }).pipe(
Effect.flatMap(decodeResults),
Effect.mapError((cause) => new RequestError({ providerID: provider.id, cause })),
)
return new Response({ providerID: provider.id, results })
query: Effect.fn("WebSearch.query")(function* (input, options) {
const choice = input.providerID ? undefined : yield* selection()
let provider = input.providerID
? yield* requireProvider(state.get().providers, input.providerID)
: yield* defaultProvider(choice)
if (!provider) return yield* new ProviderRequiredError()
// Keep the cell for this query so deletion/movement cannot reinsert an in-flight session's entry.
const affinity = preferred.get(options?.sessionID) ?? { provider: undefined }
if (choice === "random") {
preferred.set(options?.sessionID, affinity)
provider = randomProvider(yield* Clock.currentTimeMillis, affinity) ?? provider
}
const attempted = new Set<ID>()
while (true) {
if (options?.onProvider) yield* options.onProvider({ id: provider.id, name: provider.name })
let cooldown = choice === "random" ? cooldowns.get(provider.id) : undefined
if (!cooldown || cooldown.until <= (yield* Clock.currentTimeMillis)) {
attempted.add(provider.id)
const result = yield* provider
.execute({ query: input.query })
.pipe(Effect.flatMap(decodeResults), Effect.result)
if (result._tag === "Success") return new Response({ providerID: provider.id, results: result.success })
const cause = result.failure
const error = new RequestError({ providerID: provider.id, cause })
if (choice !== "random" || !HttpClientError.isHttpClientError(cause) || cause.response?.status !== 429)
return yield* error
const now = yield* Clock.currentTimeMillis
cooldown = { until: now + cooldownMillis(cause.response.headers["retry-after"], now), error }
cooldowns.set(provider.id, cooldown)
}
provider = randomProvider(yield* Clock.currentTimeMillis, affinity, attempted)
if (!provider) return yield* cooldown.error
}
}),
})
}),
)
function cooldownMillis(value: string | undefined, now: number) {
if (!value?.trim()) return 60_000
const seconds = Number(value)
if (Number.isFinite(seconds)) return seconds >= 0 ? seconds * 1000 : 60_000
const date = Date.parse(value)
return Number.isFinite(date) ? Math.max(0, date - now) : 60_000
}
export const node = makeLocationNode({
service: Service,
layer,
+21 -2
View File
@@ -1,20 +1,36 @@
export * as TestWebSearch from "./websearch"
import { Context, Deferred, Effect, Layer } from "effect"
import { HttpClientError, HttpClientRequest, HttpClientResponse } from "effect/unstable/http"
import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
import { LayerNode } from "@opencode-ai/util/effect/layer-node"
import { Bus } from "@opencode-ai/core/bus"
import { KV } from "@opencode-ai/core/kv"
import { WebSearch } from "@opencode-ai/core/websearch"
import type { Session } from "@opencode-ai/schema/session"
export interface Interface extends WebSearch.Interface {
readonly queries: readonly WebSearch.Input[]
readonly sessionIDs: readonly (Session.ID | undefined)[]
/** Waits for query arrivals, not provider execution or query completion. */
readonly wait: (count: number) => Effect.Effect<void>
}
export class Service extends Context.Service<Service, Interface>()("test/WebSearch") {}
export function httpError(status = 429, retryAfter?: string, url = "https://search.example.com") {
const request = HttpClientRequest.post(url)
return new HttpClientError.HttpClientError({
reason: new HttpClientError.StatusCodeError({
request,
response: HttpClientResponse.fromWeb(
request,
new Response(null, { status, headers: retryAfter === undefined ? {} : { "Retry-After": retryAfter } }),
),
}),
})
}
// No providers are installed: tests register local executors through transform.
// The normal Bus and KV implementations use the default in-memory database.
export const layer = Layer.effectContext(
@@ -22,6 +38,7 @@ export const layer = Layer.effectContext(
const context = yield* Layer.build(AppNodeBuilder.build(LayerNode.group([WebSearch.node, Bus.node, KV.node])))
const websearch = Context.get(context, WebSearch.Service)
const queries: WebSearch.Input[] = []
const sessionIDs: (Session.ID | undefined)[] = []
let started = yield* Deferred.make<void>()
const wait = (count: number): Effect.Effect<void> =>
Effect.suspend(() =>
@@ -30,13 +47,15 @@ export const layer = Layer.effectContext(
const test = Service.of({
...websearch,
queries,
sessionIDs,
wait,
query: Effect.fnUntraced(function* (input: WebSearch.Input) {
query: Effect.fnUntraced(function* (input, options) {
queries.push({ ...input })
sessionIDs.push(options?.sessionID)
const previous = started
started = yield* Deferred.make<void>()
yield* Deferred.succeed(previous, undefined)
return yield* websearch.query(input)
return yield* websearch.query(input, options)
}),
})
return Context.add(context, WebSearch.Service, test).pipe(Context.add(Service, test))
@@ -0,0 +1,221 @@
import { describe, expect } from "bun:test"
import { Context, Deferred, Duration, Effect, Fiber, Layer, LayerMap, RcMap, Schema } from "effect"
import { TestClock } from "effect/testing"
import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
import { LayerNode } from "@opencode-ai/util/effect/layer-node"
import { makeGlobalNode } from "@opencode-ai/util/effect/app-node"
import { Bus } from "@opencode-ai/core/bus"
import { Database } from "@opencode-ai/core/database/database"
import { Form } from "@opencode-ai/core/form"
import { Location } from "@opencode-ai/core/location"
import { LocationActivity } from "@opencode-ai/core/location-activity"
import { LocationServiceMap, type LocationServices } from "@opencode-ai/core/location-services"
import { Project } from "@opencode-ai/core/project"
import { ProjectTable } from "@opencode-ai/core/project/sql"
import { AbsolutePath } from "@opencode-ai/core/schema"
import { Session } from "@opencode-ai/core/session"
import { SessionExecution } from "@opencode-ai/core/session/execution"
import { SessionEvent } from "@opencode-ai/core/session/event"
import { SessionRunner } from "@opencode-ai/core/session/runner/index"
import { SessionTable } from "@opencode-ai/core/session/sql"
import { SessionStore } from "@opencode-ai/core/session/store"
import { Workspace } from "@opencode-ai/core/workspace"
import { testEffect } from "./lib/effect"
// Keep real execution ownership, location caching, forms, and eviction. The fixture
// runner waits on a form instead of making a model request before asking a question.
const locations = Layer.effect(
LocationServiceMap.Service,
Effect.gen(function* () {
const bus = yield* Bus.Service
const map = yield* LayerMap.make(
(ref: Location.Ref) =>
// The fixture only exercises these three Location services.
// oxlint-disable-next-line typescript-eslint/no-unsafe-type-assertion
Layer.merge(
Layer.succeed(
Location.Service,
Location.Service.of({
directory: ref.directory,
workspaceID: ref.workspaceID,
project: { id: Project.ID.global, directory: ref.directory, canonical: ref.directory },
}),
),
Layer.effect(
SessionRunner.Service,
Effect.gen(function* () {
const forms = yield* Form.Service
return SessionRunner.Service.of({
drain: ({ sessionID }) =>
forms
.ask({
sessionID,
title: "Questions",
fields: [{ key: "runtime", type: "string" }],
})
.pipe(
Effect.orDie,
Effect.as(SessionRunner.DrainResult.Complete()),
Effect.onInterrupt(() => Effect.sleep("5 minutes")),
),
})
}),
),
).pipe(
Layer.provideMerge(Form.layer),
Layer.provide(Layer.succeed(Bus.Service, bus)),
Layer.fresh,
) as unknown as Layer.Layer<LocationServices>,
{ idleTimeToLive: Duration.infinity },
)
return {
...map,
get: (ref: Location.Ref) => map.get(LocationServiceMap.canonical(ref)),
contextEffect: (ref: Location.Ref) => map.contextEffect(LocationServiceMap.canonical(ref)),
contextEffectOption: (ref: Location.Ref) => map.contextEffectOption(LocationServiceMap.canonical(ref)),
invalidate: (ref: Location.Ref) => map.invalidate(LocationServiceMap.canonical(ref)),
}
}),
)
const it = testEffect(
AppNodeBuilder.build(
LayerNode.group([
Database.node,
Bus.node,
SessionStore.node,
LocationServiceMap.node,
SessionExecution.node,
LocationActivity.node,
]),
[
LocationServiceMap.node.replace(
makeGlobalNode({
service: LocationServiceMap.Service,
layer: locations,
deps: [Bus.node],
}),
),
],
),
)
describe("LocationActivity eviction", () => {
for (const [count, admission] of [
[1, "none"],
[2, "none"],
[1, "other"],
[1, "same"],
] as const) {
const newWork = admission !== "none"
it.effect(
`interrupts ${count} waiting executions before eviction (${admission} session admitted during cleanup)`,
() =>
Effect.gen(function* () {
const db = (yield* Database.Service).db
const bus = yield* Bus.Service
const map = yield* LocationServiceMap.Service
const execution = yield* SessionExecution.Service
const store = yield* SessionStore.Service
const sessionIDs = Array.from({ length: count }, (_, index) =>
Session.ID.make(`ses_waiting_question_${index}`),
)
const newcomer = admission === "same" ? sessionIDs[0] : Session.ID.make("ses_new_question")
const ref = LocationServiceMap.canonical({ directory: AbsolutePath.make("/project") })
const idle = Location.Ref.make({ directory: ref.directory, workspaceID: Workspace.ID.make("wrk_idle") })
yield* db
.insert(ProjectTable)
.values({ id: Project.ID.global, worktree: ref.directory, sandboxes: [] })
.run()
.pipe(Effect.orDie)
yield* db
.insert(SessionTable)
.values(
Array.from(new Set([...sessionIDs, newcomer]), (sessionID) => ({
id: sessionID,
project_id: Project.ID.global,
slug: "question",
directory: ref.directory,
title: "Waiting question",
version: "test",
})),
)
.run()
.pipe(Effect.orDie)
const created = yield* Deferred.make<void>()
const newCreated = yield* Deferred.make<void>()
const pending: Form.Info[] = []
const interrupted: SessionEvent.Execution.Interrupted["data"][] = []
const unsubscribe = yield* bus.listen((event) =>
Effect.gen(function* () {
if (event.type === SessionEvent.Execution.Interrupted.type) {
interrupted.push(Schema.decodeUnknownSync(SessionEvent.Execution.Interrupted.data)(event.data))
}
if (event.type !== Form.Event.Created.type) return
pending.push(Schema.decodeUnknownSync(Form.Event.Created.data)(event.data).form)
if (pending.length === count) yield* Deferred.succeed(created, undefined)
if (pending.length > count) yield* Deferred.succeed(newCreated, undefined)
}),
)
yield* Effect.addFinalizer(() => unsubscribe)
const running = yield* Effect.forEach(sessionIDs, (sessionID) =>
execution.resume(sessionID).pipe(Effect.exit, Effect.forkScoped),
)
yield* Effect.addFinalizer(() =>
Effect.forEach([...sessionIDs, newcomer], (sessionID) => execution.interrupt(sessionID)).pipe(
Effect.andThen(TestClock.adjust("5 minutes")),
),
)
yield* Deferred.await(created)
const context = yield* map.contextEffect(ref).pipe(Effect.scoped)
const forms = Context.get(context, Form.Service)
expect((yield* store.listSuspended()).toSorted()).toEqual(sessionIDs.toSorted())
yield* Location.Service.pipe(Effect.provide(map.get(idle)), Effect.scoped)
// Human input produces no durable activity while the question is pending.
yield* TestClock.adjust("1 minute")
yield* TestClock.adjust("62 minutes")
// Interruption has cancelled each question, but slow cleanup still owns the graph.
expect(Array.from(yield* execution.active).toSorted()).toEqual(sessionIDs.toSorted())
expect(Array.from(yield* RcMap.keys(map.rcMap))).toEqual([ref])
expect(yield* forms.list()).toEqual([])
for (const form of pending) expect(yield* forms.state(form.id)).toEqual({ status: "cancelled" })
if (newWork) {
yield* execution.wake(newcomer)
if (admission === "other") yield* Deferred.await(newCreated)
}
yield* TestClock.adjust("5 minutes")
if (newWork) yield* Deferred.await(newCreated)
const results = yield* Effect.forEach(running, Fiber.join)
expect(results.every((exit) => exit._tag === "Failure")).toBe(true)
expect(Array.from(yield* execution.active)).toEqual(newWork ? [newcomer] : [])
expect(yield* store.listSuspended()).toEqual(newWork ? [newcomer] : [])
expect(interrupted.toSorted((a, b) => a.sessionID.localeCompare(b.sessionID))).toEqual(
sessionIDs.map((sessionID) => ({ sessionID, reason: "inactivity" })),
)
expect(Array.from(yield* RcMap.keys(map.rcMap))).toEqual(newWork ? [ref] : [])
if (newWork) {
expect(yield* forms.list({ sessionID: newcomer })).toEqual([pending[count]])
if (admission === "same") {
const later = LocationServiceMap.canonical({ directory: AbsolutePath.make("/later") })
yield* Location.Service.pipe(Effect.provide(map.get(later)), Effect.scoped)
yield* TestClock.adjust("30 minutes")
// Keep fresh work active while a different graph reaches its own deadline.
yield* bus.publish(SessionEvent.Execution.Started, { sessionID: newcomer }, { location: ref })
yield* TestClock.adjust("32 minutes")
expect(Array.from(yield* execution.active)).toEqual([newcomer])
expect(Array.from(yield* RcMap.keys(map.rcMap))).toEqual([ref])
}
yield* execution.interrupt(newcomer)
yield* TestClock.adjust("5 minutes")
yield* execution.awaitIdle(newcomer)
yield* TestClock.adjust("62 minutes")
expect(yield* store.listSuspended()).toEqual([])
expect(Array.from(yield* RcMap.keys(map.rcMap))).toEqual([])
}
}),
)
}
})
@@ -17,24 +17,29 @@ interface WebSearchRequest {
}
export const requests: WebSearchRequest[] = []
export const signals: AbortSignal[] = []
let responseBody = ""
let responseStatus = 200
export function resetWebSearchFixture(body: string) {
export function resetWebSearchFixture(body: string, status = 200) {
requests.length = 0
signals.length = 0
responseBody = body
responseStatus = status
}
const http = Layer.succeed(
HttpClient.HttpClient,
HttpClient.make((request) =>
HttpClient.make((request, _url, signal) =>
Effect.sync(() => {
signals.push(signal)
if (request.body._tag !== "Uint8Array") throw new Error(`Unexpected request body: ${request.body._tag}`)
requests.push({
url: request.url,
headers: request.headers,
body: JSON.parse(new TextDecoder().decode(request.body.body)),
})
return HttpClientResponse.fromWeb(request, new Response(responseBody, { status: 200 }))
return HttpClientResponse.fromWeb(request, new Response(responseBody, { status: responseStatus }))
}),
),
)
+20 -1
View File
@@ -7,7 +7,7 @@ import { WebSearchFirecrawl } from "@opencode-ai/core/plugin/websearch/firecrawl
import { WebSearchParallel } from "@opencode-ai/core/plugin/websearch/parallel"
import { WebSearchTavily } from "@opencode-ai/core/plugin/websearch/tavily"
import { host, integrationHost, webSearchHost } from "./host"
import { requests, resetWebSearchFixture, webSearchIntegrationTest } from "./websearch-fixture"
import { requests, signals, resetWebSearchFixture, webSearchIntegrationTest } from "./websearch-fixture"
beforeEach(() => {
resetWebSearchFixture(
@@ -30,6 +30,25 @@ beforeEach(() => {
const it = webSearchIntegrationTest
describe("built-in web search providers", () => {
;[WebSearchExa.Plugin, WebSearchParallel.Plugin, WebSearchFirecrawl.Plugin, WebSearchTavily.Plugin].forEach(
(plugin) => {
it.effect(`releases rate-limited HTTP requests for ${plugin.id} before caching their errors`, () =>
Effect.gen(function* () {
resetWebSearchFixture("Rate limited", 429)
const integrations = yield* Integration.Service
const websearch = yield* WebSearch.Service
yield* plugin.effect(
host({ integration: integrationHost(integrations), websearch: webSearchHost(websearch) }),
)
yield* websearch.select("random")
expect(yield* websearch.query({ query: "limited" }).pipe(Effect.flip)).toBeInstanceOf(WebSearch.RequestError)
expect(signals).toHaveLength(1)
expect(signals[0]?.aborted).toBe(true)
}),
)
},
)
it.effect("registers a provider without an integration", () =>
Effect.gen(function* () {
const integrations = yield* Integration.Service
+86 -12
View File
@@ -1,6 +1,6 @@
import { describe, expect } from "bun:test"
import { Context, Effect, Layer } from "effect"
import { HttpClientError, HttpClientRequest, HttpClientResponse } from "effect/unstable/http"
import type { HttpClientError } from "effect/unstable/http"
import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
import { LayerNode } from "@opencode-ai/util/effect/layer-node"
import { Permission } from "@opencode-ai/core/permission"
@@ -131,10 +131,11 @@ describe("WebSearchTool registration", () => {
expect(fixture.websearch.queries).toEqual([
{
query: "effect typescript",
providerID: WebSearch.ID.make("exa"),
providerID: undefined,
},
])
expect(fixture.events).toEqual(["permission", "query"])
expect(fixture.websearch.sessionIDs).toEqual([sessionID])
}),
)
@@ -215,7 +216,7 @@ describe("WebSearchTool registration", () => {
})
expect(first.status).toBe("completed")
expect(["exa", "parallel"]).toContain(first.metadata?.provider)
expect(first.metadata?.provider).toBe(fixture.websearch.queries[1]?.providerID)
expect(fixture.websearch.sessionIDs).toEqual([sessionID, sessionID])
expect(yield* fixture.kv.get(WebSearch.ProviderKey)).toBe("random")
expect(fixture.websearch.queries).toHaveLength(2)
expect(fixture.formRequests).toEqual([
@@ -253,12 +254,31 @@ describe("WebSearchTool registration", () => {
})
expect(second.status).toBe("completed")
expect(["exa", "parallel"]).toContain(second.metadata?.provider)
expect(second.metadata?.provider).toBe(fixture.websearch.queries[2]?.providerID)
expect(second.metadata?.provider).toBe(first.metadata?.provider)
expect(fixture.formRequests).toHaveLength(1)
expect(fixture.websearch.queries).toHaveLength(3)
}),
)
it.effect("honors automatic consent when the configured provider is unavailable", () =>
Effect.gen(function* () {
const fixture = yield* setup
yield* fixture.websearch.transform((editor) => editor.default.set(WebSearch.ID.make("missing")))
fixture.formResponse = { status: "answered", answer: { choice: "allow" } }
const result = yield* executeTool(fixture.registry, {
sessionID,
...toolIdentity,
call: { type: "tool-call", id: "call-missing", name: "websearch", input: { query: "effect" } },
})
expect(result.status).toBe("completed")
expect(yield* fixture.kv.get(WebSearch.ProviderKey)).toBe("random")
expect(result.metadata?.provider).toBe(
(yield* fixture.websearch.query({ query: "next" }, { sessionID })).providerID,
)
expect(fixture.formRequests).toHaveLength(1)
}),
)
it.effect("asks a second form when choosing another provider", () =>
Effect.gen(function* () {
const fixture = yield* setup
@@ -347,6 +367,67 @@ describe("WebSearchTool registration", () => {
}),
)
it.effect("keeps provider progress, output, and metadata accurate across automatic failover", () =>
Effect.gen(function* () {
const fixture = yield* setup
yield* fixture.websearch.select("random")
const first = (yield* fixture.websearch.query({ query: "seed" }, { sessionID })).providerID
yield* fixture.websearch.transform((editor) =>
editor.add({
id: first,
name: first,
execute: () => Effect.fail(TestWebSearch.httpError()),
}),
)
const progress: Tool.Metadata[] = []
const tools = yield* fixture.registry.snapshot()
const result = yield* tools.execute({
sessionID,
...toolIdentity,
call: { type: "tool-call", id: "call-failover", name: "websearch", input: { query: "effect" } },
progress: (metadata) =>
Effect.sync(() => {
progress.push(metadata)
}),
})
const replacement = WebSearch.ID.make(first === "exa" ? "parallel" : "exa")
expect(progress).toEqual([{ provider: first }, { provider: replacement }])
expect(result).toMatchObject({
output: { provider: replacement, results: fixture.results },
metadata: { provider: replacement },
})
expect(fixture.formRequests).toEqual([])
expect((yield* fixture.websearch.query({ query: "next" }, { sessionID })).providerID).toBe(replacement)
}),
)
it.effect("does not reopen consent when all automatic providers are cooling down", () =>
Effect.gen(function* () {
const fixture = yield* setup
yield* fixture.websearch.select("random")
fixture.error = TestWebSearch.httpError()
const tools = yield* fixture.registry.snapshot()
yield* Effect.forEach(["first", "cooling"], (query) =>
Effect.gen(function* () {
const error = yield* tools
.execute({
sessionID,
...toolIdentity,
call: { type: "tool-call", id: `call-${query}`, name: "websearch", input: { query } },
})
.pipe(Effect.flip)
expect(toSessionError(error)).toEqual({
type: "tool.execution",
message: "Web search rate limited (HTTP 429)",
})
expect(error.metadata).toMatchObject({ provider: expect.stringMatching(/^(exa|parallel)$/) })
}),
)
expect(fixture.events.filter((event) => event === "query")).toHaveLength(2)
expect(fixture.formRequests).toEqual([])
}),
)
it.effect("reports safe HTTP failures with the attempted provider", () =>
Effect.gen(function* () {
const fixture = yield* setup
@@ -362,14 +443,7 @@ describe("WebSearchTool registration", () => {
],
({ status, message }, index) =>
Effect.gen(function* () {
const request = HttpClientRequest.post("https://mcp.exa.ai/mcp?exaApiKey=secret")
fixture.error = new HttpClientError.HttpClientError({
reason: new HttpClientError.StatusCodeError({
request,
response: HttpClientResponse.fromWeb(request, new Response(null, { status })),
description: "non 2xx status code",
}),
})
fixture.error = TestWebSearch.httpError(status, undefined, "https://mcp.exa.ai/mcp?exaApiKey=secret")
const progress: Tool.Metadata[] = []
const error = yield* tools
.execute({
+444 -6
View File
@@ -1,24 +1,34 @@
import { describe, expect } from "bun:test"
import { Effect, Exit, Scope } from "effect"
import { Deferred, Effect, Exit, Fiber, Scope } from "effect"
import { TestClock } from "effect/testing"
import { KV } from "@opencode-ai/core/kv"
import { Bus } from "@opencode-ai/core/bus"
import { WebSearch } from "@opencode-ai/core/websearch"
import { Session } from "@opencode-ai/schema/session"
import { SessionEvent } from "@opencode-ai/schema/session-event"
import { Project } from "@opencode-ai/schema/project"
import { AbsolutePath } from "@opencode-ai/schema/schema"
import { testEffect } from "./lib/effect"
import { TestWebSearch } from "./lib/websearch"
const it = testEffect(TestWebSearch.layer)
const firstSession = Session.ID.make("ses_search_first")
const secondSession = Session.ID.make("ses_search_second")
const register = (id: string) =>
Effect.gen(function* () {
const websearch = yield* WebSearch.Service
const providerID = WebSearch.ID.make(id)
const calls: WebSearch.ProviderInput[] = []
yield* websearch.transform((editor) => {
const failure: { cause?: unknown } = {}
const registration = yield* websearch.transform((editor) => {
editor.add({
id: providerID,
name: id.toUpperCase(),
execute: (input) =>
Effect.sync(() => {
Effect.gen(function* () {
calls.push(input)
if (failure.cause !== undefined) return yield* Effect.fail(failure.cause)
return [
{
url: `https://${id}.example.com`,
@@ -30,7 +40,7 @@ const register = (id: string) =>
}),
})
})
return { providerID, calls }
return { providerID, calls, failure, dispose: registration.dispose }
})
describe("WebSearch", () => {
@@ -137,17 +147,445 @@ describe("WebSearch", () => {
}),
)
it.effect("chooses a registered provider for random selection", () =>
it.effect("keeps the random provider across queries, default lookups, and reloads", () =>
Effect.gen(function* () {
yield* register("exa")
yield* register("parallel")
const websearch = yield* WebSearch.Service
yield* websearch.transform((editor) => editor.default.set("random"))
expect(["exa", "parallel"]).toContain((yield* websearch.query({ query: "random" })).providerID)
const first = yield* websearch.query({ query: "first" })
expect(["exa", "parallel"]).toContain(first.providerID)
expect((yield* websearch.default())?.id).toBe(first.providerID)
yield* websearch.reload()
const results = yield* Effect.all(
Array.from({ length: 10 }, () => websearch.query({ query: "next" })),
{ concurrency: "unbounded" },
)
expect(results.every((result) => result.providerID === first.providerID)).toBe(true)
}),
)
it.effect("preserves persisted random selection and keeps its provider", () =>
Effect.gen(function* () {
yield* register("exa")
yield* register("parallel")
const websearch = yield* WebSearch.Service
const kv = yield* KV.Service
yield* kv.set(WebSearch.ProviderKey, "random")
const first = yield* websearch.query({ query: "legacy" })
expect((yield* websearch.query({ query: "sticky" })).providerID).toBe(first.providerID)
yield* websearch.select("random")
expect(yield* kv.get(WebSearch.ProviderKey)).toBe("random")
expect((yield* websearch.query({ query: "canonical" })).providerID).toBe(first.providerID)
}),
)
it.effect("fails over on rate limits with random and keeps the replacement after cooldown", () =>
Effect.gen(function* () {
const exa = yield* register("exa")
const parallel = yield* register("parallel")
const websearch = yield* WebSearch.Service
yield* websearch.transform((editor) => editor.default.set("random"))
const first = yield* websearch.query({ query: "first" })
const limited = first.providerID === exa.providerID ? exa : parallel
const replacement = first.providerID === exa.providerID ? parallel : exa
limited.failure.cause = TestWebSearch.httpError()
const progress: WebSearch.ID[] = []
expect(
(yield* websearch.query(
{ query: "retry" },
{
onProvider: (provider) =>
Effect.sync(() => {
progress.push(provider.id)
}),
},
)).providerID,
).toBe(replacement.providerID)
expect(progress).toEqual([limited.providerID, replacement.providerID])
expect(limited.calls.at(-1)).toEqual({ query: "retry" })
expect(replacement.calls).toEqual([{ query: "retry" }])
limited.failure.cause = undefined
yield* TestClock.adjust("59 seconds")
expect((yield* websearch.query({ query: "cooling" })).providerID).toBe(replacement.providerID)
expect(limited.calls).toHaveLength(2)
yield* TestClock.adjust("1 second")
expect((yield* websearch.query({ query: "still sticky" })).providerID).toBe(replacement.providerID)
replacement.failure.cause = TestWebSearch.httpError()
expect((yield* websearch.query({ query: "recovered" })).providerID).toBe(limited.providerID)
}),
)
it.effect("reselects when a concurrent query cools down the provider while progress is pending", () =>
Effect.gen(function* () {
const exa = yield* register("exa")
const parallel = yield* register("parallel")
const websearch = yield* WebSearch.Service
yield* websearch.select("random")
yield* websearch.query({ query: "seed" })
const first = yield* websearch.default()
if (!first) return yield* Effect.die("Expected an automatic provider")
const limited = first.id === exa.providerID ? exa : parallel
const replacement = first.id === exa.providerID ? parallel : exa
const paused = yield* Deferred.make<void>()
const resume = yield* Deferred.make<void>()
const progress: WebSearch.ID[] = []
const pending = yield* websearch
.query(
{ query: "pending" },
{
onProvider: (provider) =>
Effect.gen(function* () {
progress.push(provider.id)
if (provider.id !== first.id) return
yield* Deferred.succeed(paused, undefined)
yield* Deferred.await(resume)
}),
},
)
.pipe(Effect.forkChild)
yield* Deferred.await(paused)
limited.failure.cause = TestWebSearch.httpError()
expect((yield* websearch.query({ query: "trigger" })).providerID).toBe(replacement.providerID)
yield* Deferred.succeed(resume, undefined)
expect((yield* Fiber.join(pending)).providerID).toBe(replacement.providerID)
expect(progress).toEqual([limited.providerID, replacement.providerID])
expect(limited.calls).toEqual([{ query: "seed" }, { query: "trigger" }])
expect(replacement.calls).toEqual([{ query: "trigger" }, { query: "pending" }])
}),
)
it.effect("fails promptly when all providers are cooling down without asking for a provider", () =>
Effect.gen(function* () {
const providers = [yield* register("exa"), yield* register("parallel"), yield* register("tavily")]
const websearch = yield* WebSearch.Service
yield* websearch.select("random")
providers.forEach((provider) => {
provider.failure.cause = TestWebSearch.httpError()
})
expect(yield* websearch.query({ query: "limited" }).pipe(Effect.flip)).toBeInstanceOf(WebSearch.RequestError)
expect(providers.map((provider) => provider.calls.length)).toEqual([1, 1, 1])
expect(yield* websearch.default()).toBeDefined()
expect(yield* websearch.query({ query: "still limited" }).pipe(Effect.flip)).toBeInstanceOf(
WebSearch.RequestError,
)
expect(providers.map((provider) => provider.calls.length)).toEqual([1, 1, 1])
}),
)
it.effect("tries each provider only once per query even with a zero cooldown", () =>
Effect.gen(function* () {
const providers = [yield* register("exa"), yield* register("parallel")]
const websearch = yield* WebSearch.Service
yield* websearch.select("random")
providers.forEach((provider) => {
provider.failure.cause = TestWebSearch.httpError(429, "0")
})
expect(yield* websearch.query({ query: "limited" }).pipe(Effect.flip)).toBeInstanceOf(WebSearch.RequestError)
expect(providers.map((provider) => provider.calls.length)).toEqual([1, 1])
}),
)
;[
{ header: "120", millis: 120_000 },
{ header: "Thu, 01 Jan 1970 00:02:00 GMT", millis: 120_000 },
{ header: undefined, millis: 60_000 },
{ header: "invalid", millis: 60_000 },
{ header: "", millis: 60_000 },
{ header: "-1", millis: 60_000 },
].forEach(({ header, millis }) => {
it.effect(`respects Retry-After ${JSON.stringify(header)} and recovers after cooldown`, () =>
Effect.gen(function* () {
const provider = yield* register("exa")
const websearch = yield* WebSearch.Service
yield* websearch.select("random")
provider.failure.cause = TestWebSearch.httpError(429, header)
expect(yield* websearch.query({ query: "limited" }).pipe(Effect.flip)).toBeInstanceOf(WebSearch.RequestError)
provider.failure.cause = undefined
yield* TestClock.adjust(millis - 1)
expect(yield* websearch.query({ query: "early" }).pipe(Effect.flip)).toBeInstanceOf(WebSearch.RequestError)
expect(provider.calls).toHaveLength(1)
yield* TestClock.adjust(1)
expect((yield* websearch.query({ query: "recovered" })).providerID).toBe(provider.providerID)
expect(provider.calls).toHaveLength(2)
}),
)
})
it.effect("does not rotate or cool down providers for other failures", () =>
Effect.gen(function* () {
const exa = yield* register("exa")
const parallel = yield* register("parallel")
const websearch = yield* WebSearch.Service
yield* websearch.select("random")
const first = yield* websearch.query({ query: "first" })
const provider = first.providerID === exa.providerID ? exa : parallel
yield* Effect.forEach(
[TestWebSearch.httpError(401), TestWebSearch.httpError(500), new Error("timeout")],
(cause) =>
Effect.gen(function* () {
provider.failure.cause = cause
expect(yield* websearch.query({ query: "failure" }).pipe(Effect.flip)).toMatchObject({
providerID: first.providerID,
cause,
})
expect((yield* websearch.default())?.id).toBe(first.providerID)
}),
)
provider.failure.cause = undefined
expect((yield* websearch.query({ query: "recovered" })).providerID).toBe(first.providerID)
expect((first.providerID === exa.providerID ? parallel : exa).calls).toEqual([])
}),
)
it.effect("does not fail over fixed or explicitly requested providers", () =>
Effect.gen(function* () {
const exa = yield* register("exa")
const parallel = yield* register("parallel")
const websearch = yield* WebSearch.Service
exa.failure.cause = TestWebSearch.httpError()
yield* websearch.select(exa.providerID)
expect(yield* websearch.query({ query: "fixed" }).pipe(Effect.flip)).toMatchObject({ providerID: exa.providerID })
yield* websearch.select("random")
expect(yield* websearch.query({ query: "explicit", providerID: exa.providerID }).pipe(Effect.flip)).toMatchObject(
{
providerID: exa.providerID,
},
)
expect(exa.calls).toHaveLength(2)
expect(parallel.calls).toEqual([])
}),
)
it.effect("reselects when the active provider is removed", () =>
Effect.gen(function* () {
const exa = yield* register("exa")
const websearch = yield* WebSearch.Service
yield* websearch.select("random")
expect((yield* websearch.query({ query: "first" })).providerID).toBe(exa.providerID)
const parallel = yield* register("parallel")
expect((yield* websearch.query({ query: "still sticky" })).providerID).toBe(exa.providerID)
yield* exa.dispose
expect((yield* websearch.query({ query: "removed" })).providerID).toBe(parallel.providerID)
}),
)
it.effect("uses updated registrations for the sticky provider", () =>
Effect.gen(function* () {
const exa = yield* register("exa")
const websearch = yield* WebSearch.Service
yield* websearch.select("random")
expect((yield* websearch.query({ query: "original" })).results).toHaveLength(1)
const updated = yield* websearch.transform((editor) =>
editor.add({
id: exa.providerID,
name: "Updated Exa",
execute: () => Effect.succeed([]),
}),
)
expect(yield* websearch.default()).toEqual({ id: exa.providerID, name: "Updated Exa" })
expect((yield* websearch.query({ query: "updated" })).results).toEqual([])
yield* updated.dispose
expect((yield* websearch.query({ query: "restored" })).results).toHaveLength(1)
expect(exa.calls).toEqual([{ query: "original" }, { query: "restored" }])
}),
)
it.effect("keeps independent session affinities across parallel initial and subsequent searches", () =>
Effect.gen(function* () {
yield* register("exa")
yield* register("parallel")
yield* register("tavily")
const websearch = yield* WebSearch.Service
yield* websearch.select("random")
const results = yield* Effect.forEach(
[firstSession, secondSession],
(sessionID) =>
Effect.all(
Array.from({ length: 8 }, () => websearch.query({ query: "parallel" }, { sessionID })),
{
concurrency: "unbounded",
},
),
{ concurrency: "unbounded" },
)
expect(results.map((group) => new Set(group.map((result) => result.providerID)).size)).toEqual([1, 1])
expect((yield* websearch.query({ query: "later" }, { sessionID: firstSession })).providerID).toBe(
results[0]?.[0]?.providerID,
)
expect((yield* websearch.query({ query: "later" }, { sessionID: secondSession })).providerID).toBe(
results[1]?.[0]?.providerID,
)
}),
)
it.effect("does not overwrite peer or Location affinity when a session switches providers", () =>
Effect.gen(function* () {
const exa = yield* register("exa")
const websearch = yield* WebSearch.Service
yield* websearch.select("random")
yield* websearch.query({ query: "location" })
yield* websearch.query({ query: "first" }, { sessionID: firstSession })
yield* websearch.query({ query: "second" }, { sessionID: secondSession })
const parallel = yield* register("parallel")
exa.failure.cause = TestWebSearch.httpError()
expect((yield* websearch.query({ query: "switch" }, { sessionID: firstSession })).providerID).toBe(
parallel.providerID,
)
// Even while Exa is cooling down, inspection must not reroute any caller.
expect((yield* websearch.default())?.id).toBe(exa.providerID)
exa.failure.cause = undefined
yield* TestClock.adjust("1 minute")
expect((yield* websearch.query({ query: "peer" }, { sessionID: secondSession })).providerID).toBe(exa.providerID)
expect((yield* websearch.query({ query: "location" })).providerID).toBe(exa.providerID)
expect((yield* websearch.query({ query: "sticky replacement" }, { sessionID: firstSession })).providerID).toBe(
parallel.providerID,
)
}),
)
it.effect("shares cooldowns without sending a peer back to the rate-limited provider", () =>
Effect.gen(function* () {
const exa = yield* register("exa")
const websearch = yield* WebSearch.Service
yield* websearch.select("random")
yield* websearch.query({ query: "first" }, { sessionID: firstSession })
yield* websearch.query({ query: "second" }, { sessionID: secondSession })
const parallel = yield* register("parallel")
exa.failure.cause = TestWebSearch.httpError(429, "120")
yield* websearch.query({ query: "switch" }, { sessionID: firstSession })
expect((yield* websearch.query({ query: "peer" }, { sessionID: secondSession })).providerID).toBe(
parallel.providerID,
)
expect(exa.calls).toHaveLength(3)
exa.failure.cause = undefined
yield* TestClock.adjust("2 minutes")
expect((yield* websearch.query({ query: "sticky peer" }, { sessionID: secondSession })).providerID).toBe(
parallel.providerID,
)
}),
)
it.effect("converges overlapping session failures and ignores a late success on the old provider", () =>
Effect.gen(function* () {
const websearch = yield* WebSearch.Service
const arrived = yield* Deferred.make<void>()
const failures = yield* Deferred.make<void>()
const lateStarted = yield* Deferred.make<void>()
const lateRelease = yield* Deferred.make<void>()
const calls: string[] = []
yield* websearch.transform((editor) =>
editor.add({
id: WebSearch.ID.make("exa"),
name: "Exa",
execute: (input) =>
Effect.gen(function* () {
if (input.query === "seed") return []
if (input.query === "late") {
yield* Deferred.succeed(lateStarted, undefined)
yield* Deferred.await(lateRelease)
return []
}
calls.push(input.query)
if (calls.length === 3) yield* Deferred.succeed(arrived, undefined)
yield* Deferred.await(failures)
return yield* TestWebSearch.httpError()
}),
}),
)
yield* websearch.select("random")
yield* websearch.query({ query: "seed" }, { sessionID: firstSession })
const late = yield* websearch.query({ query: "late" }, { sessionID: firstSession }).pipe(Effect.forkChild)
yield* Deferred.await(lateStarted)
yield* register("parallel")
yield* register("tavily")
const pending = yield* Effect.all(
Array.from({ length: 3 }, (_, index) =>
websearch.query({ query: `fail-${index}` }, { sessionID: firstSession }),
),
{ concurrency: "unbounded" },
).pipe(Effect.forkChild)
yield* Deferred.await(arrived)
yield* Deferred.succeed(failures, undefined)
const results = yield* Fiber.join(pending)
expect(new Set(results.map((result) => result.providerID)).size).toBe(1)
expect(results[0]?.providerID).not.toBe(WebSearch.ID.make("exa"))
yield* Deferred.succeed(lateRelease, undefined)
expect((yield* Fiber.join(late)).providerID).toBe(WebSearch.ID.make("exa"))
expect((yield* websearch.query({ query: "later" }, { sessionID: firstSession })).providerID).toBe(
results[0]?.providerID,
)
}),
)
it.effect("keeps fixed and explicit providers pinned with session context", () =>
Effect.gen(function* () {
const exa = yield* register("exa")
const parallel = yield* register("parallel")
const websearch = yield* WebSearch.Service
exa.failure.cause = TestWebSearch.httpError()
yield* websearch.select(exa.providerID)
expect(yield* websearch.query({ query: "fixed" }, { sessionID: firstSession }).pipe(Effect.flip)).toMatchObject({
providerID: exa.providerID,
})
yield* websearch.select("random")
expect(
yield* websearch
.query({ query: "explicit", providerID: exa.providerID }, { sessionID: firstSession })
.pipe(Effect.flip),
).toMatchObject({ providerID: exa.providerID })
expect(parallel.calls).toEqual([])
}),
)
;["delete", "move"].forEach((operation) => {
it.effect(`forgets affinity on session ${operation} without retaining it through an in-flight query`, () =>
Effect.gen(function* () {
const exa = yield* register("exa")
const websearch = yield* WebSearch.Service
const bus = yield* Bus.Service
yield* websearch.select("random")
yield* websearch.query({ query: "seed" }, { sessionID: firstSession })
const started = yield* Deferred.make<void>()
const release = yield* Deferred.make<void>()
const pending = yield* websearch
.query(
{ query: "pending" },
{
sessionID: firstSession,
onProvider: (provider) =>
provider.id === exa.providerID
? Deferred.succeed(started, undefined).pipe(Effect.andThen(Deferred.await(release)))
: Effect.void,
},
)
.pipe(Effect.forkChild)
yield* Deferred.await(started)
yield* operation === "delete"
? bus.publish(SessionEvent.Deleted, { sessionID: firstSession })
: bus.publish(SessionEvent.Moved, {
sessionID: firstSession,
location: { directory: AbsolutePath.make("/moved") },
projectID: Project.ID.global,
})
yield* Effect.yieldNow
yield* exa.dispose
const parallel = yield* register("parallel")
expect((yield* websearch.query({ query: "new affinity" }, { sessionID: firstSession })).providerID).toBe(
parallel.providerID,
)
yield* parallel.dispose
const tavily = yield* register("tavily")
exa.failure.cause = TestWebSearch.httpError()
yield* Deferred.succeed(release, undefined)
expect((yield* Fiber.join(pending)).providerID).toBe(tavily.providerID)
yield* register("parallel")
expect((yield* websearch.query({ query: "still new affinity" }, { sessionID: firstSession })).providerID).toBe(
parallel.providerID,
)
}),
)
})
it.effect("fails when web search is explicitly disabled", () =>
Effect.gen(function* () {
yield* register("exa")
+1
View File
@@ -3,6 +3,7 @@
- Follow Solid best practices, leave a comment when violating this: https://www.brenelz.com/posts/solid-js-best-practices/
- Renderer process should only call `window.api` from `src/preload`.
- Main process should register IPC handlers in `src/main/ipc.ts`.
- Avoid FS operations where possible. For any desktop persistence prefer sqlite in most cases, as performance and EPERM and many other things, especially on windows can be quite painful. Using anything other than sqlite should come with strong reasons.
- NEVER hardcode user-visible English strings in production code. ALWAYS use an i18n key for native menus, picker titles, dialogs, buttons, accessible labels, and displayed errors.
- When migrating existing copy to i18n, preserve the English text byte-for-byte unless the task explicitly requests a copy change.
- NEVER change existing English text or English keys to facilitate translation. English is intentional, designer-written source copy; adapt locale-specific translations and i18n mechanics around it.
+7
View File
@@ -0,0 +1,7 @@
import { defineConfig } from "drizzle-kit"
export default defineConfig({
dialect: "sqlite",
schema: "./src/main/storage/schema.ts",
out: "./src/main/storage/migration",
})
+4 -1
View File
@@ -78,7 +78,10 @@ const require = __cjs_mod__.createRequire(import.meta.url);
input: { index: "src/preload/index.ts" },
output: {
format: "cjs",
entryFileNames: "[name].js",
// The package is "type": "module". Under --no-sandbox Electron loads the preload
// through Node's module loader, which treats a .js file as ESM and fails on
// require("electron"). The sandboxed path ignores the extension.
entryFileNames: "[name].cjs",
},
},
},
+2
View File
@@ -13,6 +13,7 @@
"typecheck": "tsgo -b",
"test": "bun test --timeout 30000",
"dev": "bun ./scripts/dev.ts",
"migration": "bun ./scripts/migration.ts",
"prebuild": "bun ./scripts/prebuild.ts",
"build": "electron-vite build",
"preview": "electron-vite preview",
@@ -46,6 +47,7 @@
"@types/node": "catalog:",
"@typescript/native-preview": "catalog:",
"app-builder-lib": "26.15.7",
"drizzle-kit": "catalog:",
"drizzle-orm": "catalog:",
"effect": "catalog:",
"electron": "42.10.1",
+56
View File
@@ -0,0 +1,56 @@
#!/usr/bin/env bun
// Renders the drizzle-kit output under src/main/storage/migration into migration.gen.ts so the
// main bundle and Bun tests import plain TypeScript instead of reading SQL files at runtime.
//
// bun run migration --name <change> generate a migration from schema.ts, then render
// bun run migration render only
// bun run migration --check fail when the rendered registry is stale
import { $ } from "bun"
import path from "node:path"
import { parseArgs } from "node:util"
const root = path.resolve(import.meta.dirname, "..")
const directory = path.join(root, "src/main/storage/migration")
const output = path.join(root, "src/main/storage/migration.gen.ts")
const args = parseArgs({
args: process.argv.slice(2),
options: {
check: { type: "boolean" },
name: { type: "string" },
},
})
if (args.values.name) await $`bun drizzle-kit generate --name ${args.values.name}`.cwd(root)
const rendered = await render()
if (!args.values.check) {
await Bun.write(output, rendered)
process.exit(0)
}
if ((await Bun.file(output).text()) !== rendered) {
throw new Error("Desktop storage migrations are stale. Run `bun run migration` from packages/desktop.")
}
async function render() {
const ids = (await Array.fromAsync(new Bun.Glob("*/migration.sql").scan({ cwd: directory })))
.map((file) => path.dirname(file))
.sort()
const migrations = await Promise.all(
ids.map(async (id) => ({
id,
// Normalize so a CRLF checkout renders the same registry as an LF one.
statements: (await Bun.file(path.join(directory, id, "migration.sql")).text())
.replaceAll("\r\n", "\n")
.split("--> statement-breakpoint")
.map((statement) => statement.trim())
.filter((statement) => statement.length > 0),
})),
)
const source = `// Generated by scripts/migration.ts from src/main/storage/migration. Do not edit.
export const migrations = ${JSON.stringify(migrations, null, 2)}
`
const prettier = await import("prettier")
return prettier.format(source, { parser: "typescript", semi: false, printWidth: 120 })
}
@@ -6,24 +6,17 @@ export const storageHandlers = StorageRpcs.toLayer(
Effect.gen(function* () {
const storage = yield* DesktopStorage.Service
return StorageRpcs.of({
StorageGet: ({ name, key }) => Effect.sync(() => storage.get(name, key)),
StorageSet: ({ name, key, value }) => Effect.sync(() => storage.set(name, key, value)),
StorageDelete: ({ name, key }) => storage.deleteValue(name, key).pipe(Effect.orDie),
StorageClear: ({ name }) => storage.clear(name).pipe(Effect.orDie),
StorageKeys: ({ name }) => Effect.sync(() => storage.keys(name)),
StorageLength: ({ name }) => Effect.sync(() => storage.length(name)),
StorageGet: ({ name, key }) => Effect.sync(() => storage.state.get(name, key)),
StorageSet: ({ name, key, value }) => Effect.sync(() => storage.state.set(name, key, value)),
StorageDelete: ({ name, key }) => Effect.sync(() => storage.state.delete(name, key)),
StorageClear: ({ name }) => Effect.sync(() => storage.state.clear(name)),
StorageKeys: ({ name }) => Effect.sync(() => storage.state.keys(name)),
StorageLength: ({ name }) => Effect.sync(() => storage.state.length(name)),
DraftsGet: ({ key }) => Effect.sync(() => storage.drafts.get(key)),
DraftsSet: ({ key, value }) => Effect.sync(() => storage.drafts.set(key, value)),
DraftsDelete: ({ key }) => Effect.sync(() => storage.drafts.set(key, null)),
DraftsPutBlob: ({ data }) =>
Effect.sync(() =>
storage.drafts.putBlob(data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength) as ArrayBuffer),
),
DraftsGetBlob: ({ id }) =>
Effect.sync(() => {
const data = storage.drafts.getBlob(id)
return data ? new Uint8Array(data) : null
}),
DraftsPutBlob: ({ data }) => Effect.sync(() => storage.drafts.putBlob(data)),
DraftsGetBlob: ({ id }) => Effect.sync(() => storage.drafts.getBlob(id)),
})
}),
)
+1 -2
View File
@@ -19,12 +19,11 @@ import { ApplicationLifecycle } from "./lifecycle"
import { showCliInstaller } from "./native/install-cli"
import { createMenu, sendMenuCommand } from "./native/menu"
import { DesktopCli } from "./service/desktop-cli"
import { DesktopStorage } from "./storage"
import { Updater } from "./updater"
import { getLastFocusedWindow } from "./windows"
import { Wsl } from "./wsl/start"
const services = Layer.mergeAll(DesktopFiles.layer, DesktopStorage.layer, Wsl.layer)
const services = Layer.mergeAll(DesktopFiles.layer, Wsl.layer)
const handlers = Layer.mergeAll(
appHandlers,
storageHandlers,
@@ -10,7 +10,6 @@ import {
prepareApplicationEnvironment,
prepareDesktop,
} from "./environment"
import { initializeFirstLaunchOnboarding } from "./onboarding"
export interface Interface {
readonly version: string
@@ -23,7 +22,6 @@ export const layer = Layer.effect(
Service,
Effect.gen(function* () {
const logging = yield* DesktopLogging.Service
yield* initializeFirstLaunchOnboarding(app.getPath("userData"))
yield* prepareApplicationEnvironment
yield* preferApplicationEnvironment
yield* loadProxyEnvironment
@@ -8,7 +8,6 @@ import { Effect, FileSystem, Path } from "effect"
import { CHANNEL } from "../constants"
import { DesktopPaths } from "../paths"
import { getUserShell, loadShellEnv } from "../service/shell-env"
import { cleanupStoreFiles } from "../storage/cleanup"
import { registerRendererProtocol, setDockIcon } from "../windows"
const appNames: Record<string, string> = {
@@ -74,14 +73,6 @@ export const preferApplicationEnvironment = Effect.gen(function* () {
export const prepareDesktop = Effect.gen(function* () {
const path = yield* Path.Path
const paths = yield* DesktopPaths.resolve
yield* cleanupStoreFiles(app.getPath("userData")).pipe(
Effect.tap((result) =>
result.deleted.length === 0
? Effect.void
: Effect.logInfo("cleaned scoped store files", { count: result.deleted.length, scanned: result.scanned }),
),
Effect.catch((error) => Effect.logWarning("failed to clean scoped store files", { error })),
)
if (app.isPackaged || process.env.OPENCODE_DESKTOP_DISABLE_PROTOCOL_REGISTRATION !== "1")
app.setAsDefaultProtocolClient("opencode")
yield* registerRendererProtocol()
+12 -1
View File
@@ -6,9 +6,11 @@ import { Context, Effect, Layer } from "effect"
import { DeepLinksOpened } from "../../shared/ipc-rpc/events"
import { emitIpcEvent } from "../ipc-events"
import { DesktopLogging, scoped } from "../native/logging"
import { DesktopStorage } from "../storage"
import { safeWebContentsURL } from "../windows/state"
import { getLastFocusedWindow, makeMainWindows, setAppQuitting, setRelaunchHandler } from "../windows"
import { acquireApplicationLock, configureApplication } from "./environment"
import { initializeFirstLaunchOnboarding } from "./onboarding"
import { Shutdown } from "./shutdown"
export interface Interface {
@@ -143,13 +145,22 @@ const runtime = Layer.effect(
}),
)
const platform = Layer.merge(DesktopLogging.layer, Shutdown.layer)
// Storage opens after configureApplication has set userData and before windows exist, so window
// teardown can clear a window's persisted state and every renderer request finds it ready.
const platform = Layer.mergeAll(
DesktopLogging.layer,
Shutdown.layer,
DesktopStorage.layer.pipe(Layer.provide(DesktopLogging.layer)),
)
export const layer = Layer.unwrap(
Effect.gen(function* () {
// Electron scopes the single-instance lock to userData.
yield* configureApplication()
if (!acquireApplicationLock()) return yield* Effect.interrupt
// Decide first-launch state before the storage layer creates drafts.sqlite, which would
// otherwise read as evidence of an earlier launch on a fresh install.
yield* initializeFirstLaunchOnboarding(app.getPath("userData"))
return runtime.pipe(Layer.provideMerge(platform))
}),
)
+18 -7
View File
@@ -2,7 +2,7 @@ export * as DesktopLogging from "./logging"
import log from "electron-log/main.js"
import { app, crashReporter, netLog, shell } from "electron"
import { Context, Effect, FileSystem, Layer, Logger, Option, Path, References } from "effect"
import { Context, Effect, FileSystem, Layer, Logger, Option, Path, References, Stream } from "effect"
import { homedir } from "node:os"
import { VERSION } from "../constants"
@@ -134,11 +134,15 @@ function exportDebugLogsEffect(fs: FileSystem.FileSystem, path: Path.Path) {
const output = path.join(app.getPath("downloads"), `opencode-debug-${stamp()}.zip`)
return yield* Effect.gen(function* () {
yield* Effect.logInfo("exporting debug logs", { output })
yield* writeZip(fs, output, [
{ name: "manifest.json", data: Buffer.from(JSON.stringify(manifest(path), null, 2)) },
const files = [
...(yield* collect(fs, path, root, "desktop")),
...(yield* Effect.forEach(serverLogRoots(path), (dir, i) => collect(fs, path, dir, `server-${i + 1}`))).flat(),
...(yield* collect(fs, path, app.getPath("crashDumps"), "crashpad")),
]
const truncated = files.filter((file) => file.offset > 0).map((file) => file.name)
yield* writeZip(fs, output, [
{ name: "manifest.json", data: Buffer.from(JSON.stringify({ ...manifest(path), truncated }, null, 2)) },
...files,
])
yield* Effect.sync(() => shell.showItemInFolder(output))
return output
@@ -229,7 +233,7 @@ function serverLogRoots(path: Path.Path) {
]
}
type Entry = { name: string; path: string } | { name: string; data: Uint8Array }
type Entry = { name: string; path: string; offset: number } | { name: string; data: Uint8Array }
function collect(fs: FileSystem.FileSystem, path: Path.Path, dir: string, prefix: string) {
return Effect.gen(function* () {
@@ -242,9 +246,11 @@ function collect(fs: FileSystem.FileSystem, path: Path.Path, dir: string, prefix
const info = yield* fs.stat(file)
if (info.type === "Directory") return null
if (Option.getOrElse(info.mtime, () => new Date(0)).getTime() < cutoff) return null
if (info.size > FileSystem.Size(MAX_EXPORT_FILE_SIZE)) return null
if (file.endsWith(".heapsnapshot")) return null
return { name: path.join(prefix, entry).replace(/\\/g, "/"), path: file }
// Server logs append forever without rotation, so the active log is often the largest
// file. Export its tail rather than dropping the most relevant file from the bundle.
const offset = Math.max(0, Number(info.size) - MAX_EXPORT_FILE_SIZE)
return { name: path.join(prefix, entry).replace(/\\/g, "/"), path: file, offset }
}),
)).filter((entry) => entry !== null)
})
@@ -258,7 +264,12 @@ function writeZip(fs: FileSystem.FileSystem, output: string, entries: Entry[]) {
entries,
(entry) =>
Effect.gen(function* () {
const data = "data" in entry ? entry.data : yield* fs.readFile(entry.path)
const data =
"data" in entry
? entry.data
: entry.offset === 0
? yield* fs.readFile(entry.path)
: Buffer.concat(yield* Stream.runCollect(fs.stream(entry.path, { offset: entry.offset })))
yield* Effect.tryPromise(() => writer.add(entry.name, new BlobReader(new Blob([new Uint8Array(data)]))))
}),
{ concurrency: 1, discard: true },
+1 -1
View File
@@ -13,7 +13,7 @@ export const resolve = Effect.gen(function* () {
const root = path.dirname(yield* path.fromFileUrl(new URL(import.meta.url)))
return {
developmentResourcesRoot: path.join(root, "../../resources"),
preloadPath: path.join(root, "../preload/index.js"),
preloadPath: path.join(root, "../preload/index.cjs"),
rendererRoot: path.join(root, "../renderer"),
} satisfies Resolved
}).pipe(Effect.orDie)
@@ -1,141 +0,0 @@
import * as NodeFileSystem from "@effect/platform-node/NodeFileSystem"
import * as NodePath from "@effect/platform-node/NodePath"
import { afterEach, describe, expect, test } from "bun:test"
import { tmpdir } from "node:os"
import { Effect, FileSystem, Layer, Path } from "effect"
import { cleanupStoreFiles, deleteStoreFileIfEmpty } from "./cleanup"
const roots: string[] = []
const platform = Layer.merge(NodeFileSystem.layer, NodePath.layer)
const run = <A, E>(effect: Effect.Effect<A, E, FileSystem.FileSystem | Path.Path>) =>
Effect.runPromise(effect.pipe(Effect.provide(platform)))
const tempRoot = Effect.fn("StorageTest.tempRoot")(function* () {
const fs = yield* FileSystem.FileSystem
const root = yield* fs.makeTempDirectory({ directory: tmpdir(), prefix: "opencode-store-cleanup-" })
roots.push(root)
return root
})
const writeStore = Effect.fn("StorageTest.writeStore")(function* (
root: string,
name: string,
value: string,
modified: Date,
) {
const fs = yield* FileSystem.FileSystem
const path = yield* Path.Path
yield* fs.writeFileString(path.join(root, name), value)
yield* fs.utimes(path.join(root, name), modified, modified)
})
afterEach(() =>
run(
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem
yield* Effect.forEach(roots.splice(0), (root) => fs.remove(root, { recursive: true, force: true }), {
concurrency: "unbounded",
discard: true,
})
}),
),
)
describe("store cleanup", () => {
test("removes empty scoped stores and leaves global stores alone", () =>
run(
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem
const root = yield* tempRoot()
const now = new Date("2026-07-01T00:00:00.000Z")
yield* writeStore(root, "opencode.draft.empty.dat", "{}", now)
yield* writeStore(root, "opencode.workspace.empty.dat", "{\n}", now)
yield* writeStore(root, "opencode.global.dat", "{}", now)
yield* writeStore(root, "opencode.workspace.empty.dat.json", "{}", now)
const result = yield* cleanupStoreFiles(root, now.getTime())
expect(result.deleted.sort()).toEqual(["opencode.draft.empty.dat", "opencode.workspace.empty.dat"])
expect((yield* fs.readDirectory(root)).sort()).toEqual([
"opencode.global.dat",
"opencode.workspace.empty.dat.json",
])
}),
),
)
test("removes stale drafts by age without removing non-empty workspace stores", () =>
run(
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem
const root = yield* tempRoot()
const now = new Date("2026-07-01T00:00:00.000Z")
yield* writeStore(
root,
"opencode.draft.old.dat",
'{"draft:prompt":"hello"}',
new Date("2026-05-01T00:00:00.000Z"),
)
yield* writeStore(root, "opencode.draft.recent.dat", '{"draft:prompt":"hello"}', now)
yield* writeStore(
root,
"opencode.workspace.old.dat",
'{"workspace:layout":"wide"}',
new Date("2025-01-01T00:00:00.000Z"),
)
yield* writeStore(root, "opencode.workspace.recent.dat", '{"workspace:layout":"wide"}', now)
const result = yield* cleanupStoreFiles(root, now.getTime())
expect(result.deleted).toEqual(["opencode.draft.old.dat"])
expect((yield* fs.readDirectory(root)).sort()).toEqual([
"opencode.draft.recent.dat",
"opencode.workspace.old.dat",
"opencode.workspace.recent.dat",
])
}),
),
)
test("caps scoped stores by recency", () =>
run(
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem
const root = yield* tempRoot()
const now = new Date("2026-07-01T00:00:00.000Z")
yield* Effect.forEach(
Array.from({ length: 102 }, (_, index) => index),
(index) =>
writeStore(
root,
`opencode.draft.${index}.dat`,
'{"draft:prompt":"hello"}',
new Date(now.getTime() - index * 1000),
),
{ concurrency: "unbounded", discard: true },
)
const result = yield* cleanupStoreFiles(root, now.getTime())
const remaining = yield* fs.readDirectory(root)
expect(result.deleted.sort()).toEqual(["opencode.draft.100.dat", "opencode.draft.101.dat"])
expect(remaining).toHaveLength(100)
}),
),
)
test("removes a scoped store immediately when it becomes empty", () =>
run(
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem
const root = yield* tempRoot()
yield* writeStore(root, "opencode.draft.empty.dat", "{}", new Date("2026-07-01T00:00:00.000Z"))
yield* writeStore(root, "opencode.global.dat", "{}", new Date("2026-07-01T00:00:00.000Z"))
expect(yield* deleteStoreFileIfEmpty(root, "opencode.draft.empty.dat")).toBe(true)
expect(yield* deleteStoreFileIfEmpty(root, "opencode.global.dat")).toBe(false)
expect(yield* fs.readDirectory(root)).toEqual(["opencode.global.dat"])
}),
),
)
})
@@ -1,104 +0,0 @@
import { Effect, FileSystem, Option, Path } from "effect"
const EMPTY_STORE_MAX_BYTES = 128
const DRAFT_RETENTION_MS = 30 * 24 * 60 * 60 * 1000
const DRAFT_KEEP_RECENT = 100
type StoreKind = "draft" | "workspace"
type StoreCandidate = {
name: string
path: string
kind: StoreKind
modified: number
empty: boolean
}
export const cleanupStoreFiles = Effect.fn("Storage.cleanupStoreFiles")(function* (
userDataPath: string,
now = Date.now(),
) {
const fs = yield* FileSystem.FileSystem
const path = yield* Path.Path
const entries = yield* fs.readDirectory(userDataPath).pipe(Effect.orElseSucceed(() => []))
const candidates = (yield* Effect.forEach(
entries,
Effect.fnUntraced(function* (entry) {
const kind = storeKind(entry)
if (!kind) return
const file = path.join(userDataPath, entry)
const stats = yield* fs.stat(file).pipe(Effect.orElseSucceed(() => undefined))
if (stats?.type !== "File") return
return {
name: entry,
path: file,
kind,
modified: Option.getOrElse(stats.mtime, () => new Date(0)).getTime(),
empty: yield* isEmptyStore(file, stats.size),
}
}),
{ concurrency: 5 },
)).filter((candidate) => !!candidate)
const stale = new Set<StoreCandidate>()
for (const candidate of candidates) {
if (candidate.empty) stale.add(candidate)
if (candidate.kind === "draft" && now - candidate.modified > DRAFT_RETENTION_MS) stale.add(candidate)
}
candidates
.filter((candidate) => candidate.kind === "draft" && !candidate.empty)
.sort((a, b) => b.modified - a.modified)
.slice(DRAFT_KEEP_RECENT)
.forEach((candidate) => stale.add(candidate))
const deleted = yield* Effect.forEach(
stale,
Effect.fnUntraced(function* (candidate) {
yield* fs.remove(candidate.path, { force: true })
return candidate.name
}),
{ concurrency: "unbounded" },
)
return { scanned: candidates.length, deleted }
})
export const deleteStoreFileIfEmpty = Effect.fn("Storage.deleteStoreFileIfEmpty")(function* (
userDataPath: string,
name: string,
) {
if (!storeKind(name)) return false
const fs = yield* FileSystem.FileSystem
const path = yield* Path.Path
const file = path.join(userDataPath, name)
const stats = yield* fs.stat(file).pipe(Effect.orElseSucceed(() => undefined))
if (stats?.type !== "File") return false
if (!(yield* isEmptyStore(file, stats.size))) return false
yield* fs.remove(file, { force: true })
return true
})
function storeKind(name: string): StoreKind | undefined {
if (/^opencode\.draft\..+\.dat$/.test(name)) return "draft"
if (/^opencode\.workspace\..+\.dat$/.test(name)) return "workspace"
}
const isEmptyStore = Effect.fn("Storage.isEmptyStore")(function* (file: string, size: FileSystem.Size) {
if (size > FileSystem.Size(EMPTY_STORE_MAX_BYTES)) return false
const fs = yield* FileSystem.FileSystem
const raw = yield* fs.readFileString(file).pipe(Effect.orElseSucceed(() => undefined))
if (raw === undefined) return false
if (raw.trim() === "") return true
try {
const parsed = JSON.parse(raw) as unknown
return typeof parsed === "object" && parsed !== null && !Array.isArray(parsed) && Object.keys(parsed).length === 0
} catch {
return false
}
})
@@ -0,0 +1,48 @@
import { describe, expect, test } from "bun:test"
import { DatabaseSync } from "node:sqlite"
import path from "node:path"
import { sql } from "drizzle-orm"
import { drizzle } from "drizzle-orm/node-sqlite"
import { migrate, openDatabase } from "./database"
import { migrations } from "./migration.gen"
const tables = (db: ReturnType<typeof drizzle>) =>
db
.all<{ name: string }>(sql`SELECT name FROM sqlite_master WHERE type = 'table' ORDER BY name`)
.map((row) => row.name)
describe("database", () => {
test("bootstraps every table on a fresh database and is idempotent", () => {
const database = openDatabase(":memory:")
expect(tables(database.db)).toEqual(["blob", "document", "migration", "state"])
expect(migrate(database.db)).toEqual([])
database.close()
})
test("adopts a drafts.sqlite created before the journal existed", () => {
const native = new DatabaseSync(":memory:")
native.exec(
"CREATE TABLE document (key TEXT PRIMARY KEY, value TEXT NOT NULL); CREATE TABLE blob (id TEXT PRIMARY KEY, data BLOB NOT NULL); INSERT INTO document VALUES ('k', 'v')",
)
const db = drizzle({ client: native })
expect(migrate(db)).toEqual(migrations.map((migration) => migration.id))
expect(tables(db)).toEqual(["blob", "document", "migration", "state"])
expect(db.all<{ value: string }>(sql`SELECT value FROM document`)).toEqual([{ value: "v" }])
expect(migrate(db)).toEqual([])
})
test("rendered registry matches the drizzle-kit output on disk", async () => {
const directory = path.join(import.meta.dirname, "migration")
const ids = (await Array.fromAsync(new Bun.Glob("*/migration.sql").scan({ cwd: directory })))
.map((file) => path.dirname(file))
.sort()
expect(migrations.map((migration) => migration.id)).toEqual(ids)
for (const migration of migrations) {
const source = (await Bun.file(path.join(directory, migration.id, "migration.sql")).text()).replaceAll(
"\r\n",
"\n",
)
for (const statement of migration.statements) expect(source).toContain(statement)
}
})
})
@@ -0,0 +1,48 @@
import { DatabaseSync } from "node:sqlite"
import { sql } from "drizzle-orm"
import { drizzle } from "drizzle-orm/node-sqlite"
import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core"
import { migrations } from "./migration.gen"
export type Database = ReturnType<typeof drizzle>
// Owned by the runner rather than schema.ts so drizzle-kit never tries to migrate the journal itself.
const journal = sqliteTable("migration", {
id: text().primaryKey(),
time_completed: integer().notNull(),
})
export function openDatabase(filename: string) {
const native = new DatabaseSync(filename)
// WAL keeps readers off the writer. NORMAL fsyncs at checkpoints only, which survives an app
// crash but not power loss; the right trade for UI state and far cheaper on Windows.
native.exec("PRAGMA journal_mode=WAL; PRAGMA synchronous=NORMAL; PRAGMA temp_store=MEMORY")
const db = drizzle({ client: native })
migrate(db)
return { db, close: () => native.close() }
}
export function migrate(db: Database) {
db.run(sql`CREATE TABLE IF NOT EXISTS ${journal} (id TEXT PRIMARY KEY, time_completed INTEGER NOT NULL)`)
const applied = new Set(
db
.select({ id: journal.id })
.from(journal)
.all()
.map((row) => row.id),
)
// drafts.sqlite predates the journal: its tables were created by hand, so the migration that
// would create them is recorded as applied instead of run.
const legacy =
applied.size === 0 &&
db.get(sql`SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'document'`) !== undefined
const pending = migrations.filter((migration) => !applied.has(migration.id))
if (pending.length === 0) return []
db.transaction((tx) => {
pending.forEach((migration, index) => {
if (!(legacy && index === 0)) migration.statements.forEach((statement) => tx.run(sql.raw(statement)))
tx.insert(journal).values({ id: migration.id, time_completed: Date.now() }).run()
})
})
return pending.map((migration) => migration.id)
}
@@ -1,29 +1,32 @@
import { expect, test } from "bun:test"
import { createDesktopDraftStore } from "./drafts"
import { describe, expect, test } from "bun:test"
import { sql } from "drizzle-orm"
import { openDatabase } from "./database"
import { createDraftStore } from "./drafts"
test("flushes the latest buffered draft and stores blobs", () => {
const store = createDesktopDraftStore(":memory:")
store.set("prompt", "first")
store.set("prompt", "latest")
expect(store.get("prompt")).toBe("latest")
store.flush()
expect(store.get("prompt")).toBe("latest")
describe("draft store", () => {
test("queues documents and reads them back before and after flush", () => {
const database = openDatabase(":memory:")
const drafts = createDraftStore(database.db, { delay: 1_000 })
drafts.set("a:draft:prompt", "{}")
expect(drafts.get("a:draft:prompt")).toBe("{}")
drafts.flush()
expect(drafts.get("a:draft:prompt")).toBe("{}")
drafts.set("a:draft:prompt", null)
expect(drafts.get("a:draft:prompt")).toBeNull()
drafts.flush()
expect(database.db.all(sql`SELECT key FROM document`)).toEqual([])
})
const bytes = new TextEncoder().encode("image")
const id = store.putBlob(bytes)
expect(store.getBlob(id)).toEqual(bytes)
store.close()
})
test("allows repeated flushes until closing", () => {
const store = createDesktopDraftStore(":memory:")
store.set("prompt", "first")
store.flush()
store.set("prompt", "draft")
store.flush()
expect(store.get("prompt")).toBe("draft")
store.close()
expect(() => store.flush()).not.toThrow()
expect(() => store.close()).not.toThrow()
test("stores blobs by content hash and collects unreferenced ones on open", () => {
const database = openDatabase(":memory:")
const first = createDraftStore(database.db, { delay: 1_000 })
const used = first.putBlob(new Uint8Array([1, 2, 3]))
const unused = first.putBlob(new Uint8Array([4, 5, 6]))
expect(first.putBlob(new Uint8Array([1, 2, 3]))).toBe(used)
first.set("doc", JSON.stringify({ parts: [{ blob: { id: used } }] }))
first.flush()
const second = createDraftStore(database.db, { delay: 1_000 })
expect(second.getBlob(used)).toEqual(new Uint8Array([1, 2, 3]))
expect(second.getBlob(unused)).toBeNull()
})
})
+46 -70
View File
@@ -1,72 +1,39 @@
import { createHash } from "node:crypto"
import { DatabaseSync } from "node:sqlite"
import { eq } from "drizzle-orm"
import { drizzle } from "drizzle-orm/node-sqlite"
import { blob, sqliteTable, text } from "drizzle-orm/sqlite-core"
import { eq, sql } from "drizzle-orm"
import type { Database } from "./database"
import { blobs, document } from "./schema"
import { createWriteBehind } from "./write-behind"
const documents = sqliteTable("document", {
key: text().primaryKey(),
value: text().notNull(),
})
const blobs = sqliteTable("blob", {
id: text().primaryKey(),
data: blob({ mode: "buffer" }).notNull(),
})
export type DraftStore = ReturnType<typeof createDraftStore>
export function createDesktopDraftStore(filename: string) {
const native = new DatabaseSync(filename)
native.exec(
"PRAGMA journal_mode=WAL; CREATE TABLE IF NOT EXISTS document (key TEXT PRIMARY KEY, value TEXT NOT NULL); CREATE TABLE IF NOT EXISTS blob (id TEXT PRIMARY KEY, data BLOB NOT NULL);",
)
const db = drizzle({ client: native })
const used = new Set<string>()
db.select({ value: documents.value })
.from(documents)
.all()
.forEach(({ value }) =>
JSON.parse(value, (_key, item) => {
if (item?.blob && typeof item.blob.id === "string") used.add(item.blob.id)
return item
export function createDraftStore(db: Database, input: { delay?: number; onError?: (error: unknown) => void } = {}) {
collectBlobs(db)
const byKey = eq(document.key, sql.placeholder("key"))
const read = db.select({ value: document.value }).from(document).where(byKey).prepare()
const remove = db.delete(document).where(byKey).prepare()
const upsert = db
.insert(document)
.values({ key: sql.placeholder("key"), value: sql.placeholder("value") })
.onConflictDoUpdate({ target: document.key, set: { value: sql.placeholder("value") } })
.prepare()
const writer = createWriteBehind<string | null>({
delay: input.delay ?? 500,
onError: input.onError,
write: (batch) =>
db.transaction(() => {
for (const [key, value] of batch) {
if (value === null) remove.run({ key })
else upsert.run({ key, value })
}
}),
)
db.select({ id: blobs.id })
.from(blobs)
.all()
.filter(({ id }) => !used.has(id))
.forEach(({ id }) => db.delete(blobs).where(eq(blobs.id, id)).run())
const pending = new Map<string, string | null>()
let timer: ReturnType<typeof setTimeout> | undefined
let closed = false
const flush = () => {
if (timer) clearTimeout(timer)
timer = undefined
if (closed) return
const writes = [...pending]
pending.clear()
if (!writes.length) return
db.transaction((tx) => {
writes.forEach(([key, value]) => {
if (value === null) tx.delete(documents).where(eq(documents.key, key)).run()
else
tx.insert(documents)
.values({ key, value })
.onConflictDoUpdate({ target: documents.key, set: { value } })
.run()
})
})
}
const schedule = () => {
if (!timer) timer = setTimeout(flush, 500)
}
})
return {
get: (key: string) =>
pending.has(key)
? (pending.get(key) ?? null)
: (db.select({ value: documents.value }).from(documents).where(eq(documents.key, key)).get()?.value ?? null),
set(key: string, value: string | null) {
pending.set(key, value)
schedule()
get(key: string) {
if (writer.has(key)) return writer.get(key) ?? null
return read.get({ key })?.value ?? null
},
set: (key: string, value: string | null) => writer.set(key, value),
putBlob(data: Uint8Array) {
const id = createHash("sha256").update(data).digest("hex")
db.insert(blobs)
@@ -75,13 +42,22 @@ export function createDesktopDraftStore(filename: string) {
.run()
return id
},
getBlob: (id: string) => db.select({ data: blobs.data }).from(blobs).where(eq(blobs.id, id)).get()?.data ?? null,
flush,
close() {
if (closed) return
flush()
closed = true
native.close()
getBlob(id: string): Uint8Array | null {
return db.select({ data: blobs.data }).from(blobs).where(eq(blobs.id, id)).get()?.data ?? null
},
flush: writer.flush,
close: writer.close,
}
}
// Blobs are content-addressed and shared; drop the ones no document references anymore. SQLite
// walks the JSON itself, so startup does not parse every draft and history entry in JavaScript.
function collectBlobs(db: Database) {
db.run(sql`
DELETE FROM ${blobs} WHERE ${blobs.id} NOT IN (
SELECT json_extract(node.value, '$.id')
FROM ${document}, json_tree(${document.value}) AS node
WHERE json_valid(${document.value}) AND node.key = 'blob' AND node.type = 'object'
)
`)
}
+37 -43
View File
@@ -2,8 +2,10 @@ export * as DesktopStorage from "./index"
import { app, BrowserWindow } from "electron"
import { Context, Effect, Layer, Path } from "effect"
import { createDesktopDraftStore } from "./drafts"
import { getStore, removeStoreFileIfEmpty } from "./store"
import { openDatabase } from "./database"
import { createDraftStore } from "./drafts"
import { importLegacyStores } from "./legacy"
import { createStateStore } from "./state"
export type Interface = ReturnType<typeof make>
@@ -13,60 +15,52 @@ export const layer = Layer.effect(
Service,
Effect.gen(function* () {
const path = yield* Path.Path
const storage = make(path.join(app.getPath("userData"), "drafts.sqlite"))
const flush = () => storage.drafts.flush()
const wire = (_event: Electron.Event, win: BrowserWindow) => win.on("session-end", flush)
app.on("before-quit", flush)
const runFork = Effect.runForkWith(yield* Effect.context())
const userData = app.getPath("userData")
const storage = make(path.join(userData, "drafts.sqlite"), (error) =>
runFork(Effect.logError("storage flush failed", { error })),
)
yield* importLegacyStores(storage.db, userData).pipe(
Effect.tap((result) =>
result.removed.length === 0
? Effect.void
: Effect.logInfo("imported legacy store files", { imported: result.imported, files: result.removed }),
),
Effect.catch((error) => Effect.logWarning("failed to import legacy store files", { error })),
)
const wire = (_event: Electron.Event, win: BrowserWindow) => win.on("session-end", storage.flush)
app.on("before-quit", storage.flush)
app.on("browser-window-created", wire)
BrowserWindow.getAllWindows().forEach((win) => wire({} as Electron.Event, win))
yield* Effect.addFinalizer(() =>
Effect.sync(() => {
app.off("before-quit", flush)
app.off("before-quit", storage.flush)
app.off("browser-window-created", wire)
BrowserWindow.getAllWindows().forEach((win) => win.off("session-end", flush))
storage.drafts.close()
BrowserWindow.getAllWindows().forEach((win) => win.off("session-end", storage.flush))
storage.close()
}),
)
return Service.of(storage)
}),
)
function make(draftFile: string) {
const drafts = createDesktopDraftStore(draftFile)
const deleteValue = Effect.fn("DesktopStorage.delete")(function* (name: string, key: string) {
getStore(name).delete(key)
yield* removeStoreFileIfEmpty(name).pipe(Effect.ignore)
})
const clear = Effect.fn("DesktopStorage.clear")(function* (name: string) {
getStore(name).clear()
yield* removeStoreFileIfEmpty(name).pipe(Effect.ignore)
})
// The file keeps its historical name; renaming it would mean moving the drafts it already holds.
export function make(filename: string, onError?: (error: unknown) => void) {
const database = openDatabase(filename)
const state = createStateStore(database.db, { onError })
const drafts = createDraftStore(database.db, { onError })
return {
get(name: string, key: string) {
try {
const value = getStore(name).get(key)
if (value === undefined || value === null) return null
return typeof value === "string" ? value : JSON.stringify(value)
} catch {
return null
}
db: database.db,
state,
drafts,
flush() {
state.flush()
drafts.flush()
},
set: (name: string, key: string, value: string) => getStore(name).set(key, value),
deleteValue,
clear,
keys: (name: string) => Object.keys(getStore(name).store),
length: (name: string) => Object.keys(getStore(name).store).length,
drafts: {
get: (key: string) => drafts.get(key),
set: (key: string, value: string | null) => drafts.set(key, value),
putBlob: (data: ArrayBuffer) => drafts.putBlob(new Uint8Array(data)),
getBlob(id: string) {
const data = drafts.getBlob(id)
return data ? new Uint8Array(data).buffer : null
},
flush: drafts.flush,
close: drafts.close,
close() {
state.close()
drafts.close()
database.close()
},
}
}
@@ -13,6 +13,7 @@ describe("hasExistingAppState", () => {
test("recognizes state written by an earlier OpenCode launch", () => {
expect(hasExistingAppState([file("opencode.settings")])).toBe(true)
expect(hasExistingAppState([file("opencode.global.dat")])).toBe(true)
expect(hasExistingAppState([file("drafts.sqlite")])).toBe(true)
expect(hasExistingAppState([file("window-state-abc.json")])).toBe(true)
expect(hasExistingAppState([directory("opencode")])).toBe(true)
})
@@ -2,6 +2,7 @@ export function hasExistingAppState(entries: Array<{ name: string; directory: bo
return entries.some((entry) => {
if (entry.name === "opencode.settings") return true
if (entry.name.endsWith(".dat")) return true
if (entry.name === "drafts.sqlite") return true
if (/^window-state-.+\.json$/.test(entry.name)) return true
return entry.directory && entry.name === "opencode"
})
@@ -0,0 +1,148 @@
import * as NodeFileSystem from "@effect/platform-node/NodeFileSystem"
import * as NodePath from "@effect/platform-node/NodePath"
import { afterEach, describe, expect, test } from "bun:test"
import { tmpdir } from "node:os"
import { sql } from "drizzle-orm"
import { Effect, FileSystem, Layer, Path } from "effect"
import { openDatabase } from "./database"
import { importLegacyStores } from "./legacy"
const roots: string[] = []
const platform = Layer.merge(NodeFileSystem.layer, NodePath.layer)
const run = <A, E>(effect: Effect.Effect<A, E, FileSystem.FileSystem | Path.Path>) =>
Effect.runPromise(effect.pipe(Effect.provide(platform)))
const now = new Date("2026-07-01T00:00:00.000Z")
const day = 24 * 60 * 60 * 1000
const tempRoot = Effect.fn("LegacyTest.tempRoot")(function* () {
const fs = yield* FileSystem.FileSystem
const root = yield* fs.makeTempDirectory({ directory: tmpdir(), prefix: "opencode-legacy-store-" })
roots.push(root)
return root
})
const writeStore = Effect.fn("LegacyTest.writeStore")(function* (
root: string,
name: string,
value: string,
modified = now,
) {
const fs = yield* FileSystem.FileSystem
const path = yield* Path.Path
yield* fs.writeFileString(path.join(root, name), value)
yield* fs.utimes(path.join(root, name), modified, modified)
})
const listing = Effect.fn("LegacyTest.listing")(function* (root: string) {
const fs = yield* FileSystem.FileSystem
return (yield* fs.readDirectory(root)).sort()
})
const rows = (db: ReturnType<typeof openDatabase>["db"]) =>
db.all<{ name: string; key: string; value: string }>(sql`SELECT name, key, value FROM state ORDER BY name, key`)
afterEach(() =>
run(
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem
yield* Effect.forEach(roots.splice(0), (root) => fs.remove(root, { recursive: true, force: true }), {
concurrency: "unbounded",
discard: true,
})
}),
),
)
describe("legacy store import", () => {
test("copies every namespace into state and removes the files", () =>
run(
Effect.gen(function* () {
const root = yield* tempRoot()
const database = openDatabase(":memory:")
yield* writeStore(root, "opencode.global.dat", JSON.stringify({ model: "m", layout: { sidebar: 1 } }))
yield* writeStore(root, "opencode.window.w1.dat", JSON.stringify({ tabs: "[]" }))
yield* writeStore(root, "default.dat", JSON.stringify({ "settings.v3": "{}" }))
yield* writeStore(root, "opencode.settings", JSON.stringify({ keep: true }))
yield* writeStore(root, "unrelated.txt", "x")
const result = yield* importLegacyStores(database.db, root, now.getTime())
expect(result.imported).toBe(4)
expect(rows(database.db)).toEqual([
{ name: "default.dat", key: "settings.v3", value: "{}" },
{ name: "opencode.global.dat", key: "layout", value: '{"sidebar":1}' },
{ name: "opencode.global.dat", key: "model", value: "m" },
{ name: "opencode.window.w1.dat", key: "tabs", value: "[]" },
])
expect(yield* listing(root)).toEqual(["opencode.settings", "unrelated.txt"])
}),
))
test("does not overwrite state that already exists", () =>
run(
Effect.gen(function* () {
const root = yield* tempRoot()
const database = openDatabase(":memory:")
database.db.run(sql`INSERT INTO state VALUES ('opencode.global.dat', 'model', 'new', 0)`)
yield* writeStore(root, "opencode.global.dat", JSON.stringify({ model: "old" }))
yield* importLegacyStores(database.db, root, now.getTime())
expect(rows(database.db)).toEqual([{ name: "opencode.global.dat", key: "model", value: "new" }])
expect(yield* listing(root)).toEqual([])
}),
))
test("leaves unreadable files in place", () =>
run(
Effect.gen(function* () {
const root = yield* tempRoot()
const database = openDatabase(":memory:")
yield* writeStore(root, "opencode.global.dat", "{not json")
yield* writeStore(root, "opencode.workspace.x.dat", JSON.stringify({ terminal: "{}" }))
const result = yield* importLegacyStores(database.db, root, now.getTime())
expect(result.imported).toBe(1)
expect(yield* listing(root)).toEqual(["opencode.global.dat"])
}),
))
test("applies draft retention: skips empty, stale, and excess draft files", () =>
run(
Effect.gen(function* () {
const root = yield* tempRoot()
const database = openDatabase(":memory:")
yield* writeStore(root, "opencode.draft.empty.dat", "{}")
yield* writeStore(
root,
"opencode.draft.stale.dat",
JSON.stringify({ "draft:prompt": "old" }),
new Date(now.getTime() - 31 * day),
)
yield* Effect.forEach(
Array.from({ length: 102 }, (_, index) => index),
(index) =>
writeStore(
root,
`opencode.draft.${index}.dat`,
JSON.stringify({ "draft:prompt": `${index}` }),
new Date(now.getTime() - index * 1_000),
),
{ concurrency: 5 },
)
const result = yield* importLegacyStores(database.db, root, now.getTime())
expect(result.imported).toBe(100)
const names = rows(database.db).map((row) => row.name)
expect(names).toContain("opencode.draft.0.dat")
expect(names).toContain("opencode.draft.99.dat")
expect(names).not.toContain("opencode.draft.100.dat")
expect(names).not.toContain("opencode.draft.101.dat")
expect(names).not.toContain("opencode.draft.stale.dat")
expect(names).not.toContain("opencode.draft.empty.dat")
expect(yield* listing(root)).toEqual([])
}),
))
})
@@ -0,0 +1,84 @@
import { Effect, FileSystem, Option, Path, Schema } from "effect"
import type { Database } from "./database"
import { state } from "./schema"
const DRAFT_RETENTION_MS = 30 * 24 * 60 * 60 * 1000
const DRAFT_KEEP_RECENT = 100
const Entries = Schema.fromJsonString(Schema.Record(Schema.String, Schema.Unknown))
const decode = Schema.decodeUnknownOption(Entries)
type Candidate = { name: string; path: string; modified: number; entries: Record<string, unknown> }
// Before the state table existed, every namespace the renderer persisted was an electron-store
// JSON file in userData. Copy them into SQLite once and remove them; this is the only place the
// storage layer touches the filesystem, and it runs before the first renderer request.
export const importLegacyStores = Effect.fn("DesktopStorage.importLegacyStores")(function* (
db: Database,
userData: string,
now = Date.now(),
) {
const fs = yield* FileSystem.FileSystem
const path = yield* Path.Path
const names = (yield* fs.readDirectory(userData).pipe(Effect.orElseSucceed(() => []))).filter(isStoreFile)
const files = yield* Effect.forEach(
names,
Effect.fnUntraced(function* (name) {
const file = path.join(userData, name)
const stats = yield* fs.stat(file).pipe(Effect.orElseSucceed(() => undefined))
if (stats?.type !== "File") return
const raw = yield* fs.readFileString(file).pipe(Effect.orElseSucceed(() => undefined))
if (raw === undefined) return
const entries = decode(raw)
if (Option.isNone(entries)) {
yield* Effect.logWarning("legacy store is not readable, leaving it in place", { name })
return
}
return {
name,
path: file,
modified: Option.getOrElse(stats.mtime, () => new Date(0)).getTime(),
entries: entries.value,
} satisfies Candidate
}),
{ concurrency: 5 },
)
const candidates = files.filter((file) => !!file)
const kept = new Set(
candidates
.filter((file) => isDraft(file.name) && Object.keys(file.entries).length > 0)
.filter((file) => now - file.modified <= DRAFT_RETENTION_MS)
.sort((a, b) => b.modified - a.modified)
.slice(0, DRAFT_KEEP_RECENT)
.map((file) => file.name),
)
const rows = candidates
.filter((file) => !isDraft(file.name) || kept.has(file.name))
.flatMap((file) =>
Object.entries(file.entries).map(([key, value]) => ({
name: file.name,
key,
value: typeof value === "string" ? value : JSON.stringify(value),
updated_at: file.modified,
})),
)
// Existing rows win: a file left behind by an interrupted import must not overwrite newer state.
if (rows.length > 0) {
db.transaction((tx) => {
rows.forEach((row) => tx.insert(state).values(row).onConflictDoNothing().run())
})
}
yield* Effect.forEach(candidates, (file) => fs.remove(file.path, { force: true }), {
concurrency: "unbounded",
discard: true,
})
return { imported: rows.length, removed: candidates.map((file) => file.name) }
})
function isStoreFile(name: string) {
return name === "default.dat" || /^opencode\..+\.dat$/.test(name)
}
function isDraft(name: string) {
return name.startsWith("opencode.draft.")
}
@@ -0,0 +1,17 @@
// Generated by scripts/migration.ts from src/main/storage/migration. Do not edit.
export const migrations = [
{
id: "20260906234153_drafts",
statements: [
"CREATE TABLE `blob` (\n\t`id` text PRIMARY KEY,\n\t`data` blob NOT NULL\n);",
"CREATE TABLE `document` (\n\t`key` text PRIMARY KEY,\n\t`value` text NOT NULL\n);",
],
},
{
id: "20260906234220_state",
statements: [
"CREATE TABLE `state` (\n\t`name` text NOT NULL,\n\t`key` text NOT NULL,\n\t`value` text NOT NULL,\n\t`updated_at` integer NOT NULL,\n\tCONSTRAINT `state_pk` PRIMARY KEY(`name`, `key`)\n);",
],
},
]
@@ -0,0 +1,9 @@
CREATE TABLE `blob` (
`id` text PRIMARY KEY,
`data` blob NOT NULL
);
--> statement-breakpoint
CREATE TABLE `document` (
`key` text PRIMARY KEY,
`value` text NOT NULL
);
@@ -0,0 +1,77 @@
{
"version": "7",
"dialect": "sqlite",
"id": "463be56f-5f29-4831-9d00-1b9c6bd957e3",
"prevIds": [
"00000000-0000-0000-0000-000000000000"
],
"ddl": [
{
"name": "blob",
"entityType": "tables"
},
{
"name": "document",
"entityType": "tables"
},
{
"type": "text",
"notNull": false,
"autoincrement": false,
"default": null,
"generated": null,
"name": "id",
"entityType": "columns",
"table": "blob"
},
{
"type": "blob",
"notNull": true,
"autoincrement": false,
"default": null,
"generated": null,
"name": "data",
"entityType": "columns",
"table": "blob"
},
{
"type": "text",
"notNull": false,
"autoincrement": false,
"default": null,
"generated": null,
"name": "key",
"entityType": "columns",
"table": "document"
},
{
"type": "text",
"notNull": true,
"autoincrement": false,
"default": null,
"generated": null,
"name": "value",
"entityType": "columns",
"table": "document"
},
{
"columns": [
"id"
],
"nameExplicit": false,
"name": "blob_pk",
"table": "blob",
"entityType": "pks"
},
{
"columns": [
"key"
],
"nameExplicit": false,
"name": "document_pk",
"table": "document",
"entityType": "pks"
}
],
"renames": []
}
@@ -0,0 +1,7 @@
CREATE TABLE `state` (
`name` text NOT NULL,
`key` text NOT NULL,
`value` text NOT NULL,
`updated_at` integer NOT NULL,
CONSTRAINT `state_pk` PRIMARY KEY(`name`, `key`)
);
@@ -0,0 +1,131 @@
{
"version": "7",
"dialect": "sqlite",
"id": "5a2f8b7e-2765-4a0b-8877-a74ee2f29b20",
"prevIds": [
"463be56f-5f29-4831-9d00-1b9c6bd957e3"
],
"ddl": [
{
"name": "blob",
"entityType": "tables"
},
{
"name": "document",
"entityType": "tables"
},
{
"name": "state",
"entityType": "tables"
},
{
"type": "text",
"notNull": false,
"autoincrement": false,
"default": null,
"generated": null,
"name": "id",
"entityType": "columns",
"table": "blob"
},
{
"type": "blob",
"notNull": true,
"autoincrement": false,
"default": null,
"generated": null,
"name": "data",
"entityType": "columns",
"table": "blob"
},
{
"type": "text",
"notNull": false,
"autoincrement": false,
"default": null,
"generated": null,
"name": "key",
"entityType": "columns",
"table": "document"
},
{
"type": "text",
"notNull": true,
"autoincrement": false,
"default": null,
"generated": null,
"name": "value",
"entityType": "columns",
"table": "document"
},
{
"type": "text",
"notNull": true,
"autoincrement": false,
"default": null,
"generated": null,
"name": "name",
"entityType": "columns",
"table": "state"
},
{
"type": "text",
"notNull": true,
"autoincrement": false,
"default": null,
"generated": null,
"name": "key",
"entityType": "columns",
"table": "state"
},
{
"type": "text",
"notNull": true,
"autoincrement": false,
"default": null,
"generated": null,
"name": "value",
"entityType": "columns",
"table": "state"
},
{
"type": "integer",
"notNull": true,
"autoincrement": false,
"default": null,
"generated": null,
"name": "updated_at",
"entityType": "columns",
"table": "state"
},
{
"columns": [
"name",
"key"
],
"nameExplicit": false,
"name": "state_pk",
"entityType": "pks",
"table": "state"
},
{
"columns": [
"id"
],
"nameExplicit": false,
"name": "blob_pk",
"table": "blob",
"entityType": "pks"
},
{
"columns": [
"key"
],
"nameExplicit": false,
"name": "document_pk",
"table": "document",
"entityType": "pks"
}
],
"renames": []
}
@@ -0,0 +1,27 @@
import { blob, integer, primaryKey, sqliteTable, text } from "drizzle-orm/sqlite-core"
// Prompt drafts and history. `key` is `${storage}:${key}` as the renderer draft store writes it.
export const document = sqliteTable("document", {
key: text().primaryKey(),
value: text().notNull(),
})
// Images referenced from documents by content hash.
export const blobs = sqliteTable("blob", {
id: text().primaryKey(),
data: blob({ mode: "buffer" }).notNull(),
})
// Everything the renderer persists through `platform.storage(name)`. `name` is the storage
// namespace the app chooses (still spelled like the `.dat` file it used to be) and `key` the
// entry within it, so the app's relocation and alias logic keeps working unchanged.
export const state = sqliteTable(
"state",
{
name: text().notNull(),
key: text().notNull(),
value: text().notNull(),
updated_at: integer().notNull(),
},
(table) => [primaryKey({ columns: [table.name, table.key] })],
)
@@ -0,0 +1,111 @@
import { afterEach, describe, expect, test } from "bun:test"
import { mkdtemp, rm } from "node:fs/promises"
import { tmpdir } from "node:os"
import path from "node:path"
import { sql } from "drizzle-orm"
import { openDatabase } from "./database"
import { createStateStore } from "./state"
const roots: string[] = []
// Bun's node:sqlite shim keeps prepared statements alive after close(), which pins the WAL files
// on Windows. Node (and so Electron) finalizes them; tolerate the leftover here only.
afterEach(() =>
Promise.all(roots.splice(0).map((root) => rm(root, { recursive: true, force: true }).catch(() => undefined))),
)
const open = () => {
const database = openDatabase(":memory:")
return { db: database.db, store: createStateStore(database.db, { delay: 1_000 }) }
}
const rows = (db: ReturnType<typeof openDatabase>["db"]) =>
db.all<{ name: string; key: string; value: string }>(sql`SELECT name, key, value FROM state ORDER BY name, key`)
describe("state store", () => {
test("reads its own queued writes before they reach the database", () => {
const { db, store } = open()
store.set("global", "model", "a")
expect(store.get("global", "model")).toBe("a")
expect(rows(db)).toEqual([])
store.flush()
expect(rows(db)).toEqual([{ name: "global", key: "model", value: "a" }])
expect(store.get("global", "model")).toBe("a")
})
test("delete is visible immediately and removes the row on flush", () => {
const { db, store } = open()
store.set("global", "model", "a")
store.flush()
store.delete("global", "model")
expect(store.get("global", "model")).toBeNull()
expect(store.keys("global")).toEqual([])
store.flush()
expect(rows(db)).toEqual([])
})
test("keys and length merge stored rows with queued changes", () => {
const { store } = open()
store.set("w", "tabs", "[]")
store.set("w", "recent", "{}")
store.flush()
store.set("w", "info", "{}")
store.delete("w", "recent")
expect(store.keys("w").sort()).toEqual(["info", "tabs"])
expect(store.length("w")).toBe(2)
expect(store.keys("other")).toEqual([])
})
test("clear drops a namespace including queued writes and leaves others alone", () => {
const { db, store } = open()
store.set("w1", "tabs", "[]")
store.set("w2", "tabs", "[]")
store.flush()
store.set("w1", "recent", "{}")
store.clear("w1")
expect(store.get("w1", "recent")).toBeNull()
store.flush()
expect(rows(db)).toEqual([{ name: "w2", key: "tabs", value: "[]" }])
})
test("a failed flush keeps every acknowledged write until a later flush succeeds", () => {
const database = openDatabase(":memory:")
const errors: unknown[] = []
const store = createStateStore(database.db, { delay: 1_000, onError: (error) => errors.push(error) })
store.set("w", "tabs", "[1]")
store.set("w", "recent", "{}")
database.db.run(sql`DROP TABLE state`)
store.flush()
expect(errors).toHaveLength(1)
expect(store.get("w", "tabs")).toBe("[1]")
database.db.run(
sql`CREATE TABLE state (name TEXT NOT NULL, key TEXT NOT NULL, value TEXT NOT NULL, updated_at INTEGER NOT NULL, PRIMARY KEY (name, key))`,
)
store.set("w", "info", "{}")
store.flush()
expect(rows(database.db)).toEqual([
{ name: "w", key: "info", value: "{}" },
{ name: "w", key: "recent", value: "{}" },
{ name: "w", key: "tabs", value: "[1]" },
])
})
test("survives close and reopen on disk", async () => {
const root = await mkdtemp(path.join(tmpdir(), "opencode-state-"))
roots.push(root)
const file = path.join(root, "drafts.sqlite")
const first = openDatabase(file)
const store = createStateStore(first.db, { delay: 1_000 })
store.set("global", "model", "a")
store.close()
first.close()
const second = openDatabase(file)
expect(createStateStore(second.db).get("global", "model")).toBe("a")
second.close()
})
test("a burst of writes to one key lands as a single upsert", () => {
const { db, store } = open()
for (let index = 0; index < 100; index++) store.set("global", "layout", `${index}`)
store.flush()
expect(rows(db)).toEqual([{ name: "global", key: "layout", value: "99" }])
})
})
@@ -0,0 +1,78 @@
import { and, eq, sql } from "drizzle-orm"
import type { Database } from "./database"
import { state } from "./schema"
import { createWriteBehind } from "./write-behind"
export type StateStore = ReturnType<typeof createStateStore>
type Row = { name: string; key: string; value: string | null }
// Reads hit SQLite directly: they happen at mount time and a point lookup on the primary key
// costs microseconds, so a second in-memory copy would only duplicate the renderer's cache.
export function createStateStore(db: Database, input: { delay?: number; onError?: (error: unknown) => void } = {}) {
// Prepared once; the flush loop then only binds values instead of rebuilding SQL per row.
const byKey = and(eq(state.name, sql.placeholder("name")), eq(state.key, sql.placeholder("key")))
const read = db.select({ value: state.value }).from(state).where(byKey).prepare()
const remove = db.delete(state).where(byKey).prepare()
const upsert = db
.insert(state)
.values({
name: sql.placeholder("name"),
key: sql.placeholder("key"),
value: sql.placeholder("value"),
updated_at: sql.placeholder("updated_at"),
})
.onConflictDoUpdate({
target: [state.name, state.key],
set: { value: sql.placeholder("value"), updated_at: sql.placeholder("updated_at") },
})
.prepare()
const writer = createWriteBehind<Row>({
delay: input.delay ?? 250,
onError: input.onError,
write: (batch) =>
db.transaction(() => {
const updated_at = Date.now()
for (const row of batch.values()) {
if (row.value === null) remove.run({ name: row.name, key: row.key })
else upsert.run({ name: row.name, key: row.key, value: row.value, updated_at })
}
}),
})
const id = (name: string, key: string) => `${name}\0${key}`
const keys = (name: string) => {
const result = new Set(
db
.select({ key: state.key })
.from(state)
.where(eq(state.name, name))
.all()
.map((row) => row.key),
)
for (const row of writer.entries()) {
if (row.name !== name) continue
if (row.value === null) result.delete(row.key)
else result.add(row.key)
}
return [...result]
}
return {
get(name: string, key: string) {
const queued = writer.get(id(name, key))
if (queued) return queued.value
return read.get({ name, key })?.value ?? null
},
set: (name: string, key: string, value: string) => writer.set(id(name, key), { name, key, value }),
delete: (name: string, key: string) => writer.set(id(name, key), { name, key, value: null }),
keys,
length: (name: string) => keys(name).length,
// Rare (window closed for good, explicit clear) so it goes straight to the database.
clear(name: string) {
writer.drop((row) => row.name === name)
db.delete(state).where(eq(state.name, name)).run()
},
flush: writer.flush,
close: writer.close,
}
}
+5 -10
View File
@@ -1,12 +1,15 @@
import Store from "electron-store"
import electron from "electron"
import { Effect } from "effect"
import { deleteStoreFileIfEmpty } from "./cleanup"
import { SETTINGS_STORE } from "./keys"
const cache = new Map<string, Store>()
// Main-process settings only (onboarding, default server, window list, appearance, updater).
// These are read synchronously before the storage layer exists and written on user action, so
// electron-store's synchronous file write is acceptable here. Renderer state goes through
// DesktopStorage instead.
//
// We cannot instantiate the electron-store at module load time because
// module import hoisting causes this to run before app.setPath("userData", ...)
// in index.ts has executed, which would result in files being written to the default directory
@@ -23,11 +26,3 @@ export function getStore(name = SETTINGS_STORE) {
cache.set(name, next)
return next
}
export const removeStoreFileIfEmpty = Effect.fn("DesktopStorage.removeStoreFileIfEmpty")(function* (name: string) {
if (yield* deleteStoreFileIfEmpty(electron.app.getPath("userData"), name)) cache.delete(name)
})
export function forgetStore(name: string) {
cache.delete(name)
}
@@ -0,0 +1,73 @@
import { describe, expect, test } from "bun:test"
import { createWriteBehind } from "./write-behind"
const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
describe("write-behind", () => {
test("coalesces a burst into one batch with the latest value per key", async () => {
const batches: Map<string, number>[] = []
const writer = createWriteBehind<number>({ delay: 10, write: (batch) => batches.push(batch) })
writer.set("a", 1)
writer.set("b", 1)
writer.set("a", 2)
expect(writer.get("a")).toBe(2)
expect(batches).toHaveLength(0)
await wait(30)
expect(batches).toHaveLength(1)
expect([...batches[0]!]).toEqual([
["a", 2],
["b", 1],
])
expect(writer.has("a")).toBe(false)
})
test("flush writes immediately and close stops accepting writes", () => {
const batches: Map<string, number>[] = []
const writer = createWriteBehind<number>({ delay: 1_000, write: (batch) => batches.push(batch) })
writer.set("a", 1)
writer.flush()
expect(batches).toHaveLength(1)
writer.set("b", 2)
writer.close()
expect(batches).toHaveLength(2)
writer.set("c", 3)
writer.flush()
expect(batches).toHaveLength(2)
})
test("drop removes queued entries matching a predicate", () => {
const batches: Map<string, number>[] = []
const writer = createWriteBehind<number>({ delay: 1_000, write: (batch) => batches.push(batch) })
writer.set("a", 1)
writer.set("b", 2)
writer.drop((value) => value === 1)
writer.flush()
expect([...batches[0]!]).toEqual([["b", 2]])
})
test("keeps a failed batch queued and retries it on the next flush", () => {
const errors: unknown[] = []
let fail = true
const batches: Map<string, number>[] = []
const writer = createWriteBehind<number>({
delay: 1_000,
onError: (error) => errors.push(error),
write: (batch) => {
if (fail) throw new Error("disk full")
batches.push(batch)
},
})
writer.set("a", 1)
writer.set("b", 1)
writer.flush()
expect(errors).toHaveLength(1)
expect(writer.get("a")).toBe(1)
fail = false
writer.set("a", 2)
writer.flush()
expect([...batches[0]!].sort()).toEqual([
["a", 2],
["b", 1],
])
})
})
@@ -0,0 +1,47 @@
// Coalesces a burst of writes into one batch. The latest value per key wins, so a store that
// re-serializes on every mutation costs one row write per flush instead of one per change.
export function createWriteBehind<T>(input: {
delay: number
write: (batch: Map<string, T>) => void
onError?: (error: unknown) => void
}) {
const pending = new Map<string, T>()
let timer: ReturnType<typeof setTimeout> | undefined
let closed = false
const flush = () => {
clearTimeout(timer)
timer = undefined
if (closed || pending.size === 0) return
const batch = new Map(pending)
pending.clear()
try {
input.write(batch)
} catch (error) {
// The renderer already saw these writes succeed. Keep them queued so the next flush retries
// them; anything written for the same key since then takes precedence.
for (const [key, value] of batch) if (!pending.has(key)) pending.set(key, value)
if (!input.onError) throw error
input.onError(error)
}
}
return {
get: (key: string) => pending.get(key),
has: (key: string) => pending.has(key),
entries: () => pending.values(),
set(key: string, value: T) {
if (closed) return
pending.set(key, value)
timer ??= setTimeout(flush, input.delay)
},
drop(predicate: (value: T) => boolean) {
for (const [key, value] of pending) if (predicate(value)) pending.delete(key)
},
flush,
close() {
flush()
closed = true
},
}
}
+7 -6
View File
@@ -5,7 +5,8 @@ import { Effect, FileSystem, Path } from "effect"
import { openExternalURL } from "../files"
import { scoped } from "../native/logging"
import { DesktopPaths } from "../paths"
import { forgetStore, getStore } from "../storage/store"
import { DesktopStorage } from "../storage"
import { getStore } from "../storage/store"
import { WINDOW_IDS_KEY } from "../storage/keys"
import { windowIDArgument } from "../../shared/window-bootstrap"
import {
@@ -77,6 +78,7 @@ export const makeMainWindows = Effect.fn("Window.make")(function* () {
const fs = yield* FileSystem.FileSystem
const path = yield* Path.Path
const paths = yield* DesktopPaths.resolve
const storage = yield* DesktopStorage.Service
const runFork = Effect.runForkWith(yield* Effect.context())
const wireWindowRecovery = yield* makeWindowRecovery
@@ -141,14 +143,12 @@ export const makeMainWindows = Effect.fn("Window.make")(function* () {
win.on("session-end", () => registry.setQuitting())
win.on("closed", () => {
if (!registry.closed(id)) return
const data = windowDataFile(id)
runFork(
Effect.gen(function* () {
yield* Effect.try(() => storage.state.clear(windowDataFile(id)))
yield* fs.remove(path.join(app.getPath("userData"), windowStateFile(id)), { force: true })
yield* fs.remove(path.join(app.getPath("userData"), data), { force: true })
}).pipe(
Effect.tap(() => Effect.sync(() => forgetStore(data))),
Effect.catch((error) => scoped("window", Effect.logError("failed to clean window files", { id, error }))),
Effect.catch((error) => scoped("window", Effect.logError("failed to clean window state", { id, error }))),
),
)
})
@@ -161,7 +161,8 @@ function windowStateFile(id: string) {
return `window-state-${safeWindowID(id)}.json`
}
// Mirrors windowStorage() in packages/app/src/utils/persist.ts.
// Mirrors windowStorage() in packages/app/src/runtime/persistence/storage.ts; it is the state
// namespace the renderer persists this window's tabs under.
function windowDataFile(id: string) {
return `opencode.window.${safeWindowID(id)}.dat`
}
+6 -2
View File
@@ -7407,7 +7407,7 @@
}
}
},
"description": "Update project display metadata and workspace commands.",
"description": "Update the project canonical directory, display metadata, and workspace commands.",
"summary": "Update project",
"requestBody": {
"content": {
@@ -7415,6 +7415,9 @@
"schema": {
"type": "object",
"properties": {
"canonical": {
"type": "string"
},
"name": {
"type": "string"
},
@@ -14589,7 +14592,8 @@
"anyOf": [
{
"type": "string",
"enum": ["random"]
"enum": ["random"],
"description": "Reuse a randomly selected provider until it is rate limited, then switch to another available provider."
},
{
"type": "string"
+1 -1
View File
@@ -29,7 +29,7 @@ export const ProjectGroup = HttpApiGroup.make("server.project")
OpenApi.annotations({
identifier: "v2.project.update",
summary: "Update project",
description: "Update project display metadata and workspace commands.",
description: "Update the project canonical directory, display metadata, and workspace commands.",
}),
),
)
+7 -1
View File
@@ -4,7 +4,13 @@ import { Schema } from "effect"
import { WebSearch } from "../websearch.js"
export class Info extends Schema.Class<Info>("ConfigWebSearch.Info")({
provider: Schema.Union([Schema.Literal("random"), WebSearch.ID]),
provider: Schema.Union([
Schema.Literal("random").annotate({
description:
"Reuse a randomly selected provider until it is rate limited, then switch to another available provider.",
}),
WebSearch.ID,
]),
}) {}
export const Selection = Schema.Union([Schema.Literal(false), Info])
+1
View File
@@ -50,6 +50,7 @@ export interface Info extends Schema.Schema.Type<typeof Info> {}
export const UpdateInput = Schema.Struct({
projectID: ID,
canonical: optional(AbsolutePath),
name: optional(Schema.String),
icon: optional(Icon),
commands: optional(Commands),
+1 -1
View File
@@ -233,7 +233,7 @@ export namespace Execution {
export const Interrupted = Event.durable({
type: "session.execution.interrupted",
...options,
schema: { ...Base, reason: Schema.Literals(["user", "shutdown", "superseded"]) },
schema: { ...Base, reason: Schema.Literals(["user", "shutdown", "superseded", "inactivity"]) },
})
export type Interrupted = typeof Interrupted.Type
}
+1
View File
@@ -0,0 +1 @@
export declare function componentConfig(directory: string): import("@playwright/test").PlaywrightTestConfig<{}, {}>;
+9
View File
@@ -0,0 +1,9 @@
import { expect } from "@playwright/test";
import type { Locator } from "@playwright/test";
export { expect };
export declare const story: import("@playwright/test").TestType<import("@playwright/test").PlaywrightTestArgs & import("@playwright/test").PlaywrightTestOptions & {
mount: (id: string, options?: {
args?: Record<string, string | boolean> | undefined;
globals?: Record<string, string> | undefined;
} | undefined) => Promise<Locator>;
}, import("@playwright/test").PlaywrightWorkerArgs & import("@playwright/test").PlaywrightWorkerOptions>;
@@ -0,0 +1,212 @@
import {
CliRenderer,
RGBA,
RootTextNodeRenderable,
TextNodeRenderable,
type Renderable,
type RenderContext,
type TextNodeOptions,
} from "@opentui/core"
import { extend } from "@opentui/solid"
import { createEffect, onCleanup } from "solid-js"
import { smootherstep } from "./tab-pulse"
import { stringWidth } from "../util/string-width"
import { webSearchProviderName } from "../util/tool-display"
type Value = { id: string; provider: string; running: boolean }
type Options = TextNodeOptions & { value?: Value; enabled?: boolean }
const FADE = 80
const RESIZE = 60
const DURATION = FADE * 2 + RESIZE
/** An inline span: keep native wrapping, selection, and the surrounding tool's styles. */
export class RetryProviderRenderable extends TextNodeRenderable {
private readonly ctx: CliRenderer
private current?: Value
private enabledValue: boolean
private painted = false
private disposed = false
private elapsed: number | undefined
private fresh = false
private source = ""
private target = ""
private displayed = ""
private opacity = 1
private fromOpacity = 1
constructor(ctx: RenderContext, options: Options) {
super(options)
if (!(ctx instanceof CliRenderer)) throw new Error("RetryProvider requires a renderer frame clock")
this.ctx = ctx
this.enabledValue = options.enabled ?? true
if (options.value) this.value = options.value
}
private markPainted = () => {
if (!this.onScreen()) return
this.painted = true
this.ctx.off("frame", this.markPainted)
}
private onScreen() {
let node: TextNodeRenderable | null = this
while (node && !(node instanceof RootTextNodeRenderable)) {
if (!node.visible) return false
node = node.parent
}
if (!node) return false
const text = node.textParent
if (
text.width <= 0 ||
text.height <= 0 ||
text.screenX < 0 ||
text.screenY < 0 ||
text.screenX + text.width > this.ctx.width ||
text.screenY + text.height > this.ctx.height
)
return false
// Be conservative with clipped rows: do not start a transition as history enters the viewport.
for (let parent: Renderable | null = text; parent; parent = parent.parent) {
if (!parent.visible) return false
if (parent.overflow === "visible") continue
if (
text.screenX < parent.screenX ||
text.screenY < parent.screenY ||
text.screenX + text.width > parent.screenX + parent.width ||
text.screenY + text.height > parent.screenY + parent.height
)
return false
}
return true
}
set value(value: Value) {
if (this.disposed) return
const previous = this.current
this.current = value
if (!value.running) this.ctx.off("frame", this.markPainted)
if (previous?.id === value.id && previous.provider === value.provider) return
this.target = webSearchProviderName(value.provider)
const retry =
this.enabledValue &&
this.painted &&
previous?.id === value.id &&
previous.running &&
value.running &&
this.onScreen()
if (!retry) {
this.finish()
this.painted = false
this.ctx.off("frame", this.markPainted)
if (value.running) this.ctx.on("frame", this.markPainted)
return
}
// During fade-out, replace the destination without restarting or queuing transitions.
if (this.elapsed !== undefined && this.elapsed < FADE) return
this.source = this.displayed
this.fromOpacity = this.opacity
const start = this.elapsed === undefined
this.elapsed = 0
this.fresh = true
if (start) {
this.ctx.setFrameCallback(this.tick)
this.ctx.requestLive()
}
}
set enabled(value: boolean) {
if (value === this.enabledValue) return
this.enabledValue = value
if (!value) this.finish()
}
private tick = async (delta: number) => {
if (this.elapsed === undefined) return
if (!this.onScreen()) {
this.finish()
this.painted = false
if (this.current?.running) this.ctx.on("frame", this.markPainted)
return
}
// Ignore time the renderer spent idle before this transition started.
this.elapsed += this.fresh ? 0 : delta
this.fresh = false
if (this.elapsed >= DURATION) return this.finish()
if (this.elapsed < FADE) {
return this.show(this.source, this.fromOpacity * (1 - smootherstep(this.elapsed / FADE)))
}
if (this.elapsed < FADE + RESIZE) {
// Move the query only while the name is invisible; never reveal partial provider glyphs.
const from = stringWidth(this.source)
const to = stringWidth(this.target)
return this.show(" ".repeat(Math.round(from + (to - from) * smootherstep((this.elapsed - FADE) / RESIZE))), 0)
}
this.show(this.target, smootherstep((this.elapsed - FADE - RESIZE) / FADE))
}
private show(text: string, opacity: number) {
if (text === this.displayed && opacity === this.opacity) return
this.displayed = text
this.opacity = opacity
this.children = [text]
}
private stop() {
if (this.elapsed !== undefined) {
this.ctx.removeFrameCallback(this.tick)
this.ctx.dropLive()
}
this.elapsed = undefined
}
private finish() {
this.stop()
this.show(this.target, 1)
}
override gatherWithInheritedStyle(style?: Parameters<TextNodeRenderable["gatherWithInheritedStyle"]>[0]) {
const chunks = super.gatherWithInheritedStyle(style)
if (this.opacity === 1) return chunks
return chunks.map((chunk) => {
const fg = RGBA.clone(chunk.fg ?? RGBA.defaultForeground())
fg.a *= this.opacity
return { ...chunk, fg }
})
}
override destroy() {
if (this.disposed) return
this.disposed = true
this.ctx.off("frame", this.markPainted)
this.stop()
super.destroy()
}
override destroyRecursively() {
this.destroy()
}
}
extend({ retry_provider: RetryProviderRenderable })
export function RetryProvider(props: { value: Value; enabled: boolean }) {
// Solid's text-node reconciler only applies inline styles; control the custom span through its ref.
return (
<retry_provider
ref={(node) => {
onCleanup(() => node.destroy())
createEffect(() => {
node.enabled = props.enabled
node.value = props.value
})
}}
/>
)
}
declare module "@opentui/solid" {
interface OpenTUIComponents {
retry_provider: typeof RetryProviderRenderable
}
}
+19 -2
View File
@@ -47,8 +47,8 @@ import {
primitiveInputSummary,
toolDisplayContent,
toolDisplayMetadata,
webSearchProviderLabel,
} from "../../util/tool-display"
import { RetryProvider } from "../../component/retry-provider"
import { useRenderer, useTerminalDimensions, type JSX } from "@opentui/solid"
import { useClient } from "../../context/client"
import { useEditorContext } from "../../context/editor"
@@ -3374,9 +3374,26 @@ function WebFetch(props: ToolProps) {
}
function WebSearch(props: ToolProps) {
const ctx = use()
const provider = createMemo(() => stringValue(props.metadata.provider))
return (
<InlineTool icon="◈" pending="Searching web…" complete={stringValue(props.input.query)} part={props.part}>
{webSearchProviderLabel(props.metadata.provider)} "{stringValue(props.input.query)}"
<Show when={provider()} fallback="Web Search">
{(value) => (
<>
Web Search via{" "}
<RetryProvider
value={{
id: `${ctx.sessionID}:${props.part.time.created}:${props.part.id}`,
provider: value(),
running: props.part.state.status === "running",
}}
enabled={ctx.config.animations ?? true}
/>
</>
)}
</Show>{" "}
"{stringValue(props.input.query)}"
</InlineTool>
)
}
+7 -2
View File
@@ -19,9 +19,14 @@ export function primitiveInputSummary(input: Record<string, unknown>, omit: read
return `[${entries.map(([key, value]) => `${key}=${String(value)}`).join(", ")}]`
}
export function webSearchProviderName(provider: unknown) {
if (typeof provider !== "string" || !provider) return ""
return `${provider[0].toUpperCase()}${provider.slice(1)}`
}
export function webSearchProviderLabel(provider: unknown) {
if (typeof provider !== "string" || !provider) return "Web Search"
return `Web Search via ${provider[0].toUpperCase()}${provider.slice(1)}`
const name = webSearchProviderName(provider)
return name ? `Web Search via ${name}` : "Web Search"
}
export function toolDisplayMetadata(state: unknown): Record<string, unknown> {
@@ -0,0 +1,280 @@
import { expect, test } from "bun:test"
import { BoxRenderable, RGBA, TextAttributes, TextRenderable } from "@opentui/core"
import { createTestRenderer, ManualClock } from "@opentui/core/testing"
import { testRender } from "@opentui/solid"
import { createSignal, Show } from "solid-js"
import { RetryProvider, RetryProviderRenderable } from "../../src/component/retry-provider"
async function fixture() {
const clock = new ManualClock()
const app = await createTestRenderer({ width: 60, height: 2, useThread: false, clock })
app.renderer.pause()
const text = new TextRenderable(app.renderer, { fg: "#eeeeee", bg: "#111111", attributes: TextAttributes.BOLD })
const provider = new RetryProviderRenderable(app.renderer, {
value: { id: "call-1", provider: "exa", running: true },
})
text.add("Web Search via ")
text.add(provider)
text.add(' "query"')
app.renderer.root.add(text)
const renderOnce = async () => {
await app.waitFor(() => !app.renderer.getSchedulerState().isRendering)
await app.renderOnce()
}
return {
...app,
clock,
text,
provider,
renderOnce,
step: async (millis: number) => {
clock.setTime(clock.now() + millis)
await renderOnce()
},
[Symbol.dispose]: () => {
provider.destroy()
app.renderer.destroy()
},
}
}
test("only an already-painted running call starts a provider transition", async () => {
using app = await fixture()
// Updates before the first paint are not a visible retry.
app.provider.value = { id: "call-1", provider: "parallel", running: true }
await app.renderOnce()
expect(app.captureCharFrame().trim()).toBe('Web Search via Parallel "query"')
expect(app.renderer.liveRequestCount).toBe(0)
app.provider.value = { id: "call-1", provider: "parallel", running: true }
app.provider.value = { id: "call-1", provider: "parallel", running: false }
app.provider.value = { id: "call-1", provider: "tavily", running: false }
await app.renderOnce()
expect(app.captureCharFrame().trim()).toBe('Web Search via Tavily "query"')
expect(app.renderer.liveRequestCount).toBe(0)
// A new invocation/history view must not animate from the previous call's label.
app.provider.value = { id: "call-2", provider: "exa", running: true }
await app.renderOnce()
expect(app.captureCharFrame().trim()).toBe('Web Search via Exa "query"')
expect(app.renderer.liveRequestCount).toBe(0)
})
test("fades only the provider, changes width while invisible, and settles without a timer", async () => {
using app = await fixture()
await app.renderOnce()
const initial = app.captureSpans()
app.clock.setTime(2000)
app.provider.value = { id: "call-1", provider: "parallel", running: true }
await app.renderOnce()
expect(app.captureSpans()).toEqual(initial)
expect(app.renderer.liveRequestCount).toBe(1)
await app.step(40)
const middle = app.captureSpans().lines[0].spans
expect(middle.find((span) => span.text.includes("Exa"))?.fg.toInts()).not.toEqual(
initial.lines[0].spans[0].fg.toInts(),
)
expect(middle.find((span) => span.text.includes("Web Search"))?.fg.toInts()).toEqual(
initial.lines[0].spans[0].fg.toInts(),
)
expect(middle.find((span) => span.text.includes("query"))?.fg.toInts()).toEqual(initial.lines[0].spans[0].fg.toInts())
expect(
middle.filter((span) => span.text.trim()).every((span) => Boolean(span.attributes & TextAttributes.BOLD)),
).toBe(true)
await app.step(40)
const before = app.captureCharFrame().indexOf('"query"')
await app.step(30)
const moving = app.captureCharFrame().indexOf('"query"')
expect(moving).toBeGreaterThan(before)
expect(moving).toBeLessThan("Web Search via Parallel ".length)
await app.step(30)
await app.step(80)
expect(app.captureCharFrame().trim()).toBe('Web Search via Parallel "query"')
expect(app.captureSpans().lines[0].spans[0].fg.toInts()).toEqual(initial.lines[0].spans[0].fg.toInts())
expect(app.renderer.liveRequestCount).toBe(0)
})
test("coalesces rapid fallbacks and does not restart on completion", async () => {
using app = await fixture()
await app.renderOnce()
app.provider.value = { id: "call-1", provider: "parallel", running: true }
await app.renderOnce()
await app.step(40)
app.provider.value = { id: "call-1", provider: "firecrawl", running: true }
app.provider.value = { id: "call-1", provider: "firecrawl", running: false }
await app.step(180)
expect(app.captureCharFrame().trim()).toBe('Web Search via Firecrawl "query"')
expect(app.renderer.liveRequestCount).toBe(0)
})
test("retargets a fading-in provider without flashing back to full brightness", async () => {
using app = await fixture()
await app.renderOnce()
app.provider.value = { id: "call-1", provider: "parallel", running: true }
await app.renderOnce()
await app.step(180)
const middle = app.captureSpans()
app.provider.value = { id: "call-1", provider: "tavily", running: true }
await app.renderOnce()
expect(app.captureSpans()).toEqual(middle)
await app.step(220)
expect(app.captureCharFrame().trim()).toBe('Web Search via Tavily "query"')
expect(app.renderer.liveRequestCount).toBe(0)
})
test("disabling animations settles immediately and recursive destruction releases active work", async () => {
using app = await fixture()
await app.renderOnce()
app.provider.value = { id: "call-1", provider: "parallel", running: true }
await app.renderOnce()
app.provider.enabled = false
await app.renderOnce()
expect(app.captureCharFrame().trim()).toBe('Web Search via Parallel "query"')
expect(app.renderer.liveRequestCount).toBe(0)
app.provider.value = { id: "call-1", provider: "exa", running: true }
await app.renderOnce()
expect(app.captureCharFrame().trim()).toBe('Web Search via Exa "query"')
expect(app.renderer.liveRequestCount).toBe(0)
app.provider.enabled = true
app.provider.value = { id: "call-1", provider: "tavily", running: true }
expect(app.renderer.liveRequestCount).toBe(1)
app.text.remove(app.provider)
app.provider.destroyRecursively()
expect(app.renderer.liveRequestCount).toBe(0)
})
test("a hidden first paint does not arm the transition", async () => {
using app = await fixture()
app.text.visible = false
await app.renderOnce()
app.provider.value = { id: "call-1", provider: "parallel", running: true }
await app.renderOnce()
expect(app.renderer.liveRequestCount).toBe(0)
app.text.visible = true
await app.renderOnce()
expect(app.captureCharFrame().trim()).toBe('Web Search via Parallel "query"')
expect(app.renderer.liveRequestCount).toBe(0)
app.provider.value = { id: "call-1", provider: "tavily", running: true }
expect(app.renderer.liveRequestCount).toBe(1)
})
test("offscreen and ancestor-clipped rows show the latest provider on entry, without animation", async () => {
using app = await fixture()
app.text.top = 4
await app.renderOnce()
app.provider.value = { id: "call-1", provider: "parallel", running: true }
await app.renderOnce()
expect(app.renderer.liveRequestCount).toBe(0)
app.text.top = 0
await app.renderOnce()
expect(app.captureCharFrame().trim()).toBe('Web Search via Parallel "query"')
const clip = new BoxRenderable(app.renderer, { width: 50, height: 1, overflow: "hidden" })
app.renderer.root.remove(app.text)
clip.add(app.text)
app.renderer.root.add(clip)
app.text.top = 1
await app.renderOnce()
app.provider.value = { id: "call-1", provider: "tavily", running: true }
await app.renderOnce()
expect(app.renderer.liveRequestCount).toBe(0)
app.text.top = 0
await app.renderOnce()
expect(app.captureCharFrame().trim()).toBe('Web Search via Tavily "query"')
expect(app.renderer.liveRequestCount).toBe(0)
})
test("a transition settles when hidden and waits for a new paint before animating again", async () => {
using app = await fixture()
await app.renderOnce()
app.provider.value = { id: "call-1", provider: "parallel", running: true }
await app.renderOnce()
await app.step(40)
app.text.visible = false
await app.step(40)
expect(app.renderer.liveRequestCount).toBe(0)
expect(app.captureCharFrame().trim()).toBe("")
app.text.visible = true
app.provider.value = { id: "call-1", provider: "tavily", running: true }
expect(app.renderer.liveRequestCount).toBe(0)
await app.renderOnce()
expect(app.captureCharFrame().trim()).toBe('Web Search via Tavily "query"')
app.provider.value = { id: "call-1", provider: "exa", running: true }
expect(app.renderer.liveRequestCount).toBe(1)
app.text.top = 4
await app.step(40)
expect(app.renderer.liveRequestCount).toBe(0)
app.text.top = 0
await app.renderOnce()
expect(app.captureCharFrame().trim()).toBe('Web Search via Exa "query"')
expect(app.renderer.liveRequestCount).toBe(0)
})
test("Solid unmount releases a running transition and its frame listener", async () => {
const clock = new ManualClock()
const [visible, setVisible] = createSignal(true)
const [provider, setProvider] = createSignal("exa")
const app = await testRender(
() => (
<Show when={visible()}>
<text fg="#eeeeee">
Web Search via <RetryProvider value={{ id: "call", provider: provider(), running: true }} enabled={true} />
</text>
</Show>
),
{ width: 60, height: 2, useThread: false, clock },
)
try {
app.renderer.pause()
await app.renderOnce()
setProvider("parallel")
await app.waitFor(() => app.renderer.liveRequestCount === 1)
setVisible(false)
await app.waitFor(() => app.renderer.liveRequestCount === 0)
await app.renderOnce()
expect(app.captureCharFrame().trim()).toBe("")
expect(app.renderer.listenerCount("frame")).toBe(0)
} finally {
app.renderer.destroy()
}
})
test("the Solid inline span inherits colors and wraps like ordinary text after a Unicode retry", async () => {
const clock = new ManualClock()
const [provider, setProvider] = createSignal("firecrawl")
const [running, setRunning] = createSignal(true)
const app = await testRender(
() => (
<box width={24}>
<text fg="#222222" bg="#eeeeee">
Web Search via{" "}
<RetryProvider value={{ id: "call", provider: provider(), running: running() }} enabled={true} /> "a longer
query"
</text>
<text fg="#222222" bg="#eeeeee">
Web Search via "a longer query"
</text>
</box>
),
{ width: 24, height: 6, useThread: false, clock },
)
try {
app.renderer.pause()
await app.renderOnce()
expect(app.captureCharFrame()).toContain("Firecrawl")
setProvider("日本語")
await app.renderOnce()
clock.setTime(220)
await app.renderOnce()
setRunning(false)
await app.renderOnce()
const lines = app
.captureCharFrame()
.trimEnd()
.split("\n")
.map((line) => line.trimEnd())
expect(lines.slice(0, 2)).toEqual(lines.slice(2, 4))
expect(app.captureCharFrame()).not.toContain("Firecrawl")
expect(app.captureSpans().lines[0].spans[0].fg.equals(RGBA.fromHex("#222222"))).toBe(true)
expect(app.renderer.liveRequestCount).toBe(0)
} finally {
app.renderer.destroy()
}
})
@@ -231,6 +231,7 @@
[data-component="button-v2"][data-variant="ghost-muted"] {
background-color: transparent;
color: var(--v2-text-text-muted);
font-weight: 440;
}
[data-component="button-v2"][data-variant="ghost-muted"] [data-slot="icon-svg"] {
+8
View File
@@ -100,6 +100,14 @@ const icons = {
viewBox: "0 0 16 16",
body: `<path d="M2 10.668V14.0013H10.6667M13.9974 10.6667V2H2.66406M13.9974 10.668V14.0013H10.6641M2 10V2H5.33333" stroke="currentColor" stroke-miterlimit="10" stroke-linecap="square"/><path d="M10.6693 10.6654V5.33203H5.33594V10.6654H10.6693Z" fill="currentColor"/>`,
},
"outline-worktree": {
viewBox: "0 0 16 16",
body: `<path d="M12 3.11133L14.3885 5.49977L12 7.88822M14.5 5.49972H10.058L5.13456 11.8139M14.5 11.6606L8.5 11.6606M5.50012 11.6597H1.5" stroke="currentColor"/>`,
},
"outline-trash": {
viewBox: "0 0 16 16",
body: `<path d="M2.44434 4.22224H13.5554M5.99994 4.22224V2.44446C5.99994 1.95557 5.99992 1.55557 5.99992 1.55557H9.99989C9.99989 1.55557 9.99994 1.95557 9.99994 2.44446V4.22224M6.55545 7.77779L6.7485 11.7778M9.44437 7.77779L9.2513 11.7778M12.1756 6.88891L11.8666 12.7556C11.8168 13.7068 11.7748 14.4445 11.7748 14.4445H4.22511C4.22511 14.4445 4.18392 13.7067 4.13414 12.7556L3.82509 6.88891" stroke="currentColor"/>`,
},
close: {
viewBox: "0 0 20 20",
body: `<path d="M14.4446 5.55566L5.55566 14.4446M5.55566 5.55566L14.4446 14.4446" stroke="currentColor" stroke-linejoin="round"/>`,
@@ -65,6 +65,13 @@
}
}
[data-component="tooltip-v2"][data-appearance="large"] {
padding: 12px;
font-size: 13px;
line-height: var(--line-height-compact);
letter-spacing: -0.04px;
}
@keyframes tooltipV2In {
from {
opacity: 0;
@@ -8,7 +8,7 @@ Floating tooltip built on Kobalte's tooltip primitive.
- \`value\`: Content rendered inside the floating tooltip.
- \`children\`: The trigger element that activates the tooltip on hover/focus.
- \`placement\`: Kobalte placement string (e.g. "top", "bottom", "left", "right").
- \`appearance\`: \`compact\` (default) or \`standard\`.
- \`appearance\`: \`compact\` (default), \`standard\`, or \`large\`.
- \`inactive\`: When true, renders only the trigger without tooltip behavior.
- \`forceOpen\`: Forces the tooltip to stay open.
- Inherits Kobalte Tooltip root props.
@@ -49,6 +49,9 @@ export const Appearances = {
<Tooltip appearance="compact" value="Compact tooltip">
<span>Compact</span>
</Tooltip>
<Tooltip appearance="large" value="Large tooltip">
<span>Large</span>
</Tooltip>
</div>
),
}
+1 -1
View File
@@ -6,7 +6,7 @@ import "./tooltip.css"
export interface TooltipProps extends ComponentProps<typeof Root> {
value: JSX.Element
appearance?: "standard" | "compact"
appearance?: "standard" | "compact" | "large"
class?: string
contentClass?: string
contentStyle?: JSX.CSSProperties
+98 -2
View File
@@ -1,8 +1,18 @@
import { Effect, FileSystem, Formatter, Logger, type LogLevel } from "effect"
import { Effect, FileSystem, Formatter, Logger, Option, Schedule, Stream, type LogLevel } from "effect"
import path from "path"
import { Global } from "../global.js"
import { runID } from "./shared.js"
// One log file is shared by every opencode process on the machine and only ever appended to, so it
// is bounded by compacting in place instead of rotating: once it passes LOG_MAX_BYTES the head is
// dropped so roughly LOG_KEEP_BYTES remain, rounded forward to the next line boundary.
export const LOG_MAX_BYTES = 50 * 1024 * 1024
export const LOG_KEEP_BYTES = 25 * 1024 * 1024
export const LOG_TRIM_INTERVAL = "1 hour"
// A trim of a 1 GB log takes well under a second, so a lock older than this belongs to a dead process.
export const LOG_TRIM_LOCK_STALE_MS = 5 * 60 * 1000
const LOG_TRIM_CHUNK = 64 * 1024
function formatter(id: string = runID()) {
return Logger.map(Logger.formatStructured, (output) => {
const messages = Array.isArray(output.message) ? output.message : [output.message]
@@ -56,7 +66,93 @@ export function fileLogger(target = file(), id: string = runID()) {
return Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem
yield* fs.makeDirectory(path.dirname(target), { recursive: true })
return yield* Logger.toFile(formatter(id), target, { flag: "a" })
const logger = yield* Logger.toFile(formatter(id), target, { flag: "a" })
yield* trim(target).pipe(
Effect.ignore,
Effect.repeat(Schedule.spaced(LOG_TRIM_INTERVAL)),
Effect.forkScoped({ startImmediately: true }),
)
return logger
})
}
// Compacts in place rather than writing a temp file and renaming over the log. Other processes hold
// the same file open with O_APPEND, so they keep appending to the compacted file, whereas a rename
// would strand them on the unlinked inode and lose their output.
//
// Appenders are not coordinated with the final truncate, so a batch flushed by another process
// between the last read and the truncate is lost. That window is a few milliseconds once per trim,
// which is an accepted trade for not wrapping every log write in a cross-process lock.
export const trim = Effect.fn("Logging.trim")(function* (
target: string,
options: { max?: number; keep?: number } = {},
) {
const max = options.max ?? LOG_MAX_BYTES
const keep = options.keep ?? LOG_KEEP_BYTES
const fs = yield* FileSystem.FileSystem
// Every opencode process on the machine runs this against the same file. Two trimmers racing
// would have one compute its cut from a size the other already shrank, so only one may proceed
// and the rest skip until the next interval. mkdir is the atomic primitive on every platform.
// acquireRelease keeps the mkdir uninterruptible, so a scope closing mid-call cannot leave a lock
// on disk with no finalizer registered to remove it.
const lock = `${target}.trim`
const acquired = yield* Effect.acquireRelease(
fs.makeDirectory(lock).pipe(
Effect.as(true),
Effect.catchIf(
(error) => error.reason._tag === "AlreadyExists",
() => breakStaleLock(fs, lock),
),
),
(acquired) => (acquired ? fs.remove(lock, { recursive: true }).pipe(Effect.ignore) : Effect.void),
)
if (!acquired) return
const size = Number((yield* fs.stat(target)).size)
if (size <= max) return
const handle = yield* fs.open(target, { flag: "r+" })
const start = yield* lineStart(handle, size - keep)
yield* handle.seek(0, "start")
// Reads run to the current EOF so lines appended since the stat survive; the write cursor always
// trails the read cursor so the forward copy never overwrites unread bytes.
const written = yield* fs.stream(target, { offset: start, chunkSize: LOG_TRIM_CHUNK }).pipe(
Stream.runFoldEffect(
() => 0,
(total, chunk) => handle.writeAll(chunk).pipe(Effect.as(total + chunk.length)),
),
)
yield* handle.truncate(written)
}, Effect.scoped)
// Removes a lock left by a process that died mid-trim. Still yields this round: the next interval
// acquires cleanly, and a process that is legitimately trimming right now keeps its lock.
//
// Two processes that both observe the same stale lock can race here: one removes it, a third
// acquires fresh, and the other removes that fresh lock. That needs a crash inside a sub-second trim
// followed by three processes ticking within the same few milliseconds, and the consequence is the
// same bounded tail loss documented on `trim`. Not worth a breaker protocol; see EffectFlock if it is.
function breakStaleLock(fs: FileSystem.FileSystem, lock: string) {
return Effect.gen(function* () {
const info = yield* fs.stat(lock).pipe(Effect.option)
const modified = Option.flatMap(info, (value) => value.mtime)
if (Option.isNone(modified)) return false
if (Date.now() - modified.value.getTime() < LOG_TRIM_LOCK_STALE_MS) return false
yield* fs.remove(lock, { recursive: true }).pipe(Effect.ignore)
return false
})
}
// First byte after the first newline at or beyond `from`, or EOF when the tail has no newline.
function lineStart(handle: FileSystem.File, from: number) {
return Effect.gen(function* () {
let cursor = from
while (true) {
yield* handle.seek(cursor, "start")
const chunk = yield* handle.readAlloc(LOG_TRIM_CHUNK)
if (Option.isNone(chunk)) return cursor
const newline = chunk.value.indexOf(10)
if (newline !== -1) return cursor + newline + 1
cursor += chunk.value.length
}
})
}
+6 -2
View File
@@ -7407,7 +7407,7 @@
}
}
},
"description": "Update project display metadata and workspace commands.",
"description": "Update the project canonical directory, display metadata, and workspace commands.",
"summary": "Update project",
"requestBody": {
"content": {
@@ -7415,6 +7415,9 @@
"schema": {
"type": "object",
"properties": {
"canonical": {
"type": "string"
},
"name": {
"type": "string"
},
@@ -14589,7 +14592,8 @@
"anyOf": [
{
"type": "string",
"enum": ["random"]
"enum": ["random"],
"description": "Reuse a randomly selected provider until it is rate limited, then switch to another available provider."
},
{
"type": "string"

Some files were not shown because too many files have changed in this diff Show More