Compare commits

...

1 Commits

Author SHA1 Message Date
fullstack-engineer 36df1fe30e fix(both): surface actionable error_detail in canvas (#1420)
Block internal-flavored paths / Block forbidden paths (pull_request) Successful in 6s
CI / Detect changes (pull_request) Successful in 11s
E2E API Smoke Test / detect-changes (pull_request) Successful in 10s
E2E Chat / detect-changes (pull_request) Successful in 10s
Handlers Postgres Integration / detect-changes (pull_request) Successful in 10s
Harness Replays / detect-changes (pull_request) Successful in 10s
gate-check-v3 / gate-check (pull_request) Successful in 5s
Secret scan / Scan diff for credential-shaped strings (pull_request) Successful in 7s
qa-review / approved (pull_request) Successful in 7s
Runtime PR-Built Compatibility / detect-changes (pull_request) Successful in 14s
security-review / approved (pull_request) Successful in 6s
CI / Shellcheck (E2E scripts) (pull_request) Successful in 3s
E2E Chat / E2E Chat (pull_request) Failing after 3s
CI / Python Lint & Test (pull_request) Successful in 3s
sop-tier-check / tier-check (pull_request) Successful in 5s
Runtime PR-Built Compatibility / PR-built wheel + import smoke (pull_request) Successful in 2s
Harness Replays / Harness Replays (pull_request) Successful in 14s
lint-required-no-paths / lint-required-no-paths (pull_request) Successful in 39s
E2E API Smoke Test / E2E API Smoke Test (pull_request) Successful in 1m41s
Handlers Postgres Integration / Handlers Postgres Integration (pull_request) Successful in 2m58s
CI / Platform (Go) (pull_request) Successful in 7m17s
CI / Canvas (Next.js) (pull_request) Successful in 8m38s
CI / Canvas Deploy Reminder (pull_request) Has been skipped
CI / all-required (pull_request) Successful in 2s
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
sop-checklist / na-declarations (pull_request) N/A: (none)
Adds error_detail to the live ACTIVITY_LOGGED WebSocket broadcast so the
canvas can render an actionable error reason (e.g. oauth_org_not_allowed)
instead of the opaque "Agent error (Exception) — see workspace logs for
details." dead end.

**Server (Go)**
- logActivityExec now includes error_detail in the broadcast payload when
  set (omitted when nil, matching request_body/response_body pattern)
- New tests: broadcast includes error_detail / omits when nil

**Canvas (TypeScript)**
- useChatSocket: error_detail extracted from ACTIVITY_LOGGED payload,
  passed to onSendError (preference: error_detail > summary > generic)
- Old "Agent error (Exception) — see workspace logs for details." removed
- New 8 tests covering error_detail/summary/generic/empty/wrong-workspace

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-18 03:14:35 +00:00
4 changed files with 307 additions and 3 deletions
@@ -0,0 +1,204 @@
// @vitest-environment jsdom
/**
* Tests for actionable error rendering in useChatSocket (issue #1420).
*
* When a workspace agent returns an error on a canvas message/send, the canvas
* should surface the actionable error_detail (e.g. oauth_org_not_allowed)
* rather than the opaque "Agent error (Exception) — see workspace logs for details."
* fallback. Falls back to summary, then a generic hint.
*/
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { renderHook, act } from "@testing-library/react";
import React from "react";
import { useChatSocket, type UseChatSocketCallbacks } from "../hooks/useChatSocket";
import { emitSocketEvent, _resetSocketEventListenersForTests } from "@/store/socket-events";
import type { WSMessage } from "@/store/socket";
// Silence React StrictMode double-invoke noise.
const WARN = console.warn;
beforeEach(() => { console.warn = () => {}; });
afterEach(() => { console.warn = WARN; });
beforeEach(() => {
_resetSocketEventListenersForTests();
vi.useFakeTimers();
vi.setSystemTime(new Date("2026-05-18T10:00:00Z"));
});
afterEach(() => {
vi.useRealTimers();
_resetSocketEventListenersForTests();
});
const WORKSPACE_ID = "00000000-0000-0000-0000-000000000001";
function makeActivityErrorEvent(
workspaceId: string,
overrides: Partial<{
error_detail: string;
summary: string;
method: string;
status: string;
}> = {},
): WSMessage {
const {
error_detail = "",
summary = "",
method = "message/send",
status = "error",
} = overrides;
return {
event: "ACTIVITY_LOGGED",
workspace_id: workspaceId,
timestamp: "2026-05-18T10:00:00Z",
payload: {
activity_type: "a2a_receive",
method,
status,
target_id: workspaceId,
duration_ms: 500,
summary,
...(error_detail ? { error_detail } : {}),
} as Record<string, unknown>,
};
}
describe("useChatSocket actionable error rendering", () => {
it("calls onSendError with error_detail when present in the payload", () => {
const onSendError = vi.fn();
const callbacks: UseChatSocketCallbacks = { onSendError };
renderHook(() => useChatSocket(WORKSPACE_ID, callbacks));
act(() => {
emitSocketEvent(
makeActivityErrorEvent(WORKSPACE_ID, {
error_detail: "403 Forbidden: oauth_org_not_allowed — Your organization has disabled Claude subscription access. Use an API key instead.",
}),
);
});
expect(onSendError).toHaveBeenCalledTimes(1);
expect(onSendError.mock.calls[0][0]).toContain("oauth_org_not_allowed");
expect(onSendError.mock.calls[0][0]).not.toContain("workspace logs");
expect(onSendError.mock.calls[0][0]).not.toContain("Agent error (Exception)");
});
it("falls back to summary when error_detail is absent", () => {
const onSendError = vi.fn();
const callbacks: UseChatSocketCallbacks = { onSendError };
renderHook(() => useChatSocket(WORKSPACE_ID, callbacks));
act(() => {
emitSocketEvent(
makeActivityErrorEvent(WORKSPACE_ID, {
summary: "A2A request to ws-agent failed: connection refused",
}),
);
});
expect(onSendError).toHaveBeenCalledTimes(1);
expect(onSendError.mock.calls[0][0]).toBe("A2A request to ws-agent failed: connection refused");
});
it("falls back to generic hint when neither error_detail nor summary is present", () => {
const onSendError = vi.fn();
const callbacks: UseChatSocketCallbacks = { onSendError };
renderHook(() => useChatSocket(WORKSPACE_ID, callbacks));
act(() => {
emitSocketEvent(makeActivityErrorEvent(WORKSPACE_ID, {}));
});
expect(onSendError).toHaveBeenCalledTimes(1);
expect(onSendError.mock.calls[0][0]).toContain("Agent error");
// Should NOT be the old opaque phrase
expect(onSendError.mock.calls[0][0]).not.toContain("workspace logs");
expect(onSendError.mock.calls[0][0]).not.toContain("Agent error (Exception)");
});
it("does NOT call onSendError for other workspaces", () => {
const onSendError = vi.fn();
const callbacks: UseChatSocketCallbacks = { onSendError };
renderHook(() => useChatSocket(WORKSPACE_ID, callbacks));
act(() => {
emitSocketEvent(
makeActivityErrorEvent("00000000-0000-0000-0000-000000000099", {
error_detail: "some provider error",
}),
);
});
expect(onSendError).not.toHaveBeenCalled();
});
it("does NOT call onSendError for ok status", () => {
const onSendError = vi.fn();
const callbacks: UseChatSocketCallbacks = { onSendError };
renderHook(() => useChatSocket(WORKSPACE_ID, callbacks));
act(() => {
emitSocketEvent(
makeActivityErrorEvent(WORKSPACE_ID, {
status: "ok",
error_detail: "this should not appear",
}),
);
});
expect(onSendError).not.toHaveBeenCalled();
});
it("does NOT call onSendError when error_detail is an empty string", () => {
const onSendError = vi.fn();
const callbacks: UseChatSocketCallbacks = { onSendError };
renderHook(() => useChatSocket(WORKSPACE_ID, callbacks));
act(() => {
emitSocketEvent(
makeActivityErrorEvent(WORKSPACE_ID, {
error_detail: "",
summary: "",
}),
);
});
// Empty strings are falsy — falls through to the generic hint
expect(onSendError).toHaveBeenCalledTimes(1);
expect(onSendError.mock.calls[0][0]).toContain("Agent error");
expect(onSendError.mock.calls[0][0]).not.toContain("workspace logs");
});
it("prefers error_detail over summary (error_detail is more actionable)", () => {
const onSendError = vi.fn();
const callbacks: UseChatSocketCallbacks = { onSendError };
renderHook(() => useChatSocket(WORKSPACE_ID, callbacks));
act(() => {
emitSocketEvent(
makeActivityErrorEvent(WORKSPACE_ID, {
error_detail: "403: api_key_expired",
summary: "A2A request failed",
}),
);
});
expect(onSendError).toHaveBeenCalledTimes(1);
expect(onSendError.mock.calls[0][0]).toBe("403: api_key_expired");
});
it("does NOT call onSendError when onSendError is undefined (no-op guard)", () => {
const callbacks: UseChatSocketCallbacks = { onAgentMessage: vi.fn() };
expect(() =>
renderHook(() => useChatSocket(WORKSPACE_ID, callbacks)),
).not.toThrow();
act(() => {
emitSocketEvent(
makeActivityErrorEvent(WORKSPACE_ID, {
error_detail: "some error",
}),
);
});
// No error thrown even without onSendError
});
});
@@ -53,6 +53,7 @@ export function useChatSocket(
const targetId = (p.target_id as string) || "";
const durationMs = p.duration_ms as number | undefined;
const summary = (p.summary as string) || "";
const errorDetail = typeof p.error_detail === "string" ? p.error_detail : "";
let line = "";
if (type === "a2a_receive" && method === "message/send") {
@@ -67,9 +68,14 @@ export function useChatSocket(
const own = (targetId || msg.workspace_id) === workspaceId;
if (own) {
callbacksRef.current.onSendComplete?.();
callbacksRef.current.onSendError?.(
"Agent error (Exception) — see workspace logs for details.",
);
// Prefer the actionable error_detail from the workspace agent
// (e.g. "403 Forbidden: oauth_org_not_allowed ...") over the
// opaque generic. Fall back to a generic hint so the user
// always sees something actionable. Closes #1420.
const displayError = errorDetail
|| summary
|| "Agent error — please try again or check the agent's configuration.";
callbacksRef.current.onSendError?.(displayError);
}
}
} else if (type === "a2a_send") {
@@ -672,6 +672,13 @@ func logActivityExec(ctx context.Context, exec activityExecutor, broadcaster eve
if len(params.ToolTrace) > 0 {
payload["tool_trace"] = json.RawMessage(params.ToolTrace)
}
// Include error_detail in the live broadcast so the canvas can surface
// an actionable error reason (e.g. oauth_org_not_allowed) instead of the
// opaque "Agent error (Exception)" fallback. The runtime's
// report_activity helper caps this at 4096 chars.
if params.ErrorDetail != nil {
payload["error_detail"] = *params.ErrorDetail
}
// Include request/response bodies in the live broadcast so the
// canvas's Agent Comms panel can render the actual task text
// and reply text immediately, instead of falling back to the
@@ -934,6 +934,93 @@ func TestLogActivity_Broadcast_IncludesRequestAndResponseBodies(t *testing.T) {
}
}
// TestLogActivity_Broadcast_IncludesErrorDetail pins the fix for #1420:
// error_detail was stored in the DB but never included in the live
// ACTIVITY_LOGGED WebSocket broadcast, so the canvas could only show
// "Agent error (Exception) — see workspace logs for details." without
// surfacing the actionable error reason (e.g. oauth_org_not_allowed).
func TestLogActivity_Broadcast_IncludesErrorDetail(t *testing.T) {
mock := setupTestDB(t)
defer mock.ExpectationsWereMet()
mock.ExpectExec("INSERT INTO activity_logs").
WillReturnResult(sqlmock.NewResult(1, 1))
cb := &recordingBroadcaster{}
srcID := "ws-canvas"
tgtID := "ws-agent"
method := "message/send"
summary := "A2A request to ws-agent failed"
errorDetail := "403 Forbidden: oauth_org_not_allowed — Your organization has disabled Claude subscription access. Use an API key or ask your admin to enable access."
status := "error"
LogActivity(context.Background(), cb, ActivityParams{
WorkspaceID: srcID,
ActivityType: "a2a_receive",
SourceID: &srcID,
TargetID: &tgtID,
Method: &method,
Summary: &summary,
Status: status,
ErrorDetail: &errorDetail,
})
if len(cb.calls) != 1 {
t.Fatalf("expected 1 broadcast, got %d", len(cb.calls))
}
payload := cb.calls[0].payload
if payload["activity_type"] != "a2a_receive" {
t.Errorf("activity_type = %v, want a2a_receive", payload["activity_type"])
}
ed, ok := payload["error_detail"].(string)
if !ok {
t.Fatalf("error_detail missing from broadcast payload: got %#v", payload["error_detail"])
}
if ed != errorDetail {
t.Errorf("error_detail = %q, want %q", ed, errorDetail)
}
if payload["status"] != status {
t.Errorf("status = %v, want %q", payload["status"], status)
}
}
// TestLogActivity_Broadcast_OmitsNilErrorDetail verifies that when
// ErrorDetail is nil the broadcast does not include an empty error_detail key
// (matching the same omission pattern as request_body/response_body above).
func TestLogActivity_Broadcast_OmitsNilErrorDetail(t *testing.T) {
mock := setupTestDB(t)
defer mock.ExpectationsWereMet()
mock.ExpectExec("INSERT INTO activity_logs").
WillReturnResult(sqlmock.NewResult(1, 1))
cb := &recordingBroadcaster{}
srcID := "ws-canvas"
tgtID := "ws-agent"
method := "message/send"
summary := "A2A request succeeded"
status := "ok"
LogActivity(context.Background(), cb, ActivityParams{
WorkspaceID: srcID,
ActivityType: "a2a_receive",
SourceID: &srcID,
TargetID: &tgtID,
Method: &method,
Summary: &summary,
Status: status,
// ErrorDetail intentionally omitted (nil)
})
if len(cb.calls) != 1 {
t.Fatalf("expected 1 broadcast, got %d", len(cb.calls))
}
payload := cb.calls[0].payload
if _, present := payload["error_detail"]; present {
t.Errorf("error_detail should be omitted when nil, got %v", payload["error_detail"])
}
}
// TestLogActivityTx_DefersBroadcastUntilCommitHook pins the #149
// contract: LogActivityTx returns a commitHook that the caller MUST
// invoke after tx.Commit(); the broadcast MUST NOT fire from inside