diff --git a/.github/workflows/coding-agent-attachments-tests.yml b/.github/workflows/coding-agent-attachments-tests.yml new file mode 100644 index 000000000..704fb9c2a --- /dev/null +++ b/.github/workflows/coding-agent-attachments-tests.yml @@ -0,0 +1,68 @@ +name: Coding-agent attachments +on: + pull_request: + paths: + - 'typescript/packages/fi-core/src/attachments/**' + - 'typescript/packages/traceai_agent_core/**' + - 'typescript/packages/traceai_pi_plugin/**' + - 'typescript/packages/traceai_oh_my_pi_plugin/**' + - 'typescript/packages/traceai_claude_code/**' + - 'typescript/packages/traceai_cursor_sdk/src/**' + - '.github/workflows/coding-agent-attachments-tests.yml' + workflow_dispatch: +permissions: + contents: read +jobs: + attachments: + name: Coding-agent attachments (Node ${{ matrix.node }}) + runs-on: ubuntu-24.04 + timeout-minutes: 25 + strategy: + fail-fast: false + matrix: + # 22.19.0 is the Pi plugin engines floor. + node: ['22.19.0', '24'] + defaults: + run: + working-directory: typescript + steps: + - uses: actions/checkout@v4 + with: + persist-credentials: false + - uses: actions/setup-node@v4 + with: + node-version: ${{ matrix.node }} + - uses: pnpm/action-setup@v4 + with: + version: 10.30.1 + - name: Frozen filtered install + run: >- + pnpm install --frozen-lockfile --ignore-scripts + --filter '@traceai/agent-core...' --filter '@traceai/pi-plugin...' + --filter '@traceai/oh-my-pi-plugin...' --filter '@traceai/claude-code...' + --filter '@traceai/cursor-sdk...' + - name: Build + run: | + for p in fi-semantic-conventions fi-core agent-core pi-coding-agent pi-plugin oh-my-pi-plugin claude-code cursor-sdk; do + pnpm --filter "@traceai/$p" run build + done + - name: Package suites + run: | + pnpm --filter @traceai/agent-core run test + pnpm --filter @traceai/pi-plugin run test + pnpm --filter @traceai/oh-my-pi-plugin run test + pnpm --filter @traceai/claude-code run test + pnpm --filter @traceai/cursor-sdk exec jest --runInBand + - name: Real HTTPS attachment wire with real Pi host (floor) and OMP plugin API + run: | + mkdir -p "$RUNNER_TEMP/att-tls" + openssl req -x509 -newkey rsa:2048 -nodes -keyout "$RUNNER_TEMP/att-tls/key.pem" -out "$RUNNER_TEMP/att-tls/cert.pem" \ + -days 2 -subj '/CN=127.0.0.1' -addext 'subjectAltName=IP:127.0.0.1,DNS:localhost' > /dev/null 2>&1 + export NODE_EXTRA_CA_CERTS="$RUNNER_TEMP/att-tls/cert.pem" ATTACHMENT_TEST_KEY="$RUNNER_TEMP/att-tls/key.pem" + node packages/traceai_pi_plugin/test/attachments-native.cjs + node packages/traceai_oh_my_pi_plugin/test/attachments-wire.cjs + - name: Real Pi host (latest) + run: | + npm install --prefix "$RUNNER_TEMP/pi-latest" --ignore-scripts --no-audit --no-fund @earendil-works/pi-coding-agent@latest + export NODE_EXTRA_CA_CERTS="$RUNNER_TEMP/att-tls/cert.pem" ATTACHMENT_TEST_KEY="$RUNNER_TEMP/att-tls/key.pem" + PI_TEST_BINARY="$RUNNER_TEMP/pi-latest/node_modules/.bin/pi" node packages/traceai_pi_plugin/test/attachments-native.cjs diff --git a/typescript/packages/traceai_agent_core/src/attachment-runtime.ts b/typescript/packages/traceai_agent_core/src/attachment-runtime.ts new file mode 100644 index 000000000..7a309763f --- /dev/null +++ b/typescript/packages/traceai_agent_core/src/attachment-runtime.ts @@ -0,0 +1,102 @@ +import { + ATTACHMENT_TRANSPORT_V1, + AttachmentTransportError, + attachmentNodeHttpClient, + createAttachmentHttpTransportV1, + type NodeAttachmentManagerOptions, +} from "@traceai/fi-core/attachments/node"; +import type { AttachmentTransport, AttachmentTransportV1 } from "@traceai/fi-core/attachments"; +import type { ResolvedConfig } from "./config.js"; +import { collectionEnabled } from "./collection.js"; +import { approvedAttachmentEndpoint } from "./attachment-status.js"; + +/** The versioned surface of a caller transport: V1 itself, or the V1 member a legacy-shaped HTTP transport carries. */ +function versionedTransport(transport: AttachmentTransport | AttachmentTransportV1): AttachmentTransportV1 | undefined { + const carried = (transport as { [ATTACHMENT_TRANSPORT_V1]?: AttachmentTransportV1 })[ATTACHMENT_TRANSPORT_V1]; + if (carried && typeof carried.putContent === "function") return carried; + return typeof (transport as AttachmentTransportV1).putContent === "function" ? transport as AttachmentTransportV1 : undefined; +} + +/** Every wire call re-checks the provider's collection policy and route revalidation first. */ +function guardV1(delegate: AttachmentTransportV1, check: () => Promise): AttachmentTransportV1 { + return { + async bootstrap(body, key, signal) { await check(); return delegate.bootstrap(body, key, signal); }, + async renewSession(current, sequence, signal) { await check(); return delegate.renewSession(current, sequence, signal); }, + async initialize(session, body, key, signal) { await check(); return delegate.initialize(session, body, key, signal); }, + async renewUpload(session, upload, body, signal) { await check(); return delegate.renewUpload(session, upload, body, signal); }, + async putContent(session, upload, source, signal, pacer) { await check(); return delegate.putContent(session, upload, source, signal, pacer); }, + async complete(session, upload, key, signal) { await check(); return delegate.complete(session, upload, key, signal); }, + async abort(session, upload, key, signal) { await check(); return delegate.abort(session, upload, key, signal); }, + async uploadStatus(session, upload, signal) { await check(); return delegate.uploadStatus(session, upload, signal); }, + async putManifest(session, capability, recordingId, body, key, signal) { + await check(); return delegate.putManifest(session, capability, recordingId, body, key, signal); + }, + async submitImport(session, body, key, signal) { await check(); return delegate.submitImport(session, body, key, signal); }, + async importStatus(session, jobId, signal) { await check(); return delegate.importStatus(session, jobId, signal); }, + }; +} + +/** + * A caller-supplied four-method transport stays four-method, so fi-core still marks it legacy and emits + * FI_ATTACHMENTS_LEGACY_TRANSPORT once (no renewal, recordings or imports). Pass an AttachmentTransportV1 instead. + */ +function guardLegacy(delegate: AttachmentTransport, check: () => Promise): AttachmentTransport { + return { + async bootstrap(request, signal) { await check(); return delegate.bootstrap(request, signal); }, + async initialize(session, request, signal) { await check(); return delegate.initialize(session, request, signal); }, + async upload(session, upload, data, signal) { await check(); return delegate.upload(session, upload, data, signal); }, + async complete(session, upload, request, signal) { await check(); return delegate.complete(session, upload, request, signal); }, + }; +} + +/** One provider-owned runtime with the same credential and revocation boundary as OTLP. */ +export function agentAttachmentRuntime( + config: ResolvedConfig, + options: NodeAttachmentManagerOptions = {}, +): NodeAttachmentManagerOptions { + // Neither the collector URL nor provider/tool content is a control-plane origin, and the + // credential goes only to the approved destination's origin (destination approval binds a + // saved credential to that origin). Any other endpoint stays unconfigured, with no headers. + const endpoint = approvedAttachmentEndpoint(config, options.endpoint ?? process.env.FI_ATTACHMENTS_ENDPOINT); + const headers: Readonly> = config.credentials && endpoint ? { + "x-api-key": config.credentials.apiKey, + "x-secret-key": config.credentials.secretKey, + } : {}; + const allowed = () => collectionEnabled(config) && config.captureContent && + config.destinationApproved && !!config.credentials; + const check = async () => { + if (!allowed() || (config.revalidate && !(await config.revalidate()))) + throw new AttachmentTransportError("policy_denied"); + }; + let delegate: AttachmentTransportV1 | undefined; + let legacy: AttachmentTransport | undefined; + if (options.transport) { + delegate = versionedTransport(options.transport); + if (!delegate) legacy = options.transport as AttachmentTransport; + } else if (endpoint && allowed()) { + try { delegate = createAttachmentHttpTransportV1({ endpoint, headers, client: attachmentNodeHttpClient }); } + catch { /* Unsupported/untrusted control origin remains unconfigured. */ } + } + // Always hand the manager a transport, so fi-core never builds its own from + // FI_ATTACHMENTS_ENDPOINT. Without an approved endpoint every call is endpoint_unconfigured. + // The stub is V1-shaped so an unapproved endpoint is not reported as a legacy transport. + const unconfigured = async (): Promise => { throw new AttachmentTransportError("endpoint_unconfigured"); }; + const transport = legacy ? guardLegacy(legacy, check) : guardV1(delegate ?? { + bootstrap: unconfigured, renewSession: unconfigured, initialize: unconfigured, renewUpload: unconfigured, + putContent: unconfigured, complete: unconfigured, abort: unconfigured, uploadStatus: unconfigured, + putManifest: unconfigured, submitImport: unconfigured, importStatus: unconfigured, + }, check); + return { + ...options, endpoint, headers, transport, + enabled: allowed() ? options.enabled : false, + projectName: config.projectName, + projectType: "observe", + environment: () => allowed() ? process.env : { FI_ATTACHMENTS_ENABLED: "false" }, + traceConfig: { + ...options.traceConfig, + hideInputs: config.hideInputs || options.traceConfig?.hideInputs, + hideOutputs: config.hideOutputs || options.traceConfig?.hideOutputs, + hideErrorDetails: config.hideErrorDetails || options.traceConfig?.hideErrorDetails, + }, + }; +} diff --git a/typescript/packages/traceai_agent_core/src/attachment-status.ts b/typescript/packages/traceai_agent_core/src/attachment-status.ts new file mode 100644 index 000000000..6897424e4 --- /dev/null +++ b/typescript/packages/traceai_agent_core/src/attachment-status.ts @@ -0,0 +1,74 @@ +import type { ResolvedConfig } from "./config.js"; + +export type AgentAttachmentSource = "typed-image-blocks" | "source_unavailable"; + +/** + * Per-host media source readiness, keyed by adapter id. Separate from span support, + * server authorization and product playback. A plugin, hook or native OTel feed is + * not screenshot access: only hosts that hand the integration inline typed image + * bytes are capturable with the current Bytes/Blob attachment core. + */ +export const AGENT_ATTACHMENT_SOURCES: Readonly> = Object.freeze({ + pi: "typed-image-blocks", + "oh-my-pi": "typed-image-blocks", + claude: "typed-image-blocks", + codex: "source_unavailable", + cursor: "source_unavailable", + "cursor-cli": "source_unavailable", + "cursor-cloud": "source_unavailable", + opencode: "source_unavailable", + "opencode-v1": "source_unavailable", + openclaw: "source_unavailable", + hermes: "source_unavailable", + goose: "source_unavailable", + gemini: "source_unavailable", + copilot: "source_unavailable", + "copilot-vscode": "source_unavailable", + "kiro-cli": "source_unavailable", + "kiro-ide": "source_unavailable", + antigravity: "source_unavailable", + "antigravity-ide": "source_unavailable", + augment: "source_unavailable", + "augment-ide": "source_unavailable", + "devin-cascade": "source_unavailable", + grok: "source_unavailable", + muse: "source_unavailable", +}); + +/** + * The attachment control-plane endpoint, only when its origin is the approved OTLP + * destination's origin. Destination approval binds a saved credential to that origin, so an + * endpoint from elsewhere (env, flags or options) never receives the credential. + */ +export function approvedAttachmentEndpoint( + config: Pick, + candidate: string | undefined, +): string | undefined { + if (!candidate || !config.endpoint) return undefined; + try { + return new URL(candidate).origin === new URL(config.endpoint).origin ? candidate : undefined; + } catch { + return undefined; + } +} + +/** Local inspection does not mint a capability or confuse OTLP acceptance with attachment scope. */ +export function agentAttachmentStatus(config: ResolvedConfig, env: NodeJS.ProcessEnv = process.env) { + const source: AgentAttachmentSource = Object.prototype.hasOwnProperty.call(AGENT_ATTACHMENT_SOURCES, config.agent) + ? AGENT_ATTACHMENT_SOURCES[config.agent] + : "source_unavailable"; + const requested = /^(true|1|yes)$/i.test(env.FI_ATTACHMENTS_ENABLED?.trim() ?? ""); + return { + state: !requested ? "disabled" : + !config.enabled || !config.captureContent || (config.hideInputs && config.hideOutputs) ? "policy_denied" : + source === "source_unavailable" ? "source_unavailable" : + !approvedAttachmentEndpoint(config, env.FI_ATTACHMENTS_ENDPOINT) ? "endpoint_unconfigured" : "scope_unverified", + source, + credentialScope: "unverified-runtime-bootstrap-required", + productPlayback: "unverified", + recording: "not-created", + maxImageBytes: 256 * 1024, + localFile: "source_unavailable", + remoteUrl: "source_unavailable", + } as const; +} diff --git a/typescript/packages/traceai_agent_core/src/attachments.ts b/typescript/packages/traceai_agent_core/src/attachments.ts new file mode 100644 index 000000000..a59eccc5a --- /dev/null +++ b/typescript/packages/traceai_agent_core/src/attachments.ts @@ -0,0 +1,128 @@ +import { randomUUID } from "node:crypto"; +import type { TracerProvider } from "@opentelemetry/api"; +import type { + AttachmentAdmission, + AttachmentAdmissionTicket, + AttachmentHandle, + AttachmentManager, + AttachmentOptions, + AttachmentSpan, +} from "@traceai/fi-core/attachments"; + +/** A bounded callback budget, not a claim that every provider image is capturable. */ +export const MAX_AGENT_IMAGE_BYTES = 256 * 1024; +const IMAGE_MIMES = new Set(["image/png", "image/jpeg", "image/webp"]); +// Identity-only replay guard. Distinct parts/positions/spans keep distinct occurrences. +const admitted = new WeakMap>>>(); +export type AgentImageOptions = Omit; + +/** Reuse the configured provider's manager. Never create another uploader or inspect credentials. */ +export function providerAttachments(provider: TracerProvider): AttachmentManager | undefined { + const candidate = provider as TracerProvider & { + attachments?: AttachmentManager; + getDelegate?: () => TracerProvider; + }; + return candidate.attachments ?? (candidate.getDelegate?.() as typeof candidate | undefined)?.attachments; +} + +/** + * Exact decoded length of strict, padded base64, computed without decoding. Returns "too_large" for a + * string longer than `limit` bytes can encode (never scanned), and undefined for anything malformed. + */ +function strictBase64Length(value: unknown, limit: number): number | "too_large" | undefined { + if (typeof value !== "string" || !value.length || value.length % 4 !== 0) return undefined; + if (value.length > 4 * Math.ceil(Math.max(limit, 0) / 3)) return "too_large"; + if (!/^[A-Za-z0-9+/]*={0,2}$/.test(value)) return undefined; + const length = (value.length / 4) * 3 - (value.endsWith("==") ? 2 : value.endsWith("=") ? 1 : 0); + if (length <= 0) return undefined; + return length > limit ? "too_large" : length; +} + +/** A terminal handle for a part the adapter refused after admission; the ticket is already released. */ +function refused(admission: AttachmentAdmission): AttachmentHandle { + return Object.freeze({ attachmentId: randomUUID(), occurrenceId: randomUUID(), admission, + completion: Promise.resolve(Object.freeze({ status: admission })) }); +} +const NO_SOURCE = Object.freeze({ kind: "Bytes" as const, data: new Uint8Array(0) }); + +/** Typed inline image shapes: Pi/OMP `{type, mimeType, data}` and Anthropic/Claude Code base64 `source`. */ +function imageShape(part: unknown): { mimeType: string; read: () => unknown } | undefined { + if (!part || typeof part !== "object") return undefined; + const block = part as { type?: unknown; mimeType?: unknown; data?: unknown; source?: unknown }; + if (block.type !== "image") return undefined; + if (block.source === undefined) { + const mimeType = block.mimeType; + return typeof mimeType === "string" && IMAGE_MIMES.has(mimeType) ? { mimeType, read: () => block.data } : undefined; + } + const source = block.source as { type?: unknown; media_type?: unknown; data?: unknown } | null; + // URL/file sources are retrieval, not inline bytes; they stay unsupported. + if (!source || typeof source !== "object" || source.type !== "base64") return undefined; + const mimeType = source.media_type; + return typeof mimeType === "string" && IMAGE_MIMES.has(mimeType) ? { mimeType, read: () => source.data } : undefined; +} + +/** + * The typed inline image content boundary shared by coding-agent hosts. Not a recursive object walker. + * Each part is preflighted (deny-wins policy, sampling, correlation, budget) BEFORE its base64 is read; + * only an admitted part is read, strictly validated, decoded and handed to `ticket.attach`. A part the + * per-call budget or the manager's limits refuse is re-preflighted once at its exact decoded length + * (policy already passed, so reading the string length is allowed). No paths/URLs fetched. + * The caller must select the original occurrence, not replayed provider history. + */ +export function captureAgentImages( + manager: AttachmentManager | undefined, + span: AttachmentSpan, + blocks: unknown, + options: AgentImageOptions, +): readonly AttachmentHandle[] { + if (!manager || !Array.isArray(blocks) || manager.status().state !== "ready") return []; + const handles: AttachmentHandle[] = []; + let remaining = MAX_AGENT_IMAGE_BYTES; + let spans = admitted.get(manager); + if (!spans) admitted.set(manager, spans = new WeakMap()); + let seen = spans.get(span); + if (!seen) spans.set(span, seen = new WeakMap()); + const { direction, purpose, capture, ...fields } = options; + for (let partIndex = 0; partIndex < Math.min(blocks.length, 64); partIndex++) { + let ticket: AttachmentAdmissionTicket | undefined; + try { + const part = blocks[partIndex]; + const shape = imageShape(part); + if (!shape || seen.get(part)?.has(partIndex)) continue; + const preflight = (maxBytes: number) => manager.preflight(span, { mimeType: shape.mimeType, direction, purpose, capture, maxBytes }); + ticket = preflight(remaining); + let value: unknown; + let read = false; + if (!ticket.admitted && (ticket.reason === "too_large" || ticket.reason === "queue_full")) { + // Budget denials come only after the policy gate passed. + value = shape.read(); read = true; + const exact = strictBase64Length(value, remaining); + if (typeof exact === "number") ticket = preflight(exact); + } + // A denied ticket mints its own terminal handle without touching the source. + if (!ticket.admitted) { handles.push(ticket.attach(NO_SOURCE)); ticket = undefined; continue; } + if (!read) value = shape.read(); + const length = strictBase64Length(value, Math.min(remaining, ticket.maxBytes)); + if (typeof length !== "number") { + ticket.release(); ticket = undefined; + handles.push(refused(length === "too_large" ? "too_large" : "source_unavailable")); + continue; + } + const result = ticket.attach({ kind: "Bytes", data: Buffer.from(value as string, "base64") }, { ...fields, partIndex }); + ticket = undefined; + handles.push(result); + if (result.admission === "queued") { + // Only bytes the core actually owns count against the per-call budget. + remaining -= length; + let positions = seen.get(part); + if (!positions) seen.set(part, positions = new Set()); + positions.add(partIndex); + } + } catch { + // Host data, including throwing accessors, must not affect inference or diagnostics. + // A part that was preflighted reports why; a malformed block before preflight is skipped. + if (ticket) { ticket.release(); handles.push(refused("source_unavailable")); } + } + } + return handles; +} diff --git a/typescript/packages/traceai_agent_core/src/index.ts b/typescript/packages/traceai_agent_core/src/index.ts index 1157c79fe..205a908b5 100644 --- a/typescript/packages/traceai_agent_core/src/index.ts +++ b/typescript/packages/traceai_agent_core/src/index.ts @@ -9,4 +9,6 @@ export * from "./lifecycle.js"; export * from "./delivery.js"; export * from "./tracer.js"; export * from "./capture.js"; +export * from "./attachments.js"; +export * from "./attachment-status.js"; export { VERSION } from "./version.js"; diff --git a/typescript/packages/traceai_agent_core/src/lifecycle.ts b/typescript/packages/traceai_agent_core/src/lifecycle.ts index 28643a301..06788ce1c 100644 --- a/typescript/packages/traceai_agent_core/src/lifecycle.ts +++ b/typescript/packages/traceai_agent_core/src/lifecycle.ts @@ -23,6 +23,7 @@ import { type ResolveOptions, type ResolvedConfig, } from "./config.js"; +import { agentAttachmentStatus } from "./attachment-status.js"; import { contentHash, readJson, @@ -585,6 +586,7 @@ export interface DeliveryState { binding?: string; } export interface AgentStatus { + attachments: ReturnType; agent: string; pendingTurns?: number; registrationDetail?: string; @@ -682,6 +684,7 @@ export async function inspectStatus( destination: config.endpoint, projectName: config.projectName, privacy: config.captureContent ? "content-on-by-default" : "structure-only", + attachments: agentAttachmentStatus(config, options.env), provenance: config.provenance, credentials: config.credentials ? "present" : "missing", credentialCheck: check, diff --git a/typescript/packages/traceai_agent_core/src/tracer.ts b/typescript/packages/traceai_agent_core/src/tracer.ts index 5efcd12e4..3d25b396d 100644 --- a/typescript/packages/traceai_agent_core/src/tracer.ts +++ b/typescript/packages/traceai_agent_core/src/tracer.ts @@ -28,6 +28,13 @@ import { redact } from "./privacy.js"; import { contentHash } from "./files.js"; import { VERSION } from "./version.js"; import type { DeliveryResult } from "./validation.js"; +import type { NodeAttachmentManagerOptions } from "@traceai/fi-core/attachments/node"; +import type { AttachmentManager } from "@traceai/fi-core/attachments"; +import { captureAgentImages, type AgentImageOptions } from "./attachments.js"; +import { agentAttachmentRuntime } from "./attachment-runtime.js"; + +/** Provider-owned attachment runtime options; defaults come from FI_ATTACHMENTS_* and the resolved config. */ +export type AgentAttachmentRuntimeOptions = NodeAttachmentManagerOptions; export type TurnOutcome = "success" | "error" | "interrupted" | "incomplete"; export type EventTiming = @@ -94,6 +101,8 @@ export interface ChildInput { fidelity?: Fidelity; } export interface ChildHandle { + /** Original typed Pi/OMP images only, before end(); no history replay or file retrieval. */ + images?(blocks: unknown, options: AgentImageOptions): void; end(value?: EndInput): void; context?: SpanContext; } @@ -108,6 +117,7 @@ export interface TurnHandle extends ChildHandle { child(value: ChildInput): ChildHandle; } export interface TurnTracer { + readonly attachments?: Pick; startTurn(value: TurnInput): TurnHandle; flush(): Promise; shutdown(): Promise; @@ -144,6 +154,7 @@ function content( /** A private FI provider: no global tracer/context registration, host/process detectors or model gateway. */ export function createTurnTracer(options: { config: ResolvedConfig; + attachments?: AgentAttachmentRuntimeOptions; identityKey?: Uint8Array; onDelivery?: (result: DeliveryResult) => void; /** Revalidate a durable snapshot's privacy/routing policy immediately before each HTTP batch. */ @@ -191,6 +202,7 @@ export function createTurnTracer(options: { project_type: ProjectType.OBSERVE, }), spanProcessors: [processor], + attachments: agentAttachmentRuntime(config, options.attachments), verbose: false, idGenerator: { generateTraceId: () => nextTrace, @@ -412,6 +424,10 @@ export function createTurnTracer(options: { eventCount = 0; const turn: TurnHandle = { context: root.spanContext(), + images(blocks, options) { + if (!ended && canCollect() && config.captureContent) + captureAgentImages(provider.attachments, root, blocks, options); + }, gap(reason): void { if (!ended && canCollect() && /^[A-Z_]{1,80}$/.test(reason)) root.setAttribute(SC.FI_AGENT_GAP, reason); @@ -551,6 +567,10 @@ export function createTurnTracer(options: { children.set(child.id, state); return { context: span.spanContext(), + images(blocks, options) { + if (!state.done && !state.skipped && !ended && canCollect() && config.captureContent) + captureAgentImages(provider.attachments, span, blocks, options); + }, end(end = {}) { if (state.done) return; state.done = true; @@ -685,6 +705,7 @@ export function createTurnTracer(options: { } return { startTurn, + attachments: provider.attachments, state: processor.state, async flush() { try { @@ -695,6 +716,7 @@ export function createTurnTracer(options: { }, async discard() { closed = true; + provider.attachments.invalidate("policy_denied"); active.clear(); processor.discard(); try { diff --git a/typescript/packages/traceai_agent_core/test/attachments.test.cjs b/typescript/packages/traceai_agent_core/test/attachments.test.cjs new file mode 100644 index 000000000..864807b3c --- /dev/null +++ b/typescript/packages/traceai_agent_core/test/attachments.test.cjs @@ -0,0 +1,434 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const http = require('node:http'); +const https = require('node:https'); +const { execFileSync, spawn } = require('node:child_process'); +const { once } = require('node:events'); +const fs = require('node:fs/promises'); +const os = require('node:os'); +const path = require('node:path'); +const { AttachmentManager, AttachmentTransportError } = require('@traceai/fi-core/attachments'); +const { captureAgentImages, MAX_AGENT_IMAGE_BYTES, createTurnTracer, agentAttachmentStatus } = require('../dist/src/index.js'); +const { agentAttachmentRuntime } = require('../dist/src/attachment-runtime.js'); +const { identity } = require('../../fi-core/test/attachments-fixtures.cjs'); +const { v1 } = require('../../fi-core/test/attachments-v1.cjs'); +// Every FI_ATTACHMENTS_* process warning: the V1 paths below must emit none (the legacy row, last, emits one). +const warnings = []; +process.on('warning', w => { if (String(w.code).startsWith('FI_ATTACHMENTS_')) warnings.push(w.code); }); +/** An in-memory AttachmentTransportV1 double: `calls` lists wire operations, `body()` the last content PUT. */ +function fake(overrides = {}) { + const d = v1(overrides); + return { transport: d.transport, get calls() { return d.ops(); }, body: () => [...[...d.contents.values()].at(-1)] }; +} +const image = () => ({ type: 'image', mimeType: 'image/png', data: 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQIHWP4z8DwHwAFgAI/ScLbtAAAAABJRU5ErkJggg==' }); +const input = { direction: 'input', purpose: 'model_input', role: 'user' }; +const tool = { direction: 'output', purpose: 'tool_result', role: 'tool', toolCallId: 'call_1', capture: { modelInput: true } }; +async function fixture(t, overrides = {}, transportOverrides = {}) { + const f = fake(transportOverrides); + const manager = new AttachmentManager({ enabled: true, projectName: 'fixture', projectType: 'observe', transport: f.transport, ...overrides }); + t.after(() => manager.shutdown()); + await manager.ready(); + return { ...f, manager }; +} + +test('default-OFF reads no typed blocks, decodes nothing and makes no request', async t => { + const f = await fixture(t, { enabled: false }); + const parts = [{ get type() { throw Error('must not inspect denied blocks'); } }]; + assert.deepEqual(captureAgentImages(f.manager, identity, parts, input), []); + assert.deepEqual(f.calls, []); +}); +for (const [key, options] of [ + ['hideInputs', input], ['hideInputMessages', input], ['hideInputText', input], + ['hideOutputs', tool], ['hideOutputMessages', tool], ['hideOutputText', tool], + ['hideInputImages', tool], ['hideInputs', tool], ['piiRedaction', input], + ['hideErrorDetails', { ...tool, capture: { modelInput: true, errorDetail: true } }], +]) test(`${key} denies before base64 access and upload`, async t => { + const f = await fixture(t, { traceConfig: { [key]: true } }); + let reads = 0; + const part = { type: 'image', mimeType: 'image/png', get data() { reads++; throw Error('synthetic-secret'); } }; + const result = captureAgentImages(f.manager, identity, [part], options); + assert.equal(result.length, 1); + assert.equal(result[0].admission, key === 'piiRedaction' ? 'redaction_unavailable' : 'policy_denied'); + await f.manager.flush(); + assert.equal(reads, 0); + assert.deepEqual(f.calls, ['bootstrap']); +}); +for (const capture of [{ muted: true }, { temporaryChat: true }, { captureContent: false }, { recordInputs: false }]) + test(`integration restriction ${Object.keys(capture)[0]} cannot be overridden`, async t => { + const f = await fixture(t); + const part = { ...image(), get data() { throw Error('denied source read'); } }; + assert.equal(captureAgentImages(f.manager, identity, [part], { ...input, capture })[0].admission, 'policy_denied'); + await f.manager.flush(); assert.deepEqual(f.calls, ['bootstrap']); + }); + +test('only explicit typed image bytes; arbitrary paths, URLs and nested objects are not fetched', async t => { + const f = await fixture(t); + const parts = [ + { type: 'image', mimeType: 'image/svg+xml', data: '