fix(quota): exclude agents from quota rules and usage records (#325)

* fix(quota): exclude agents from quota rules and usage records

* refactor(quota): remove agent_skill_call_count resource type and related logic

* chore(db): replace manual migration with prisma-generated migration
This commit is contained in:
Yiling-J
2026-08-18 11:56:03 +08:00
committed by GitHub
parent f806272c06
commit 8a75d015a1
22 changed files with 127 additions and 318 deletions
+97 -14
View File
@@ -209,26 +209,26 @@ describe('QuotaService', () => {
data: { name: 'Bob', email: 'bob@example.com', password: 'pw' },
})
// Create rule: each member with role editor gets 5 skill calls per 1hour
// Create rule: each member with role editor gets 5 MCP calls per 1hour
await quotaService.createRule(team.id, {
scopeMode: 'each_member',
role: 'editor',
resource: 'agent_skill_call_count',
resourceData: { id: 'skill_1' },
resource: 'agent_mcp_call_count',
resourceData: { id: 'server_1' },
limit: 5,
period: '1hour',
enabled: true,
})
// Alice consumes 5 skill calls
// Alice consumes 5 MCP calls
for (let i = 0; i < 5; i++) {
await quotaService.consumeQuota(
{
teamId: team.id,
userId: userAlice.id,
role: 'editor',
resource: 'agent_skill_call_count',
resourceData: { id: 'skill_1' },
resource: 'agent_mcp_call_count',
resourceData: { id: 'server_1' },
},
1,
)
@@ -241,8 +241,8 @@ describe('QuotaService', () => {
teamId: team.id,
userId: userAlice.id,
role: 'editor',
resource: 'agent_skill_call_count',
resourceData: { id: 'skill_1' },
resource: 'agent_mcp_call_count',
resourceData: { id: 'server_1' },
},
1,
),
@@ -255,8 +255,8 @@ describe('QuotaService', () => {
teamId: team.id,
userId: userBob.id,
role: 'editor',
resource: 'agent_skill_call_count',
resourceData: { id: 'skill_1' },
resource: 'agent_mcp_call_count',
resourceData: { id: 'server_1' },
},
1,
),
@@ -268,8 +268,8 @@ describe('QuotaService', () => {
teamId: team.id,
userId: userBob.id,
role: 'reviewer',
resource: 'agent_skill_call_count',
resourceData: { id: 'skill_1' },
resource: 'agent_mcp_call_count',
resourceData: { id: 'server_1' },
},
1,
)
@@ -670,10 +670,10 @@ describe('QuotaService', () => {
period: '1hour',
})
// Updating resource to agent_skill_call_count without resourceData.id should fail validation
// Updating resource to agent_mcp_call_count without resourceData.id should fail validation
await expect(
quotaService.updateRule(team.id, rule.id, {
resource: 'agent_skill_call_count',
resource: 'agent_mcp_call_count',
}),
).rejects.toThrow()
@@ -766,4 +766,87 @@ describe('QuotaService', () => {
expect(records).toHaveLength(1)
expect(records[0].consumed).toBe(150)
})
it('excludes agents from quota usage records and prevents creating quota rules for agents', async () => {
const team = await prisma.team.create({ data: { name: 'Exclude Agent Team' } })
// Create human user
const humanUser = await prisma.user.create({
data: {
name: 'Human Member',
email: 'human.member@example.com',
type: 'human',
password: 'pw',
},
})
await prisma.teamMember.create({
data: { teamId: team.id, userId: humanUser.id, role: 'reviewer' },
})
// Create agent user and agent
const agentUser = await prisma.user.create({
data: {
name: 'AI Agent Member',
email: 'ai.agent@shumai.ai',
type: 'agent',
},
})
await prisma.agent.create({
data: {
id: agentUser.id,
teamId: team.id,
type: 'chat',
config: { provider: 'google', model: 'gemini' },
},
})
await prisma.teamMember.create({
data: { teamId: team.id, userId: agentUser.id, role: 'reviewer' },
})
// 1. Quota rule with each_member
const eachMemberRule = await quotaService.createRule(team.id, {
scopeMode: 'each_member',
resource: 'agent_total_tokens',
limit: 10000,
period: '1hour',
enabled: true,
})
// listRuleRecords should ONLY return human members, NOT agents
const recordsResult = await quotaService.listRuleRecords(team.id, eachMemberRule.id)
expect(recordsResult.total).toBe(1)
expect(recordsResult.records).toHaveLength(1)
expect(recordsResult.records[0].userId).toBe(humanUser.id)
expect(recordsResult.records[0].user?.email).toBe('human.member@example.com')
// 2. Quota rule with selected_members targeting agentUser should be rejected
await expect(
quotaService.createRule(team.id, {
scopeMode: 'selected_members',
userIds: [agentUser.id],
resource: 'agent_total_tokens',
limit: 5000,
period: '1day',
}),
).rejects.toThrow('One or more selected users are not members of the team')
// 3. Updating quota rule with selected_members targeting agentUser should be rejected
const validSelectedRule = await quotaService.createRule(team.id, {
scopeMode: 'selected_members',
userIds: [humanUser.id],
resource: 'agent_total_tokens',
limit: 5000,
period: '1day',
})
await expect(
quotaService.updateRule(team.id, validSelectedRule.id, {
userIds: [agentUser.id],
}),
).rejects.toThrow('One or more selected users are not members of the team')
const selectedRecords = await quotaService.listRuleRecords(team.id, validSelectedRule.id)
expect(selectedRecords.total).toBe(1)
expect(selectedRecords.records[0].userId).toBe(humanUser.id)
})
})
+9 -16
View File
@@ -16,7 +16,6 @@ import {
type UpdateQuotaRuleRequest,
normalizeQuotaPeriod,
formatQuotaPeriod,
skillResourceDataSchema,
mcpResourceDataSchema,
bashResourceDataSchema,
toolResourceDataSchema,
@@ -121,11 +120,7 @@ function matchesQuotaRule(rule: QuotaRule, event: QuotaEvent): boolean {
const resourceData = (rule.resourceData as Record<string, unknown> | null) ?? null
// Resource data check
if (rule.resource === 'agent_skill_call_count') {
const targetId = resourceData?.id ?? resourceData?.skillId
const eventSkillId = event.resourceData?.skillId ?? event.resourceData?.id
if (targetId && eventSkillId !== targetId) return false
} else if (rule.resource === 'agent_mcp_call_count') {
if (rule.resource === 'agent_mcp_call_count') {
const targetId = resourceData?.id ?? resourceData?.mcpServerId
const eventMcpId = event.resourceData?.mcpServerId ?? event.resourceData?.id
if (targetId && eventMcpId !== targetId) return false
@@ -198,6 +193,7 @@ export class QuotaService {
where: {
teamId,
userId: { in: req.userIds },
user: { type: 'human' },
},
select: { userId: true },
})
@@ -274,6 +270,7 @@ export class QuotaService {
where: {
teamId,
userId: { in: effectiveUserIds },
user: { type: 'human' },
},
select: { userId: true },
})
@@ -284,14 +281,7 @@ export class QuotaService {
}
}
if (effectiveResource === 'agent_skill_call_count') {
const res = skillResourceDataSchema.safeParse(effectiveResourceData)
if (!res.success) {
throw new HTTPException(400, {
message: 'resourceData.id is required for agent_skill_call_count',
})
}
} else if (effectiveResource === 'agent_mcp_call_count') {
if (effectiveResource === 'agent_mcp_call_count') {
const res = mcpResourceDataSchema.safeParse(effectiveResourceData)
if (!res.success) {
throw new HTTPException(400, {
@@ -481,13 +471,16 @@ export class QuotaService {
const userIds = Array.isArray(rule.userIds) ? (rule.userIds as string[]) : []
if (userIds.length > 0) {
targetUsers = await this.prismaClient.user.findMany({
where: { id: { in: userIds } },
where: { id: { in: userIds }, type: 'human' },
select: { id: true, name: true, email: true, image: true },
})
}
} else {
// each_member
const memberWhere: Prisma.TeamMemberWhereInput = { teamId }
const memberWhere: Prisma.TeamMemberWhereInput = {
teamId,
user: { type: 'human' },
}
if (rule.role) {
memberWhere.role = rule.role
}