Repository navigation
perf(server): skip already-published task outcomes in maintenance #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kvnloo
wants to merge
3
commits into
CopilotKit:main
Choose a base branch
from
kvnloo:perf/maintain-outcome-marker-safe
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,240 @@ | ||
| import assert from "node:assert/strict"; | ||
| import { createHash } from "node:crypto"; | ||
| import { test } from "node:test"; | ||
| import { createStore, type Store } from "../apps/server/src/db.ts"; | ||
| import { AgentService } from "../apps/server/src/engine/service.ts"; | ||
| import type { AgentNotification, AgentTask, Goal } from "../packages/domain/src/agent.ts"; | ||
|
|
||
| function task(id: string, status: AgentTask["status"], extra: Partial<AgentTask> = {}): AgentTask { | ||
| return { | ||
| id, | ||
| title: `Task ${id}`, | ||
| prompt: `Task ${id}`, | ||
| kind: "agent", | ||
| status, | ||
| plan: [], | ||
| evidence: [], | ||
| input: {}, | ||
| state: {}, | ||
| createdAt: new Date().toISOString(), | ||
| updatedAt: new Date().toISOString(), | ||
| attempts: status === "failed" ? 3 : 1, | ||
| error: status === "failed" ? "simulated failure" : null, | ||
| leaseId: null, | ||
| leaseUntil: null, | ||
| artifactIds: [], | ||
| ...extra, | ||
| }; | ||
| } | ||
|
|
||
| function serviceWith(db: Store) { | ||
| const service = new AgentService( | ||
| db, | ||
| {} as never, | ||
| {} as never, | ||
| {} as never, | ||
| {} as never, | ||
| {} as never, | ||
| ); | ||
| return service as unknown as { maintain(): Promise<void> }; | ||
| } | ||
|
|
||
| async function notifications(db: Store): Promise<AgentNotification[]> { | ||
| return (await db.scan<AgentNotification>("notifications")).map(({ value }) => value); | ||
| } | ||
|
|
||
| test("maintenance recovers a lost publication once, then skips the marked row", async () => { | ||
| const db = await createStore(); | ||
| try { | ||
| await db.put("owner", "tasks", task("task1", "succeeded", { result: "done" })); | ||
| const service = serviceWith(db); | ||
|
|
||
| await service.maintain(); | ||
| assert.equal((await notifications(db)).filter((n) => n.taskId === "task1").length, 1); | ||
| assert.equal( | ||
| (await db.get<AgentTask>("owner", "tasks", "task1"))?.state.publishedOutcome, | ||
| "task-done:task1", | ||
| ); | ||
|
|
||
| let taskGets = 0; | ||
| const originalGet = db.get.bind(db); | ||
| db.get = (async (owner: string, kind: string, id: string) => { | ||
| if (kind === "tasks") taskGets++; | ||
| return originalGet(owner, kind, id); | ||
| }) as Store["get"]; | ||
| await service.maintain(); | ||
| db.get = originalGet; | ||
| assert.equal(taskGets, 0); | ||
| assert.equal((await notifications(db)).filter((n) => n.taskId === "task1").length, 1); | ||
| } finally { | ||
| await db.close(); | ||
| } | ||
| }); | ||
|
|
||
| test("outcome marker preserves a concurrent sibling state write", async () => { | ||
| const db = await createStore(); | ||
| try { | ||
| await db.put( | ||
| "owner", | ||
| "tasks", | ||
| task("task1", "succeeded", { result: "done", state: { existing: "value" } }), | ||
| ); | ||
| const service = serviceWith(db); | ||
| const originalInsert = db.insertIfAbsent.bind(db); | ||
| let injected = false; | ||
| db.insertIfAbsent = (async (owner: string, kind: string, value: { id: string }) => { | ||
| if (kind === "notifications" && !injected) { | ||
| injected = true; | ||
| const latest = await db.get<AgentTask>("owner", "tasks", "task1"); | ||
| assert.ok(latest); | ||
| await db.compareAndSwap( | ||
| "owner", | ||
| "tasks", | ||
| "task1", | ||
| { status: "succeeded" }, | ||
| { state: { ...latest.state, concurrent: "kept" } }, | ||
| ); | ||
| } | ||
| return originalInsert(owner, kind, value); | ||
| }) as Store["insertIfAbsent"]; | ||
|
|
||
| await service.maintain(); | ||
| const saved = await db.get<AgentTask>("owner", "tasks", "task1"); | ||
| assert.equal(saved?.state.existing, "value"); | ||
| assert.equal(saved?.state.concurrent, "kept"); | ||
| assert.equal(saved?.state.publishedOutcome, "task-done:task1"); | ||
| } finally { | ||
| await db.close(); | ||
| } | ||
| }); | ||
|
|
||
| test("a same-status outcome change is not hidden by a stale marker", async () => { | ||
| const db = await createStore(); | ||
| try { | ||
| await db.put("owner", "tasks", task("task1", "waiting_input", { question: "Old question?" })); | ||
| const service = serviceWith(db); | ||
| const originalInsert = db.insertIfAbsent.bind(db); | ||
| let changed = false; | ||
| db.insertIfAbsent = (async (owner: string, kind: string, value: { id: string }) => { | ||
| if (kind === "notifications" && !changed) { | ||
| changed = true; | ||
| await db.compareAndSwap( | ||
| "owner", | ||
| "tasks", | ||
| "task1", | ||
| { status: "waiting_input", question: "Old question?" }, | ||
| { question: "New question?" }, | ||
| ); | ||
| } | ||
| return originalInsert(owner, kind, value); | ||
| }) as Store["insertIfAbsent"]; | ||
|
|
||
| await service.maintain(); | ||
| let saved = await db.get<AgentTask>("owner", "tasks", "task1"); | ||
| assert.equal(saved?.question, "New question?"); | ||
| assert.equal(saved?.state.publishedOutcome, undefined); | ||
|
|
||
| db.insertIfAbsent = originalInsert as Store["insertIfAbsent"]; | ||
| await service.maintain(); | ||
| saved = await db.get<AgentTask>("owner", "tasks", "task1"); | ||
| const expected = `input:task1:${createHash("sha256").update("New question?").digest("hex")}`; | ||
| assert.equal(saved?.state.publishedOutcome, expected); | ||
| assert.equal((await notifications(db)).filter((n) => n.taskId === "task1").length, 2); | ||
| } finally { | ||
| await db.close(); | ||
| } | ||
| }); | ||
|
|
||
| test("outcome marker waits for goal milestone reconciliation after contention", async () => { | ||
| const db = await createStore(); | ||
| const originalCompareAndSwap = db.compareAndSwap.bind(db); | ||
| try { | ||
| const goal: Goal = { | ||
| id: "goal1", | ||
| title: "Goal", | ||
| description: "", | ||
| category: "Personal", | ||
| status: "active", | ||
| milestones: [], | ||
| createdAt: new Date().toISOString(), | ||
| }; | ||
| await db.put("owner", "goals", goal); | ||
| await db.put("owner", "tasks", task("task1", "succeeded", { result: "done", goalId: goal.id })); | ||
| let rejectedGoalWrites = 0; | ||
| db.compareAndSwap = (async ( | ||
| owner: string, | ||
| kind: string, | ||
| id: string, | ||
| expected: Record<string, unknown>, | ||
| patch: Record<string, unknown>, | ||
| ) => { | ||
| if (kind === "goals" && rejectedGoalWrites < 8) { | ||
| rejectedGoalWrites++; | ||
| return null; | ||
| } | ||
| return originalCompareAndSwap(owner, kind, id, expected, patch); | ||
| }) as Store["compareAndSwap"]; | ||
| const service = serviceWith(db); | ||
|
|
||
| await service.maintain(); | ||
| assert.equal(rejectedGoalWrites, 8); | ||
| assert.equal((await db.get<Goal>("owner", "goals", goal.id))?.milestones.length, 0); | ||
| assert.equal( | ||
| (await db.get<AgentTask>("owner", "tasks", "task1"))?.state.publishedOutcome, | ||
| undefined, | ||
| ); | ||
| assert.equal((await notifications(db)).filter((n) => n.taskId === "task1").length, 1); | ||
|
|
||
| await service.maintain(); | ||
| assert.deepEqual((await db.get<Goal>("owner", "goals", goal.id))?.milestones, [ | ||
| { id: "task1", title: "Task task1", done: true }, | ||
| ]); | ||
| assert.equal( | ||
| (await db.get<AgentTask>("owner", "tasks", "task1"))?.state.publishedOutcome, | ||
| "task-done:task1", | ||
| ); | ||
| assert.equal((await notifications(db)).filter((n) => n.taskId === "task1").length, 1); | ||
| } finally { | ||
| db.compareAndSwap = originalCompareAndSwap as Store["compareAndSwap"]; | ||
| await db.close(); | ||
| } | ||
| }); | ||
|
|
||
| test("linked milestone completion stays manual while the outcome is marked published", async () => { | ||
| const db = await createStore(); | ||
| try { | ||
| const goal: Goal = { | ||
| id: "goal1", | ||
| title: "Goal", | ||
| description: "", | ||
| category: "Personal", | ||
| status: "active", | ||
| milestones: [{ id: "manual", title: "Manual milestone", done: false }], | ||
| createdAt: new Date().toISOString(), | ||
| }; | ||
| await db.put("owner", "goals", goal); | ||
| await db.put( | ||
| "owner", | ||
| "tasks", | ||
| task("task1", "succeeded", { | ||
| result: "done", | ||
| goalId: goal.id, | ||
| milestoneId: "manual", | ||
| }), | ||
| ); | ||
| const service = serviceWith(db); | ||
|
|
||
| await service.maintain(); | ||
|
|
||
| assert.deepEqual((await db.get<Goal>("owner", "goals", goal.id))?.milestones, [ | ||
| { id: "manual", title: "Manual milestone", done: false }, | ||
| ]); | ||
| assert.equal( | ||
| (await db.get<AgentTask>("owner", "tasks", "task1"))?.state.publishedOutcome, | ||
| "task-done:task1", | ||
| ); | ||
| assert.equal((await notifications(db)).filter((n) => n.taskId === "task1").length, 1); | ||
| } finally { | ||
| await db.close(); | ||
| } | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.