Files
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

99 lines
4.2 KiB
Go

package approval
import (
"context"
"testing"
"github.com/stretchr/testify/require"
"academy/internal/agent/policy"
"academy/internal/types"
)
type riskChecker struct {
stubChecker
row *types.MCPToolApproval
observed []string
}
func (c *riskChecker) Policy(context.Context, uint64, string, string) (*types.MCPToolApproval, error) {
return c.row, c.err
}
func (c *riskChecker) ObserveRisk(_ context.Context, _ uint64, _, _ string, risk string) error {
c.observed = append(c.observed, risk)
return nil
}
func readOnlyTool(readOnly bool) *types.MCPTool {
return &types.MCPTool{Name: "t", Annotations: &types.MCPToolAnnotations{ReadOnlyHint: &readOnly}}
}
func TestDeclaredRiskReadsOnlyTheReadOnlyHint(t *testing.T) {
destructive := true
require.Equal(t, policy.RiskRead, DeclaredRisk(readOnlyTool(true)))
require.Equal(t, policy.RiskWrite, DeclaredRisk(readOnlyTool(false)))
require.Equal(t, policy.RiskWrite, DeclaredRisk(&types.MCPTool{Name: "t"}))
require.Equal(t, policy.RiskWrite, DeclaredRisk(&types.MCPTool{
Name: "t", Annotations: &types.MCPToolAnnotations{DestructiveHint: &destructive},
}), "destructive is the workspace's call, not the service's")
}
func TestToolRisk(t *testing.T) {
require.Equal(t, policy.RiskRead, ToolRisk(nil, policy.RiskRead))
raised := &types.MCPToolApproval{ObservedRisk: "write"}
require.Equal(t, policy.RiskWrite, ToolRisk(raised, policy.RiskRead), "a service cannot lower its own risk")
require.True(t, PendingDowngrade(raised, policy.RiskRead))
require.Equal(t, policy.RiskWrite, ToolRisk(&types.MCPToolApproval{ObservedRisk: "read"}, policy.RiskWrite),
"a raised declaration counts at once")
explicit := &types.MCPToolApproval{Risk: "read", ObservedRisk: "write"}
require.Equal(t, policy.RiskRead, ToolRisk(explicit, policy.RiskWrite), "the workspace's setting wins")
require.False(t, PendingDowngrade(explicit, policy.RiskRead))
}
func TestGateVerdictAppliesTheRunLevelToMCP(t *testing.T) {
ctxAt := func(level types.AssistantAccessLevel) context.Context {
return types.WithAccessLevel(context.Background(), level)
}
g := NewGate(nil, &riskChecker{}, nil)
read, write := readOnlyTool(true), readOnlyTool(false)
require.Equal(t, policy.Allow, g.Verdict(ctxAt(types.AssistantAccessReadOnly), 1, "svc", read))
require.Equal(t, policy.Deny, g.Verdict(ctxAt(types.AssistantAccessReadOnly), 1, "svc", write))
require.Equal(t, policy.Ask, g.Verdict(ctxAt(types.AssistantAccessConfirm), 1, "svc", write))
require.Equal(t, policy.Allow, g.Verdict(ctxAt(types.AssistantAccessAutoWrite), 1, "svc", write))
require.Equal(t, policy.Allow, g.Verdict(context.Background(), 1, "svc", write),
"a run without a level keeps the unconfigured agent's auto_write")
always := NewGate(nil, &riskChecker{row: &types.MCPToolApproval{RequireApproval: true}}, nil)
require.Equal(t, policy.Ask, always.Verdict(ctxAt(types.AssistantAccessAutoWrite), 1, "svc", read))
destructive := NewGate(nil, &riskChecker{row: &types.MCPToolApproval{Risk: "destructive"}}, nil)
require.Equal(t, policy.Ask, destructive.Verdict(ctxAt(types.AssistantAccessAutoWrite), 1, "svc", write))
}
func TestGateVerdictRecordsOnlyRaisedDeclarations(t *testing.T) {
ctx := types.WithAccessLevel(context.Background(), types.AssistantAccessConfirm)
checker := &riskChecker{}
g := NewGate(nil, checker, nil)
g.Verdict(ctx, 1, "svc", readOnlyTool(true))
require.Equal(t, []string{"read"}, checker.observed, "a first declaration is recorded")
checker.row, checker.observed = &types.MCPToolApproval{ObservedRisk: "read"}, nil
require.Equal(t, policy.Ask, g.Verdict(ctx, 1, "svc", readOnlyTool(false)))
require.Equal(t, []string{"write"}, checker.observed, "a raised declaration is recorded")
checker.row, checker.observed = &types.MCPToolApproval{ObservedRisk: "write"}, nil
require.Equal(t, policy.Ask, g.Verdict(ctx, 1, "svc", readOnlyTool(true)),
"a lowered declaration waits for the workspace")
require.Empty(t, checker.observed)
}
func TestGateToolRisksReadsPoliciesInBulk(t *testing.T) {
g := NewGate(nil, nil, nil)
risks, err := g.ToolRisks(context.Background(), 1, "svc", []*types.MCPTool{readOnlyTool(true)})
require.NoError(t, err)
require.Equal(t, policy.RiskRead, risks["t"])
}