Compare commits

...

2 Commits

Author SHA1 Message Date
core-be 7b690318b6 fix(handlers): compile error in approvals.go + broken test mock in p1102
- approvals.go: err was already declared at line 37 (ctxJSON, err := json.Marshal).
  Reusing with = instead of := to fix "no new variables on left side of :=".
- approvals_test.go: TestApprovals_Create_NilContextFallsBackToEmptyJSON mock
  expected 6 args for an INSERT with 5 columns. Remove spurious
  sqlmock.AnyArg() that caused "expected 6, got 5 arguments" at runtime.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-15 05:51:20 +00:00
fullstack-engineer 147876f338 fix/approvals: log and guard json.Marshal error before DB insert
Block internal-flavored paths / Block forbidden paths (pull_request) Successful in 28s
Harness Replays / detect-changes (pull_request) Successful in 21s
E2E API Smoke Test / detect-changes (pull_request) Successful in 36s
CI / Detect changes (pull_request) Successful in 37s
Handlers Postgres Integration / detect-changes (pull_request) Successful in 34s
Secret scan / Scan diff for credential-shaped strings (pull_request) Successful in 17s
gate-check-v3 / gate-check (pull_request) Successful in 19s
security-review / approved (pull_request) Successful in 21s
qa-review / approved (pull_request) Successful in 22s
sop-tier-check / tier-check (pull_request) Successful in 31s
Runtime PR-Built Compatibility / detect-changes (pull_request) Successful in 40s
lint-required-no-paths / lint-required-no-paths (pull_request) Successful in 1m25s
Harness Replays / Harness Replays (pull_request) Successful in 6s
CI / Canvas (Next.js) (pull_request) Successful in 9s
CI / Shellcheck (E2E scripts) (pull_request) Successful in 10s
CI / Python Lint & Test (pull_request) Successful in 12s
Runtime PR-Built Compatibility / PR-built wheel + import smoke (pull_request) Successful in 10s
E2E API Smoke Test / E2E API Smoke Test (pull_request) Failing after 1m36s
CI / Platform (Go) (pull_request) Failing after 4m44s
Handlers Postgres Integration / Handlers Postgres Integration (pull_request) Failing after 5m7s
CI / Canvas Deploy Reminder (pull_request) Has been skipped
CI / all-required (pull_request) Successful in 11s
sop-checklist / all-items-acked (pull_request) [info tier:low] acked: 0/7 — missing: comprehensive-testing, local-postgres-e2e, staging-smoke, +4 — body-unfilled: comprehensive-testing, l
Bug: json.Marshal returns []byte{} (empty slice, NOT nil) on error,
so the old `if ctxJSON == nil` guard never fired. The error was
silently ignored and an empty/zero byte slice was passed to the DB.

Fix: check `err != nil` explicitly, log it, and fall back to "{}".
Also add a defensive `len(ctxJSON) == 0` guard as in-depth defense.

Add TestApprovals_Create_NilContextFallsBackToEmptyJSON to cover the
nil-context path (was entirely untested) and document the expected
SQL binding behavior.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-15 00:17:04 +00:00
2 changed files with 41 additions and 3 deletions
@@ -34,13 +34,19 @@ func (h *ApprovalsHandler) Create(c *gin.Context) {
return
}
ctxJSON, _ := json.Marshal(body.Context)
if ctxJSON == nil {
ctxJSON, err := json.Marshal(body.Context)
if err != nil {
log.Printf("Create approval: json.Marshal(context) error: %v", err)
ctxJSON = []byte("{}")
} else if len(ctxJSON) == 0 {
// json.Marshal returns []byte{} (empty slice, not nil) on error;
// guard against it defensively even though map[string]interface{}
// cannot fail in practice — defensive in depth.
ctxJSON = []byte("{}")
}
var approvalID string
err := db.DB.QueryRowContext(ctx, `
err = db.DB.QueryRowContext(ctx, `
INSERT INTO approval_requests (workspace_id, task_id, action, reason, context)
VALUES ($1, $2, $3, $4, $5::jsonb)
RETURNING id
@@ -328,3 +328,35 @@ func TestApprovals_Decide_MissingDecision(t *testing.T) {
t.Errorf("expected 400, got %d", w.Code)
}
}
func TestApprovals_Create_NilContextFallsBackToEmptyJSON(t *testing.T) {
mock := setupTestDB(t)
setupTestRedis(t)
broadcaster := newTestBroadcaster()
handler := NewApprovalsHandler(broadcaster)
mock.ExpectQuery("INSERT INTO approval_requests").
WithArgs("ws-1", "task-0", "approve", "none", sqlmock.AnyArg()).
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow("appr-nil"))
mock.ExpectExec("INSERT INTO structure_events").
WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectQuery("SELECT parent_id FROM workspaces WHERE id").
WithArgs("ws-1").
WillReturnRows(sqlmock.NewRows([]string{"parent_id"}).AddRow(nil))
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Params = gin.Params{{Key: "id", Value: "ws-1"}}
// context is nil (zero value of map[string]interface{})
body := `{"action":"approve","reason":"none","task_id":"task-0","context":null}`
c.Request = httptest.NewRequest("POST", "/", bytes.NewBufferString(body))
c.Request.Header.Set("Content-Type", "application/json")
handler.Create(c)
if w.Code != http.StatusCreated {
t.Errorf("expected 201, got %d: %s", w.Code, w.Body.String())
}
}