Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 88 additions & 1 deletion web/src/components/SearchModal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ vi.mock("../state/connection", () => ({
}));

import SearchModal from "./SearchModal";
import { _resetSessionsForTest } from "../state/sessions";
import {
_resetSessionsForTest,
focusSession,
ingestSessionList,
} from "../state/sessions";

beforeEach(() => {
vi.useFakeTimers();
Expand Down Expand Up @@ -85,3 +89,86 @@ describe("SearchModal stale in-flight results", () => {
expect(queryByText(/search exploded/)).toBeNull();
});
});

describe("SearchModal scope toggle", () => {
/** Seed one focused session so there is a workspace worth scoping to. */
function seedFocused(workdir = "/repos/alpha") {
ingestSessionList([
{
id: "sess-a",
name: "alpha",
workdir,
status: "idle",
createdBy: "user:t",
createdAt: new Date().toISOString(),
} as never,
]);
focusSession("sess-a");
}

function lastSearch(): Record<string, unknown> {
return requestMock.mock.calls.at(-1)![0] as Record<string, unknown>;
}

it("scopes to the focused session's workdir by default", async () => {
seedFocused();
const { input } = openModal();

fireEvent.input(input, { target: { value: "auth token" } });
await vi.advanceTimersByTimeAsync(250);

expect(lastSearch().scope).toBe("workspace");
expect(lastSearch().workdir).toBe("/repos/alpha");
});

it("searches every workspace when nothing is focused", async () => {
const { input } = openModal();

fireEvent.input(input, { target: { value: "auth token" } });
await vi.advanceTimersByTimeAsync(250);

expect(lastSearch().scope).toBe("all");
// No anchor may ride along — the daemon decides global on its absence.
expect(lastSearch().workdir).toBeUndefined();
});

it("re-runs the query globally when the scope toggle is used", async () => {
seedFocused();
const { input, getByText } = openModal();

fireEvent.input(input, { target: { value: "auth token" } });
await vi.advanceTimersByTimeAsync(250);
expect(lastSearch().scope).toBe("workspace");
const before = requestMock.mock.calls.length;

fireEvent.click(getByText("All workspaces"));
await vi.advanceTimersByTimeAsync(250);

// Same query, other regime — no retyping required.
expect(requestMock.mock.calls.length).toBeGreaterThan(before);
expect(lastSearch().scope).toBe("all");
expect(lastSearch().query).toBe("auth token");
expect(lastSearch().workdir).toBeUndefined();
});

it("hides the toggle when there is no workspace to scope to", () => {
const { queryByText } = openModal();
expect(queryByText("All workspaces")).toBeNull();
expect(queryByText("This workspace")).toBeNull();
});

it("offers a widen button when a scoped search finds nothing", async () => {
seedFocused();
const { input, findByText } = openModal();

fireEvent.input(input, { target: { value: "auth token" } });
await vi.advanceTimersByTimeAsync(250);

// Zero hits under workspace scope is exactly when widening helps.
expect(await findByText(/No matches in this workspace/)).toBeTruthy();
fireEvent.click(await findByText("Search all workspaces"));
await vi.advanceTimersByTimeAsync(250);

expect(lastSearch().scope).toBe("all");
});
});
95 changes: 88 additions & 7 deletions web/src/components/SearchModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,34 @@ import type {

const DEBOUNCE_MS = 220;

/**
* Which sessions a search covers. `workspace` scopes to the focused session's
* directory; `all` runs the cross-workspace resolution path (global fusion +
* cross-encoder rerank) — see docs/session-resolution.md.
*
* A workspace is a DIRECTORY, not a session family: every session that ever
* ran in that workdir under the same tenant shares it. Forks isolated into a
* git worktree get their own path, so they land OUTSIDE the parent's scope —
* which is precisely the case cross-workspace search exists to recover.
*/
type SearchScope = "workspace" | "all";

const SearchModal: Component = () => {
const [open, setOpen] = createSignal(false);
const [query, setQuery] = createSignal("");
const [hits, setHits] = createSignal<SessionSearchHit[]>([]);
const [busy, setBusy] = createSignal(false);
const [error, setError] = createSignal<string | null>(null);
const [highlight, setHighlight] = createSignal(0);
// Null until the user chooses; the effective scope falls back to "scoped if
// something is focused". Keeping the override separate from the derived
// default means focusing a session later doesn't silently undo the choice.
const [scopeOverride, setScopeOverride] = createSignal<SearchScope | null>(null);
const scope = createMemo<SearchScope>(
() => scopeOverride() ?? (focusedSession()?.workdir ? "workspace" : "all"),
);
/** Scoping is only meaningful when there is a workspace to scope to. */
const canScope = createMemo(() => Boolean(focusedSession()?.workdir));

let inputRef: HTMLInputElement | undefined;
let debounceTimer: ReturnType<typeof setTimeout> | undefined;
Expand All @@ -45,6 +66,7 @@ const SearchModal: Component = () => {
setError(null);
setHighlight(0);
setBusy(false);
setScopeOverride(null);
}

// Global Ctrl+K to toggle, Esc to close.
Expand All @@ -71,9 +93,11 @@ const SearchModal: Component = () => {
}),
);

// Debounce + dispatch search on every query change.
// Debounce + dispatch search on every query OR scope change, so flipping
// the scope re-runs the current query through the other regime rather than
// leaving stale hits from the previous one on screen.
createEffect(
on(query, async (q) => {
on([query, scope] as const, async ([q]) => {
if (debounceTimer) clearTimeout(debounceTimer);
const trimmed = q.trim();
if (trimmed.length < 2) {
Expand All @@ -97,9 +121,12 @@ const SearchModal: Component = () => {
id,
query: trimmed,
limit: 10,
...(focusedSession()?.workdir
? { workdir: focusedSession()!.workdir, scope: "workspace" }
: { scope: "all" }),
// Omit the anchor entirely when global — passing a workdir
// alongside scope:"all" is contradictory, and the daemon
// decides global-vs-scoped on the workspaceId's absence.
...(scope() === "workspace" && focusedSession()?.workdir
? { workdir: focusedSession()!.workdir, scope: "workspace" as const }
: { scope: "all" as const }),
},
{
waitForResult: (m) =>
Expand Down Expand Up @@ -172,7 +199,41 @@ const SearchModal: Component = () => {
autocomplete="off"
spellcheck={false}
/>
<span class="rounded border border-border px-1.5 py-0.5 text-[10px] text-fg-faint">
<Show when={canScope()}>
<div
class="flex shrink-0 overflow-hidden rounded border border-border text-[10px]"
role="group"
aria-label="Search scope"
>
<button
type="button"
onClick={() => setScopeOverride("workspace")}
aria-pressed={scope() === "workspace"}
title="Search only sessions that ran in this session's directory"
class={`px-2 py-0.5 transition ${
scope() === "workspace"
? "bg-bg-active text-fg"
: "text-fg-faint hover:bg-bg-hover"
}`}
>
This workspace
</button>
<button
type="button"
onClick={() => setScopeOverride("all")}
aria-pressed={scope() === "all"}
title="Search every workspace, including forks isolated in their own git worktree"
class={`border-l border-border px-2 py-0.5 transition ${
scope() === "all"
? "bg-bg-active text-fg"
: "text-fg-faint hover:bg-bg-hover"
}`}
>
All workspaces
</button>
</div>
</Show>
<span class="shrink-0 rounded border border-border px-1.5 py-0.5 text-[10px] text-fg-faint">
Ctrl+K
</span>
</div>
Expand All @@ -184,6 +245,8 @@ const SearchModal: Component = () => {
highlight={highlight()}
setHighlight={setHighlight}
onPick={pick}
scope={scope()}
onWiden={() => setScopeOverride("all")}
/>
</div>
</div>
Expand All @@ -199,6 +262,8 @@ const ResultsBody: Component<{
highlight: number;
setHighlight: (n: number) => void;
onPick: (idx: number) => void;
scope: SearchScope;
onWiden: () => void;
}> = (props) => {
const empty = createMemo(
() =>
Expand All @@ -216,11 +281,27 @@ const ResultsBody: Component<{
<div class="p-3 text-xs text-fg-faint">searching…</div>
</Show>
<Show when={empty()}>
<div class="p-6 text-center text-sm text-fg-muted">No matches.</div>
<div class="p-6 text-center text-sm text-fg-muted">
<p>No matches in {props.scope === "all" ? "any workspace" : "this workspace"}.</p>
<Show when={props.scope === "workspace"}>
<button
type="button"
onClick={props.onWiden}
class="mt-2 rounded border border-border px-2 py-1 text-xs text-fg-muted transition hover:bg-bg-hover hover:text-fg"
>
Search all workspaces
</button>
</Show>
</div>
</Show>
<Show when={!props.busy && !props.error && props.query.trim().length < 2}>
<div class="p-6 text-center text-sm text-fg-muted">
<p>Type at least 2 characters to search across sessions.</p>
<p class="mt-1 text-xs text-fg-faint">
{props.scope === "all"
? "Searching every workspace."
: "Searching this session's directory only."}
</p>
</div>
</Show>
<ul class="divide-y divide-border">
Expand Down
Loading