Skip to content
Open
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
157 changes: 156 additions & 1 deletion templates/content/actions/document-discovery.db.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";

import { closeDbExec } from "@agent-native/core/db";
import { runWithRequestContext } from "@agent-native/core/server";
import { afterAll, beforeAll, describe, expect, it } from "vitest";

Expand Down Expand Up @@ -79,11 +80,165 @@ beforeAll(async () => {
}
}, 60_000);

afterAll(() => {
afterAll(async () => {
await closeDbExec();
rmSync(TEST_DB_PATH, { force: true, recursive: true });
});

describe("bounded document discovery", () => {
it("matches case-insensitively while treating wildcard input literally", async () => {
await getDb().insert(schema.documents).values({
id: "search-literal",
ownerEmail: OWNER,
title: "Literal 100%_ Match",
});
const result = await asUser(OWNER, () =>
searchDocuments.run({
query: "literal 100%_ match",
searchFields: "title",
limit: 8,
offset: 0,
}),
);
expect(result.documents.map((doc) => doc.id)).toEqual(["search-literal"]);
const ordinary = await asUser(OWNER, () =>
searchDocuments.run({
query: "BOUNDED DOCUMENT",
searchFields: "title",
limit: 8,
offset: 0,
}),
);
expect(ordinary.pagination.totalItems).toBe(203);
});
it("filters title and modified date before pagination and returns authorized parent context", async () => {
const first = await asUser(OWNER, () =>
searchDocuments.run({
query: "Bounded document",
searchFields: "title",
spaceId: SPACE_ID,
modifiedAfter: "2020-01-01T00:00:00.000Z",
modifiedBefore: "2100-01-01T00:00:00.000Z",
documentType: "page",
limit: 8,
offset: 0,
}),
);
const later = await asUser(OWNER, () =>
searchDocuments.run({
query: "Bounded document",
searchFields: "title",
spaceId: SPACE_ID,
modifiedAfter: "2020-01-01T00:00:00.000Z",
modifiedBefore: "2100-01-01T00:00:00.000Z",
documentType: "page",
limit: 8,
offset: first.pagination.nextOffset!,
}),
);
expect(first.pagination.totalItems).toBe(203);
expect(later.documents).toHaveLength(8);
expect(
later.documents.some((doc) =>
first.documents.some((prior) => prior.id === doc.id),
),
).toBe(false);
expect(first.documents[0]).toMatchObject({
parentTitle: "Discovery parent",
documentType: "page",
});
const bodyOnly = await asUser(OWNER, () =>
searchDocuments.run({
query: "needle payload",
searchFields: "title",
limit: 8,
offset: 0,
}),
);
expect(bodyOnly.pagination.totalItems).toBe(0);
const future = await asUser(OWNER, () =>
searchDocuments.run({
query: "Bounded document",
modifiedAfter: "2100-01-01T00:00:00.000Z",
limit: 8,
offset: 0,
}),
);
expect(future.pagination.totalItems).toBe(0);
});

it("does not disclose a private parent through an independently visible child", async () => {
await getDb().insert(schema.documents).values({
id: "search-shared-child",
parentId: PARENT_ID,
ownerEmail: OUTSIDER,
title: "Independent child match",
content: "child excerpt",
visibility: "private",
});
const result = await asUser(OUTSIDER, () =>
searchDocuments.run({
query: "Independent child match",
limit: 8,
offset: 0,
}),
);
expect(result.documents).toHaveLength(1);
expect(result.documents[0]).toMatchObject({
parentId: null,
parentTitle: null,
snippet: "child excerpt",
});
expect(JSON.stringify(result)).not.toContain("Discovery parent");
expect(JSON.stringify(result)).not.toContain(PARENT_ID);
});

it("counts and paginates hidden and database matches in the Action", async () => {
await getDb()
.insert(schema.documents)
.values([
{
id: "search-hidden",
ownerEmail: OWNER,
title: "Kind needle hidden",
hideFromSearch: 1,
},
{
id: "search-kind-page",
ownerEmail: OWNER,
title: "Kind needle page",
},
{
id: "search-kind-db",
ownerEmail: OWNER,
title: "Kind needle database",
},
]);
await getDb().insert(schema.contentDatabases).values({
id: "search-kind-database",
documentId: "search-kind-db",
ownerEmail: OWNER,
title: "Kind needle database",
});
const all = await asUser(OWNER, () =>
searchDocuments.run({ query: "Kind needle", limit: 8, offset: 0 }),
);
expect(all.pagination.totalItems).toBe(2);
const database = await asUser(OWNER, () =>
searchDocuments.run({
query: "Kind needle",
documentType: "database",
limit: 8,
offset: 0,
}),
);
expect(database.pagination.totalItems).toBe(1);
expect(database.documents[0]).toMatchObject({
id: "search-kind-db",
documentType: "database",
});
});

it("returns explicit continuation metadata through a terminal list page", async () => {
const first = await asUser(OWNER, () =>
listDocuments.run({ parentId: PARENT_ID, limit: 100, offset: 0 }),
Expand Down
88 changes: 83 additions & 5 deletions templates/content/actions/search-documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,19 @@ import {
getRequestOrgId,
getRequestUserEmail,
} from "@agent-native/core/server/request-context";
import { asc, desc, sql } from "drizzle-orm";
import {
and,
asc,
desc,
eq,
exists,
gte,
inArray,
isNull,
lt,
or,
sql,
} from "drizzle-orm";
import { z } from "zod";

import { getDb, schema } from "../server/db/index.js";
Expand Down Expand Up @@ -66,6 +78,18 @@ export default defineAction({
.enum(["page", "database"])
.optional()
.describe("Only ordinary pages or database pages"),
searchFields: z
.enum(["all", "title"])
.optional()
.describe("Match title only, or title, description and body (default)"),
modifiedAfter: z.iso
.datetime()
.optional()
.describe("Modified at or after this UTC timestamp"),
modifiedBefore: z.iso
.datetime()
.optional()
.describe("Modified before this UTC timestamp"),
limit: z.coerce
.number()
.int()
Expand Down Expand Up @@ -109,9 +133,25 @@ export default defineAction({
parentId: args.parentId,
spaceId: args.spaceId,
documentType: args.documentType,
additional: pattern
? sql`(${schema.documents.title} LIKE ${pattern} ESCAPE '\\' OR ${schema.documents.description} LIKE ${pattern} ESCAPE '\\' OR ${schema.documents.content} LIKE ${pattern} ESCAPE '\\')`
: undefined,
additional: and(
args.query
? or(
eq(schema.documents.hideFromSearch, 0),
isNull(schema.documents.hideFromSearch),
)
: undefined,
pattern
? args.searchFields === "title"
? sql`${schema.documents.title} ILIKE ${pattern} ESCAPE '\\'`
: sql`(${schema.documents.title} ILIKE ${pattern} ESCAPE '\\' OR ${schema.documents.description} ILIKE ${pattern} ESCAPE '\\' OR ${schema.documents.content} ILIKE ${pattern} ESCAPE '\\')`
: undefined,
args.modifiedAfter
? gte(schema.documents.updatedAt, args.modifiedAfter)
Comment on lines +148 to +149

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Compare modification filters as timestamps rather than text

documents.updatedAt is a text column, but this predicate compares it directly with ISO-8601 T/Z strings. Rows written through the schema's now() default can use a space-separated PostgreSQL timestamp representation, so lexical ordering can exclude documents that fall within the requested date range and make filtered counts/pagination incorrect. Cast/normalize the column and bound to timestamps before comparing.

Additional Info
Reported by 1 of 4 randomized code-review agents; browser probe could not verify due unavailable Chrome tooling.

Fix in Builder

: undefined,
args.modifiedBefore
? lt(schema.documents.updatedAt, args.modifiedBefore)
: undefined,
),
});
const [countRow] = await db
.select({ count: sql<number>`count(*)` })
Expand All @@ -138,17 +178,55 @@ export default defineAction({
contentLength: sql<number>`length(${schema.documents.content})`,
hideFromSearch: schema.documents.hideFromSearch,
updatedAt: schema.documents.updatedAt,
sourceKind: schema.documents.sourceKind,
sourceUpdatedAt: schema.documents.sourceUpdatedAt,
documentType: sql<"page" | "database">`case when ${exists(
db
.select({ id: schema.contentDatabases.id })
.from(schema.contentDatabases)
.where(
and(
eq(schema.contentDatabases.documentId, schema.documents.id),
isNull(schema.contentDatabases.deletedAt),
),
),
)} then 'database' else 'page' end`,
})
.from(schema.documents)
.where(where)
.orderBy(desc(schema.documents.updatedAt), asc(schema.documents.id))
.limit(args.limit)
.offset(args.offset);

const parentIds = [
...new Set(docs.flatMap((doc) => (doc.parentId ? [doc.parentId] : []))),
];
const parents = parentIds.length
? await db
.select({ id: schema.documents.id, title: schema.documents.title })
.from(schema.documents)
.where(
documentDiscoveryWhere({
userEmail,
authorizedOrgIds,
spaceId: args.spaceId,
additional: inArray(schema.documents.id, parentIds),
}),
)
: [];
const parentById = new Map(parents.map((parent) => [parent.id, parent]));

return {
documents: docs.map((doc) => ({
id: doc.id,
parentId: doc.parentId,
parentId:
doc.parentId && parentById.has(doc.parentId) ? doc.parentId : null,
parentTitle: doc.parentId
? (parentById.get(doc.parentId)?.title ?? null)
: null,
documentType: doc.documentType,
sourceKind: doc.sourceKind,
sourceUpdatedAt: doc.sourceUpdatedAt,
title: doc.title,
description: doc.description,
icon: doc.icon,
Expand Down
Loading
Loading