-
Notifications
You must be signed in to change notification settings - Fork 53
fix(agent-builder): resolve target revision for set_secret/connect_mcp punch-outs #3323
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
dmarticus
wants to merge
3
commits into
main
Choose a base branch
from
posthog-code/fix-agent-builder-set-secret-revision
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
Show all changes
3 commits
Select commit
Hold shift + click to select a range
f9d0bfc
fix(agent-builder): resolve target revision for set_secret/connect_mc…
dmarticus 3328a2e
fix(agent-builder): don't let a viewed draft override rotate-to-live …
dmarticus 1e0b1bc
fix(agent-builder): verify explicit revision_id belongs to the target…
dmarticus 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
231 changes: 231 additions & 0 deletions
231
packages/ui/src/features/agent-applications/agent-builder/useAgentBuilderClientTools.test.ts
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,231 @@ | ||
| import { renderHook } from "@testing-library/react"; | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| vi.mock("@tanstack/react-router", () => ({ | ||
| useNavigate: () => vi.fn(), | ||
| })); | ||
|
|
||
| const client = { | ||
| getAgentApplication: vi.fn(), | ||
| listAgentRevisions: vi.fn(), | ||
| getAgentRevision: vi.fn(), | ||
| }; | ||
|
|
||
| vi.mock("@posthog/ui/features/auth/authClient", () => ({ | ||
| useAuthenticatedClient: () => client, | ||
| })); | ||
|
|
||
| import type { ClientToolCallData } from "@posthog/core/agent-chat/identifiers"; | ||
| import { useAgentBuilderStore } from "./agentBuilderStore"; | ||
| import { useAgentBuilderClientTools } from "./useAgentBuilderClientTools"; | ||
|
|
||
| function call( | ||
| tool_id: string, | ||
| args: Record<string, unknown>, | ||
| ): ClientToolCallData { | ||
| return { call_id: "call-1", tool_id, args }; | ||
| } | ||
|
|
||
| function handler() { | ||
| return renderHook(() => useAgentBuilderClientTools()).result.current; | ||
| } | ||
|
|
||
| describe("useAgentBuilderClientTools revision resolution", () => { | ||
| beforeEach(() => { | ||
| client.getAgentApplication.mockReset(); | ||
| client.listAgentRevisions.mockReset(); | ||
| client.getAgentRevision.mockReset(); | ||
| useAgentBuilderStore.setState({ | ||
| page: { kind: "unknown" }, | ||
| pendingSecret: null, | ||
| pendingMcpConnect: null, | ||
| }); | ||
| }); | ||
|
|
||
| it("uses an explicit revision_id once it verifies as belonging to the agent", async () => { | ||
| client.getAgentRevision.mockResolvedValue({ id: "rev-explicit" }); | ||
|
|
||
| const outcome = await handler()( | ||
| call("set_secret", { | ||
| agent_slug: "my-agent", | ||
| secret: "API_KEY", | ||
| revision_id: "rev-explicit", | ||
| }), | ||
| ); | ||
|
|
||
| expect(outcome).toEqual({ defer: true }); | ||
| expect(useAgentBuilderStore.getState().pendingSecret).toMatchObject({ | ||
| agentSlug: "my-agent", | ||
| secret: "API_KEY", | ||
| revisionId: "rev-explicit", | ||
| }); | ||
| expect(client.getAgentRevision).toHaveBeenCalledWith( | ||
| "my-agent", | ||
| "rev-explicit", | ||
| ); | ||
| expect(client.getAgentApplication).not.toHaveBeenCalled(); | ||
| expect(client.listAgentRevisions).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("rejects an explicit revision_id that does not belong to the agent", async () => { | ||
| // The nested revision route 404s (→ null) for another agent's revision. | ||
| client.getAgentRevision.mockResolvedValue(null); | ||
|
|
||
| const outcome = await handler()( | ||
| call("set_secret", { | ||
| agent_slug: "my-agent", | ||
| secret: "API_KEY", | ||
| revision_id: "rev-of-other-agent", | ||
| }), | ||
| ); | ||
|
|
||
| expect(outcome).toEqual({ | ||
| error: "revision_not_found: rev-of-other-agent on my-agent", | ||
| }); | ||
| expect(useAgentBuilderStore.getState().pendingSecret).toBeNull(); | ||
| }); | ||
|
|
||
| it("falls back to the revision open on this agent's config page", async () => { | ||
| useAgentBuilderStore.setState({ | ||
| page: { kind: "agent-config", slug: "my-agent", revision: "rev-page" }, | ||
| }); | ||
|
|
||
| const outcome = await handler()( | ||
| call("set_secret", { agent_slug: "my-agent", secret: "API_KEY" }), | ||
| ); | ||
|
|
||
| expect(outcome).toEqual({ defer: true }); | ||
| expect(useAgentBuilderStore.getState().pendingSecret?.revisionId).toBe( | ||
| "rev-page", | ||
| ); | ||
| expect(client.getAgentApplication).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("rotation targets live even while a draft config page is open", async () => { | ||
| useAgentBuilderStore.setState({ | ||
| page: { kind: "agent-config", slug: "my-agent", revision: "rev-draft" }, | ||
| }); | ||
| client.getAgentApplication.mockResolvedValue({ live_revision: "rev-live" }); | ||
| client.listAgentRevisions.mockResolvedValue([ | ||
| { id: "rev-draft", state: "draft" }, | ||
| { id: "rev-live", state: "ready" }, | ||
| ]); | ||
|
|
||
| await handler()( | ||
| call("set_secret", { | ||
| agent_slug: "my-agent", | ||
| secret: "API_KEY", | ||
| mode: "rotate", | ||
| }), | ||
| ); | ||
|
|
||
| expect(useAgentBuilderStore.getState().pendingSecret?.revisionId).toBe( | ||
| "rev-live", | ||
| ); | ||
| }); | ||
|
|
||
| it("ignores the page revision when it belongs to a different agent", async () => { | ||
| useAgentBuilderStore.setState({ | ||
| page: { kind: "agent-config", slug: "other-agent", revision: "rev-page" }, | ||
| }); | ||
| client.getAgentApplication.mockResolvedValue({ live_revision: null }); | ||
| client.listAgentRevisions.mockResolvedValue([ | ||
| { id: "rev-draft", state: "draft" }, | ||
| ]); | ||
|
|
||
| await handler()( | ||
| call("set_secret", { agent_slug: "my-agent", secret: "API_KEY" }), | ||
| ); | ||
|
|
||
| expect(useAgentBuilderStore.getState().pendingSecret?.revisionId).toBe( | ||
| "rev-draft", | ||
| ); | ||
| }); | ||
|
|
||
| it.each([ | ||
| // A new secret targets the draft being authored (secrets only copy | ||
| // forward at draft creation), even when a live revision exists. | ||
| { mode: "set", expected: "rev-draft" }, | ||
| // A rotation targets what's running. | ||
| { mode: "rotate", expected: "rev-live" }, | ||
| ])( | ||
| "set_secret mode=$mode resolves to $expected via the API", | ||
| async ({ mode, expected }) => { | ||
| client.getAgentApplication.mockResolvedValue({ | ||
| live_revision: "rev-live", | ||
| }); | ||
| client.listAgentRevisions.mockResolvedValue([ | ||
| { id: "rev-draft", state: "draft" }, | ||
| { id: "rev-live", state: "ready" }, | ||
| ]); | ||
|
|
||
| await handler()( | ||
| call("set_secret", { agent_slug: "my-agent", secret: "API_KEY", mode }), | ||
| ); | ||
|
|
||
| expect(useAgentBuilderStore.getState().pendingSecret?.revisionId).toBe( | ||
| expected, | ||
| ); | ||
| }, | ||
| ); | ||
|
|
||
| it("set_secret falls back to the newest revision when no live or draft exists", async () => { | ||
| client.getAgentApplication.mockResolvedValue({ live_revision: null }); | ||
| client.listAgentRevisions.mockResolvedValue([ | ||
| { id: "rev-newest", state: "ready" }, | ||
| { id: "rev-older", state: "ready" }, | ||
| ]); | ||
|
|
||
| await handler()( | ||
| call("set_secret", { agent_slug: "my-agent", secret: "API_KEY" }), | ||
| ); | ||
|
|
||
| expect(useAgentBuilderStore.getState().pendingSecret?.revisionId).toBe( | ||
| "rev-newest", | ||
| ); | ||
| }); | ||
|
|
||
| it("errors when the agent has no revisions at all", async () => { | ||
| client.getAgentApplication.mockResolvedValue({ live_revision: null }); | ||
| client.listAgentRevisions.mockResolvedValue([]); | ||
|
|
||
| const outcome = await handler()( | ||
| call("set_secret", { agent_slug: "my-agent", secret: "API_KEY" }), | ||
| ); | ||
|
|
||
| expect(outcome).toEqual({ error: "no_target_revision: my-agent" }); | ||
| expect(useAgentBuilderStore.getState().pendingSecret).toBeNull(); | ||
| }); | ||
|
|
||
| it("errors when revision lookup fails", async () => { | ||
| client.getAgentApplication.mockRejectedValue(new Error("network")); | ||
| client.listAgentRevisions.mockRejectedValue(new Error("network")); | ||
|
|
||
| const outcome = await handler()( | ||
| call("set_secret", { agent_slug: "my-agent", secret: "API_KEY" }), | ||
| ); | ||
|
|
||
| expect(outcome).toEqual({ error: "no_target_revision: my-agent" }); | ||
| }); | ||
|
|
||
| it("connect_mcp prefers the newest draft (spec edits are draft-only)", async () => { | ||
| client.getAgentApplication.mockResolvedValue({ | ||
| live_revision: "rev-live", | ||
| }); | ||
| client.listAgentRevisions.mockResolvedValue([ | ||
| { id: "rev-draft", state: "draft" }, | ||
| { id: "rev-live", state: "ready" }, | ||
| ]); | ||
|
|
||
| const outcome = await handler()( | ||
| call("connect_mcp", { agent_slug: "my-agent", url: "https://mcp.test" }), | ||
| ); | ||
|
|
||
| expect(outcome).toEqual({ defer: true }); | ||
| expect(useAgentBuilderStore.getState().pendingMcpConnect).toMatchObject({ | ||
| agentSlug: "my-agent", | ||
| revisionId: "rev-draft", | ||
| url: "https://mcp.test", | ||
| }); | ||
| }); | ||
| }); |
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
Oops, something went wrong.
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.