Files
ai_academy/internal/agent/approval/grants.go
T
runny 9058149576
Build & Publish (dsh-academy) / End-to-end inside dsh (latest, advisory) (push) Skipped
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Frontend / Docker multi-stage image (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-academy) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-academy) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-academy) / Publish to npm (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Helm / chart (push) Canceled after 0s
refactor: 项目更名 WeKnora → Academy
- 全量替换代码/配置/文档中的项目名(保留上游 GitHub 链接、weixin 文档域名、Docker Hub 镜像名)
- 重命名 32 个含项目名的文件/目录;Go module 路径改为 academy(含 client/cli 子模块)
- docreader proto 描述符采用等长替换,保持序列化字节布局有效
- Dockerfile.app 构建网络修复: browserskill 离线产物通道、PyPI/apt 国内镜像
- 运行验证: go build ✓ docker compose up ✓ 健康检查 ✓
2026-11-13 20:52:51 +08:00

125 lines
4.1 KiB
Go

package approval
import (
"context"
"encoding/json"
"time"
"academy/internal/agent/policy"
"academy/internal/logger"
"academy/internal/types"
)
// GrantStore keeps the approvals a user allowed to be remembered.
type GrantStore interface {
FindGrant(ctx context.Context, tenantID uint64, userID string, scope types.GrantScope, key string) (
*types.AgentGrant, error)
SaveGrant(ctx context.Context, grant *types.AgentGrant) error
TouchGrant(ctx context.Context, id string, at time.Time) error
}
// WithGrants makes the gate remember "allow for the rest" approvals in
// store. Without one, every call asks.
func (g *Gate) WithGrants(store GrantStore) *Gate {
g.grants = store
return g
}
// grantScope is where grants of a run on ctx live: the scope the run set,
// else the session of the call.
func grantScope(ctx context.Context, sessionID string) (types.GrantScope, bool) {
if scope, ok := types.GrantScopeFromContext(ctx); ok {
return scope, true
}
if sessionID == "" {
return types.GrantScope{}, false
}
return types.GrantScope{Scope: types.AgentGrantScopeSession, ScopeID: sessionID}, true
}
// granted reports whether the user already allowed calls with req's grant,
// in the run's own scope or its standing one. When they had but it no
// longer applies, lapse says why; a revoked grant is not reported, since the
// user withdrew it. Any lookup failure asks again.
func (g *Gate) granted(ctx context.Context, req PendingRequest) (ok bool, lapse string) {
if g.grants == nil || req.Grant == "" || req.UserID == "" || req.TenantID == 0 {
return false, ""
}
var scopes []types.GrantScope
if scope, ok := grantScope(ctx, req.SessionID); ok {
scopes = append(scopes, scope)
}
if scope, ok := types.StandingGrantScopeFromContext(ctx); ok {
scopes = append(scopes, scope)
}
for _, scope := range scopes {
ok, why := g.grantedIn(ctx, req, scope)
if ok {
return true, ""
}
if lapse == "" {
lapse = why
}
}
return false, lapse
}
func (g *Gate) grantedIn(ctx context.Context, req PendingRequest, scope types.GrantScope) (bool, string) {
grant, err := g.grants.FindGrant(ctx, req.TenantID, req.UserID, scope, req.Grant)
if err != nil {
logger.Warnf(ctx, "tool approval: read grant: %v", err)
return false, ""
}
if grant == nil {
return false, ""
}
if !policy.Covers(policy.Risk(grant.Risk), req.grantRisk()) {
logger.Infof(ctx, "tool approval: grant %s was approved for %s, not %s; asking",
grant.ID, grant.Risk, req.grantRisk())
return false, ""
}
now := time.Now()
if lapse := grant.LapseAt(now, scope.BindingHash); lapse != "" {
if lapse == types.AgentGrantLapseRevoked {
return false, ""
}
logger.Infof(ctx, "tool approval: grant %s %s; asking again", grant.ID, lapse)
return false, lapse
}
if err := g.grants.TouchGrant(ctx, grant.ID, now); err != nil {
logger.Warnf(ctx, "tool approval: record grant use: %v", err)
}
if req.grantRisk() == policy.RiskDestructive {
logger.GetLogger(ctx).WithFields(logger.Fields{
"agent_action": req.Grant, "agent_grant_id": grant.ID, "agent_granted_destructive": true,
"agent_acting_via": types.ActingViaFromContext(ctx),
}).Info("agent policy decision")
}
return true, ""
}
// remember stores the grant an approved prompt allowed for the rest of the
// run's scope.
func (g *Gate) remember(ctx context.Context, d Decision, prompt *types.AgentPrompt) {
if g.grants == nil || prompt == nil || !d.Approved || !d.AllowInSession {
return
}
var request types.ToolApprovalPromptRequest
if json.Unmarshal(prompt.Request, &request) != nil || request.Grant == "" {
return
}
scope, ok := grantScope(ctx, prompt.SessionID)
if !ok || prompt.TenantID == 0 || prompt.UserID == "" {
return
}
grant := &types.AgentGrant{
TenantID: prompt.TenantID, UserID: prompt.UserID,
Scope: scope.Scope, ScopeID: scope.ScopeID, GrantKey: request.Grant,
Label: request.GrantScope, BindingHash: scope.BindingHash, PromptID: prompt.ID,
Risk: string(policy.RiskWrite),
}
if err := g.grants.SaveGrant(context.WithoutCancel(ctx), grant); err != nil {
logger.Warnf(ctx, "tool approval: remember grant: %v", err)
}
}