Skip to content

Commit d6d568b

Browse files
BrunoQuaresmaaslilac
authored andcommitted
fix(site): fix agent logs streaming for third party apps (#14541)
(cherry picked from commit 242b1ea)
1 parent fc1210a commit d6d568b

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

site/src/modules/resources/AgentLogs/useAgentLogs.test.tsx

+5-6
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe("useAgentLogs", () => {
2828
expect(wsSpy).not.toHaveBeenCalled();
2929
});
3030

31-
it("should return existing logs without network calls", async () => {
31+
it("should return existing logs without network calls if state is off", async () => {
3232
const queryClient = createTestQueryClient();
3333
queryClient.setQueryData(
3434
agentLogsKey(MockWorkspace.id, MockWorkspaceAgent.id),
@@ -39,7 +39,7 @@ describe("useAgentLogs", () => {
3939
const { result } = renderUseAgentLogs(queryClient, {
4040
workspaceId: MockWorkspace.id,
4141
agentId: MockWorkspaceAgent.id,
42-
agentLifeCycleState: "ready",
42+
agentLifeCycleState: "off",
4343
});
4444
await waitFor(() => {
4545
expect(result.current).toHaveLength(5);
@@ -48,12 +48,12 @@ describe("useAgentLogs", () => {
4848
expect(wsSpy).not.toHaveBeenCalled();
4949
});
5050

51-
it("should fetch logs when empty and should not connect to WebSocket when not starting", async () => {
51+
it("should fetch logs when empty", async () => {
5252
const queryClient = createTestQueryClient();
5353
const fetchSpy = jest
5454
.spyOn(API, "getWorkspaceAgentLogs")
5555
.mockResolvedValueOnce(generateLogs(5));
56-
const wsSpy = jest.spyOn(APIModule, "watchWorkspaceAgentLogs");
56+
jest.spyOn(APIModule, "watchWorkspaceAgentLogs");
5757
const { result } = renderUseAgentLogs(queryClient, {
5858
workspaceId: MockWorkspace.id,
5959
agentId: MockWorkspaceAgent.id,
@@ -63,10 +63,9 @@ describe("useAgentLogs", () => {
6363
expect(result.current).toHaveLength(5);
6464
});
6565
expect(fetchSpy).toHaveBeenCalledWith(MockWorkspaceAgent.id);
66-
expect(wsSpy).not.toHaveBeenCalled();
6766
});
6867

69-
it("should fetch logs and connect to websocket when agent is starting", async () => {
68+
it("should fetch logs and connect to websocket", async () => {
7069
const queryClient = createTestQueryClient();
7170
const logs = generateLogs(5);
7271
const fetchSpy = jest

site/src/modules/resources/AgentLogs/useAgentLogs.ts

+13-6
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,13 @@ export type UseAgentLogsOptions = Readonly<{
1717

1818
/**
1919
* Defines a custom hook that gives you all workspace agent logs for a given
20-
* workspace.
21-
*
22-
* Depending on the status of the workspace, all logs may or may not be
23-
* available.
20+
* workspace.Depending on the status of the workspace, all logs may or may not
21+
* be available.
2422
*/
2523
export function useAgentLogs(
2624
options: UseAgentLogsOptions,
2725
): readonly WorkspaceAgentLog[] | undefined {
2826
const { workspaceId, agentId, agentLifeCycleState, enabled = true } = options;
29-
3027
const queryClient = useQueryClient();
3128
const queryOptions = agentLogs(workspaceId, agentId);
3229
const { data: logs, isFetched } = useQuery({ ...queryOptions, enabled });
@@ -55,7 +52,17 @@ export function useAgentLogs(
5552
});
5653

5754
useEffect(() => {
58-
if (agentLifeCycleState !== "starting" || !isFetched) {
55+
// Stream data only for new logs. Old logs should be loaded beforehand
56+
// using a regular fetch to avoid overloading the websocket with all
57+
// logs at once.
58+
if (!isFetched) {
59+
return;
60+
}
61+
62+
// If the agent is off, we don't need to stream logs. This is the only state
63+
// where the Coder API can't receive logs for the agent from third-party
64+
// apps like envbuilder.
65+
if (agentLifeCycleState === "off") {
5966
return;
6067
}
6168

0 commit comments

Comments
 (0)