Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion server/src/__tests__/heartbeat-run-log.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, expect, it } from "vitest";
import { compactRunLogChunk } from "../services/heartbeat.js";
import type { Db } from "@paperclipai/db";
import { compactRunLogChunk, heartbeatService } from "../services/heartbeat.js";

describe("compactRunLogChunk", () => {
it("redacts inline base64 image data from structured log chunks", () => {
Expand Down Expand Up @@ -39,3 +40,24 @@ describe("compactRunLogChunk", () => {
expect(compacted).not.toContain("paperclip-flag-secret");
});
});

describe("heartbeat run log reads", () => {
it("returns a pending empty payload when an existing run has no initialized log yet", async () => {
const service = heartbeatService({} as Db);

await expect(
service.readLog({
id: "run-pending-log",
companyId: "company-1",
logStore: null,
logRef: null,
}),
).resolves.toEqual({
runId: "run-pending-log",
store: null,
logRef: null,
content: "",
pending: true,
});
});
});
10 changes: 9 additions & 1 deletion server/src/services/heartbeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10116,7 +10116,15 @@ export function heartbeatService(db: Db, options: HeartbeatServiceOptions = {})
const run = typeof runOrLookup === "string" ? await getRunLogAccess(runOrLookup) : runOrLookup;
const runId = typeof runOrLookup === "string" ? runOrLookup : runOrLookup.id;
if (!run) throw notFound("Heartbeat run not found");
if (!run.logStore || !run.logRef) throw notFound("Run log not found");
if (!run.logStore || !run.logRef) {
return {
runId,
store: run.logStore,
logRef: run.logRef,
content: "",
pending: true,
};
}

const result = await runLogStore.read(
{
Expand Down
2 changes: 1 addition & 1 deletion ui/src/api/heartbeats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export const heartbeatsApi = {
`/heartbeat-runs/${runId}/events?afterSeq=${encodeURIComponent(String(afterSeq))}&limit=${encodeURIComponent(String(limit))}`,
),
log: (runId: string, offset = 0, limitBytes = 256000) =>
api.get<{ runId: string; store: string; logRef: string; content: string; nextOffset?: number }>(
api.get<{ runId: string; store: string | null; logRef: string | null; content: string; nextOffset?: number; pending?: boolean }>(
`/heartbeat-runs/${runId}/log?offset=${encodeURIComponent(String(offset))}&limitBytes=${encodeURIComponent(String(limitBytes))}`,
),
workspaceOperations: (runId: string) =>
Expand Down
39 changes: 39 additions & 0 deletions ui/src/components/transcript/useLiveRunTranscripts.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ describe("useLiveRunTranscripts", () => {
});

afterEach(() => {
vi.useRealTimers();
globalThis.WebSocket = OriginalWebSocket;
});

Expand Down Expand Up @@ -229,6 +230,44 @@ describe("useLiveRunTranscripts", () => {
container.remove();
});

it("stops polling active runs after a persisted-log 404", async () => {
vi.useFakeTimers();
logMock.mockReset();
logMock.mockRejectedValue(new ApiError("Run log not found", 404, { error: "Run log not found" }));

function Harness() {
useLiveRunTranscripts({
companyId: "company-1",
runs: [{ id: "run-stale", status: "running", adapterType: "codex_local" }],
logPollIntervalMs: 10,
});
return null;
}

const container = document.createElement("div");
document.body.appendChild(container);
const root = createRoot(container);

await act(async () => {
root.render(<Harness />);
await Promise.resolve();
});

expect(logMock).toHaveBeenCalledTimes(1);

await act(async () => {
vi.advanceTimersByTime(30);
await Promise.resolve();
});

expect(logMock).toHaveBeenCalledTimes(1);

act(() => {
root.unmount();
});
container.remove();
});

it("can hydrate active runs without opening the live event socket", async () => {
function Harness() {
useLiveRunTranscripts({
Expand Down
12 changes: 6 additions & 6 deletions ui/src/components/transcript/useLiveRunTranscripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export function useLiveRunTranscripts({
const seenChunkKeysRef = useRef(new Set<string>());
const pendingLogRowsByRunRef = useRef(new Map<string, string>());
const logOffsetByRunRef = useRef(new Map<string, number>());
const missingTerminalLogRunIdsRef = useRef(new Set<string>());
const missingLogRunIdsRef = useRef(new Set<string>());
const transcriptCacheRef = useRef(new Map<string, {
adapterType: string;
chunks: RunLogChunk[];
Expand Down Expand Up @@ -196,9 +196,9 @@ export function useLiveRunTranscripts({
logOffsetByRunRef.current.delete(runId);
}
}
for (const runId of missingTerminalLogRunIdsRef.current.keys()) {
for (const runId of missingLogRunIdsRef.current.keys()) {
if (!knownRunIds.has(runId)) {
missingTerminalLogRunIdsRef.current.delete(runId);
missingLogRunIdsRef.current.delete(runId);
}
}
for (const runId of transcriptCacheRef.current.keys()) {
Expand All @@ -214,7 +214,7 @@ export function useLiveRunTranscripts({
let cancelled = false;

const readRunLog = async (run: RunTranscriptSource) => {
if (missingTerminalLogRunIdsRef.current.has(run.id)) {
if (missingLogRunIdsRef.current.has(run.id)) {
return;
}
const offset = logOffsetByRunRef.current.get(run.id) ?? resolveInitialLogOffset(run, logReadLimitBytes);
Expand All @@ -232,8 +232,8 @@ export function useLiveRunTranscripts({
logOffsetByRunRef.current.set(run.id, offset + result.content.length);
}
} catch (error) {
if (error instanceof ApiError && error.status === 404 && isTerminalStatus(run.status)) {
missingTerminalLogRunIdsRef.current.add(run.id);
if (error instanceof ApiError && error.status === 404) {
missingLogRunIdsRef.current.add(run.id);
}
} finally {
if (!cancelled) {
Expand Down
Loading