feat(db): trim stored hook payloads; price the session list like a session
Analytics parity, phase 1 (data hygiene), from the analytics-parity session. events.data is now stored through the SQL function trim_event_data(?) (server/lib/event-payload.js, registered in server/db.js) on every event INSERT and in the duplicate guard: Edit/MultiEdit/NotebookEdit/Write drop the whole-file mirrors (originalFile, oldString/newString, Write content), Read drops file.content/base64, and any string in tool_input/tool_response over 8 KB keeps a 6 KB head + 2 KB tail. structuredPatch and every field outside those two stay whole; _trimmed names what was dropped. The full text stays in the transcript and its snapshot. Rows stored before this are trimmed once in the background (sweepEventPayloads, 200 rows per write transaction, user_version 4). No VACUUM, no row pruned. The session list now prices every token column /api/pricing/cost/:id does (speed, geo, tier, the 1h cache write, server-tool fees) through one tokensForSessions helper, so a row's cost equals the session's own.
This commit is contained in:
@@ -562,7 +562,7 @@ function importCompactions(dbModule, sessionId, mainAgentId, compactions) {
|
||||
if (!compactions || compactions.length === 0) return 0;
|
||||
const { db, stmts } = dbModule;
|
||||
const insertEvent = db.prepare(
|
||||
"INSERT INTO events (session_id, agent_id, event_type, tool_name, summary, data, created_at) VALUES (?, ?, ?, ?, ?, ?, ?)"
|
||||
"INSERT INTO events (session_id, agent_id, event_type, tool_name, summary, data, created_at) VALUES (?, ?, ?, ?, ?, trim_event_data(?), ?)"
|
||||
);
|
||||
let created = 0;
|
||||
for (let i = 0; i < compactions.length; i++) {
|
||||
@@ -620,7 +620,7 @@ function importSubagents(dbModule, sessionId, mainAgentId, toolUses) {
|
||||
if (!toolUses || toolUses.length === 0) return 0;
|
||||
const { stmts } = dbModule;
|
||||
const insertEvent = dbModule.db.prepare(
|
||||
"INSERT INTO events (session_id, agent_id, event_type, tool_name, summary, data, created_at) VALUES (?, ?, ?, ?, ?, ?, ?)"
|
||||
"INSERT INTO events (session_id, agent_id, event_type, tool_name, summary, data, created_at) VALUES (?, ?, ?, ?, ?, trim_event_data(?), ?)"
|
||||
);
|
||||
|
||||
let created = 0;
|
||||
@@ -679,7 +679,7 @@ function importApiErrors(dbModule, sessionId, mainAgentId, apiErrors) {
|
||||
if (!apiErrors || apiErrors.length === 0) return 0;
|
||||
const { db } = dbModule;
|
||||
const insertEvent = db.prepare(
|
||||
"INSERT INTO events (session_id, agent_id, event_type, tool_name, summary, data, created_at) VALUES (?, ?, ?, ?, ?, ?, ?)"
|
||||
"INSERT INTO events (session_id, agent_id, event_type, tool_name, summary, data, created_at) VALUES (?, ?, ?, ?, ?, trim_event_data(?), ?)"
|
||||
);
|
||||
let created = 0;
|
||||
for (const err of apiErrors) {
|
||||
@@ -996,7 +996,7 @@ function importSubagentFromJsonl(dbModule, sessionId, mainAgentId, subData) {
|
||||
// importSession level (subagents have their own JSONL with separate usage).
|
||||
|
||||
const insertEvent = db.prepare(
|
||||
"INSERT INTO events (session_id, agent_id, event_type, tool_name, summary, data, created_at) VALUES (?, ?, ?, ?, ?, ?, ?)"
|
||||
"INSERT INTO events (session_id, agent_id, event_type, tool_name, summary, data, created_at) VALUES (?, ?, ?, ?, ?, trim_event_data(?), ?)"
|
||||
);
|
||||
|
||||
// Spawn marker under the parent (main) agent — only emit once per subagent,
|
||||
@@ -1192,7 +1192,7 @@ function importSession(dbModule, session) {
|
||||
|
||||
const mainAgentId = `${session.sessionId}-main`;
|
||||
const insertEvent = db.prepare(
|
||||
"INSERT INTO events (session_id, agent_id, event_type, tool_name, summary, data, created_at) VALUES (?, ?, ?, ?, ?, ?, ?)"
|
||||
"INSERT INTO events (session_id, agent_id, event_type, tool_name, summary, data, created_at) VALUES (?, ?, ?, ?, ?, trim_event_data(?), ?)"
|
||||
);
|
||||
const importedData = JSON.stringify({ imported: true });
|
||||
let backfilled = false;
|
||||
@@ -1591,7 +1591,7 @@ function importSession(dbModule, session) {
|
||||
// Create synthetic events at actual message timestamps so the activity heatmap
|
||||
// reflects when work actually happened, not just session start/end.
|
||||
const insertEvent = db.prepare(
|
||||
"INSERT INTO events (session_id, agent_id, event_type, tool_name, summary, data, created_at) VALUES (?, ?, ?, ?, ?, ?, ?)"
|
||||
"INSERT INTO events (session_id, agent_id, event_type, tool_name, summary, data, created_at) VALUES (?, ?, ?, ?, ?, trim_event_data(?), ?)"
|
||||
);
|
||||
const importedData = JSON.stringify({ imported: true });
|
||||
|
||||
|
||||
Reference in New Issue
Block a user