Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/session-log-routing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@moonshot-ai/agent-core": patch
"@moonshot-ai/kimi-code": patch
---

Route session-tagged log entries exclusively to the session sink instead of duplicating them to the global sink. Consistently omit stable main-agent context keys from all session log lines that carry `agentId=main`.
12 changes: 5 additions & 7 deletions packages/agent-core/src/logging/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,21 +122,19 @@ class RootLoggerImpl implements RootLogger {
if (config === undefined || config.level === 'off') return;
if (!levelEnabled(config.level, entry.level)) return;

const formatted = formatEntry(entry);
if (formatted.dropped) return;

this.globalSink?.enqueue(formatted.text + '\n');

const session = this.resolveSessionEntry(entry);
if (session !== undefined) {
const omitContextKeys =
entry.msg === 'llm request' ? llmRequestSessionLogOmittedKeys(entry) : undefined;
const omitContextKeys = llmRequestSessionLogOmittedKeys(entry);
const sessionFormatted = formatEntry(entry, {
omitContextKeys,
});
if (!sessionFormatted.dropped) {
session.sink.enqueue(sessionFormatted.text + '\n');
}
} else {
const formatted = formatEntry(entry);
if (formatted.dropped) return;
this.globalSink?.enqueue(formatted.text + '\n');
}
}

Expand Down
17 changes: 9 additions & 8 deletions packages/agent-core/test/logging/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ describe('createChild', () => {
});

describe('session routing', () => {
it('writes sessionId-tagged entries to both global and session sink', async () => {
it('writes sessionId-tagged entries to session sink only', async () => {
const sessionDir = await mkdtemp(join(tmpdir(), 'logger-session-'));
try {
await getRootLogger().configure(defaultConfig());
Expand All @@ -248,15 +248,15 @@ describe('session routing', () => {
await getRootLogger().flush();
const global = await readGlobal();
const session = await readFile(join(sessionDir, 'logs', 'kimi-code.log'), 'utf-8');
expect(global).toContain('hello');
expect(global).not.toContain('hello');
expect(session).toContain('hello');
await handle.close();
} finally {
await rm(sessionDir, { recursive: true, force: true });
}
});

it('prints sessionId once on llm config but omits stable main-agent fields from session llm request lines', async () => {
it('omits stable main-agent fields from all session lines with agentId=main', async () => {
const sessionDir = await mkdtemp(join(tmpdir(), 'logger-session-'));
try {
await getRootLogger().configure(defaultConfig());
Expand All @@ -268,11 +268,12 @@ describe('session routing', () => {
await getRootLogger().flush();

const global = await readGlobal();
expect(global).toMatch(/llm config.*sessionId=ses_abc/);
expect(global).toMatch(/llm request.*sessionId=ses_abc/);
expect(global).not.toMatch(/llm config/);
expect(global).not.toMatch(/llm request/);

const session = await readFile(join(sessionDir, 'logs', 'kimi-code.log'), 'utf-8');
expect(session).toMatch(/llm config.*sessionId=ses_abc/);
expect(session).toMatch(/llm config(?!.*sessionId=ses_abc)/);
expect(session).toMatch(/llm config(?!.*agentId=main)/);
expect(session).toMatch(/llm request(?!.*sessionId=ses_abc)/);
expect(session).toMatch(/llm request(?!.*agentId=main)/);
await handle.close();
Expand Down Expand Up @@ -347,8 +348,8 @@ describe('session routing', () => {
expect(secondText).not.toContain('ambiguous session id');

const global = await readGlobal();
expect(global).toContain('first only');
expect(global).toContain('second only');
expect(global).not.toContain('first only');
expect(global).not.toContain('second only');
expect(global).toContain('ambiguous session id');

await first.close();
Expand Down
8 changes: 5 additions & 3 deletions packages/node-sdk/test/local-logging.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function readZipEntries(buf: Buffer): Map<string, Buffer> {
}

describe('Local logging — harness integration', () => {
it('writes session-tagged entries to both global and session log files', async () => {
it('writes session-tagged entries to session log only and untagged entries to global', async () => {
const homeDir = await makeTempDir('kimi-log-home-');
const workDir = await makeTempDir('kimi-log-work-');

Expand Down Expand Up @@ -131,7 +131,7 @@ describe('Local logging — harness integration', () => {

const global = await readFile(globalPath, 'utf-8');
const sessionLog = await readFile(sessionLogPath, 'utf-8');
expect(global).toContain('session diagnostic');
expect(global).not.toContain('session diagnostic');
expect(sessionLog).toContain('session diagnostic');
expect(global).toContain('untagged event');
expect(sessionLog).not.toContain('untagged event');
Expand Down Expand Up @@ -284,6 +284,7 @@ describe('Local logging — harness integration', () => {
const harness = new KimiHarness({ identity: TEST_IDENTITY, homeDir });
const session = await harness.createSession({ id: 'ses_flush_warning', workDir });
log.warn('flush warning setup', { sessionId: session.id });
log.warn('global untagged marker');

const root = getRootLogger();
const flushSessionSpy = vi
Expand All @@ -305,7 +306,8 @@ describe('Local logging — harness integration', () => {
const globalLog = entries.get('logs/global/kimi-code.log')!.toString('utf-8');
expect(sessionLog).toContain('export session log flush failed');
expect(sessionLog).toContain('export global log flush failed');
expect(globalLog).toContain('export global log flush failed');
expect(globalLog).toContain('global untagged marker');
expect(globalLog).not.toContain('export global log flush failed');
} finally {
flushSessionSpy.mockRestore();
flushGlobalSpy.mockRestore();
Expand Down
Loading