Files
ai_academy/internal/agent/policy/policy.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

114 lines
3.8 KiB
Go

// Package policy decides whether an agent may run an action without asking
// the user. It is the only place that maps an action's risk and the run's
// access level to allow, ask or deny; tools ask it instead of comparing
// levels themselves. What the agent can reach at all (registered tools,
// workspace roles, writable knowledge bases) is decided elsewhere and is not
// widened by anything here.
//
// See docs/design/agent-permission-model.md.
package policy
import "academy/internal/types"
// Risk classifies an action by its effect on user data.
type Risk string
const (
// RiskRead has no side effects.
RiskRead Risk = "read"
// RiskProbe changes no data but does real work to answer: it connects to
// an external service or runs a retrieval.
RiskProbe Risk = "probe"
// RiskWrite changes state in a recoverable way (reparse, status change).
RiskWrite Risk = "write"
// RiskDestructive removes data or cannot be undone.
RiskDestructive Risk = "destructive"
)
// Valid reports whether r is a known risk.
func (r Risk) Valid() bool {
return r == RiskRead || r == RiskProbe || r == RiskWrite || r == RiskDestructive
}
// Mutates reports whether the action changes data.
func (r Risk) Mutates() bool { return r == RiskWrite || r == RiskDestructive }
var riskRank = map[Risk]int{RiskRead: 1, RiskProbe: 2, RiskWrite: 3, RiskDestructive: 4}
// Above reports whether r is a higher risk than other. Every known risk is
// above an unknown or empty one.
func (r Risk) Above(other Risk) bool { return riskRank[r] > riskRank[other] }
// Action is one call the agent wants to make.
type Action struct {
Risk Risk
// Always asks every time, whatever the level: a skill installation, or an
// MCP tool the workspace marked as requiring approval.
Always bool
}
// Rules are what the run allows.
type Rules struct {
// Level is the run's effective access level.
Level types.AssistantAccessLevel
}
// Verdict is the outcome of Decide.
type Verdict int
const (
// Allow runs the action without asking.
Allow Verdict = iota
// Ask runs the action only after the user approves it.
Ask
// Deny refuses the action: the run may not make it even with approval.
Deny
)
// Decide returns whether a may run under r. Mutations a read-only run must
// not make are kept from the model by Offered; one that still arrives is
// denied.
func Decide(a Action, r Rules) Verdict {
if !Offered(r.Level, a.Risk) {
return Deny
}
if a.Always {
return Ask
}
switch a.Risk {
case RiskRead, RiskProbe:
return Allow
case RiskWrite:
if r.Level == types.AssistantAccessAutoWrite {
return Allow
}
return Ask
default:
return Ask
}
}
// Grantable reports whether approving a during a run may be remembered for
// later calls of the same kind: only recoverable changes the workspace does
// not require approving every time.
func Grantable(a Action) bool { return a.Risk == RiskWrite && !a.Always }
// Pregrantable reports whether a standing grant, approved ahead of time
// for a specific resource, may cover a. Unlike Grantable this includes
// destructive actions, which only an automation's settings may pre-approve;
// Always still asks every time.
func Pregrantable(a Action) bool {
return (a.Risk == RiskWrite || a.Risk == RiskDestructive) && !a.Always
}
// Covers reports whether a grant approved for risk granted applies to a call
// of risk r: a grant never covers a riskier call than it was approved for.
func Covers(granted, r Risk) bool { return r.Valid() && !r.Above(granted) }
// Offered reports whether an action of risk should be offered to the model
// at all under level. A read-only run is not shown changes it cannot make,
// nor anything whose risk is unknown.
func Offered(level types.AssistantAccessLevel, risk Risk) bool {
return level != types.AssistantAccessReadOnly || risk == RiskRead || risk == RiskProbe
}