feat(intercept): 模型审查补充任务上下文与裁决说明 (#124)
审查模型改为接收版本化 JSON:任务描述/目标/操作约束、工作目录、Worker 意图、当前轮输入,以及按 tool_use_id 配对的近期工具执行记录;末尾是完整的 tool_name 与 arguments。 - 任务约束每次审查从数据库重新读取;读取失败或约束超预算转人工。 - 历史只保留唯一配对的执行事实,排除 Agent 自述、未完成请求与平台拒绝反馈。 - 裁决改为 JSON decision(allow/ask/deny) + comment,三种结果均须说明实际操作、成功后果与命中规则。 - 所有模型裁决(含自动放行)保存实际模型输入与 SHA-256 指纹,详情页可展开。 - 对话审批卡片支持展开完整请求、模型输入、裁决与执行结果。 - 修复父目录 lockfile 导致的 Turbopack 根目录误判。 官方默认拦截规则、工具范围与优先级不变。
This commit is contained in:
1 parent
234bd8bf8e
commit
c0d1aa6f37
26 files changed
+1159
-199
No files matched your search
+31
-13
@@ -118,7 +118,7 @@ func (p *pendingManager) remove(id int64) {
|
||||
// as a Decision (Action ∈ allow|ask|deny; empty Action means the reply could not
|
||||
// be parsed). It is injected by the server layer so the intercept package stays
|
||||
// free of any llm dependency. profileID == 0 means "use the active/default profile".
|
||||
type Reviewer func(ctx context.Context, profileID int64, prompt, tool, command string) (Decision, error)
|
||||
type Reviewer func(ctx context.Context, profileID int64, prompt string, input ReviewInput) (Decision, error)
|
||||
|
||||
// Interceptor loads intercept rules from the database and evaluates them on
|
||||
// tool calls. It is safe for concurrent use.
|
||||
@@ -283,16 +283,18 @@ func (i *Interceptor) SetEnabledTools(tools []string) error {
|
||||
|
||||
// Decision is the outcome of a successful rule match.
|
||||
type Decision struct {
|
||||
ModelFallback bool
|
||||
RuleName string
|
||||
ConfigDigest string
|
||||
ProfileID int64
|
||||
Action string // "allow" | "deny" | "ask"
|
||||
Message string
|
||||
RuleID int64
|
||||
TimeoutEnabled bool
|
||||
TimeoutSeconds int
|
||||
TimeoutAction string // "deny" | "allow"
|
||||
ModelInput json.RawMessage
|
||||
ModelInputDigest string
|
||||
ModelFallback bool
|
||||
RuleName string
|
||||
ConfigDigest string
|
||||
ProfileID int64
|
||||
Action string // "allow" | "deny" | "ask"
|
||||
Message string
|
||||
RuleID int64
|
||||
TimeoutEnabled bool
|
||||
TimeoutSeconds int
|
||||
TimeoutAction string // "deny" | "allow"
|
||||
}
|
||||
|
||||
// --- LLM fallback judge ---
|
||||
@@ -424,7 +426,7 @@ func (i *Interceptor) SetJudgeConfig(c JudgeConfig) error {
|
||||
// keeps the current behavior: allow). On model error or an unparseable reply it
|
||||
// falls back to the configured FailAction. Ask verdicts carry the human-approval
|
||||
// timeout so the existing HandleAsk consumes them unchanged.
|
||||
func (i *Interceptor) Judge(ctx context.Context, tool, command string) (Decision, bool) {
|
||||
func (i *Interceptor) Judge(ctx context.Context, tool string, arguments json.RawMessage) (Decision, bool) {
|
||||
cfg := i.judgeConfig()
|
||||
i.mu.RLock()
|
||||
rv := i.reviewer
|
||||
@@ -440,7 +442,19 @@ func (i *Interceptor) Judge(ctx context.Context, tool, command string) (Decision
|
||||
defer cancel()
|
||||
}
|
||||
|
||||
out, err := rv(cctx, cfg.ProfileID, cfg.Prompt, tool, command)
|
||||
input, contextErr := BuildReviewInput(cctx, tool, arguments)
|
||||
cfg.Prompt = EffectiveJudgePrompt(cfg.Prompt)
|
||||
var out Decision
|
||||
var err error
|
||||
var modelInput []byte
|
||||
if contextErr != nil {
|
||||
// Missing task policy must not become an implicit permission via the
|
||||
// model's configurable fail-open strategy. A human can resolve the gap.
|
||||
out = Decision{Action: "ask", ModelFallback: true, Message: "审查上下文不完整,需要人工确认:" + contextErr.Error()}
|
||||
} else {
|
||||
modelInput, _ = json.Marshal(input)
|
||||
out, err = rv(cctx, cfg.ProfileID, cfg.Prompt, input)
|
||||
}
|
||||
if err != nil {
|
||||
out = Decision{ProfileID: out.ProfileID, ModelFallback: true, Action: cfg.FailAction, Message: "模型审批失败,按失败策略处理: " + err.Error()}
|
||||
}
|
||||
@@ -452,6 +466,10 @@ func (i *Interceptor) Judge(ctx context.Context, tool, command string) (Decision
|
||||
}
|
||||
// A model verdict never carries a rule; keep RuleID 0 (→ NULL) for history.
|
||||
out.RuleID = 0
|
||||
if len(modelInput) > 0 {
|
||||
out.ModelInput = modelInput
|
||||
out.ModelInputDigest = digestInput(modelInput)
|
||||
}
|
||||
if out.ProfileID != 0 {
|
||||
cfg.ProfileID = out.ProfileID
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user