fix: add agent context to ws logs

This commit is contained in:
Peter Steinberger
2026-01-17 20:12:38 +00:00
parent f5d5ef6857
commit d9c29f5ce5
5 changed files with 33 additions and 1 deletions

View File

@@ -29,12 +29,15 @@ describe("gateway ws log helpers", () => {
test("summarizeAgentEventForWsLog extracts useful fields", () => { test("summarizeAgentEventForWsLog extracts useful fields", () => {
const summary = summarizeAgentEventForWsLog({ const summary = summarizeAgentEventForWsLog({
runId: "12345678-1234-1234-1234-123456789abc", runId: "12345678-1234-1234-1234-123456789abc",
sessionKey: "agent:main:main",
stream: "assistant", stream: "assistant",
seq: 2, seq: 2,
data: { text: "hello world", mediaUrls: ["a", "b"] }, data: { text: "hello world", mediaUrls: ["a", "b"] },
}); });
expect(summary).toMatchObject({ expect(summary).toMatchObject({
agent: "main",
run: "12345678…9abc", run: "12345678…9abc",
session: "main",
stream: "assistant", stream: "assistant",
aseq: 2, aseq: 2,
text: "hello world", text: "hello world",

View File

@@ -1,5 +1,6 @@
import chalk from "chalk"; import chalk from "chalk";
import { isVerbose } from "../globals.js"; import { isVerbose } from "../globals.js";
import { parseAgentSessionKey } from "../routing/session-key.js";
import { createSubsystemLogger, shouldLogSubsystemToConsole } from "../logging.js"; import { createSubsystemLogger, shouldLogSubsystemToConsole } from "../logging.js";
import { getDefaultRedactPatterns, redactSensitiveText } from "../logging/redact.js"; import { getDefaultRedactPatterns, redactSensitiveText } from "../logging/redact.js";
import { DEFAULT_WS_SLOW_MS, getGatewayWsLogStyle } from "./ws-logging.js"; import { DEFAULT_WS_SLOW_MS, getGatewayWsLogStyle } from "./ws-logging.js";
@@ -88,11 +89,21 @@ export function summarizeAgentEventForWsLog(payload: unknown): Record<string, un
const runId = typeof rec.runId === "string" ? rec.runId : undefined; const runId = typeof rec.runId === "string" ? rec.runId : undefined;
const stream = typeof rec.stream === "string" ? rec.stream : undefined; const stream = typeof rec.stream === "string" ? rec.stream : undefined;
const seq = typeof rec.seq === "number" ? rec.seq : undefined; const seq = typeof rec.seq === "number" ? rec.seq : undefined;
const sessionKey = typeof rec.sessionKey === "string" ? rec.sessionKey : undefined;
const data = const data =
rec.data && typeof rec.data === "object" ? (rec.data as Record<string, unknown>) : undefined; rec.data && typeof rec.data === "object" ? (rec.data as Record<string, unknown>) : undefined;
const extra: Record<string, unknown> = {}; const extra: Record<string, unknown> = {};
if (runId) extra.run = shortId(runId); if (runId) extra.run = shortId(runId);
if (sessionKey) {
const parsed = parseAgentSessionKey(sessionKey);
if (parsed) {
extra.agent = parsed.agentId;
extra.session = parsed.rest;
} else {
extra.session = sessionKey;
}
}
if (stream) extra.stream = stream; if (stream) extra.stream = stream;
if (seq !== undefined) extra.aseq = seq; if (seq !== undefined) extra.aseq = seq;

View File

@@ -51,8 +51,14 @@ export function resetAgentRunContextForTest() {
export function emitAgentEvent(event: Omit<AgentEventPayload, "seq" | "ts">) { export function emitAgentEvent(event: Omit<AgentEventPayload, "seq" | "ts">) {
const nextSeq = (seqByRun.get(event.runId) ?? 0) + 1; const nextSeq = (seqByRun.get(event.runId) ?? 0) + 1;
seqByRun.set(event.runId, nextSeq); seqByRun.set(event.runId, nextSeq);
const context = runContextById.get(event.runId);
const sessionKey =
typeof event.sessionKey === "string" && event.sessionKey.trim()
? event.sessionKey
: context?.sessionKey;
const enriched: AgentEventPayload = { const enriched: AgentEventPayload = {
...event, ...event,
sessionKey,
seq: nextSeq, seq: nextSeq,
ts: Date.now(), ts: Date.now(),
}; };

View File

@@ -148,7 +148,15 @@ function formatConsoleLine(opts: {
? color.gray ? color.gray
: color.cyan; : color.cyan;
const displayMessage = stripRedundantSubsystemPrefixForConsole(opts.message, displaySubsystem); const displayMessage = stripRedundantSubsystemPrefixForConsole(opts.message, displaySubsystem);
const time = opts.style === "pretty" ? color.gray(new Date().toISOString().slice(11, 19)) : ""; const time = (() => {
if (opts.style === "pretty") {
return color.gray(new Date().toISOString().slice(11, 19));
}
if (loggingState.consoleTimestampPrefix) {
return color.gray(new Date().toISOString());
}
return "";
})();
const prefixToken = prefixColor(prefix); const prefixToken = prefixColor(prefix);
const head = [time, prefixToken].filter(Boolean).join(" "); const head = [time, prefixToken].filter(Boolean).join(" ");
return `${head} ${levelColor(displayMessage)}`; return `${head} ${levelColor(displayMessage)}`;

View File

@@ -46,14 +46,18 @@ async function main() {
{ setGatewayWsLogStyle }, { setGatewayWsLogStyle },
{ setVerbose }, { setVerbose },
{ defaultRuntime }, { defaultRuntime },
{ enableConsoleCapture, setConsoleTimestampPrefix },
] = await Promise.all([ ] = await Promise.all([
import("../config/config.js"), import("../config/config.js"),
import("../gateway/server.js"), import("../gateway/server.js"),
import("../gateway/ws-logging.js"), import("../gateway/ws-logging.js"),
import("../globals.js"), import("../globals.js"),
import("../runtime.js"), import("../runtime.js"),
import("../logging.js"),
]); ]);
enableConsoleCapture();
setConsoleTimestampPrefix(true);
setVerbose(hasFlag(args, "--verbose")); setVerbose(hasFlag(args, "--verbose"));
const wsLogRaw = (hasFlag(args, "--compact") ? "compact" : argValue(args, "--ws-log")) as const wsLogRaw = (hasFlag(args, "--compact") ? "compact" : argValue(args, "--ws-log")) as