Skip to content
Closed
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
2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"prepack": "npm run build",
"typecheck": "tsc --noEmit",
"test": "jest",
"test:core": "jest --runInBand --forceExit src/agent/communication/__tests__/agentMessageBus.test.ts src/agent/core/executors/__tests__/strategyExecutor.test.ts src/agent/core/executors/__tests__/hypothesisExecutor.test.ts src/agent/context/__tests__/enhancedSessionContext.test.ts src/tests/adbTools.test.ts src/services/__tests__/sessionLogger.test.ts src/services/__tests__/traceAnalysisSkillConfig.test.ts src/agent/agents/domain/__tests__/registry.test.ts src/agentv3/__tests__/sqlIncludeInjector.test.ts src/agentv3/__tests__/analysisPatternMemory.test.ts src/agentv3/__tests__/claudeRuntimeRuntimeSnapshots.test.ts src/middleware/__tests__/auth.test.ts src/services/__tests__/rbac.test.ts src/routes/__tests__/agentRoutesRbac.test.ts src/routes/__tests__/ownerGuardRoutes.test.ts src/routes/__tests__/requestContextRouteCoverage.test.ts src/middleware/__tests__/legacyApiCompatibility.test.ts src/services/__tests__/enterpriseDb.test.ts src/services/__tests__/enterpriseSchema.test.ts src/services/__tests__/enterpriseRepository.test.ts src/services/__tests__/processRss.test.ts src/services/__tests__/workingTraceProcessor.enterpriseIsolation.test.ts src/services/__tests__/traceProcessorLeaseStore.test.ts src/services/__tests__/traceProcessorLeaseModeDecision.test.ts src/services/__tests__/traceProcessorLeaseProcessorRouting.test.ts src/services/__tests__/traceProcessorSqlWorker.test.ts src/services/__tests__/traceProcessorRamBudget.test.ts src/scripts/__tests__/benchmarkTraceProcessorRss.test.ts src/services/__tests__/enterpriseKnowledgeScope.test.ts src/services/__tests__/enterpriseMigration.test.ts src/services/__tests__/runtimeSnapshotStore.test.ts src/services/providerManager/__tests__/localSecretStore.test.ts src/services/providerManager/__tests__/enterpriseProviderStore.test.ts src/routes/__tests__/enterpriseTraceMetadataRoutes.test.ts src/routes/__tests__/traceProcessorProxyRoutes.test.ts src/routes/__tests__/enterpriseReportRoutes.test.ts src/routes/__tests__/enterpriseRestartPersistence.test.ts",
"test:core": "jest --runInBand --forceExit src/agent/communication/__tests__/agentMessageBus.test.ts src/agent/core/executors/__tests__/strategyExecutor.test.ts src/agent/core/executors/__tests__/hypothesisExecutor.test.ts src/agent/context/__tests__/enhancedSessionContext.test.ts src/tests/adbTools.test.ts src/services/__tests__/sessionLogger.test.ts src/services/__tests__/traceAnalysisSkillConfig.test.ts src/agent/agents/domain/__tests__/registry.test.ts src/agentv3/__tests__/sqlIncludeInjector.test.ts src/agentv3/__tests__/analysisPatternMemory.test.ts src/agentv3/__tests__/claudeRuntimeRuntimeSnapshots.test.ts src/middleware/__tests__/auth.test.ts src/assistant/application/__tests__/assistantApplicationService.test.ts src/services/__tests__/rbac.test.ts src/routes/__tests__/agentRoutesRbac.test.ts src/routes/__tests__/ownerGuardRoutes.test.ts src/routes/__tests__/requestContextRouteCoverage.test.ts src/middleware/__tests__/legacyApiCompatibility.test.ts src/services/__tests__/enterpriseDb.test.ts src/services/__tests__/enterpriseSchema.test.ts src/services/__tests__/enterpriseRepository.test.ts src/services/__tests__/processRss.test.ts src/services/__tests__/workingTraceProcessor.enterpriseIsolation.test.ts src/services/__tests__/traceProcessorLeaseStore.test.ts src/services/__tests__/traceProcessorLeaseModeDecision.test.ts src/services/__tests__/traceProcessorLeaseProcessorRouting.test.ts src/services/__tests__/traceProcessorSqlWorker.test.ts src/services/__tests__/traceProcessorRamBudget.test.ts src/scripts/__tests__/benchmarkTraceProcessorRss.test.ts src/services/__tests__/analysisRunStore.test.ts src/services/__tests__/agentEventStore.test.ts src/services/__tests__/enterpriseKnowledgeScope.test.ts src/services/__tests__/enterpriseMigration.test.ts src/services/__tests__/runtimeSnapshotStore.test.ts src/services/providerManager/__tests__/localSecretStore.test.ts src/services/providerManager/__tests__/enterpriseProviderStore.test.ts src/routes/__tests__/enterpriseTraceMetadataRoutes.test.ts src/routes/__tests__/traceProcessorProxyRoutes.test.ts src/routes/__tests__/enterpriseReportRoutes.test.ts src/routes/__tests__/enterpriseRestartPersistence.test.ts",
"test:watch": "jest --watch",
"test:coverage": "jest --coverage",
"test:unit": "jest --testPathPatterns=src/tests",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2024-2026 Gracker (Chris)
// This file is part of SmartPerfetto. See LICENSE for details.

import { describe, expect, it, jest } from '@jest/globals';
import {
AssistantApplicationService,
type ManagedAssistantSession,
} from '../assistantApplicationService';

function session(overrides: Partial<ManagedAssistantSession> = {}): ManagedAssistantSession {
return {
sessionId: 'session-a',
status: 'running',
createdAt: 1_777_000_000_000,
lastActivityAt: 1_777_000_000_000,
sseClients: [],
...overrides,
};
}

describe('AssistantApplicationService cleanup', () => {
it('lets callers keep abandoned non-terminal sessions when an external run heartbeat is fresh', () => {
const service = new AssistantApplicationService<ManagedAssistantSession>();
const managed = session();
const onCleanup = jest.fn();
service.setSession(managed.sessionId, managed);

const removed = service.cleanupIdleSessions({
now: 1_777_000_010_000,
terminalMaxIdleMs: 1_000,
nonTerminalMaxIdleMs: 1_000,
shouldCleanup: (_sessionId, _session, context) => {
expect(context.isAbandonedNonTerminal).toBe(true);
return false;
},
onCleanup,
});

expect(removed).toEqual([]);
expect(onCleanup).not.toHaveBeenCalled();
expect(service.getSession(managed.sessionId)).toBe(managed);
});

it('still removes stale abandoned non-terminal sessions when the cleanup predicate allows it', () => {
const service = new AssistantApplicationService<ManagedAssistantSession>();
const managed = session();
service.setSession(managed.sessionId, managed);

const removed = service.cleanupIdleSessions({
now: 1_777_000_010_000,
terminalMaxIdleMs: 1_000,
nonTerminalMaxIdleMs: 1_000,
shouldCleanup: () => true,
});

expect(removed).toEqual([managed.sessionId]);
expect(service.getSession(managed.sessionId)).toBeUndefined();
});
});
18 changes: 18 additions & 0 deletions backend/src/assistant/application/assistantApplicationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ export interface SessionCleanupOptions<T extends ManagedAssistantSession> {
terminalMaxIdleMs: number;
nonTerminalMaxIdleMs: number;
now?: number;
shouldCleanup?: (
sessionId: string,
session: T,
context: {
now: number;
idleMs: number;
isTerminal: boolean;
isAbandonedNonTerminal: boolean;
},
) => boolean;
onCleanup?: (sessionId: string, session: T) => void;
}

Expand Down Expand Up @@ -99,6 +109,14 @@ export class AssistantApplicationService<T extends ManagedAssistantSession> {
(isTerminal && idle > options.terminalMaxIdleMs) ||
(isAbandonedNonTerminal && idle > options.nonTerminalMaxIdleMs)
) {
if (options.shouldCleanup?.(sessionId, session, {
now,
idleMs: idle,
isTerminal,
isAbandonedNonTerminal,
}) === false) {
continue;
}
options.onCleanup?.(sessionId, session);
this.sessions.delete(sessionId);
removed.push(sessionId);
Expand Down
114 changes: 114 additions & 0 deletions backend/src/routes/__tests__/agentRoutesRbac.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ import request from 'supertest';
import { ENTERPRISE_FEATURE_FLAG_ENV } from '../../config';
import { ENTERPRISE_DB_PATH_ENV } from '../../services/enterpriseDb';
import { ENTERPRISE_DATA_DIR_ENV, writeTraceMetadata } from '../../services/traceMetadataStore';
import {
persistSerializedAgentEvent,
resetAgentEventStoreForTests,
} from '../../services/agentEventStore';
import {
getAnalysisRunLifecycle,
resetAnalysisRunStoreForTests,
} from '../../services/analysisRunStore';
import {
getTraceProcessorLeaseStore,
setTraceProcessorLeaseStoreForTests,
Expand Down Expand Up @@ -64,6 +72,8 @@ afterEach(async () => {
jest.restoreAllMocks();
setTraceProcessorServiceForTests(null);
setTraceProcessorLeaseStoreForTests(null);
resetAgentEventStoreForTests();
resetAnalysisRunStoreForTests();
if (originalApiKey === undefined) {
delete process.env.SMARTPERFETTO_API_KEY;
} else {
Expand Down Expand Up @@ -222,4 +232,108 @@ describe('agent route RBAC', () => {
await fs.rm(tmpDir, { recursive: true, force: true });
}
});

it('replays persisted terminal SSE events before falling back to the in-memory buffer', async () => {
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'smartperfetto-agent-event-replay-'));
let leaseStore: ReturnType<typeof getTraceProcessorLeaseStore> | null = null;
try {
const traceId = 'trace-agent-event-replay';
const tracePath = path.join(tmpDir, `${traceId}.trace`);
await fs.writeFile(tracePath, 'trace bytes');
delete process.env.SMARTPERFETTO_API_KEY;
process.env.SMARTPERFETTO_SSO_TRUSTED_HEADERS = 'true';
process.env[ENTERPRISE_FEATURE_FLAG_ENV] = 'true';
process.env[ENTERPRISE_DB_PATH_ENV] = path.join(tmpDir, 'enterprise.sqlite');
process.env[ENTERPRISE_DATA_DIR_ENV] = path.join(tmpDir, 'data');
process.env.UPLOAD_DIR = path.join(tmpDir, 'uploads');

await writeTraceMetadata({
id: traceId,
filename: `${traceId}.trace`,
size: 11,
uploadedAt: new Date().toISOString(),
status: 'ready',
path: tracePath,
tenantId: 'tenant-a',
workspaceId: 'workspace-a',
userId: 'analyst-user',
});
setTraceProcessorServiceForTests({
getOrLoadTrace: jest.fn(async () => ({
id: traceId,
filename: `${traceId}.trace`,
size: 11,
filePath: tracePath,
uploadTime: new Date(),
status: 'ready',
})),
getTrace: jest.fn(() => ({
id: traceId,
filename: `${traceId}.trace`,
size: 11,
filePath: tracePath,
uploadTime: new Date(),
status: 'ready',
})),
ensureProcessorForLease: jest.fn(async () => undefined),
runWithLease: jest.fn(() => new Promise<unknown>(() => undefined)),
query: jest.fn(async () => ({ columns: [], rows: [], durationMs: 1 })),
} as any);

const analyzeRes = await analystHeaders(request(makeApp()).post('/api/agent/v1/analyze'))
.send({ traceId, query: 'analyze this trace' });

expect(analyzeRes.status).toBe(200);
const { sessionId, runId } = analyzeRes.body;
const persistedRun = getAnalysisRunLifecycle({
tenantId: 'tenant-a',
workspaceId: 'workspace-a',
userId: 'analyst-user',
}, runId);
expect(persistedRun).toEqual(expect.objectContaining({
id: runId,
status: 'running',
}));
expect(persistedRun?.heartbeatAt).toEqual(expect.any(Number));
persistSerializedAgentEvent({
tenantId: 'tenant-a',
workspaceId: 'workspace-a',
userId: 'analyst-user',
sessionId,
runId,
traceId,
query: 'analyze this trace',
}, {
cursor: 99,
eventType: 'analysis_completed',
eventData: JSON.stringify({
type: 'analysis_completed',
data: { reportUrl: '/api/reports/report-from-db' },
}),
createdAt: 1_777_000_002_000,
});

const streamRes = await analystHeaders(
request(makeApp())
.get(`/api/agent/v1/${sessionId}/stream`)
.set('Last-Event-ID', '98')
.set('Accept', 'text/event-stream'),
);

expect(streamRes.status).toBe(200);
expect(streamRes.text).toContain('id: 99');
expect(streamRes.text).toContain('event: analysis_completed');
expect(streamRes.text).toContain('/api/reports/report-from-db');
leaseStore = getTraceProcessorLeaseStore();
expect(leaseStore.listLeases({
tenantId: 'tenant-a',
workspaceId: 'workspace-a',
userId: 'analyst-user',
}, { traceId })).toHaveLength(1);
} finally {
leaseStore?.close();
setTraceProcessorLeaseStoreForTests(null);
await fs.rm(tmpDir, { recursive: true, force: true });
}
});
});
Loading
Loading