fix: add nested agent log context

This commit is contained in:
Peter Steinberger
2026-01-17 18:59:52 +00:00
parent 0e49dca53c
commit 96df70fccf
3 changed files with 69 additions and 1 deletions

View File

@@ -79,6 +79,7 @@ Docs: https://docs.clawd.bot
- Tools: include provider/session context in elevated exec denial errors.
- Tools: normalize exec tool alias naming in tool error logs.
- Logging: reuse shared ANSI stripping to keep console capture lint-clean.
- Logging: prefix nested agent output with session/run/channel context.
- Telegram: accept tg/group/telegram prefixes + topic targets for inline button validation. (#1072) — thanks @danielz1z.
- Telegram: split long captions into follow-up messages.
- Config: block startup on invalid config, preserve best-effort doctor config, and keep rolling config backups. (#1083) — thanks @mukhtharcm.

View File

@@ -219,4 +219,43 @@ describe("deliverAgentCommandResult", () => {
expect.objectContaining({ channel: "telegram", to: "123" }),
);
});
it("prefixes nested agent outputs with context", async () => {
const cfg = {} as ClawdbotConfig;
const deps = {} as CliDeps;
const runtime = {
log: vi.fn(),
error: vi.fn(),
} as unknown as RuntimeEnv;
const result = {
payloads: [{ text: "ANNOUNCE_SKIP" }],
meta: {},
};
const { deliverAgentCommandResult } = await import("./agent/delivery.js");
await deliverAgentCommandResult({
cfg,
deps,
runtime,
opts: {
message: "hello",
deliver: false,
lane: "nested",
sessionKey: "agent:main:main",
runId: "run-announce",
messageChannel: "webchat",
},
sessionEntry: undefined,
result,
payloads: result.payloads,
});
expect(runtime.log).toHaveBeenCalledTimes(1);
const line = String(runtime.log.mock.calls[0]?.[0]);
expect(line).toContain("[agent:nested]");
expect(line).toContain("session=agent:main:main");
expect(line).toContain("run=run-announce");
expect(line).toContain("channel=webchat");
expect(line).toContain("ANNOUNCE_SKIP");
});
});

View File

@@ -1,3 +1,4 @@
import { AGENT_LANE_NESTED } from "../../agents/lanes.js";
import { getChannelPlugin, normalizeChannelId } from "../../channels/plugins/index.js";
import { createOutboundSendDeps, type CliDeps } from "../../cli/outbound-send-deps.js";
import type { ClawdbotConfig } from "../../config/config.js";
@@ -22,6 +23,28 @@ type RunResult = Awaited<
ReturnType<(typeof import("../../agents/pi-embedded.js"))["runEmbeddedPiAgent"]>
>;
const NESTED_LOG_PREFIX = "[agent:nested]";
function formatNestedLogPrefix(opts: AgentCommandOpts): string {
const parts = [NESTED_LOG_PREFIX];
const session = opts.sessionKey ?? opts.sessionId;
if (session) parts.push(`session=${session}`);
if (opts.runId) parts.push(`run=${opts.runId}`);
const channel = opts.messageChannel ?? opts.channel;
if (channel) parts.push(`channel=${channel}`);
if (opts.to) parts.push(`to=${opts.to}`);
if (opts.accountId) parts.push(`account=${opts.accountId}`);
return parts.join(" ");
}
function logNestedOutput(runtime: RuntimeEnv, opts: AgentCommandOpts, output: string) {
const prefix = formatNestedLogPrefix(opts);
for (const line of output.split(/\r?\n/)) {
if (!line) continue;
runtime.log(`${prefix} ${line}`);
}
}
export async function deliverAgentCommandResult(params: {
cfg: ClawdbotConfig;
deps: CliDeps;
@@ -112,7 +135,12 @@ export async function deliverAgentCommandResult(params: {
const logPayload = (payload: NormalizedOutboundPayload) => {
if (opts.json) return;
const output = formatOutboundPayloadLog(payload);
if (output) runtime.log(output);
if (!output) return;
if (opts.lane === AGENT_LANE_NESTED) {
logNestedOutput(runtime, opts, output);
return;
}
runtime.log(output);
};
if (!deliver) {
for (const payload of deliveryPayloads) logPayload(payload);