Skip to content

Commit 1ac68f3

Browse files
kvnloojerelvelarde
andauthored
perf(server): scope refreshIdeas status queries (#109)
* perf(store): list records by status * perf(server): scope refreshIdeas status queries * test(store): cover status-scoped listing * chore: match repository formatting after main reconciliation --------- Co-authored-by: Jerel John Velarde <jerel@copilotkit.ai>
1 parent ad989b4 commit 1ac68f3

3 files changed

Lines changed: 48 additions & 6 deletions

File tree

‎apps/server/src/db.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ export class Store {
4141
);
4242
return result.rows.map((row) => row.data as T);
4343
}
44+
async listByStatus<T = Record<string, unknown>>(
45+
owner: string,
46+
kind: string,
47+
status: string,
48+
): Promise<T[]> {
49+
const result = await this.db.query(
50+
"SELECT data FROM records WHERE owner=$1 AND kind=$2 AND data->>'status'=$3 ORDER BY updated_at DESC,id",
51+
[owner, kind, status],
52+
);
53+
return result.rows.map((row) => row.data as T);
54+
}
4455
async put<T extends { id: string }>(owner: string, kind: string, value: T): Promise<T> {
4556
await this.db.query(
4657
"INSERT INTO records(owner,kind,id,data) VALUES($1,$2,$3,$4::jsonb) ON CONFLICT(owner,kind,id) DO UPDATE SET data=excluded.data,updated_at=now()",

‎apps/server/src/engine/service.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -557,17 +557,17 @@ export class AgentService {
557557
w.mail.filter((mail) => /^Sent\b/i.test(mail.label)).map((mail) => mail.id),
558558
);
559559
const completedSources = new Set(
560-
(await this.db.list<AgentTask>(owner, "tasks"))
561-
.filter((task) => task.status === "succeeded" && typeof task.input.messageId === "string")
560+
(await this.db.listByStatus<AgentTask>(owner, "tasks", "succeeded"))
561+
.filter((task) => typeof task.input.messageId === "string")
562562
.map((task) => `${task.kind}:${task.input.messageId}`),
563563
);
564564
const obsolete = (kind: AgentTask["kind"], messageId: unknown) =>
565565
typeof messageId === "string" &&
566566
(sentIds.has(messageId) || completedSources.has(`${kind}:${messageId}`));
567567
// Retire earlier suggestions as well as preventing new duplicates. A concurrent
568568
// acceptance wins its own compare-and-swap and is never overwritten here.
569-
for (const idea of await this.db.list<Idea>(owner, "ideas"))
570-
if (idea.status === "new" && obsolete(idea.kind, idea.input.messageId))
569+
for (const idea of await this.db.listByStatus<Idea>(owner, "ideas", "new"))
570+
if (obsolete(idea.kind, idea.input.messageId))
571571
await this.db.compareAndSwap(
572572
owner,
573573
"ideas",
@@ -616,8 +616,8 @@ export class AgentService {
616616
createdAt: date(),
617617
} satisfies Idea);
618618
}
619-
for (const goal of await this.db.list<Goal>(owner, "goals"))
620-
if (goal.status === "active" && !goal.milestones.length) {
619+
for (const goal of await this.db.listByStatus<Goal>(owner, "goals", "active"))
620+
if (!goal.milestones.length) {
621621
const id = hash(`goal:${goal.id}:${goal.description}`);
622622
await this.db.insertIfAbsent(owner, "ideas", {
623623
id,

‎tests/persistence.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,34 @@ test("listByGoalId matches list plus goalId filtering", async () => {
109109
await store.close();
110110
}
111111
});
112+
113+
test("listByStatus matches list plus status filtering", async () => {
114+
const store = await createStore();
115+
try {
116+
for (const [id, status] of [
117+
["i0", "new"],
118+
["i1", "dismissed"],
119+
["i2", "new"],
120+
["i3", "accepted"],
121+
] as const)
122+
await store.put("owner", "ideas", { id, status });
123+
await store.put("owner", "ideas", { id: "i-none" });
124+
125+
const scoped = await store.listByStatus<{ id: string; status?: string }>(
126+
"owner",
127+
"ideas",
128+
"new",
129+
);
130+
const expected = (await store.list<{ id: string; status?: string }>("owner", "ideas")).filter(
131+
(item) => item.status === "new",
132+
);
133+
assert.deepEqual(scoped, expected);
134+
135+
await store.put("other", "ideas", { id: "x", status: "new" });
136+
await store.put("owner", "goals", { id: "g", status: "new" });
137+
assert.equal((await store.listByStatus("owner", "ideas", "new")).length, 2);
138+
assert.equal((await store.listByStatus("nobody", "ideas", "new")).length, 0);
139+
} finally {
140+
await store.close();
141+
}
142+
});

0 commit comments

Comments
 (0)