approval

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 9, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package approval 提供审批策略(Approval Policy)接缝。

对齐上游:packages/core/approval

本文件对应任务 M27:Approval Policy 接缝。

设计要点:

  • Policy 枚举:allow-all / deny-all / ask-dangerous / ask-dangerous-tool-edit;
  • Effective(生效策略)解析遵循「预设层 → 用户层 → 会话层」三层 override 顺序, 会话层优先级最高(nearest-scope-wins);并记录 Source 用于审计;
  • ask→allowed-once 语义:ask 策略通过 User Questions 向用户询问,用户「准许」只对 这一次工具调用放行;下一次同工具调用继续 ask,绝不做按工具名的永久放行;
  • 与 M22 PreToolDecision(tools/pre-execute 三态)协同:本包产出 Allow/Deny/Ask 决策。

依赖接缝:User Questions(userq)用于 ask;预设→策略的映射以 PresetPolice 函数注入, 从而 pkg/approval 不反向依赖 pkg/presets(M27 与 M28 相互依赖,靠单向注入破除环)。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DangerFunc

type DangerFunc func(tool string) bool

DangerClass 判断某工具是否属于"危险"类别(用于 ask-dangerous 系列触发 ask)。

type Decision

type Decision int

Decision 是工具预执行决策(三态),供 M22 PreToolDecision 复用。

const (
	DecideAllow Decision = iota
	DecideDeny
	DecideAsk
)

决策三态。

func (Decision) String

func (d Decision) String() string

String 返回决策的可读形式。

type EffSource

type EffSource string

EffSource 是生效策略的来源层级。

const (
	SourcePreset  EffSource = "preset"
	SourceUser    EffSource = "user"
	SourceSession EffSource = "session"
)

type Effective

type Effective struct {
	Policy Policy    `json:"policy"`
	Source EffSource `json:"source"`
}

Effective 是一次解析出的生效审批策略(含来源审计)。

type Policy

type Policy string

Policy 是审批策略。

const (
	PolicyAllowAll         Policy = "allow-all"
	PolicyDenyAll          Policy = "deny-all"
	PolicyAskDangerous     Policy = "ask-dangerous"
	PolicyAskDangerousEdit Policy = "ask-dangerous-tool-edit"
)

审批策略枚举(与 pkg/session.ApprovalPolicy 对齐)。

type PresetPolice

type PresetPolice func(preset string) (Policy, bool)

PresetPolice 把预设名映射为审批策略;ok=false 表示该预设未定义、回落默认。

type Request

type Request struct {
	// Tool 是工具名。
	Tool string
	// CallID 是本次工具调用的唯一标识(allowed-once 粒度锚点)。
	CallID string
	// Preset 是当前权限预设名(决定预设层策略)。
	Preset string
	// SessionID 是调用会话标识(决定会话层 override;空串表示无会话 override)。
	SessionID string
	// DangerousOverride 提供危险分类;nil 时按内置白名单。
	DangerousOverride DangerFunc
}

Request 是单次工具执行的审批请求。

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service 是审批服务(ctx.approval)。

func New

func New(preset PresetPolice, uq *userq.Service) *Service

New 创建审批服务。preset 注入预设→策略映射;uq 注入用户提问服务; presetLayer 通过 PresetPolice 在运行时求值,不入 scope。

func (*Service) AskCount

func (s *Service) AskCount() int

AskCount 返回累计 ask 次数(测试断言用)。

func (*Service) ClearSessionPolicy

func (s *Service) ClearSessionPolicy(sessionID string)

ClearSessionPolicy 移除会话层 override。

func (*Service) Evaluate

func (s *Service) Evaluate(req Request) (Decision, error)

Evaluate 对一次工具调用给出 allow/deny/ask 决策。

  • allow-all → allow;deny-all → deny;
  • ask-dangerous / ask-dangerous-tool-edit → 若工具危险则 ask(调用 UQ),否则 allow;
  • ask 用户"准许"只放行本次调用(allowed-once),下一次同工具继续 ask。

func (*Service) Resolve

func (s *Service) Resolve(preset string, sessionID string) Effective

Resolve 解析生效审批策略:预设层 → 用户层 → 会话层(会话最高)。

func (*Service) SetDangerous

func (s *Service) SetDangerous(m map[string]bool)

SetDangerous 覆写危险工具白名单。

func (*Service) SetSessionPolicy

func (s *Service) SetSessionPolicy(sessionID string, p Policy)

SetSessionPolicy 设置会话层策略 override(source=session,优先级最高)。

func (*Service) SetUserPolicy

func (s *Service) SetUserPolicy(p Policy)

SetUserPolicy 设置用户层策略 override(source=user)。

func (*Service) SetUserPolicyByKey

func (s *Service) SetUserPolicyByKey(userKey string, p Policy)

SetUserPolicyByKey 设置某个用户的策略 override。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL