diff --git a/server/src/__tests__/agent-live-run-routes.test.ts b/server/src/__tests__/agent-live-run-routes.test.ts index f902d888c03..493f68a984f 100644 --- a/server/src/__tests__/agent-live-run-routes.test.ts +++ b/server/src/__tests__/agent-live-run-routes.test.ts @@ -653,6 +653,48 @@ describe("agent live run routes", () => { }); }); + it.each([ + ["was never written", async () => { throw Object.assign(Error("missing"), { name: "NoSuchKey" }); }, 404], + ["cannot be read from storage", async () => { throw Error("storage offline"); }, 500], + ] as const)("answers an object run log that %s", async (_case, getObject, status) => { + const { createObjectRunLogStore } = await vi.importActual("../services/object-run-log-store.js"); + const store = createObjectRunLogStore({ + provider: { + id: "s3", + getObject, + putObject: async () => { throw Error("unused"); }, + putObjectConditional: async () => { throw Error("unused"); }, + headObject: async () => ({ exists: false }), + deleteObject: async () => undefined, + listObjects: async () => ({ keys: [] }), + }, + keyPrefix: "logs", + assertWriterAuthority: async () => undefined, + }); + const handle = { store: "s3_segments" as const, logRef: "company-1/agent-1/aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa" }; + mockHeartbeatService.getRunLogAccess.mockResolvedValue({ + id: "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa", companyId: "company-1", scopeKind: "company", issueId: null, + logStore: handle.store, logRef: handle.logRef, + }); + mockHeartbeatService.readLog.mockImplementation(async () => store.read(handle)); + + const res = await requestApp(await createApp(), (baseUrl) => + request(baseUrl).get("/api/heartbeat-runs/aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa/log"), + ); + + expect(res.status, JSON.stringify(res.body)).toBe(status); + if (status === 404) expect(res.body).toEqual({ error: "Run log not found" }); + }); + + it("does not read an object run log for another company", async () => { + const res = await requestApp(await createApp({}, { + type: "board", userId: "board-user", companyIds: ["company-2"], source: "session", isInstanceAdmin: false, + }), (baseUrl) => request(baseUrl).get("/api/heartbeat-runs/aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa/log")); + + expect(res.status).toBe(404); + expect(mockHeartbeatService.readLog).not.toHaveBeenCalled(); + }); + it.each(["skill_test", "task_bridge"])( "denies %s keys from company-wide run and workspace logs", async (kind) => { diff --git a/server/src/services/object-run-log-store.test.ts b/server/src/services/object-run-log-store.test.ts index 2a637780194..04ccc8b2f50 100644 --- a/server/src/services/object-run-log-store.test.ts +++ b/server/src/services/object-run-log-store.test.ts @@ -321,7 +321,7 @@ describe("S3 run log store", () => { const m = memory(), store = createObjectRunLogStore({ provider: m.provider, keyPrefix: "logs", assertWriterAuthority: async () => undefined }); await expect(store.read({ store: "s3_segments", logRef: "company/agent/never-written", attemptId: "attempt" })) - .rejects.toThrow("Object run log not found"); + .rejects.toMatchObject({ status: 404, message: "Run log not found" }); }); it("batches concurrent appends, confirms durability, and resumes offsets/sequence on a replacement", async () => { const m = memory(), diff --git a/server/src/services/object-run-log-store.ts b/server/src/services/object-run-log-store.ts index 1065d61d6aa..154c996d320 100644 --- a/server/src/services/object-run-log-store.ts +++ b/server/src/services/object-run-log-store.ts @@ -4,6 +4,7 @@ import type { StorageProvider } from "../storage/types.js"; import type { RunLogStore, RunLogHandle, RunLogBeginInput } from "./run-log-store.js"; import { createObjectByteJournal, ObjectJournalSourcePrefixProofUnavailableError, type ObjectJournalIOPriority, type ObjectJournalWriter } from "./object-byte-journal.js"; import { MAX_RUN_LOG_RECORD_BYTES } from "./run-log-record-limits.js"; +import { notFound } from "../errors.js"; const MAX_SEGMENT_BYTES = 1024 * 1024; const MAX_PENDING_BYTES = MAX_RUN_LOG_RECORD_BYTES; @@ -357,7 +358,7 @@ export function createObjectRunLogStore(options: ObjectRunLogStoreOptions): RunL if (handle.store !== "s3_segments" && handle.store !== "s3_archive") throw new Error("Invalid object run log backend"); const store = journal(handle.logRef, opts.priority ?? "interactive"), snapshot = await store.head(opts.signal) ?? writers.get(handle)?.writer; - if (!snapshot || snapshot.head.deleted) throw new Error("Object run log not found"); + if (!snapshot || snapshot.head.deleted) throw notFound("Run log not found"); const offset = Math.max(0, opts.offset ?? 0), limit = opts.limitBytes ?? 256_000; if (!Number.isSafeInteger(limit) || limit < 1 || limit > MAX_READ_BYTES)