feat(quota): add 2-level database-backed resource quota system (#318)

* feat(quota): add database-backed resource quota system

* feat(webui): add resource quota management in team settings (#319)

* feat(webui): add resource quota settings in team management

* refactor(webui): update quotas list to full row layout and restrict skill/mcp to select list

* feat(quota): refactor quota system to 2-level rules and records

* fix(webui): fix overflow and scroll area in quota usage records dialog

* fix(webui): ensure quota records scroll area has explicit max height

* feat(webui): improve quota cards interaction and restrict editable fields in edit mode

* fix(quota): serialize initial records, validate merged updates, and reset on dimension changes (#320)

* feat(quota): replace network quota with agent tools quota (#321)

* feat(webui): show remaining time until next refresh in quota usage records

* fix(webui): rename bash quota resource to Agent Bash Calls

* refactor(quota): remove unused reserved field from quota records

* refactor(quota): remove quota rule cache (#322)
This commit is contained in:
Yiling-J
2026-08-17 20:24:05 +08:00
committed by GitHub
parent dd82daf65c
commit eed0264b9c
200 changed files with 13118 additions and 50 deletions
+28 -1
View File
@@ -3,6 +3,7 @@ import { Type } from 'typebox'
import { type AgentTool } from '@earendil-works/pi-agent-core'
import { agentService } from '@shumai/core/src/agent/agent'
import { resolveEffectiveRole } from '@shumai/core/src/authz/authz'
import { quotaService } from '@shumai/core/src/quota/quota-service'
const readSkillSchema = Type.Object({
skillId: Type.String({ description: 'The ID of the skill to read' }),
@@ -41,11 +42,13 @@ export const createReadSkillTool = (
}
const requiredLevel = ROLE_HIERARCHY[skill.permission] || 1
const effectiveRole = userId
? await resolveEffectiveRole(skill.teamId, projectId, userId)
: null
if (userId) {
// Resolve the user's effective role for the project context (project
// override wins; project-scoped members without project access are denied).
const effectiveRole = await resolveEffectiveRole(skill.teamId, projectId, userId)
const userLevel = effectiveRole ? ROLE_HIERARCHY[effectiveRole] || 0 : 0
if (userLevel < requiredLevel) {
throw new Error(
@@ -58,6 +61,18 @@ export const createReadSkillTool = (
)
}
// Quota pre-check
await quotaService.checkQuota(
{
teamId: skill.teamId,
userId,
role: effectiveRole,
resource: 'agent_skill_call_count',
resourceData: { id: params.skillId, skillId: params.skillId },
},
1,
)
// Capture environment variables using agentService
const envs = await agentService.getSkillEnvs(params.skillId)
onEnvsAdded(envs)
@@ -68,6 +83,18 @@ export const createReadSkillTool = (
// Notify that a skill was successfully loaded (e.g. to enable restricted tools)
await onSkillLoaded?.()
// Consume quota
await quotaService.consumeQuota(
{
teamId: skill.teamId,
userId,
role: effectiveRole,
resource: 'agent_skill_call_count',
resourceData: { id: params.skillId, skillId: params.skillId },
},
1,
)
return {
content: [{ type: 'text', text: skillMdContent }],
details: { skillId: params.skillId },
@@ -10,6 +10,8 @@ import {
formatSize,
type TruncationResult,
} from '../utils/truncate'
import { quotaService } from '@shumai/core/src/quota/quota-service'
import type { TeamMemberRole } from '@shumai/db'
const BashSource = Type.String({
enum: ['user', 'skill'],
@@ -33,6 +35,9 @@ export interface BashDetails {
}
export interface SandboxedBashOptions {
teamId?: string
userId?: string
role?: TeamMemberRole | null
getBlockedHost?: () => string
clearBlockedHost?: () => void
/**
@@ -66,6 +71,18 @@ export const createSandboxedBashTool = (
'Only commands required by a loaded skill (source="skill") can be executed.',
)
}
if (options?.teamId) {
await quotaService.checkQuota(
{
teamId: options.teamId,
userId: options.userId,
role: options.role,
resource: 'agent_bash_call_count',
resourceData: { command },
},
1,
)
}
options?.clearBlockedHost?.()
const wrappedCommand = await SandboxManager.wrapWithSandbox(command)
@@ -219,6 +236,23 @@ export const createSandboxedBashTool = (
const snapshot = await finishOutput()
const { text: outputText, details: truncationDetails } = formatOutput(snapshot)
if (options?.teamId) {
try {
await quotaService.consumeQuota(
{
teamId: options.teamId,
userId: options.userId,
role: options.role,
resource: 'agent_bash_call_count',
resourceData: { command },
},
1,
)
} catch (err) {
logger.error({ err }, 'Failed to record quota usage for bash command')
}
}
if (signal?.aborted) {
logger.error({ command, outputText }, 'Sandboxed bash command aborted')
reject(new Error(appendStatus(outputText, 'Command aborted')))