Repository navigation
feat: implement GET /api/v1/agents/<agent_id>/versions API - #15629
Conversation
- Add UserCanvasVersionDAO with ListByCanvasID and GetByID - Add CheckCanvasAccess, ListVersions, GetVersion to AgentService - Add ListAgentVersions handler with auth check - Register route GET /api/v1/agents/:agent_id/versions - Add service-level tests (5 test cases, zero mock) - Add handler-level tests (3 test cases, SQLite in-memory DB) Co-Authored-By: Claude Opus 4.8 <[email protected]>
📝 WalkthroughWalkthroughAdds UserCanvasVersionDAO, wires it into AgentService (CheckCanvasAccess, ListVersions, GetVersion), exposes GET /api/v1/agents/:agent_id/versions and GET /api/v1/agents/:agent_id/versions/:version_id handlers, updates router and Vite proxy, and adds unit/integration tests; removes unused test contents in two files. ChangesAgent Version Listing
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@internal/handler/agent_test.go`:
- Around line 91-101: The test inserts two entity.UserCanvasVersion rows without
setting UpdateTime, causing nondeterministic sort order; update the fixture to
assign distinct UpdateTime values (e.g., set one to time.Now() and the other to
time.Now().Add(-time.Second) or fixed timestamps) for the records with IDs "v2"
and "v1" so the DAO's descending order by UpdateTime is deterministically
validated; apply the same change to the other fixture occurrences around the
second block (the entries referenced at lines ~128-131).
In `@internal/service/agent.go`:
- Around line 143-152: The tenant-check branch currently grants access if any
tenant ID equals canvas.UserID without verifying canvas.Permission; update the
logic in the function containing the call to
s.userTenantDAO.GetTenantIDsByUserID so it first checks canvas.Permission (the
UserCanvas permission field) and only proceeds to compare tenant IDs when
canvas.Permission indicates team-level sharing (e.g., "team"); if permission is
not team, skip the tenant loop and do not grant access even if tenant IDs match
canvas.UserID.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 3b789144-ff15-4f29-8cd8-2b7c88c800dd
📒 Files selected for processing (6)
internal/dao/user_canvas_version.gointernal/handler/agent.gointernal/handler/agent_test.gointernal/router/router.gointernal/service/agent.gointernal/service/agent_test.go
- Add Select(...) to ListByCanvasID to only return metadata fields - Add gin.Logger() middleware for request debugging - Register migrated agent endpoints in vite hybrid proxy config - Fix test compilation issues Co-Authored-By: Claude Opus 4.8 <[email protected]>
- Add Select(...) to ListByCanvasID to only return metadata fields - Add gin.Logger() middleware for request debugging - Register migrated agent endpoints in vite hybrid proxy config - Fix test compilation issues Co-Authored-By: Claude Opus 4.8 <[email protected]>
Before this fix, any tenant member whose tenant ID matched canvas.UserID could access the canvas, even if the canvas had permission="me" (private). Now the method: - Owner always has access (unchanged) - Non-owner: only team-level permission grants tenant access - Non-owner + private canvas → denied even if in same tenant Co-Authored-By: Claude Opus 4.8 <[email protected]>
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (4)
internal/service/agent_test.go (4)
242-244:⚠️ Potential issue | 🟡 Minor | ⚡ Quick winMove
t.Helper()to the first line of the test function.Same
t.Helper()placement issue.🔧 Proposed fix
func TestCheckCanvasAccess_NotFound(t *testing.T) { + t.Helper() testDB := setupServiceTestDB(t) - t.Helper()🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@internal/service/agent_test.go` around lines 242 - 244, Move t.Helper() to the top of the TestCheckCanvasAccess_NotFound test so it runs before any helper setup; specifically, in the TestCheckCanvasAccess_NotFound function call t.Helper() as the first statement (before calling setupServiceTestDB or any other setup) to ensure test helpers report the correct caller location.
110-112:⚠️ Potential issue | 🟡 Minor | ⚡ Quick winMove
t.Helper()to the first line of the test function.Same issue as in
TestListVersions_Success–t.Helper()should be the first statement to ensure correct stack traces on test failures.🔧 Proposed fix
func TestListVersions_Empty(t *testing.T) { + t.Helper() testDB := setupServiceTestDB(t) - t.Helper()🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@internal/service/agent_test.go` around lines 110 - 112, In TestListVersions_Empty move the call to t.Helper() to be the first statement in the TestListVersions_Empty function (just like in TestListVersions_Success) so the helper is registered before any test setup; locate the TestListVersions_Empty test function and place t.Helper() at the top of that function body.
143-145:⚠️ Potential issue | 🟡 Minor | ⚡ Quick winMove
t.Helper()to the first line of the test function.Same
t.Helper()placement issue.🔧 Proposed fix
func TestCheckCanvasAccess_Owner(t *testing.T) { + t.Helper() testDB := setupServiceTestDB(t) - t.Helper()🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@internal/service/agent_test.go` around lines 143 - 145, In TestCheckCanvasAccess_Owner move the call to t.Helper() to the very first statement inside the test function (before setupServiceTestDB(t) and any other logic) so that helper reporting attributes the correct caller; update the TestCheckCanvasAccess_Owner function accordingly to call t.Helper() as the first line.
29-31:⚠️ Potential issue | 🟡 Minor | ⚡ Quick winMove
t.Helper()to the first line of the test function.
t.Helper()is currently called aftersetupServiceTestDB(t), which defeats its purpose. Whent.Helper()is not the first statement, test failure stack traces will incorrectly point to intermediate helper calls instead of the actual test invocation site.🔧 Proposed fix
func TestListVersions_Success(t *testing.T) { + t.Helper() testDB := setupServiceTestDB(t) - t.Helper()🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@internal/service/agent_test.go` around lines 29 - 31, Move the t.Helper() call to the top of the TestListVersions_Success function so it runs before any helper invocation; specifically, in TestListVersions_Success place t.Helper() as the first statement, then call setupServiceTestDB(t), ensuring t.Helper() executes before setupServiceTestDB to produce correct failure stack traces.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@internal/service/agent_test.go`:
- Around line 206-210: In TestCheckCanvasAccess_PrivateCanvas_Denied move the
call to t.Helper() to be the very first statement inside the test function
(immediately after func declaration) so that helper reporting is accurate;
locate the TestCheckCanvasAccess_PrivateCanvas_Denied function and reposition
t.Helper() before any setup such as setupServiceTestDB(t) or other
initialization.
---
Outside diff comments:
In `@internal/service/agent_test.go`:
- Around line 242-244: Move t.Helper() to the top of the
TestCheckCanvasAccess_NotFound test so it runs before any helper setup;
specifically, in the TestCheckCanvasAccess_NotFound function call t.Helper() as
the first statement (before calling setupServiceTestDB or any other setup) to
ensure test helpers report the correct caller location.
- Around line 110-112: In TestListVersions_Empty move the call to t.Helper() to
be the first statement in the TestListVersions_Empty function (just like in
TestListVersions_Success) so the helper is registered before any test setup;
locate the TestListVersions_Empty test function and place t.Helper() at the top
of that function body.
- Around line 143-145: In TestCheckCanvasAccess_Owner move the call to
t.Helper() to the very first statement inside the test function (before
setupServiceTestDB(t) and any other logic) so that helper reporting attributes
the correct caller; update the TestCheckCanvasAccess_Owner function accordingly
to call t.Helper() as the first line.
- Around line 29-31: Move the t.Helper() call to the top of the
TestListVersions_Success function so it runs before any helper invocation;
specifically, in TestListVersions_Success place t.Helper() as the first
statement, then call setupServiceTestDB(t), ensuring t.Helper() executes before
setupServiceTestDB to produce correct failure stack traces.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 32da5f3f-1665-4eff-84e9-0b29b50ad4e7
📒 Files selected for processing (5)
internal/handler/agent_test.gointernal/handler/providers_test.gointernal/service/agent.gointernal/service/agent_test.gointernal/service/model_service_test.go
💤 Files with no reviewable changes (2)
- internal/handler/providers_test.go
- internal/service/model_service_test.go
🚧 Files skipped from review as they are similar to previous changes (2)
- internal/handler/agent_test.go
- internal/service/agent.go
| // TestCheckCanvasAccess_PrivateCanvas_Denied verifies that a tenant member | ||
| // cannot access a private (default "me") canvas. | ||
| func TestCheckCanvasAccess_PrivateCanvas_Denied(t *testing.T) { | ||
| testDB := setupServiceTestDB(t) | ||
| t.Helper() |
There was a problem hiding this comment.
Move t.Helper() to the first line of the test function.
Same t.Helper() placement issue.
🔧 Proposed fix
func TestCheckCanvasAccess_PrivateCanvas_Denied(t *testing.T) {
+ t.Helper()
testDB := setupServiceTestDB(t)
- t.Helper()📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| // TestCheckCanvasAccess_PrivateCanvas_Denied verifies that a tenant member | |
| // cannot access a private (default "me") canvas. | |
| func TestCheckCanvasAccess_PrivateCanvas_Denied(t *testing.T) { | |
| testDB := setupServiceTestDB(t) | |
| t.Helper() | |
| // TestCheckCanvasAccess_PrivateCanvas_Denied verifies that a tenant member | |
| // cannot access a private (default "me") canvas. | |
| func TestCheckCanvasAccess_PrivateCanvas_Denied(t *testing.T) { | |
| t.Helper() | |
| testDB := setupServiceTestDB(t) |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@internal/service/agent_test.go` around lines 206 - 210, In
TestCheckCanvasAccess_PrivateCanvas_Denied move the call to t.Helper() to be the
very first statement inside the test function (immediately after func
declaration) so that helper reporting is accurate; locate the
TestCheckCanvasAccess_PrivateCanvas_Denied function and reposition t.Helper()
before any setup such as setupServiceTestDB(t) or other initialization.
43903b9 to
adfec07
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@internal/handler/agent.go`:
- Around line 200-217: The handler conflates backend/service errors with
authorization/not-found responses: update the logic around
h.agentService.CheckCanvasAccess(user.ID, agentID) and
h.agentService.GetVersion(agentID, versionID) to first check for non-nil err and
respond with common.CodeServerError (with a server-error message), and only if
err==nil map the boolean result (ok==false) to common.CodeOperatingError and a
clear "no permission/agent not found" message; similarly, after calling
GetVersion, if err!=nil return common.CodeServerError, and only if err==nil but
version is nil or missing return common.CodeNotFound — use the existing symbols
CheckCanvasAccess and GetVersion and preserve the current JSON shape.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 42fc8583-9b5d-4a6e-b330-e1f30fc48101
📒 Files selected for processing (5)
internal/handler/agent.gointernal/handler/agent_test.gointernal/router/router.gointernal/service/agent_test.goweb/vite.config.ts
🚧 Files skipped from review as they are similar to previous changes (2)
- internal/router/router.go
- web/vite.config.ts
| ok, err := h.agentService.CheckCanvasAccess(user.ID, agentID) | ||
| if err != nil || !ok { | ||
| c.JSON(http.StatusOK, gin.H{ | ||
| "code": common.CodeOperatingError, | ||
| "data": nil, | ||
| "message": "Agent not found or no permission.", | ||
| }) | ||
| return | ||
| } | ||
|
|
||
| version, err := h.agentService.GetVersion(agentID, versionID) | ||
| if err != nil { | ||
| c.JSON(http.StatusOK, gin.H{ | ||
| "code": common.CodeNotFound, | ||
| "data": nil, | ||
| "message": "Version not found.", | ||
| }) | ||
| return |
There was a problem hiding this comment.
Separate access/version misses from backend failures.
CheckCanvasAccess and GetVersion both propagate DAO errors, but this handler collapses every err != nil case into "Agent not found or no permission." or "Version not found.". A transient DB failure will therefore look like an authorization/not-found response to clients. Handle service errors first with CodeServerError, and only map the explicit deny/missing-version cases to CodeOperatingError / CodeNotFound.
Suggested direction
ok, err := h.agentService.CheckCanvasAccess(user.ID, agentID)
- if err != nil || !ok {
+ if err != nil {
+ c.JSON(http.StatusOK, gin.H{
+ "code": common.CodeServerError,
+ "data": nil,
+ "message": err.Error(),
+ })
+ return
+ }
+ if !ok {
c.JSON(http.StatusOK, gin.H{
"code": common.CodeOperatingError,
"data": nil,
"message": "Agent not found or no permission.",
})
return
}
version, err := h.agentService.GetVersion(agentID, versionID)
if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "code": common.CodeNotFound,
- "data": nil,
- "message": "Version not found.",
- })
+ // Prefer a sentinel from the service layer, e.g. service.ErrVersionNotFound.
+ c.JSON(http.StatusOK, gin.H{
+ "code": common.CodeServerError,
+ "data": nil,
+ "message": err.Error(),
+ })
return
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| ok, err := h.agentService.CheckCanvasAccess(user.ID, agentID) | |
| if err != nil || !ok { | |
| c.JSON(http.StatusOK, gin.H{ | |
| "code": common.CodeOperatingError, | |
| "data": nil, | |
| "message": "Agent not found or no permission.", | |
| }) | |
| return | |
| } | |
| version, err := h.agentService.GetVersion(agentID, versionID) | |
| if err != nil { | |
| c.JSON(http.StatusOK, gin.H{ | |
| "code": common.CodeNotFound, | |
| "data": nil, | |
| "message": "Version not found.", | |
| }) | |
| return | |
| ok, err := h.agentService.CheckCanvasAccess(user.ID, agentID) | |
| if err != nil { | |
| c.JSON(http.StatusOK, gin.H{ | |
| "code": common.CodeServerError, | |
| "data": nil, | |
| "message": err.Error(), | |
| }) | |
| return | |
| } | |
| if !ok { | |
| c.JSON(http.StatusOK, gin.H{ | |
| "code": common.CodeOperatingError, | |
| "data": nil, | |
| "message": "Agent not found or no permission.", | |
| }) | |
| return | |
| } | |
| version, err := h.agentService.GetVersion(agentID, versionID) | |
| if err != nil { | |
| // Prefer a sentinel from the service layer, e.g. service.ErrVersionNotFound. | |
| c.JSON(http.StatusOK, gin.H{ | |
| "code": common.CodeServerError, | |
| "data": nil, | |
| "message": err.Error(), | |
| }) | |
| return | |
| } |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@internal/handler/agent.go` around lines 200 - 217, The handler conflates
backend/service errors with authorization/not-found responses: update the logic
around h.agentService.CheckCanvasAccess(user.ID, agentID) and
h.agentService.GetVersion(agentID, versionID) to first check for non-nil err and
respond with common.CodeServerError (with a server-error message), and only if
err==nil map the boolean result (ok==false) to common.CodeOperatingError and a
clear "no permission/agent not found" message; similarly, after calling
GetVersion, if err!=nil return common.CodeServerError, and only if err==nil but
version is nil or missing return common.CodeNotFound — use the existing symbols
CheckCanvasAccess and GetVersion and preserve the current JSON shape.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #15629 +/- ##
=======================================
Coverage 93.16% 93.16%
=======================================
Files 10 10
Lines 717 717
Branches 118 118
=======================================
Hits 668 668
Misses 29 29
Partials 20 20 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…PI (#15640) ## Summary Implement the `GET /api/v1/agents/<agent_id>/versions/<version_id>` endpoint in Go, returning full version details including DSL. Depends on #15629 which introduced the version list endpoint and `UserCanvasVersionDAO` infrastructure. ### Changes - **Modified**: `internal/handler/agent.go` — Added `GetAgentVersion` handler with auth check and ownership verification - **Modified**: `internal/router/router.go` — Registered `GET /:agent_id/versions/:version_id` route - **New/Modified tests**: Service and handler tests for the version detail endpoint ### Testing ``` === RUN TestGetVersion_Success --- PASS === RUN TestGetVersion_WrongCanvas --- PASS === RUN TestGetVersion_NotFound --- PASS === RUN TestGetAgentVersionHandler_Success --- PASS === RUN TestGetAgentVersionHandler_VersionNotFound --- PASS ``` Co-Authored-By: Claude Opus 4.8 <[email protected]> --------- Co-authored-by: Claude Opus 4.8 <[email protected]>
Summary
Implement the
GET /api/v1/agents/<agent_id>/versionsendpoint in Go, listing all version snapshots for an agent canvas in descending update time order.Changes
internal/dao/user_canvas_version.go—UserCanvasVersionDAOwithListByCanvasID(ordered by update_time DESC) andGetByIDinternal/service/agent.go— AddedCheckCanvasAccess,ListVersions,GetVersionmethodsinternal/handler/agent.go— AddedListAgentVersionshandler with auth checkinternal/router/router.go— RegisteredGET /:agent_id/versionsrouteinternal/service/agent_test.go— 5 service-level tests (SQLite in-memory DB, zero mock)internal/handler/agent_test.go— 3 handler-level tests (real DB, pre-authenticated context)Testing
All 8 tests pass with zero mocking (in-memory SQLite replaces MySQL):
Co-Authored-By: Claude Opus 4.8 [email protected]