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
45 changes: 45 additions & 0 deletions packages/server/rpc/memory/memory.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,51 @@ test("search: tree lquery wildcard matches descendants", async () => {
expect(exact.results.map((r) => r.tree)).toEqual(["/share/proj/a"]);
});

test("search: `~.*{0,0}` pins to the root of the caller's home (TNT-248)", async () => {
// The web UI's exact-path filter for an expanded tree node. A memory at the
// ROOT of `~` (not under a sub-path) is the case that regressed: the old
// filter form was `~|~`, an unparseable lquery, so this search failed with an
// opaque Internal error and the UI showed "Failed to load".
await call("memory.batchCreate", {
memories: [
{ content: "at home root", tree: "~" },
{ content: "under home", tree: "~/notes" },
{ content: "shared", tree: "share" },
],
});

const atHomeRoot = await call<{ results: { content: string }[] }>(
"memory.search",
{ tree: "~.*{0,0}", limit: 1000 },
);
expect(atHomeRoot.results.map((r) => r.content)).toEqual(["at home root"]);

// The same form one level down resolves the sub-path exactly.
const underHome = await call<{ results: { content: string }[] }>(
"memory.search",
{ tree: "~.notes.*{0,0}", limit: 1000 },
);
expect(underHome.results.map((r) => r.content)).toEqual(["under home"]);
});

test("search: a malformed tree filter is a validation error, not an internal error", async () => {
// ltree's lquery/ltxtquery parsers report a bad pattern as `syntax_error`
// (42601), not 22P02 — it must still map to VALIDATION_ERROR so a caller sees
// what it did wrong instead of "Internal error" (the TNT-248 symptom).
// `~|~` is the exact filter the web UI used to send for a node at the root
// of home: `~` is not a legal lquery label (the server expands `~` only as a
// LEADING segment), so the ::lquery cast raises 42601.
await expectAppError(
call("memory.search", { tree: "~|~" }),
"VALIDATION_ERROR",
);
// An `&` classifies the filter as ltxtquery; `&&` is not valid there.
await expectAppError(
call("memory.search", { tree: "share.a&&b" }),
"VALIDATION_ERROR",
);
});

test("deleteOrphansInTree deletes stale slots; kept/foreign/unnamed survive; dryRun previews", async () => {
await call("memory.batchCreate", {
memories: [
Expand Down
7 changes: 6 additions & 1 deletion packages/server/rpc/memory/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,18 @@ const MAX_SEMANTIC_QUERY_CHARS = 8192;
* `insufficient_privilege` (42501) on access violations and
* `invalid_parameter_value` (22023) / `invalid_text_representation` (22P02)
* on malformed input; everything else propagates as an internal error.
*
* `syntax_error` (42601) is also caller-caused here: ltree's `lquery` /
* `ltxtquery` parsers report a malformed pattern (`~|~`, `a&&`) with that
* code rather than 22P02, and a tree filter is caller-supplied — so it must
* surface as a validation error, not an opaque "Internal error".
*/
function mapSpaceError(e: unknown): never {
const code = (e as { code?: string }).code;
if (code === "42501") {
throw new AppError("FORBIDDEN", "Insufficient tree access");
}
if (code === "22023" || code === "22P02") {
if (code === "22023" || code === "22P02" || code === "42601") {
throw new AppError(
"VALIDATION_ERROR",
e instanceof Error ? e.message : "Invalid parameter",
Expand Down
35 changes: 35 additions & 0 deletions packages/web/src/api/queries.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Unit tests for the pure query-param helpers (no network, no React).
*/
Comment on lines +1 to +3
import { describe, expect, test } from "bun:test";
import { ROOT_PATH } from "../lib/tree-build.ts";
import { exactTreeLquery } from "./queries.ts";

describe("exactTreeLquery", () => {
test("the empty path and the synthetic root bucket both pin to the root", () => {
// `*{0,0}` matches an ltree of exactly zero labels — the empty tree.
expect(exactTreeLquery("")).toBe("*{0,0}");
expect(exactTreeLquery(ROOT_PATH)).toBe("*{0,0}");
});

test("a concrete path allows zero further labels (exact match)", () => {
expect(exactTreeLquery("work")).toBe("work.*{0,0}");
expect(exactTreeLquery("work.projects")).toBe("work.projects.*{0,0}");
expect(exactTreeLquery("share.auth")).toBe("share.auth.*{0,0}");
});

test("the `~` home sugar stays a valid leading segment (TNT-248)", () => {
// Regression: the old label-alternation form produced `~|~`, which is not
// parseable as an lquery (the server expands `~` only as a LEADING
// segment), so `memory.search` crashed with an Internal error whenever a
// memory lived at the root of the caller's home.
expect(exactTreeLquery("~")).toBe("~.*{0,0}");
expect(exactTreeLquery("~.notes")).toBe("~.notes.*{0,0}");
});

test("never emits an lquery alternation, which `~` cannot express", () => {
for (const path of ["", ROOT_PATH, "~", "~.a.b", "work", "share.auth"]) {
expect(exactTreeLquery(path)).not.toContain("|");
}
});
});
28 changes: 17 additions & 11 deletions packages/web/src/api/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
useQuery,
useQueryClient,
} from "@tanstack/react-query";
import { ROOT_PATH } from "../lib/tree-build.ts";
import { memoryClient } from "./client.ts";

const SEARCH_LIMIT = 1000;
Expand All @@ -37,21 +38,26 @@ function memoryToDot<T extends { tree: string }>(m: T): T {
/**
* Convert an exact ltree path to an lquery pattern that matches only that
* path (no descendants). The engine's tree filter auto-detects lquery vs
* ltree by special characters; by duplicating the last label via `|` or
* using the zero-label quantifier for the empty path, we force lquery
* detection while preserving exact-match semantics.
* ltree by special characters, so we append the zero-label quantifier
* `*{0,0}`: it forces lquery detection AND pins the match to exactly this
* path (zero further labels allowed).
*
* The quantifier is used rather than duplicating the last label as an
* alternation (`work|work`) because the latter produces invalid lquery for
* the `~` home sugar (`~|~` — a `~` is not a legal lquery label, and the
* server expands `~` only as a leading segment) and for the synthetic root
* sentinel, which crashed the search RPC with a PG syntax error.
*
* Examples:
* "" -> "*{0,0}" matches only the empty tree
* "work" -> "work|work" matches only `work`
* "work.projects" -> "work.projects|projects" matches only `work.projects`
* "" -> "*{0,0}" matches only the empty tree
* "." -> "*{0,0}" the synthetic root bucket
* "work" -> "work.*{0,0}" matches only `work`
* "work.projects" -> "work.projects.*{0,0}" matches only `work.projects`
* "~" -> "~.*{0,0}" matches only the caller's home
*/
export function exactTreeLquery(path: string): string {
if (path === "") return "*{0,0}";
const labels = path.split(".");
const i = labels.length - 1;
labels[i] = `${labels[i]}|${labels[i]}`;
return labels.join(".");
if (path === "" || path === ROOT_PATH) return "*{0,0}";
return `${path}.*{0,0}`;
}

/**
Expand Down