From ee68098584724ac2be440229136da396ec5ac9ee Mon Sep 17 00:00:00 2001 From: Dotta <34892728+cryppadotta@users.noreply.github.com> Date: Wed, 18 Nov 2026 15:08:04 -0600 Subject: [PATCH] fix(runner): keep current task requests readable in OpenCode traces (#21584) ## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - Company traces let operators compare each request with its reply. > - OpenCode limits each text field to 8 KiB. > - A fresh session can put old history before the current request. > - The limit can remove the current request from the trace. > - This change selects the current task text first and links the retained submission. ## Linked Issues or Issue Description Refs #18098 and #18699. Neither merged change selects the current task request. **What existing behavior does this improve?** OpenCode company traces can contain only part of the old session history. **Current behavior** The exporter clips the full model envelope from its start. A sampled trace has a useful HOLD reply but a request that ends inside old history. A truncation flag is not proof that the task failed. **Proposed behavior** Select the task prompt before the text limit. Remove a valid leading generated handoff packet from the excerpt. Link the full retained run event through the existing API. [Source pattern](https://www.braintrust.dev/app/Paperclip/p/Paperclip%20Staging/patterns/e6d8da04-ca3e-4bd9-bd90-737f7717a9e1). ### Hypothesis and thesis for a fix The baseline exporter spends its text budget on the envelope and old history. The new regression confirms this cause: the received OTLP input loses a short current request after a long handoff. Select the current request in the OpenCode projection before redaction and clipping. Keep storage and access checks with their existing owners. ## What Changed - Select task identity and prompt for known native envelopes. Put pending user messages first in continuation excerpts. - Omit only a valid leading generated handoff packet. Keep plain text and unknown formats on the existing path. - Add a relative run-events reference under the prompt capture switch. Readers must sign in to the source instance and page to the matching turn. - Keep raw tool argument values excluded. Keep redaction, opt-in, capture switches, company access and size limits. - Add exporter, proxy, installed-runtime and private-run retrieval tests. Fix the proxy test's session-directory lookup so it skips the output-custody directory. - Document the excerpt and full-event retrieval path. ## Verification - Baseline `6b6c94a4ea8f3d1b58767d67a7a2fc4ceaecda9c`: the new long-handoff regression failed because OTLP input did not contain the current request. - Same regression passes after the fix. Exporter, redaction and bundled proxy suites: 39 tests passed. - `PAPERCLIP_OPENCODE_QUALIFY=1 pnpm --dir packages/paperclip-runner exec vitest run src/drivers/opencode/company-observability-runtime.test.ts`: 2 passed. Real installed OpenCode 1.18.34 and real HTTP OTLP receiver; local SSE model fixture. Both capture settings pass. The model receives the full original submission. - Private-run API suite: 31 tests passed on base `340157d1d6ec01ad0fa55970e148c6cd332bbcb1` with these changes. Authorized readers retrieve a complete request over 8 KiB through pagination. Other readers receive 404. - Runner TypeScript source and test checks and TypeScript build passed. `git diff --check` passed. - `pnpm verify --explain` returned exit 2 and required broader CI. No full local-suite pass is claimed. - Initial harness attempts used the wrong Vitest working directory, lacked built plugin-sdk, selected the wrong proxy fixture directory, or expected 403 instead of the API's 404. These are not red regression results. The corrected checks above pass. - Review follow-up `dfe995a77f3eb3280a6b081b4df9428fac85f540` removes added code comments required by CONTRIBUTING.md. Exporter + installed OpenCode runtime suites reran: 30 tests passed, with the local SSE model and real HTTP OTLP receiver. `git diff --check` passed. The review thread is resolved. Greptile passed on this exact revision with confidence 5/5 and no actionable findings. Final-head typecheck, build, Runner and E2E checks passed; server test shards and the amd64 Runner image build remain in progress. No hosted model call or deployed recovery is claimed. ## Risks Known envelope excerpts change shape from serialized envelope text to readable task text. The handoff selector depends on the current generated packet format; unknown framing falls back to the full prompt excerpt. Long current requests still clip. Full-event access depends on source-instance permission and retention. Export remains best effort. The new reference grants no access and includes no credential. This PR is unmerged and not deployed. ## Model Used OpenAI GPT-6 through Codex, with reasoning, tool use and code execution. The exact serving variant and context size are not exposed to this session. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge Final verification (2026-11-14): exact head `dfe995a77f3eb3280a6b081b4df9428fac85f540` has 60 successful checks, one neutral security scan, and five conditional skips (cache write, publication/attestation, incremental selection, Storybook paths). Applicable typecheck, build, general and serialized tests, Runner tests, E2E and both Runner image architectures passed. [CI](https://github.com/paperclipai/paperclip/actions/runs/43572758306), [Runner images](https://github.com/paperclipai/paperclip/actions/runs/43572758233), [Greptile](https://github.com/paperclipai/paperclip/runs/132380487679). Greptile 5/5, no unresolved actionable findings. Unmerged and not deployed. --------- Co-authored-by: Paperclip --- doc/agent-observability.md | 18 +++++ .../src/cli/opencode-proxy-input.test.ts | 10 ++- .../company-observability-runtime.test.ts | 81 +++++++++++++++++++ .../opencode/company-observability.test.ts | 64 +++++++++++++++ .../drivers/opencode/company-observability.ts | 36 ++++++++- .../heartbeat-run-privacy-routes.test.ts | 20 +++++ 6 files changed, 225 insertions(+), 4 deletions(-) create mode 100644 packages/paperclip-runner/src/drivers/opencode/company-observability-runtime.test.ts diff --git a/doc/agent-observability.md b/doc/agent-observability.md index b6eec6c3df6..8fd7d78036c 100644 --- a/doc/agent-observability.md +++ b/doc/agent-observability.md @@ -45,6 +45,24 @@ request. These are separate observations, not correlated calls. The `paperclip.tool.source` attribute distinguishes dispatch and provider records. Native tools retain their provider spans without submitted-argument diagnostics. OpenCode's performance-only OTLP sink stays separate. +For known native task envelopes, OpenCode selects the task identifier, title and +prompt before the 8 KiB limit. A valid leading fresh-session handoff packet is +omitted from this excerpt so earlier history cannot hide the current request. +Continuation excerpts put pending user messages before recovery guidance. +Plain text and unknown envelope formats retain their existing text projection. +History omission also sets the turn's truncation flag; the flag can also mean a +text or row limit was reached. + +When prompt capture is enabled, `paperclip.observability.input.events` gives a +relative URL on the originating Paperclip instance: +`/api/heartbeat-runs//events`. Sign in to that instance, read pages using +`afterSeq` and `limit`, and find the `turn.submitted` event for the trace's +`paperclip.turn.id`. Its `payload.prpEvent.payload.text` holds the retained +submission, including history, subject to existing redaction and retention. +The URL does not grant access. Run telemetry permission, company membership and +task privacy checks still apply. The reference is absent when prompt capture is +disabled. Tool argument values remain excluded from company traces. + The exporter sends once at turn completion with a three-second deadline, at most four requests in flight and no idle polling or persistent queue. Closing drains pending requests and marks an unfinished turn failed. Export failures emit a diff --git a/packages/paperclip-runner/src/cli/opencode-proxy-input.test.ts b/packages/paperclip-runner/src/cli/opencode-proxy-input.test.ts index da64ce04f17..6ecb7fcd5c1 100644 --- a/packages/paperclip-runner/src/cli/opencode-proxy-input.test.ts +++ b/packages/paperclip-runner/src/cli/opencode-proxy-input.test.ts @@ -102,7 +102,7 @@ it.each([true, false])("settles a real proxy interrupt with controller feedback env: { ...process.env, PAPERCLIP_OPENCODE_RUNTIME_DIR: runtime, OPENROUTER_API_KEY: "fixture-key", PAPERCLIP_RUN_ID: "stale-launch-run", PAPERCLIP_OBSERVABILITY_OPENCODE: "1", OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: `http://127.0.0.1:${address.port}/v1/traces`, - OTEL_RESOURCE_ATTRIBUTES: "paperclip.company.id=fixture-company" }, + OTEL_RESOURCE_ATTRIBUTES: "paperclip.company.id=fixture-company", OTEL_LOG_USER_PROMPTS: "1" }, stdio: fd === undefined ? ["pipe", "pipe", "pipe"] : ["pipe", "pipe", "pipe", fd], detached: process.platform !== "win32", }); @@ -125,7 +125,7 @@ it.each([true, false])("settles a real proxy interrupt with controller feedback await response(1); send({ id: 2, method: "thread/start", params: { cwd: root, model: "openrouter/deepseek/deepseek-v4-flash-0731", completionContract: { revision: "proxy-v1", criterionIds: ["objective"] }, conversationMode: "prepared" } }); await response(2); - send({ id: 3, method: "turn/start", params: { paperclipRunId: "current-run-1", input: [{ type: "text", text: JSON.stringify({ schema: "paperclip.native-model-envelope.v3", task: { prompt: "invalid-tool-feedback completion-feedback" }, completionContract: { revision: "proxy-v1", criteria: [{ id: "objective", requirement: "Complete the fixture." }] } }) }] } }); + send({ id: 3, method: "turn/start", params: { paperclipRunId: "current-run-1", input: [{ type: "text", text: JSON.stringify({ schema: "paperclip.native-model-envelope.v3", task: { prompt: `## Fresh session handoff\nGuidance.\n${JSON.stringify({ schema: "paperclip.native-session-handoff.v1", entries: [{ body: "old history ".repeat(2000) }] })}\n\ninvalid-tool-feedback completion-feedback current-request-canary` }, completionContract: { revision: "proxy-v1", criteria: [{ id: "objective", requirement: "Complete the fixture." }] } }) }] } }); await response(3); await expect.poll(() => messages.find(message => message.method === "item/tool/call"), { timeout: 5_000 }).toBeDefined(); const call = messages.find(message => message.method === "item/tool/call")!; @@ -147,7 +147,7 @@ it.each([true, false])("settles a real proxy interrupt with controller feedback } expect(await response(4)).toBe(true); if (respond) { - const sessions = (await readdir(runtime, { withFileTypes: true })).filter(entry => entry.isDirectory()); + const sessions = (await readdir(runtime, { withFileTypes: true })).filter(entry => entry.isDirectory() && entry.name !== "output-custody"); await expect.poll(async () => JSON.parse(await readFile(join(runtime, sessions[0]!.name, "data/fake-completion-feedback.json"), "utf8"))).toMatchObject([ { result: { content: [{ text: expect.stringContaining(feedback) }] } }, ]); @@ -164,6 +164,10 @@ it.each([true, false])("settles a real proxy interrupt with controller feedback await expect.poll(() => bodies.length, { timeout: 5_000 }).toBe(2); expect(bodies.map(body => body.resourceSpans[0]!.scopeSpans[0]!.spans[0]!.attributes .find(attribute => attribute.key === "paperclip.run.id")?.value.stringValue)).toEqual(["current-run-1", "current-run-2"]); + const attrs = Object.fromEntries(bodies[0]!.resourceSpans[0]!.scopeSpans[0]!.spans[0]!.attributes.map(attribute => [attribute.key, attribute.value.stringValue])); + expect(attrs["braintrust.input"]).toContain("current-request-canary"); + expect(attrs["braintrust.input"]).not.toContain("old history"); + expect(attrs["paperclip.observability.input.events"]).toBe("/api/heartbeat-runs/current-run-1/events"); child.stdin!.end(); } await expect.poll(() => exited, { timeout: 5_000 }).toBe(true); diff --git a/packages/paperclip-runner/src/drivers/opencode/company-observability-runtime.test.ts b/packages/paperclip-runner/src/drivers/opencode/company-observability-runtime.test.ts new file mode 100644 index 00000000000..3b311bc6e90 --- /dev/null +++ b/packages/paperclip-runner/src/drivers/opencode/company-observability-runtime.test.ts @@ -0,0 +1,81 @@ +import { createServer, type Server } from "node:http"; +import { mkdtemp, rm } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { expect, it } from "vitest"; +import { OpenCodeServerDriver } from "./opencode-server-driver.js"; + +const enabled = process.env.PAPERCLIP_OPENCODE_QUALIFY === "1"; +async function listen(server: Server) { + await new Promise(resolve => server.listen(0, "127.0.0.1", resolve)); + const address = server.address(); + if (!address || typeof address === "string") throw new Error("receiver did not bind"); + return `http://127.0.0.1:${address.port}`; +} +async function close(server: Server) { + server.closeAllConnections(); + await new Promise(resolve => server.close(() => resolve())); +} +it.skipIf(!enabled).each([true, false])("exports the current request through installed OpenCode with prompt capture %s", async capture => { + const root = await mkdtemp(join(tmpdir(), "opencode-observability-real-")); + const modelRequests: string[] = []; + const exports: any[] = []; + const model = createServer(async (request, response) => { + const chunks: Buffer[] = []; + for await (const chunk of request) chunks.push(Buffer.from(chunk)); + modelRequests.push(Buffer.concat(chunks).toString()); + response.writeHead(200, { "Content-Type": "text/event-stream" }); + for (const [delta, finish_reason] of [[{ role: "assistant", content: "observability-runtime-answer" }, null], [{}, "stop"]]) { + response.write(`data: ${JSON.stringify({ id: "chatcmpl-local", object: "chat.completion.chunk", created: 1, model: "qualification", choices: [{ index: 0, delta, finish_reason }] })}\n\n`); + } + response.end("data: [DONE]\n\n"); + }); + const receiver = createServer(async (request, response) => { + const chunks: Buffer[] = []; + for await (const chunk of request) chunks.push(Buffer.from(chunk)); + exports.push(JSON.parse(Buffer.concat(chunks).toString())); + response.end("{}"); + }); + let session: Awaited> | undefined; + try { + const modelUrl = await listen(model), receiverUrl = await listen(receiver); + const driver = new OpenCodeServerDriver({ + command: process.env.PAPERCLIP_TEST_OPENCODE_BINARY ?? "opencode", model: "paperclip/qualification", + runtimeDirectory: root, conversationMode: "prepared", permissionMode: "deny", + environment: { PATH: process.env.PATH, PAPERCLIP_AI_PROVIDER_URL: `${modelUrl}/v1`, PAPERCLIP_AI_PROVIDER_KEY: "local-fixture-only", + PAPERCLIP_OBSERVABILITY_OPENCODE: "1", OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: `${receiverUrl}/v1/traces`, + OTEL_LOG_USER_PROMPTS: capture ? "1" : "0", OTEL_LOG_ASSISTANT_RESPONSES: "1", + OTEL_RESOURCE_ATTRIBUTES: "paperclip.company.id=disposable-company" }, + }); + session = await driver.openSession({ runId: "disposable-run", normalizedSessionId: "disposable-session", workingDirectory: root }); + const history = ["## Fresh session handoff", "History guidance.", JSON.stringify({ schema: "paperclip.native-session-handoff.v1", entries: [{ body: "old history ".repeat(1800) }] })].join("\n"); + const text = JSON.stringify({ schema: "paperclip.native-model-envelope.v3", task: { identifier: "TEST-1", title: "Current task", prompt: `${history}\n\ncurrent-runtime-request-canary` } }); + await session.startTurn({ message: { role: "user", text } }); + const events = []; + for await (const event of session.events()) { + events.push(event); + if (["turn.completed", "turn.failed", "turn.interrupted", "turn.cancelled"].includes(event.eventType)) break; + } + expect(events.at(-1)?.eventType).toBe("turn.completed"); + await session.close({ reason: "test" }); + session = undefined; + expect(modelRequests.some(body => body.includes("current-runtime-request-canary") && body.includes("old history"))).toBe(true); + expect(events.find(event => event.eventType === "turn.submitted")?.payload.text).toBe(text); + expect(exports).toHaveLength(1); + const attrs = Object.fromEntries(exports[0].resourceSpans[0].scopeSpans[0].spans[0].attributes.map((a: any) => [a.key, a.value.stringValue ?? a.value.boolValue])); + expect(attrs["braintrust.output"]).toBe("observability-runtime-answer"); + if (capture) { + expect(attrs["braintrust.input"]).toContain("current-runtime-request-canary"); + expect(attrs["braintrust.input"]).not.toContain("old history"); + expect(attrs["paperclip.observability.input.events"]).toBe("/api/heartbeat-runs/disposable-run/events"); + } else { + expect(attrs).not.toHaveProperty("braintrust.input"); + expect(attrs).not.toHaveProperty("paperclip.observability.input.events"); + expect(JSON.stringify(exports)).not.toContain("current-runtime-request-canary"); + } + } finally { + await session?.close({ reason: "test" }); + await Promise.all([close(model), close(receiver)]); + await rm(root, { recursive: true, force: true }); + } +}, 60_000); diff --git a/packages/paperclip-runner/src/drivers/opencode/company-observability.test.ts b/packages/paperclip-runner/src/drivers/opencode/company-observability.test.ts index 54120e67f2e..17fc3e2fe53 100644 --- a/packages/paperclip-runner/src/drivers/opencode/company-observability.test.ts +++ b/packages/paperclip-runner/src/drivers/opencode/company-observability.test.ts @@ -166,3 +166,67 @@ it("retains provider failures for declared tools even before any dispatch", asyn expect(spans[1].status.code).toBe(2); expect(spanAttributes(spans[1])).toMatchObject({ "paperclip.tool.source": "provider", "braintrust.output": "request exceeded limit" }); }); + +it("keeps the current request readable after a long generated handoff and links the full submitted event", async () => { + const { observer, bodies } = setup(); + const handoff = ["## Fresh session handoff", "Generated history guidance.", JSON.stringify({ + schema: "paperclip.native-session-handoff.v1", entries: [{ body: "old context ".repeat(1800) }], + })].join("\n"); + observer.observe(event("turn.submitted", { text: JSON.stringify({ + schema: "paperclip.native-model-envelope.v3", requestedSkills: ["skill"], + task: { identifier: "TEST-1", title: "Review current request", prompt: `${handoff}\n\nCheck the current request: provider-secret secret-canary 😀` }, + completionContract: { private: "excluded-contract" }, + }) })); + observer.observe(event("turn.completed")); + await observer.close(); + const attrs = spanAttributes(bodies[0].resourceSpans[0].scopeSpans[0].spans[0]); + expect(attrs["braintrust.input"]).toContain("Check the current request: [REDACTED] [REDACTED] 😀"); + expect(attrs["braintrust.input"]).toContain("TEST-1"); + expect(attrs["braintrust.input"]).not.toContain("old context"); + expect(attrs["braintrust.input"]).not.toContain("excluded-contract"); + expect(attrs["paperclip.observability.input.events"]).toBe("/api/heartbeat-runs/run-one/events"); + expect(attrs["paperclip.observability.truncated"]).toBe(true); + expect(Buffer.byteLength(String(attrs["braintrust.input"]))).toBeLessThanOrEqual(8192); +}); + +it.each(["paperclip.native-model-envelope.v1", "paperclip.native-model-envelope.v2", "paperclip.native-model-envelope.v3"])("selects the prompt before other context for %s", async schema => { + const { observer, bodies } = setup(); + observer.observe(event("turn.submitted", { text: JSON.stringify({ schema, instructions: "earlier context".repeat(2000), task: { prompt: "current task 😀" } }) })); + observer.observe(event("turn.completed")); + await observer.close(); + expect(spanAttributes(bodies[0].resourceSpans[0].scopeSpans[0].spans[0])["braintrust.input"]).toBe("current task 😀"); +}); +it.each(["plain request", '{"schema":"future","task":{"prompt":"new"}}', '{"schema":', '{"schema":"paperclip.native-model-envelope.v3","task":{}}'])("preserves an unrecognized request: %s", async text => { + const { observer, bodies } = setup(); + observer.observe(event("turn.submitted", { text })); + observer.observe(event("turn.completed")); + await observer.close(); + expect(spanAttributes(bodies[0].resourceSpans[0].scopeSpans[0].spans[0])["braintrust.input"]).toBe(text); +}); +it("retains malformed handoff framing and bounds a long current prompt without corrupting UTF-8", async () => { + for (const prompt of ["## Fresh session handoff\ninvalid packet\n\nKeep this request", "😀".repeat(5000)]) { + const { observer, bodies } = setup(); + observer.observe(event("turn.submitted", { text: JSON.stringify({ schema: "paperclip.native-model-envelope.v3", task: { prompt } }) })); + observer.observe(event("turn.completed")); + await observer.close(); + const attrs = spanAttributes(bodies[0].resourceSpans[0].scopeSpans[0].spans[0]); + expect(prompt.startsWith(String(attrs["braintrust.input"]))).toBe(true); + expect(Buffer.byteLength(String(attrs["braintrust.input"]))).toBeLessThanOrEqual(8192); + expect(attrs["braintrust.input"]).not.toContain("\uFFFD"); + } +}); +it("shows steering before recovery guidance and omits request references when prompt capture is disabled", async () => { + for (const enabled of [true, false]) { + const { observer, bodies } = setup({ ...environment, OTEL_LOG_USER_PROMPTS: enabled ? "1" : "0" }); + observer.observe(event("turn.submitted", { text: JSON.stringify({ schema: "paperclip.native-continuation.v1", events: "guidance ".repeat(2000), steeredMessages: ["latest user request"] }) })); + observer.observe(event("turn.completed")); + await observer.close(); + const attrs = spanAttributes(bodies[0].resourceSpans[0].scopeSpans[0].spans[0]); + if (enabled) expect(String(attrs["braintrust.input"])).toMatch(/^latest user request/); + else { + expect(attrs).not.toHaveProperty("braintrust.input"); + expect(attrs).not.toHaveProperty("paperclip.observability.input.events"); + expect(JSON.stringify(bodies)).not.toContain("latest user request"); + } + } +}); diff --git a/packages/paperclip-runner/src/drivers/opencode/company-observability.ts b/packages/paperclip-runner/src/drivers/opencode/company-observability.ts index 479dc2cdcd1..24ecc757678 100644 --- a/packages/paperclip-runner/src/drivers/opencode/company-observability.ts +++ b/packages/paperclip-runner/src/drivers/opencode/company-observability.ts @@ -40,6 +40,35 @@ function outputText(value: unknown): string { try { return JSON.stringify(value) ?? ""; } catch { return ""; } } +function requestText(value: unknown): { text: string; omittedHistory: boolean } { + const fallback = { text: typeof value === "string" ? value : "", omittedHistory: false }; + let envelope: Record; + try { envelope = record(JSON.parse(fallback.text)); } catch { return fallback; } + if (envelope.schema === "paperclip.native-continuation.v1" && typeof envelope.events === "string") { + const steered = Array.isArray(envelope.steeredMessages) ? envelope.steeredMessages.filter((text): text is string => typeof text === "string") : []; + return { text: [...steered, envelope.events].join("\n\n"), omittedHistory: false }; + } + if (!["paperclip.native-model-envelope.v1", "paperclip.native-model-envelope.v2", "paperclip.native-model-envelope.v3"].includes(String(envelope.schema))) return fallback; + const task = record(envelope.task); + if (typeof task.prompt !== "string") return fallback; + let prompt = task.prompt, omittedHistory = false; + if (prompt.startsWith("## Fresh session handoff\n")) { + const boundary = prompt.indexOf("\n\n"); + if (boundary !== -1) { + const history = prompt.slice(0, boundary); + try { + const packet = record(JSON.parse(history.slice(history.lastIndexOf("\n") + 1))); + if (packet.schema === "paperclip.native-session-handoff.v1" && Array.isArray(packet.entries)) { + prompt = prompt.slice(boundary + 2); + omittedHistory = true; + } + } catch {} + } + } + const identity = [task.identifier, task.title].filter((text): text is string => typeof text === "string").join(" — "); + return { text: [identity, prompt].filter(Boolean).join("\n\n"), omittedHistory }; +} + /** A controller opt-in, not OpenCode's raw OTLP. Projected PRP events never include reasoning or raw provider payloads. */ export function createOpenCodeCompanyObservability(input: { environment: NodeJS.ProcessEnv | undefined; model: string; sensitiveValues: readonly string[]; @@ -135,7 +164,12 @@ export function createOpenCodeCompanyObservability(input: { current = { runId: event.runId, turnId: event.turnId, traceId: randomBytes(16).toString("hex"), root: makeSpan("paperclip.opencode.turn", time, { "gen_ai.operation.name": "invoke_agent", "gen_ai.request.model": input.model }), tools: new Map(), usage: new Map(), truncated: false }; - if (capture("OTEL_LOG_USER_PROMPTS")) current.root.attributes["braintrust.input"] = clean(record(event.payload).text); + if (capture("OTEL_LOG_USER_PROMPTS")) { + const request = requestText(record(event.payload).text); + current.root.attributes["braintrust.input"] = clean(request.text); + current.truncated ||= request.omittedHistory; + current.root.attributes["paperclip.observability.input.events"] = `/api/heartbeat-runs/${encodeURIComponent(event.runId)}/events`; + } } const turn = current; if (!turn || event.runId !== turn.runId || event.turnId !== turn.turnId) return; diff --git a/server/src/__tests__/heartbeat-run-privacy-routes.test.ts b/server/src/__tests__/heartbeat-run-privacy-routes.test.ts index cf32abde6fe..acab7948986 100644 --- a/server/src/__tests__/heartbeat-run-privacy-routes.test.ts +++ b/server/src/__tests__/heartbeat-run-privacy-routes.test.ts @@ -367,6 +367,26 @@ describeEmbeddedPostgres("heartbeat run privacy routes", { concurrent: false }, expect(recovered.body.map((run: { id: string }) => run.id)).toEqual([timeoutId]); }); + it("resolves a long submitted request through paginated run events only for authorized readers", async () => { + const fixture = await seedFixture(); + const text = JSON.stringify({ schema: "paperclip.native-model-envelope.v3", task: { prompt: `${"earlier context ".repeat(1500)}current request after 8 KiB` } }); + await db.insert(heartbeatRunEvents).values({ + companyId: fixture.companyId, runId: fixture.privateRunId, agentId: fixture.ownerAgentId, + seq: 2, eventType: "turn.submitted", stream: "system", + payload: { prpEvent: { turnId: "turn-reference", eventType: "turn.submitted", payload: { text } } }, + }); + const reference = `/api/heartbeat-runs/${fixture.privateRunId}/events`; + for (const agentId of [fixture.ownerAgentId, fixture.grantedAgentId]) { + const app = createApp(fixture.companyId, agentId); + const first = await request(app).get(`${reference}?limit=1`).expect(200); + expect(first.body).toHaveLength(1); + const next = await request(app).get(`${reference}?afterSeq=${first.body[0].seq}&limit=1`).expect(200); + expect(next.body[0].payload.prpEvent).toMatchObject({ turnId: "turn-reference", payload: { text } }); + } + await request(createApp(fixture.companyId, fixture.otherAgentId)).get(reference).expect(404); + await request(createApp(randomUUID(), fixture.otherAgentId)).get(reference).expect(404); + }); + it("returns full private-run content to the owner and an explicitly granted agent", async () => { const fixture = await seedFixture();