feat(core): filter sessions by path and add setting to disable (#24849)

This commit is contained in:
James Long
2026-04-28 16:49:13 -04:00
committed by GitHub
parent 379e7f3f20
commit 9209c04370
10 changed files with 360 additions and 27 deletions

View File

@@ -0,0 +1,149 @@
/** @jsxImportSource @opentui/solid */
import { describe, expect, test } from "bun:test"
import { testRender } from "@opentui/solid"
import { onMount } from "solid-js"
import { Global } from "@opencode-ai/core/global"
import { ArgsProvider } from "../../../../src/cli/cmd/tui/context/args"
import { ExitProvider } from "../../../../src/cli/cmd/tui/context/exit"
import { KVProvider, useKV } from "../../../../src/cli/cmd/tui/context/kv"
import { ProjectProvider } from "../../../../src/cli/cmd/tui/context/project"
import { SDKProvider, type EventSource } from "../../../../src/cli/cmd/tui/context/sdk"
import { SyncProvider, useSync } from "../../../../src/cli/cmd/tui/context/sync"
import { tmpdir } from "../../../fixture/fixture"
const worktree = "/tmp/opencode"
const directory = `${worktree}/packages/opencode`
async function wait(fn: () => boolean, timeout = 2000) {
const start = Date.now()
while (!fn()) {
if (Date.now() - start > timeout) throw new Error("timed out waiting for condition")
await Bun.sleep(10)
}
}
function json(data: unknown) {
return new Response(JSON.stringify(data), {
headers: { "content-type": "application/json" },
})
}
function eventSource(): EventSource {
return {
subscribe: async () => () => {},
}
}
function createFetch() {
const session = [] as URL[]
const fetch = (async (input: RequestInfo | URL) => {
const url = new URL(input instanceof Request ? input.url : String(input))
if (url.pathname === "/session") session.push(url)
switch (url.pathname) {
case "/agent":
case "/command":
case "/experimental/workspace":
case "/experimental/workspace/status":
case "/formatter":
case "/lsp":
return json([])
case "/config":
case "/experimental/resource":
case "/mcp":
case "/provider/auth":
case "/session/status":
return json({})
case "/config/providers":
return json({ providers: {}, default: {} })
case "/experimental/console":
return json({ consoleManagedProviders: [], switchableOrgCount: 0 })
case "/path":
return json({ home: "", state: "", config: "", worktree, directory })
case "/project/current":
return json({ id: "proj_test" })
case "/provider":
return json({ all: [], default: {}, connected: [] })
case "/session":
return json([])
case "/vcs":
return json({ branch: "main" })
}
throw new Error(`unexpected request: ${url.pathname}`)
}) as typeof globalThis.fetch
return { fetch, session }
}
async function mount() {
const calls = createFetch()
let sync!: ReturnType<typeof useSync>
let kv!: ReturnType<typeof useKV>
let done!: () => void
const ready = new Promise<void>((resolve) => {
done = resolve
})
const app = await testRender(() => (
<ArgsProvider>
<ExitProvider>
<KVProvider>
<SDKProvider url="http://test" directory={directory} fetch={calls.fetch} events={eventSource()}>
<ProjectProvider>
<SyncProvider>
<Probe
onReady={(ctx) => {
sync = ctx.sync
kv = ctx.kv
done()
}}
/>
</SyncProvider>
</ProjectProvider>
</SDKProvider>
</KVProvider>
</ExitProvider>
</ArgsProvider>
))
await ready
await wait(() => sync.status === "complete")
return { app, kv, sync, session: calls.session }
}
function Probe(props: { onReady: (ctx: { kv: ReturnType<typeof useKV>; sync: ReturnType<typeof useSync> }) => void }) {
const kv = useKV()
const sync = useSync()
onMount(() => {
props.onReady({ kv, sync })
})
return <box />
}
describe("tui sync", () => {
test("refresh scopes sessions by default and lists project sessions when disabled", async () => {
const previous = Global.Path.state
await using tmp = await tmpdir()
Global.Path.state = tmp.path
await Bun.write(`${tmp.path}/kv.json`, "{}")
const { app, kv, sync, session } = await mount()
try {
expect(kv.get("session_directory_filter_enabled", true)).toBe(true)
expect(session.at(-1)?.searchParams.get("scope")).toBeNull()
expect(session.at(-1)?.searchParams.get("path")).toBe("packages/opencode")
kv.set("session_directory_filter_enabled", false)
await sync.session.refresh()
expect(session.at(-1)?.searchParams.get("scope")).toBe("project")
expect(session.at(-1)?.searchParams.get("path")).toBeNull()
} finally {
app.renderer.destroy()
Global.Path.state = previous
}
})
})

View File

@@ -4,8 +4,15 @@ import { Instance } from "../../src/project/instance"
import { Session as SessionNs } from "@/session/session"
import * as Log from "@opencode-ai/core/util/log"
import { tmpdir } from "../fixture/fixture"
import { Flag } from "@opencode-ai/core/flag/flag"
import { mkdir } from "fs/promises"
import path from "path"
import { Database } from "@/storage/db"
import { SessionTable } from "@/session/session.sql"
import { eq } from "drizzle-orm"
void Log.init({ print: false })
const originalWorkspaces = Flag.OPENCODE_EXPERIMENTAL_WORKSPACES
function run<A, E>(fx: Effect.Effect<A, E, SessionNs.Service>) {
return Effect.runPromise(fx.pipe(Effect.provide(SessionNs.defaultLayer)))
@@ -19,28 +26,140 @@ const svc = {
}
afterEach(async () => {
Flag.OPENCODE_EXPERIMENTAL_WORKSPACES = originalWorkspaces
await Instance.disposeAll()
})
describe("session.list", () => {
test("filters by directory", async () => {
test("does not filter by directory when directory is omitted", async () => {
Flag.OPENCODE_EXPERIMENTAL_WORKSPACES = false
await using tmp = await tmpdir({ git: true })
await mkdir(path.join(tmp.path, "packages", "opencode"), { recursive: true })
await mkdir(path.join(tmp.path, "packages", "app"), { recursive: true })
await Instance.provide({
directory: tmp.path,
fn: async () => {
const first = await svc.create({})
const root = await svc.create({ title: "root" })
await using other = await tmpdir({ git: true })
const second = await Instance.provide({
directory: other.path,
fn: async () => svc.create({}),
const parent = await Instance.provide({
directory: path.join(tmp.path, "packages"),
fn: async () => svc.create({ title: "parent" }),
})
const current = await Instance.provide({
directory: path.join(tmp.path, "packages", "opencode"),
fn: async () => svc.create({ title: "current" }),
})
const sibling = await Instance.provide({
directory: path.join(tmp.path, "packages", "app"),
fn: async () => svc.create({ title: "sibling" }),
})
const sessions = [...svc.list({ directory: tmp.path })]
const ids = sessions.map((s) => s.id)
const ids = [...svc.list()].map((s) => s.id)
expect(ids).toContain(root.id)
expect(ids).toContain(parent.id)
expect(ids).toContain(current.id)
expect(ids).toContain(sibling.id)
},
})
})
expect(ids).toContain(first.id)
expect(ids).not.toContain(second.id)
test("filters by directory when directory is provided", async () => {
Flag.OPENCODE_EXPERIMENTAL_WORKSPACES = false
await using tmp = await tmpdir({ git: true })
await mkdir(path.join(tmp.path, "packages", "opencode"), { recursive: true })
await mkdir(path.join(tmp.path, "packages", "app"), { recursive: true })
await Instance.provide({
directory: tmp.path,
fn: async () => {
const root = await svc.create({ title: "root" })
const parent = await Instance.provide({
directory: path.join(tmp.path, "packages"),
fn: async () => svc.create({ title: "parent" }),
})
const current = await Instance.provide({
directory: path.join(tmp.path, "packages", "opencode"),
fn: async () => svc.create({ title: "current" }),
})
const sibling = await Instance.provide({
directory: path.join(tmp.path, "packages", "app"),
fn: async () => svc.create({ title: "sibling" }),
})
const ids = [...svc.list({ directory: path.join(tmp.path, "packages", "opencode") })].map((s) => s.id)
expect(ids).not.toContain(root.id)
expect(ids).not.toContain(parent.id)
expect(ids).toContain(current.id)
expect(ids).not.toContain(sibling.id)
},
})
})
test("filters by path and ignores directory when path is provided", async () => {
Flag.OPENCODE_EXPERIMENTAL_WORKSPACES = false
await using tmp = await tmpdir({ git: true })
await mkdir(path.join(tmp.path, "packages", "opencode", "src", "deep"), { recursive: true })
await mkdir(path.join(tmp.path, "packages", "app"), { recursive: true })
await Instance.provide({
directory: tmp.path,
fn: async () => {
const parent = await Instance.provide({
directory: path.join(tmp.path, "packages", "opencode"),
fn: async () => svc.create({ title: "parent" }),
})
const current = await Instance.provide({
directory: path.join(tmp.path, "packages", "opencode", "src"),
fn: async () => svc.create({ title: "current" }),
})
const deeper = await Instance.provide({
directory: path.join(tmp.path, "packages", "opencode", "src", "deep"),
fn: async () => svc.create({ title: "deeper" }),
})
const sibling = await Instance.provide({
directory: path.join(tmp.path, "packages", "app"),
fn: async () => svc.create({ title: "sibling" }),
})
const pathIDs = [
...svc.list({ directory: path.join(tmp.path, "packages", "app"), path: "packages/opencode/src" }),
].map((s) => s.id)
expect(pathIDs).not.toContain(parent.id)
expect(pathIDs).toContain(current.id)
expect(pathIDs).toContain(deeper.id)
expect(pathIDs).not.toContain(sibling.id)
},
})
})
test("falls back to directory when filtering legacy sessions without path", async () => {
Flag.OPENCODE_EXPERIMENTAL_WORKSPACES = false
await using tmp = await tmpdir({ git: true })
await mkdir(path.join(tmp.path, "packages", "opencode", "src"), { recursive: true })
await mkdir(path.join(tmp.path, "packages", "app"), { recursive: true })
await Instance.provide({
directory: tmp.path,
fn: async () => {
const current = await Instance.provide({
directory: path.join(tmp.path, "packages", "opencode", "src"),
fn: async () => svc.create({ title: "legacy-current" }),
})
const sibling = await Instance.provide({
directory: path.join(tmp.path, "packages", "app"),
fn: async () => svc.create({ title: "legacy-sibling" }),
})
Database.use((db) => db.update(SessionTable).set({ path: null }).where(eq(SessionTable.id, current.id)).run())
Database.use((db) => db.update(SessionTable).set({ path: null }).where(eq(SessionTable.id, sibling.id)).run())
const pathIDs = [
...svc.list({ directory: path.join(tmp.path, "packages", "opencode", "src"), path: "packages/opencode/src" }),
].map((s) => s.id)
expect(pathIDs).toContain(current.id)
expect(pathIDs).not.toContain(sibling.id)
},
})
})