Skip to content

A personal agent with a browser, terminal, files, and work that keeps going built with CopilotKit and AG-UI.

TypeScript

perf(server): scope task detail to the rows the task names (#174)

Paolo Valletta50b016c23f4f
diff --git a/apps/server/src/db.ts b/apps/server/src/db.ts
index 4f057b6..e3103c9 100644
--- a/apps/server/src/db.ts
+++ b/apps/server/src/db.ts
@@ -41,6 +41,30 @@ export class Store {
);
return result.rows.map((row) => row.data as T);
}
+ async listByTaskId<T = Record<string, unknown>>(
+ owner: string,
+ kind: string,
+ taskId: string,
+ ): Promise<T[]> {
+ const result = await this.db.query(
+ "SELECT data FROM records WHERE owner=$1 AND kind=$2 AND data->>'taskId'=$3 ORDER BY updated_at DESC,id",
+ [owner, kind, taskId],
+ );
+ return result.rows.map((row) => row.data as T);
+ }
+ /** Rows of one kind named by id, in the same order `list` returns them. */
+ async listByIds<T = Record<string, unknown>>(
+ owner: string,
+ kind: string,
+ ids: readonly string[],
+ ): Promise<T[]> {
+ if (ids.length === 0) return [];
+ const result = await this.db.query(
+ "SELECT data FROM records WHERE owner=$1 AND kind=$2 AND id=ANY($3) ORDER BY updated_at DESC,id",
+ [owner, kind, [...ids]],
+ );
+ return result.rows.map((row) => row.data as T);
+ }
async listByStatus<T = Record<string, unknown>>(
owner: string,
kind: string,
diff --git a/apps/server/src/engine/service.ts b/apps/server/src/engine/service.ts
index 0e071ba..0f94330 100644
--- a/apps/server/src/engine/service.ts
+++ b/apps/server/src/engine/service.ts
@@ -171,22 +171,23 @@ export class AgentService {
}
async detail(owner: string, id: string) {
const task = await this.getTask(owner, id);
- const files = (await this.db.list<Artifact>(owner, "files")).filter((file) =>
- task.artifactIds.includes(file.id),
- );
- const browsers = (await this.db.list<BrowserSession>(owner, "browsers")).filter((browser) =>
- [task.state.browserId, task.state.sessionId].includes(browser.id),
+ // Scoped by the ids the task already names, so a long-lived workspace does not read
+ // every file, browser, event and artifact it has ever stored to show one task.
+ const browserIds = [task.state.browserId, task.state.sessionId].filter(
+ (value): value is string => typeof value === "string" && value.length > 0,
);
+ const [files, browsers, events, artifacts] = await Promise.all([
+ this.db.listByIds<Artifact>(owner, "files", task.artifactIds ?? []),
+ this.db.listByIds<BrowserSession>(owner, "browsers", browserIds),
+ this.db.listByTaskId<RunEvent>(owner, "run-events", id),
+ this.db.listByTaskId<AgentArtifact>(owner, "agent-artifacts", id),
+ ]);
return {
task,
files: files.map((file) => this.files.signed(owner, file)),
browsers: browsers.map((browser) => this.browser.decorate(owner, browser)),
- events: (await this.db.list<RunEvent>(owner, "run-events"))
- .filter((e) => e.taskId === id)
- .sort((a, b) => a.date.localeCompare(b.date)),
- artifacts: (await this.db.list<AgentArtifact>(owner, "agent-artifacts")).filter(
- (a) => a.taskId === id,
- ),
+ events: events.sort((a, b) => a.date.localeCompare(b.date)),
+ artifacts,
};
}
async createTask(owner: string, raw: unknown, idempotencyKey?: string, held = false) {
diff --git a/tests/persistence.test.ts b/tests/persistence.test.ts
index 7a623df..dec0274 100644
--- a/tests/persistence.test.ts
+++ b/tests/persistence.test.ts
@@ -140,3 +140,68 @@ test("listByStatus matches list plus status filtering", async () => {
await store.close();
}
});
+
+test("listByTaskId matches list plus taskId filtering", async () => {
+ const store = await createStore();
+ try {
+ // A gap between writes gives each row its own `updated_at`, so `list`'s newest-first order is
+ // the reverse of the insertion order and a helper that forgot to order cannot pass by accident.
+ const gap = () => new Promise((resolve) => setTimeout(resolve, 3));
+ for (const id of ["e0", "e1", "e2"]) {
+ await store.put("owner", "run-events", { id, taskId: "t1" });
+ await gap();
+ }
+ await store.put("owner", "run-events", { id: "e-other", taskId: "t2" });
+ await store.put("owner", "run-events", { id: "e-none" });
+
+ const scoped = await store.listByTaskId<{ id: string; taskId?: string }>(
+ "owner",
+ "run-events",
+ "t1",
+ );
+ const expected = (
+ await store.list<{ id: string; taskId?: string }>("owner", "run-events")
+ ).filter((item) => item.taskId === "t1");
+ assert.deepEqual(scoped, expected);
+ assert.deepEqual(
+ scoped.map((item) => item.id),
+ ["e2", "e1", "e0"],
+ );
+
+ await store.put("other", "run-events", { id: "x", taskId: "t1" });
+ await store.put("owner", "goals", { id: "g", taskId: "t1" });
+ assert.equal((await store.listByTaskId("owner", "run-events", "t1")).length, 3);
+ assert.equal((await store.listByTaskId("nobody", "run-events", "t1")).length, 0);
+ } finally {
+ await store.close();
+ }
+});
+
+test("listByIds matches list plus id filtering, and reads nothing for no ids", async () => {
+ const store = await createStore();
+ try {
+ const gap = () => new Promise((resolve) => setTimeout(resolve, 3));
+ for (const id of ["f0", "f1", "f2"]) {
+ await store.put("owner", "files", { id, name: id });
+ await gap();
+ }
+ await store.put("owner", "files", { id: "f-other", name: "other" });
+
+ const ids = ["f0", "f2", "f-missing"];
+ const scoped = await store.listByIds<{ id: string }>("owner", "files", ids);
+ const expected = (await store.list<{ id: string }>("owner", "files")).filter((item) =>
+ ids.includes(item.id),
+ );
+ assert.deepEqual(scoped, expected);
+ assert.deepEqual(
+ scoped.map((item) => item.id),
+ ["f2", "f0"],
+ );
+
+ assert.deepEqual(await store.listByIds("owner", "files", []), []);
+ await store.put("other", "files", { id: "f0" });
+ assert.equal((await store.listByIds("owner", "files", ["f0"])).length, 1);
+ } finally {
+ await store.close();
+ }
+});
diff --git a/tests/workflows.test.ts b/tests/workflows.test.ts
index f6d81d7..cb8e059 100644
--- a/tests/workflows.test.ts
+++ b/tests/workflows.test.ts
@@ -553,3 +553,128 @@ test("a cancelled task does not stop its idea from starting fresh work", async (
assert.ok(accepted?.taskId && accepted.taskId !== cancelled.id);
assert.equal((await server.agent.getTask(ideaOwner, accepted.taskId)).status, "queued");
});
+test("task detail reads only the rows the task names, not the whole workspace", async () => {
+ // A workspace keeps every run event, artifact and file it has ever produced, so detail() — which
+ // the app calls once per task card — must return the rows the task names and read nothing else.
+ const noiseOwner = "detail-scope";
+ const created = await server.agent.createTask(noiseOwner, {
+ kind: "agent",
+ title: "Scoped detail",
+ prompt: "Answer from the workspace",
+ input: {},
+ });
+ const now = new Date().toISOString();
+ await db.put(noiseOwner, "files", {
+ id: "detail-file",
+ name: "detail.pdf",
+ mimeType: "application/pdf",
+ size: 10,
+ pageCount: 1,
+ url: "",
+ createdAt: now,
+ source: "Gmail",
+ });
+ await db.put(noiseOwner, "browsers", {
+ id: "detail-browser",
+ title: "Example",
+ url: "https://example.com",
+ status: "active",
+ updatedAt: now,
+ });
+ await db.put(noiseOwner, "run-events", {
+ id: "detail-event",
+ taskId: created.id,
+ date: now,
+ kind: "step",
+ title: "Named",
+ detail: "",
+ });
+ await db.put(noiseOwner, "agent-artifacts", {
+ id: "detail-artifact",
+ taskId: created.id,
+ kind: "report",
+ title: "Named",
+ summary: "",
+ data: {},
+ createdAt: now,
+ });
+ await db.put(noiseOwner, "tasks", {
+ ...created,
+ artifactIds: ["detail-file"],
+ state: { ...created.state, browserId: "detail-browser" },
+ });
+ for (let index = 0; index < 200; index++) {
+ await db.put(noiseOwner, "run-events", {
+ id: `noise-event-${index}`,
+ taskId: `other-task-${index}`,
+ date: now,
+ kind: "step",
+ title: "Noise",
+ detail: "x".repeat(1000),
+ });
+ await db.put(noiseOwner, "agent-artifacts", {
+ id: `noise-artifact-${index}`,
+ taskId: `other-task-${index}`,
+ kind: "report",
+ title: "Noise",
+ summary: "",
+ data: {},
+ createdAt: now,
+ });
+ await db.put(noiseOwner, "files", {
+ id: `noise-file-${index}`,
+ name: `noise-${index}.pdf`,
+ mimeType: "application/pdf",
+ size: 10,
+ pageCount: 1,
+ url: "",
+ createdAt: now,
+ source: "Gmail",
+ });
+ }
+
+ // Count every row the store hands back while detail() runs, and put the store back afterwards.
+ const store = db as unknown as Record<string, (...args: unknown[]) => Promise<unknown>>;
+ const wrapped: string[] = [];
+ let rows = 0;
+ for (const name of Object.getOwnPropertyNames(Object.getPrototypeOf(db))) {
+ if (
+ /^(put|remove|close|compareAndSwap|insertIfAbsent|updateCredential|recoverInterrupted)/.test(
+ name,
+ )
+ )
+ continue;
+ const original = store[name].bind(db);
+ store[name] = async (...args: unknown[]) => {
+ const result = await original(...args);
+ rows += Array.isArray(result) ? result.length : result ? 1 : 0;
+ return result;
+ };
+ wrapped.push(name);
+ }
+ let detail: Awaited<ReturnType<typeof server.agent.detail>>;
+ try {
+ detail = await server.agent.detail(noiseOwner, created.id);
+ } finally {
+ for (const name of wrapped) delete store[name];
+ }
+
+ assert.equal(detail.task.id, created.id);
+ assert.deepEqual(
+ detail.files.map((file) => file.id),
+ ["detail-file"],
+ );
+ assert.deepEqual(
+ detail.browsers.map((browser) => browser.id),
+ ["detail-browser"],
+ );
+ assert.deepEqual(
+ detail.events.map((event) => event.id),
+ ["detail-event"],
+ );
+ assert.deepEqual(
+ detail.artifacts.map((artifact) => artifact.id),
+ ["detail-artifact"],
+ );
+ assert.ok(rows < 20, `detail() read ${rows} rows to show one task`);
+});