-
Notifications
You must be signed in to change notification settings - Fork 1
refactor: use RECOUP_ORG_ID for snapshot persistence on merge #269
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
sweetmantech
merged 9 commits into
test
from
sweetmantech/myc-4442-api-onmergebutton-update-snapshot-for-recoup_org
Mar 9, 2026
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
83666b4
feat(coding-agent): persist snapshot on merge via upsertAccountSnapshot
84e516d
refactor: use RECOUP_ORG_ID from shared const for snapshot persistence
sweetmantech 61a8571
fix: only persist snapshot when all PR merges succeed
sweetmantech b54b676
Merge remote-tracking branch 'origin/test' into sweetmantech/myc-4442…
sweetmantech e5f9a1d
fix: revert status to pr_created on merge failure so replies still work
sweetmantech b7c594e
refactor: extract handleMergeSuccess for SRP
sweetmantech bb3126c
Merge remote-tracking branch 'origin/test' into sweetmantech/myc-4442…
sweetmantech 4071cb6
fix: improve handleMergeSuccess error handling and multi-repo cleanup
sweetmantech 59d448c
refactor: move SNAPSHOT_EXPIRY_MS to shared const and set to 7 days
sweetmantech 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,113 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
|
|
||
| const mockDeletePRState = vi.fn(); | ||
| vi.mock("../prState", () => ({ | ||
| deleteCodingAgentPRState: (...args: unknown[]) => mockDeletePRState(...args), | ||
| })); | ||
|
|
||
| const mockUpsertAccountSnapshot = vi.fn(); | ||
| vi.mock("@/lib/supabase/account_snapshots/upsertAccountSnapshot", () => ({ | ||
| upsertAccountSnapshot: (...args: unknown[]) => mockUpsertAccountSnapshot(...args), | ||
| })); | ||
|
|
||
| const { handleMergeSuccess } = await import("../handleMergeSuccess"); | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| mockUpsertAccountSnapshot.mockResolvedValue({ data: {}, error: null }); | ||
| }); | ||
|
|
||
| describe("handleMergeSuccess", () => { | ||
| it("deletes PR state and persists snapshot", async () => { | ||
| await handleMergeSuccess({ | ||
| status: "pr_created", | ||
| branch: "agent/fix-bug", | ||
| snapshotId: "snap_abc123", | ||
| prs: [{ repo: "recoupable/api", number: 42, url: "url", baseBranch: "test" }], | ||
| }); | ||
|
|
||
| expect(mockDeletePRState).toHaveBeenCalledWith("recoupable/api", "agent/fix-bug"); | ||
| expect(mockUpsertAccountSnapshot).toHaveBeenCalledWith({ | ||
| account_id: "04e3aba9-c130-4fb8-8b92-34e95d43e66b", | ||
| snapshot_id: "snap_abc123", | ||
| expires_at: expect.any(String), | ||
| }); | ||
| }); | ||
|
|
||
| it("deletes PR state for all repos when PRs span multiple repos", async () => { | ||
| await handleMergeSuccess({ | ||
| status: "pr_created", | ||
| branch: "agent/fix-bug", | ||
| prs: [ | ||
| { repo: "recoupable/api", number: 42, url: "url", baseBranch: "test" }, | ||
| { repo: "recoupable/chat", number: 10, url: "url", baseBranch: "test" }, | ||
| { repo: "recoupable/api", number: 43, url: "url", baseBranch: "test" }, | ||
| ], | ||
| }); | ||
|
|
||
| expect(mockDeletePRState).toHaveBeenCalledTimes(2); | ||
| expect(mockDeletePRState).toHaveBeenCalledWith("recoupable/api", "agent/fix-bug"); | ||
| expect(mockDeletePRState).toHaveBeenCalledWith("recoupable/chat", "agent/fix-bug"); | ||
| }); | ||
|
|
||
| it("skips snapshot persistence when snapshotId is not in state", async () => { | ||
| await handleMergeSuccess({ | ||
| status: "pr_created", | ||
| branch: "agent/fix-bug", | ||
| prs: [{ repo: "recoupable/api", number: 42, url: "url", baseBranch: "test" }], | ||
| }); | ||
|
|
||
| expect(mockDeletePRState).toHaveBeenCalledWith("recoupable/api", "agent/fix-bug"); | ||
| expect(mockUpsertAccountSnapshot).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("logs error but does not throw when snapshot persistence fails", async () => { | ||
| mockUpsertAccountSnapshot.mockResolvedValue({ | ||
| data: null, | ||
| error: { message: "db error", code: "500" }, | ||
| }); | ||
| const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {}); | ||
|
|
||
| await handleMergeSuccess({ | ||
| status: "pr_created", | ||
| branch: "agent/fix-bug", | ||
| snapshotId: "snap_abc123", | ||
| prs: [{ repo: "recoupable/api", number: 42, url: "url", baseBranch: "test" }], | ||
| }); | ||
|
|
||
| expect(consoleSpy).toHaveBeenCalledWith( | ||
| expect.stringContaining("failed to persist snapshot"), | ||
| expect.anything(), | ||
| ); | ||
| consoleSpy.mockRestore(); | ||
| }); | ||
|
|
||
| it("does not throw when deleteCodingAgentPRState throws", async () => { | ||
| mockDeletePRState.mockRejectedValue(new Error("Redis connection failed")); | ||
| const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {}); | ||
|
|
||
| await expect( | ||
| handleMergeSuccess({ | ||
| status: "pr_created", | ||
| branch: "agent/fix-bug", | ||
| snapshotId: "snap_abc123", | ||
| prs: [{ repo: "recoupable/api", number: 42, url: "url", baseBranch: "test" }], | ||
| }), | ||
| ).resolves.toBeUndefined(); | ||
|
|
||
| expect(consoleSpy).toHaveBeenCalledWith( | ||
| "[coding-agent] post-merge cleanup failed:", | ||
| expect.any(Error), | ||
| ); | ||
| consoleSpy.mockRestore(); | ||
| }); | ||
|
|
||
| it("skips PR state cleanup when branch is missing", async () => { | ||
| await handleMergeSuccess({ | ||
| status: "pr_created", | ||
| prs: [{ repo: "recoupable/api", number: 42, url: "url", baseBranch: "test" }], | ||
| }); | ||
|
|
||
| expect(mockDeletePRState).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
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,35 @@ | ||
| import { deleteCodingAgentPRState } from "./prState"; | ||
| import { upsertAccountSnapshot } from "@/lib/supabase/account_snapshots/upsertAccountSnapshot"; | ||
| import { RECOUP_ORG_ID, SNAPSHOT_EXPIRY_MS } from "@/lib/const"; | ||
| import type { CodingAgentThreadState } from "./types"; | ||
|
|
||
| /** | ||
| * Handles post-merge cleanup after all PRs merged successfully. | ||
| * Deletes the shared PR state keys for all repos and persists the latest | ||
| * snapshot via upsertAccountSnapshot. | ||
| */ | ||
| export async function handleMergeSuccess(state: CodingAgentThreadState): Promise<void> { | ||
| try { | ||
| if (state.branch && state.prs?.length) { | ||
| const repos = [...new Set(state.prs.map(pr => pr.repo))]; | ||
| await Promise.all(repos.map(repo => deleteCodingAgentPRState(repo, state.branch!))); | ||
| } | ||
|
|
||
| if (state.snapshotId) { | ||
| const snapshotResult = await upsertAccountSnapshot({ | ||
| account_id: RECOUP_ORG_ID, | ||
| snapshot_id: state.snapshotId, | ||
| expires_at: new Date(Date.now() + SNAPSHOT_EXPIRY_MS).toISOString(), | ||
| }); | ||
|
|
||
| if (snapshotResult.error) { | ||
| console.error( | ||
| `[coding-agent] failed to persist snapshot for ${RECOUP_ORG_ID}:`, | ||
| snapshotResult.error, | ||
| ); | ||
| } | ||
| } | ||
| } catch (error) { | ||
| console.error("[coding-agent] post-merge cleanup failed:", error); | ||
| } | ||
| } | ||
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
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.