File size: 6,845 Bytes
f0634fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | import { mkdtemp, readFile, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'pathe';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { LifecycleScope } from '#/app/scopes';
import {
ScopeActivation,
_clearScopedRegistryForTests,
registerScopedService,
} from '#/_base/di/scope';
import { createScopedTestHost } from '#/_base/di/test';
import type { FileLogWriter } from '#/_base/log/fileLog';
import { ILogService } from '#/_base/log/log';
import {
logSeed,
resolveLoggingConfig,
resolveSessionLogPath,
} from '#/_base/log/logConfig';
import { AppLogService, drainLogCloses } from '#/_base/log/logService';
import { SessionLogService } from '#/session/sessionLog/sessionLogService';
import { makeSessionContext, sessionContextSeed } from '#/session/sessionContext/sessionContext';
import { ISessionStateService } from '#/session/state/sessionState';
import { SessionStateService } from '#/session/state/sessionStateService';
import { IWorkspaceStateService } from '#/workspace/state/workspaceState';
import { WorkspaceStateService } from '#/workspace/state/workspaceStateService';
let homeDir: string;
let sessionDir: string;
beforeEach(async () => {
_clearScopedRegistryForTests();
registerScopedService(
LifecycleScope.Session,
ISessionStateService,
SessionStateService,
ScopeActivation.OnScopeCreated,
'state',
);
registerScopedService(
LifecycleScope.Session,
ILogService,
SessionLogService,
ScopeActivation.OnDemand,
'log',
);
homeDir = await mkdtemp(join(tmpdir(), 'session-log-'));
sessionDir = join(homeDir, 'sessions', 's1');
});
afterEach(async () => {
await rm(homeDir, { recursive: true, force: true });
});
function buildHost() {
const cfg = resolveLoggingConfig({
homeDir,
env: { KIMI_LOG_LEVEL: 'debug', KIMI_LOG_SESSION_MAX_BYTES: '1024', KIMI_LOG_SESSION_FILES: '2' },
});
return createScopedTestHost(logSeed(cfg));
}
function testSessionSeed() {
return [
...sessionContextSeed(makeSessionContext({
sessionId: 's1',
workspaceId: 'test-workspace',
sessionDir,
sessionScope: 'sessions/test-workspace/s1',
metaScope: 'sessions/test-workspace/s1/session-meta',
cwd: sessionDir,
})),
[IWorkspaceStateService, new WorkspaceStateService()] as const,
];
}
async function readSessionLog(): Promise<string> {
try {
return await readFile(resolveSessionLogPath(sessionDir), 'utf-8');
} catch {
return '';
}
}
describe('SessionLogService', () => {
it('writes entries to the per-session log file', async () => {
const host = buildHost();
const session = host.child(LifecycleScope.Session, 's1', testSessionSeed());
const log = session.accessor.get(ILogService);
log.info('session event', { requestId: 'r1' });
await log.flush();
const text = await readSessionLog();
expect(text).toContain('session event');
expect(text).toContain('requestId=r1');
host.dispose();
});
it('omits sessionId from per-session lines', async () => {
const host = buildHost();
const session = host.child(LifecycleScope.Session, 's1', testSessionSeed());
const log = session.accessor.get(ILogService);
log.info('evt');
await log.flush();
const text = await readSessionLog();
expect(text).not.toContain('sessionId');
host.dispose();
});
it('child logger accumulates context and writes to the same file', async () => {
const host = buildHost();
const session = host.child(LifecycleScope.Session, 's1', testSessionSeed());
const log = session.accessor.get(ILogService);
log.child({ agentId: 'main' }).warn('child event');
await log.flush();
const text = await readSessionLog();
expect(text).toContain('child event');
expect(text).toContain('agentId=main');
host.dispose();
});
it('close flushes and a subsequent write is dropped', async () => {
const host = buildHost();
const session = host.child(LifecycleScope.Session, 's1', testSessionSeed());
const log = session.accessor.get(ILogService) as SessionLogService;
log.info('before-close');
await log.close();
log.info('after-close');
const text = await readSessionLog();
expect(text).toContain('before-close');
expect(text).not.toContain('after-close');
host.dispose();
});
it('dispose flushes pending entries synchronously', () => {
const host = buildHost();
const session = host.child(LifecycleScope.Session, 's1', testSessionSeed());
const log = session.accessor.get(ILogService);
log.info('on-dispose');
host.dispose();
return readSessionLog().then((text) => {
expect(text).toContain('on-dispose');
});
});
it('dispose tracks the sink close so drainLogCloses waits for it', async () => {
const host = buildHost();
const session = host.child(LifecycleScope.Session, 's1', testSessionSeed());
const log = session.accessor.get(ILogService) as SessionLogService;
const sink = (log as unknown as { sink: FileLogWriter }).sink;
const originalClose = sink.close.bind(sink);
let releaseClose!: () => void;
const closeGate = new Promise<void>((resolve) => {
releaseClose = resolve;
});
sink.close = () => originalClose().then(() => closeGate);
log.info('drain-me');
host.dispose();
let drained = false;
const draining = drainLogCloses().then(() => {
drained = true;
});
await new Promise<void>((resolve) => setImmediate(resolve));
expect(drained).toBe(false);
releaseClose();
await draining;
expect(drained).toBe(true);
});
});
describe('ILogService cross-scope resolution', () => {
beforeEach(() => {
_clearScopedRegistryForTests();
registerScopedService(LifecycleScope.Session, ISessionStateService, SessionStateService, ScopeActivation.OnScopeCreated, 'state');
registerScopedService(LifecycleScope.App, ILogService, AppLogService, ScopeActivation.OnDemand, 'log');
registerScopedService(LifecycleScope.Session, ILogService, SessionLogService, ScopeActivation.OnDemand, 'log');
});
it('resolves the single token to the nearest scope binding', () => {
const host = buildHost();
const session = host.child(LifecycleScope.Session, 's1', testSessionSeed());
const agent = host.childOf(session, LifecycleScope.Agent, 'main');
const appLog = host.app.accessor.get(ILogService);
const sessionLog = session.accessor.get(ILogService);
const agentLog = agent.accessor.get(ILogService);
expect(appLog).toBeInstanceOf(AppLogService);
expect(sessionLog).toBeInstanceOf(SessionLogService);
expect(agentLog).toBeInstanceOf(SessionLogService);
expect(appLog).not.toBe(sessionLog);
expect(agentLog).toBe(sessionLog);
host.dispose();
});
});
|