chore: 初始化仓库,导入项目基线代码
Academy v0.8.3 完整代码基线:Go 后端(internal/cmd/cli/mcp-server/docreader)、 Vue 前端(frontend)、数据库迁移(migrations)、部署编排(docker/helm/deploy)与文档。 今日开发的培训推送管理改动随后单独提交。
This commit is contained in:
commit
7ec21f35b0
5767 files changed
+1436788
No files matched your search
@@ -0,0 +1,113 @@
|
||||
// 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
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package policy
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"academy/internal/types"
|
||||
)
|
||||
|
||||
const (
|
||||
readOnly = types.AssistantAccessReadOnly
|
||||
confirm = types.AssistantAccessConfirm
|
||||
autoWrite = types.AssistantAccessAutoWrite
|
||||
)
|
||||
|
||||
func TestDecide(t *testing.T) {
|
||||
want := map[Risk]map[types.AssistantAccessLevel]Verdict{
|
||||
RiskRead: {readOnly: Allow, confirm: Allow, autoWrite: Allow},
|
||||
RiskProbe: {readOnly: Allow, confirm: Allow, autoWrite: Allow},
|
||||
RiskWrite: {readOnly: Deny, confirm: Ask, autoWrite: Allow},
|
||||
RiskDestructive: {readOnly: Deny, confirm: Ask, autoWrite: Ask},
|
||||
}
|
||||
for risk, byLevel := range want {
|
||||
for level, verdict := range byLevel {
|
||||
assert.Equal(t, verdict, Decide(Action{Risk: risk}, Rules{Level: level}), "%s at %s", risk, level)
|
||||
asked := Ask
|
||||
if verdict == Deny {
|
||||
asked = Deny
|
||||
}
|
||||
assert.Equal(t, asked, Decide(Action{Risk: risk, Always: true}, Rules{Level: level}),
|
||||
"always %s at %s", risk, level)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecideTreatsAnUnknownRiskAsDestructive(t *testing.T) {
|
||||
assert.Equal(t, Ask, Decide(Action{Risk: "unknown"}, Rules{Level: autoWrite}))
|
||||
assert.Equal(t, Deny, Decide(Action{Risk: "unknown"}, Rules{Level: readOnly}))
|
||||
}
|
||||
|
||||
func TestRiskAbove(t *testing.T) {
|
||||
assert.True(t, RiskWrite.Above(RiskRead))
|
||||
assert.True(t, RiskRead.Above(""))
|
||||
assert.False(t, RiskRead.Above(RiskWrite))
|
||||
assert.False(t, RiskWrite.Above(RiskWrite))
|
||||
}
|
||||
|
||||
func TestOffered(t *testing.T) {
|
||||
assert.True(t, Offered(readOnly, RiskRead))
|
||||
assert.True(t, Offered(readOnly, RiskProbe))
|
||||
assert.False(t, Offered(readOnly, RiskWrite))
|
||||
assert.False(t, Offered(readOnly, RiskDestructive))
|
||||
assert.True(t, Offered(confirm, RiskDestructive))
|
||||
}
|
||||
|
||||
func TestPregrantableAndCovers(t *testing.T) {
|
||||
assert.True(t, Pregrantable(Action{Risk: RiskWrite}))
|
||||
assert.True(t, Pregrantable(Action{Risk: RiskDestructive}))
|
||||
assert.False(t, Pregrantable(Action{Risk: RiskDestructive, Always: true}))
|
||||
assert.False(t, Pregrantable(Action{Risk: RiskRead}))
|
||||
assert.False(t, Grantable(Action{Risk: RiskDestructive}), "a run's card never remembers a destructive call")
|
||||
|
||||
assert.True(t, Covers(RiskDestructive, RiskWrite))
|
||||
assert.True(t, Covers(RiskWrite, RiskWrite))
|
||||
assert.False(t, Covers(RiskWrite, RiskDestructive))
|
||||
assert.False(t, Covers("", RiskWrite))
|
||||
assert.False(t, Covers(RiskDestructive, "bogus"))
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package policy
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"academy/internal/logger"
|
||||
"academy/internal/types"
|
||||
)
|
||||
|
||||
// String names v for logs.
|
||||
func (v Verdict) String() string {
|
||||
switch v {
|
||||
case Allow:
|
||||
return "allow"
|
||||
case Ask:
|
||||
return "ask"
|
||||
case Deny:
|
||||
return "deny"
|
||||
default:
|
||||
return "unknown"
|
||||
}
|
||||
}
|
||||
|
||||
// Record logs a decision on anything but a read, so what an agent changed
|
||||
// without asking, and on whose behalf, can be traced later. key names the
|
||||
// action, e.g. an operation ID or "mcp:<service>/<tool>".
|
||||
func Record(ctx context.Context, key string, a Action, r Rules, v Verdict) {
|
||||
if a.Risk == RiskRead {
|
||||
return
|
||||
}
|
||||
fields := logger.Fields{
|
||||
"agent_action": key,
|
||||
"agent_risk": string(a.Risk),
|
||||
"agent_always": a.Always,
|
||||
"agent_level": string(r.Level),
|
||||
"agent_mode": string(types.RunModeFromContext(ctx)),
|
||||
"agent_verdict": v.String(),
|
||||
}
|
||||
if via := types.ActingViaFromContext(ctx); via != "" {
|
||||
fields["agent_acting_via"] = via
|
||||
}
|
||||
logger.GetLogger(ctx).WithFields(fields).Info("agent policy decision")
|
||||
}
|
||||
Reference in new issue
Block a user