Skip to content

Commit e8f185a

Browse files
committed
pass in databaseAgentCache
1 parent d9ea580 commit e8f185a

File tree

7 files changed

+30
-16
lines changed

7 files changed

+30
-16
lines changed

backend/src/__tests__/agent-registry.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import {
1919
import {
2020
getAgentTemplate,
2121
assembleLocalAgentTemplates,
22-
clearDatabaseCache,
2322
} from '../templates/agent-registry'
2423

2524
import type { AgentTemplate } from '@codebuff/agent-runtime/templates/types'
@@ -147,10 +146,12 @@ describe('Agent Registry', () => {
147146
})
148147

149148
beforeEach(async () => {
150-
agentRuntimeImpl = { ...TEST_AGENT_RUNTIME_IMPL }
149+
agentRuntimeImpl = {
150+
...TEST_AGENT_RUNTIME_IMPL,
151+
}
152+
153+
agentRuntimeImpl.databaseAgentCache.clear()
151154

152-
// Clear cache before each test
153-
clearDatabaseCache()
154155
mockFileContext = getStubProjectFileContext()
155156

156157
// Spy on validation functions
@@ -520,8 +521,7 @@ describe('Agent Registry', () => {
520521
})
521522
expect(selectSpy).toHaveBeenCalledTimes(1)
522523

523-
// Clear cache
524-
clearDatabaseCache()
524+
agentRuntimeImpl.databaseAgentCache.clear()
525525

526526
// Third call - should hit database again after cache clear
527527
await getAgentTemplate({

backend/src/impl/agent-runtime.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { fetchAgentFromDatabase } from '../templates/agent-registry'
88
import { logger } from '../util/logger'
99
import { getUserInfoFromApiKey } from '../websockets/auth'
1010

11+
import type { AgentTemplate } from '@codebuff/agent-runtime/templates/types'
1112
import type { AgentRuntimeDeps } from '@codebuff/common/types/contracts/agent-runtime'
1213

1314
export const BACKEND_AGENT_RUNTIME_IMPL: AgentRuntimeDeps = Object.freeze({
@@ -17,6 +18,7 @@ export const BACKEND_AGENT_RUNTIME_IMPL: AgentRuntimeDeps = Object.freeze({
1718
startAgentRun,
1819
finishAgentRun,
1920
addAgentStep,
21+
databaseAgentCache: new Map<string, AgentTemplate | null>(),
2022

2123
// LLM
2224
promptAiSdkStream,

backend/src/templates/agent-registry.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ import type { ProjectFileContext } from '@codebuff/common/util/file'
1818

1919
export type AgentRegistry = Record<string, AgentTemplate>
2020

21-
// Global database cache - only state in the system
22-
const databaseAgentCache = new Map<string, AgentTemplate | null>()
23-
2421
/**
2522
* Fetch and validate an agent from the database by publisher/agent-id[@version] format
2623
*/
@@ -132,10 +129,16 @@ export async function getAgentTemplate(params: {
132129
agentId: string
133130
localAgentTemplates: Record<string, AgentTemplate>
134131
fetchAgentFromDatabase: FetchAgentFromDatabaseFn
132+
databaseAgentCache: Map<string, AgentTemplate | null>
135133
logger: Logger
136134
}): Promise<AgentTemplate | null> {
137-
const { agentId, localAgentTemplates, fetchAgentFromDatabase, logger } =
138-
params
135+
const {
136+
agentId,
137+
localAgentTemplates,
138+
fetchAgentFromDatabase,
139+
databaseAgentCache,
140+
logger,
141+
} = params
139142
// 1. Check localAgentTemplates first (dynamic agents + static templates)
140143
if (localAgentTemplates[agentId]) {
141144
return localAgentTemplates[agentId]
@@ -203,6 +206,10 @@ export function assembleLocalAgentTemplates(params: {
203206
/**
204207
* Clear the database agent cache (useful for testing)
205208
*/
206-
export function clearDatabaseCache(): void {
209+
export function clearDatabaseCache(params: {
210+
databaseAgentCache: Map<string, AgentTemplate | null>
211+
}): void {
212+
const { databaseAgentCache } = params
213+
207214
databaseAgentCache.clear()
208215
}

backend/src/websockets/middleware.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import type {
3232
AgentRuntimeDeps,
3333
AgentRuntimeScopedDeps,
3434
} from '@codebuff/common/types/contracts/agent-runtime'
35-
import type { GetUserInfoFromApiKeyFn } from '@codebuff/common/types/contracts/database'
3635
import type { Logger } from '@codebuff/common/types/contracts/logger'
3736
import type { WebSocket } from 'ws'
3837

common/src/testing/impl/agent-runtime.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import type { AgentTemplate } from '../../types/agent-template'
12
import type {
23
AgentRuntimeDeps,
34
AgentRuntimeScopedDeps,
4-
} from '@codebuff/common/types/contracts/agent-runtime'
5-
import type { Logger } from '@codebuff/common/types/contracts/logger'
5+
} from '../../types/contracts/agent-runtime'
6+
import type { Logger } from '../../types/contracts/logger'
67

78
export const testLogger: Logger = {
89
debug: () => {},
@@ -15,13 +16,14 @@ export const TEST_AGENT_RUNTIME_IMPL = Object.freeze<AgentRuntimeDeps>({
1516
// Database
1617
getUserInfoFromApiKey: async () => ({
1718
id: 'test-user-id',
18-
email: 'test-email',
19+
email: 'test-email',
1920
discord_id: 'test-discord-id',
2021
}),
2122
fetchAgentFromDatabase: async () => null,
2223
startAgentRun: async () => 'test-agent-run-id',
2324
finishAgentRun: async () => {},
2425
addAgentStep: async () => 'test-agent-step-id',
26+
databaseAgentCache: new Map<string, AgentTemplate | null>(),
2527

2628
// LLM
2729
promptAiSdkStream: async function* () {

common/src/types/contracts/agent-runtime.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { AgentTemplate } from '../agent-template'
12
import type {
23
HandleStepsLogChunkFn,
34
RequestFilesFn,
@@ -28,6 +29,7 @@ export type AgentRuntimeDeps = {
2829
startAgentRun: StartAgentRunFn
2930
finishAgentRun: FinishAgentRunFn
3031
addAgentStep: AddAgentStepFn
32+
databaseAgentCache: Map<string, AgentTemplate | null>
3133

3234
// LLM
3335
promptAiSdkStream: PromptAiSdkStreamFn

evals/impl/agent-runtime.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { AgentTemplate } from '@codebuff/common/types/agent-template'
12
import type { AgentRuntimeDeps } from '@codebuff/common/types/contracts/agent-runtime'
23

34
export const EVALS_AGENT_RUNTIME_IMPL = Object.freeze<AgentRuntimeDeps>({
@@ -11,6 +12,7 @@ export const EVALS_AGENT_RUNTIME_IMPL = Object.freeze<AgentRuntimeDeps>({
1112
startAgentRun: async () => 'test-agent-run-id',
1213
finishAgentRun: async () => {},
1314
addAgentStep: async () => 'test-agent-step-id',
15+
databaseAgentCache: new Map<string, AgentTemplate | null>(),
1416

1517
// LLM
1618
promptAiSdkStream: async function* () {

0 commit comments

Comments
 (0)