-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix(workflow): persist execution metadata and fix SQL metadata filters #1085
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
Merged
Merged
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| --- | ||
| "@voltagent/core": patch | ||
| "@voltagent/server-core": patch | ||
| "@voltagent/libsql": patch | ||
| "@voltagent/cloudflare-d1": patch | ||
| --- | ||
|
|
||
| Fix workflow execution filtering by persisted metadata across adapters. | ||
|
|
||
| - Persist `options.metadata` on workflow execution state so `/workflows/executions` filters can match tenant/user metadata. | ||
| - Preserve existing execution metadata when updating cancelled/error workflow states. | ||
| - Accept `options.metadata` in server workflow execution request schema. | ||
| - Fix LibSQL and Cloudflare D1 JSON metadata query comparisons for `metadata` and `metadata.<key>` filters. |
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,83 @@ | ||
| import type { D1Database } from "@cloudflare/workers-types"; | ||
| import { describe, expect, it, vi } from "vitest"; | ||
| import { D1MemoryAdapter } from "./memory-adapter"; | ||
|
|
||
| function createMockBinding(): D1Database { | ||
| return { | ||
| prepare: vi.fn(() => ({ | ||
| bind: vi.fn().mockReturnThis(), | ||
| run: vi.fn().mockResolvedValue({}), | ||
| all: vi.fn().mockResolvedValue({ results: [] }), | ||
| })), | ||
| batch: vi.fn().mockResolvedValue([]), | ||
| } as unknown as D1Database; | ||
| } | ||
|
|
||
| describe("D1MemoryAdapter queryWorkflowRuns", () => { | ||
| it("builds metadata filters with JSON-aware comparisons", async () => { | ||
| vi.spyOn(D1MemoryAdapter.prototype as any, "ensureInitialized").mockResolvedValue(undefined); | ||
|
|
||
| const adapter = new D1MemoryAdapter({ | ||
| binding: createMockBinding(), | ||
| tablePrefix: "test", | ||
| }); | ||
|
|
||
| const allSpy = vi.spyOn(adapter as any, "all").mockResolvedValue([ | ||
| { | ||
| id: "exec-1", | ||
| workflow_id: "workflow-1", | ||
| workflow_name: "Workflow 1", | ||
| status: "completed", | ||
| input: '{"requestId":"req-1"}', | ||
| context: '[["tenantId","acme"]]', | ||
| workflow_state: '{"phase":"done"}', | ||
| suspension: null, | ||
| events: null, | ||
| output: null, | ||
| cancellation: null, | ||
| user_id: "user-1", | ||
| conversation_id: null, | ||
| metadata: '{"tenantId":"acme"}', | ||
| created_at: "2024-01-02T00:00:00Z", | ||
| updated_at: "2024-01-02T00:00:00Z", | ||
| }, | ||
| ]); | ||
|
|
||
| const result = await adapter.queryWorkflowRuns({ | ||
| workflowId: "workflow-1", | ||
| status: "completed", | ||
| from: new Date("2024-01-01T00:00:00Z"), | ||
| to: new Date("2024-01-03T00:00:00Z"), | ||
| userId: "user-1", | ||
| metadata: { tenantId: "acme" }, | ||
| limit: 5, | ||
| offset: 2, | ||
| }); | ||
|
|
||
| expect(result).toHaveLength(1); | ||
| expect(result[0]?.input).toEqual({ requestId: "req-1" }); | ||
| expect(result[0]?.context).toEqual([["tenantId", "acme"]]); | ||
| expect(result[0]?.workflowState).toEqual({ phase: "done" }); | ||
|
|
||
| expect(allSpy).toHaveBeenCalledTimes(1); | ||
| const [sql, args] = allSpy.mock.calls[0]; | ||
|
|
||
| expect(sql).toContain("workflow_id = ?"); | ||
| expect(sql).toContain("status = ?"); | ||
| expect(sql).toContain("created_at >="); | ||
| expect(sql).toContain("user_id = ?"); | ||
| expect(sql).toContain("json_extract(metadata, ?) = json_extract(json(?), '$')"); | ||
| expect(sql).toContain("ORDER BY created_at DESC"); | ||
| expect(args).toEqual([ | ||
| "workflow-1", | ||
| "completed", | ||
| "2024-01-01T00:00:00.000Z", | ||
| "2024-01-03T00:00:00.000Z", | ||
| "user-1", | ||
| '$."tenantId"', | ||
| '"acme"', | ||
| 5, | ||
| 2, | ||
| ]); | ||
| }); | ||
| }); | ||
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
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
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,22 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { WorkflowExecutionRequestSchema } from "./agent.schemas"; | ||
|
|
||
| describe("WorkflowExecutionRequestSchema", () => { | ||
| it("accepts options.metadata payload", () => { | ||
| const parsed = WorkflowExecutionRequestSchema.parse({ | ||
| input: { value: 1 }, | ||
| options: { | ||
| userId: "user-1", | ||
| metadata: { | ||
| tenantId: "acme", | ||
| region: "us-east-1", | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| expect(parsed.options?.metadata).toEqual({ | ||
| tenantId: "acme", | ||
| region: "us-east-1", | ||
| }); | ||
| }); | ||
| }); |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to-filter SQL condition is not asserted.The test verifies
created_at >=(forfrom) and includes theto-date value in theargsarray, but never checks the upper-bound SQL condition (e.g.,created_at <=). A bug that emits the wrong operator or omits the clause entirely would pass this test.🔍 Suggested additional assertion
expect(sql).toContain("created_at >="); +expect(sql).toContain("created_at <=");📝 Committable suggestion
🤖 Prompt for AI Agents