feat(server,web): import and export Trace files in the trace viewer (#73)

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Yaowei Zheng
2026-07-27 22:54:57 +08:00
committed by GitHub
parent d29d2ba7f3
commit f192db338e
13 changed files with 592 additions and 8 deletions
+14
View File
@@ -939,6 +939,20 @@ export interface AgentTracesResponse {
dates: AgentTraceDateGroup[];
}
export interface TraceImportRequest {
/** Base64 of the Trace file content (JSON Lines; the first record must be `session_meta`). */
dataBase64: string;
}
export interface TraceImportResponse {
/** Session id taken from the imported file's `session_meta`. */
sessionId: string;
/** Allocated file index: always 1 — an import creates a new Session (a duplicate session id is rejected with 409 `trace_session_exists`). */
index: number;
/** Date directory the file landed in (local yyyy-mm-dd from the first record's timestamp, matching the Trace Writer's convention). */
date: string;
}
// ---------------------------------------------------------------------------
// Usage and cost statistics
// ---------------------------------------------------------------------------
@@ -1,17 +1,32 @@
/**
* Agent-level Trace browsing routes:
* - GET /api/projects/:p/agents/:a/traces — drills down Agent -> date -> Session -> index (reverse order);
* - GET /api/projects/:p/agents/:a/traces/:sessionId/:index (including /analysis) —
* - GET /api/projects/:p/agents/:a/traces/:sessionId/:index (including /analysis, /download) —
* read-only Trace detail endpoints (FD-3): locate the Trace file directly by
* (projectId, agentId, sessionId), without depending on the sessions table for
* tracking — any entry visible in the directory tree (subagent child Sessions,
* CLI-created Sessions) can be opened and read; access is enforced by requireProjectAccess.
* - POST /api/projects/:p/agents/:a/traces/import — uploads a Trace JSONL file (owner
* only, mirroring the Agent snapshot import); the file names itself via its
* session_meta and always becomes index 001 of a new Session — a session id the
* Agent already has is rejected with 409 trace_session_exists.
*/
import { Hono } from "hono";
import type { AppEnv } from "../../auth/middleware.js";
import { paginationQuery, positiveIntParam, requireValidId } from "../validate.js";
import type { TraceImportResponse } from "../../api/types.js";
import {
badRequest,
paginationQuery,
positiveIntParam,
readJson,
requireString,
requireValidId,
} from "../validate.js";
import type { AppDeps } from "../../app.js";
/** Import file size cap: aligned with the snapshot import (stays within the 20MB body limit after base64). */
const MAX_TRACE_BYTES = 14 * 1024 * 1024;
export function agentTracesRoutes(deps: AppDeps): Hono<AppEnv> {
const app = new Hono<AppEnv>();
@@ -44,5 +59,47 @@ export function agentTracesRoutes(deps: AppDeps): Hono<AppEnv> {
return c.json(await deps.traceService.analyze(projectId, agentId, sessionId, index));
});
// Raw-file download (any member, like the snapshot export): the file is served verbatim
// as an attachment, so what's downloaded can be re-imported byte-compatibly.
app.get("/:sessionId/:index/download", async (c) => {
const projectId = requireValidId(c, "projectId");
const agentId = requireValidId(c, "agentId");
const sessionId = requireValidId(c, "sessionId");
deps.projectService.requireProjectAccess(c.var.user.userId, projectId);
const index = positiveIntParam(c, "index");
const bytes = await deps.traceService.readFileRaw(projectId, agentId, sessionId, index);
const fileName = `${sessionId}_${String(index).padStart(3, "0")}.jsonl`;
return new Response(new Uint8Array(bytes), {
headers: {
"Content-Type": "application/x-ndjson",
"Content-Disposition": `attachment; filename*=UTF-8''${encodeURIComponent(fileName)}`,
"X-Content-Type-Options": "nosniff",
},
});
});
// Trace file upload (owner only, mirroring the Agent snapshot import): the route checks the
// transport shape (base64, size); the content itself — JSONL, leading session_meta, a
// filename-safe session_id — is validated by the service right where the path is built.
app.post("/import", async (c) => {
const projectId = requireValidId(c, "projectId");
const agentId = requireValidId(c, "agentId");
deps.projectService.requireProjectOwner(c.var.user.userId, projectId);
await deps.agentConfigService.requireExists(projectId, agentId);
const body = await readJson(c);
const dataBase64 = requireString(body, "dataBase64", { minLen: 1, maxLen: 20 * 1024 * 1024 });
const bytes = Buffer.from(dataBase64, "base64");
if (bytes.byteLength === 0) throw badRequest("Import file is empty.");
if (bytes.byteLength > MAX_TRACE_BYTES) {
throw badRequest("Import file exceeds the 14MB limit.");
}
const res: TraceImportResponse = await deps.traceService.importTraceFile(
projectId,
agentId,
bytes.toString("utf8"),
);
return c.json(res);
});
return app;
}
+96 -1
View File
@@ -13,6 +13,8 @@ import fs from "node:fs/promises";
import path from "node:path";
import {
agentsDir,
isSessionMeta,
parseTraceLines,
parseUserSteeringText,
readTraceTolerant,
tracesDir,
@@ -25,15 +27,24 @@ import type {
TraceAnalysisResponse,
TraceEventsResponse,
TraceFileInfo,
TraceImportResponse,
TraceModelSegment,
TraceTaskStats,
TraceToolSpan,
UsageTrendPointInTrace,
} from "../api/types.js";
import { HttpError } from "../http/errors.js";
import { formatLocalDate } from "../internal/dates.js";
const TRACE_FILE_RE = /^(.+)_(\d{3})\.jsonl$/;
/**
* session_id an imported Trace file may declare: same alphabet as resource ids plus a length
* cap. The value becomes part of the target **filename**, so this is a path-traversal defense,
* checked right next to the path construction — never trust the caller to have validated it.
*/
const IMPORT_SESSION_ID_RE = /^[A-Za-z0-9_-]{1,128}$/;
/** Recursion depth cap for sub-session expansion (run_subagent depth is already constrained by the SDK; this is just a defensive backstop against cycles). */
const MAX_SUBAGENT_DEPTH = 4;
@@ -687,6 +698,16 @@ export class TraceService {
sessionId: string,
index: number,
): Promise<OmniMessage[]> {
const file = await this.locateByIndex(projectId, agentId, sessionId, index);
return readTraceTolerant(file.path);
}
private async locateByIndex(
projectId: string,
agentId: string,
sessionId: string,
index: number,
): Promise<LocatedFile> {
const files = await this.locateAll(projectId, agentId, sessionId);
const file = files.find((f) => f.index === index);
if (!file) {
@@ -696,6 +717,80 @@ export class TraceService {
`This Session has no Trace file with index ${index}.`,
);
}
return readTraceTolerant(file.path);
return file;
}
/** Raw bytes of the Trace file at the given index (export/download: the file is served verbatim). */
async readFileRaw(
projectId: string,
agentId: string,
sessionId: string,
index: number,
): Promise<Buffer> {
const file = await this.locateByIndex(projectId, agentId, sessionId, index);
return fs.readFile(file.path);
}
/**
* Imports an uploaded Trace file (raw JSONL content). Validates the content itself —
* parseable JSONL whose first record is a `session_meta` carrying a filename-safe
* `session_id` (400 invalid_trace otherwise) — then writes it under the Agent's traces
* directory as a **new Session**: a session id the Agent already has is rejected with
* 409 `trace_session_exists` (splicing a further index into an existing Session would
* corrupt its concatenated transcript, silently become its resume point, and could
* collide with a live Writer's rotation), so the imported file is always index 1. The
* date dir comes from the first record's timestamp (falling back to now), formatted as
* a **local** date — the same convention as core's Trace Writer — so an export →
* import round-trip lands in the same date dir on non-UTC servers.
*/
async importTraceFile(
projectId: string,
agentId: string,
content: string,
): Promise<TraceImportResponse> {
const invalid = (message: string) => new HttpError(400, "invalid_trace", message);
let records: OmniMessage[];
try {
records = parseTraceLines(content);
} catch {
throw invalid("The file is not valid Trace JSONL.");
}
if (records.length === 0) throw invalid("The file contains no Trace records.");
const first = records[0]!;
if (!isSessionMeta(first))
throw invalid("The first record of a Trace file must be session_meta.");
// The payload type declares session_id: string, but the value came from user-supplied JSON —
// re-check the runtime shape before it becomes part of a filename.
const sessionId: unknown = (first.payload as { session_id?: unknown }).session_id;
if (typeof sessionId !== "string" || !IMPORT_SESSION_ID_RE.test(sessionId)) {
throw invalid("session_meta carries a missing or invalid session_id.");
}
const duplicate = () =>
new HttpError(
409,
"trace_session_exists",
`This Agent already has a Session with id ${sessionId}; a duplicate Trace cannot be imported.`,
);
if ((await this.locateAll(projectId, agentId, sessionId)).length > 0) throw duplicate();
const ts = Date.parse(first.timestamp);
const date = formatLocalDate(Number.isNaN(ts) ? new Date() : new Date(ts));
const index = 1;
const dir = path.join(tracesDir(this.root, projectId, agentId), date);
await fs.mkdir(dir, { recursive: true });
const file = path.join(dir, `${sessionId}_${String(index).padStart(3, "0")}.jsonl`);
try {
// Normalize to exactly one trailing newline (the JSONL convention the writer
// follows). `wx` closes the check-then-write race: two concurrent imports of the
// same new session id both pass the locateAll check, but only one can create the
// file — the loser's EEXIST maps to the same 409 as the pre-check.
await fs.writeFile(file, content.replace(/\n+$/, "") + "\n", {
encoding: "utf8",
flag: "wx",
});
} catch (err) {
if ((err as NodeJS.ErrnoException).code === "EEXIST") throw duplicate();
throw err;
}
return { sessionId, index, date };
}
}