mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-11-01 14:57:38 +00:00
security(新模块 internal/kernelmod/security)
- 凭据闸门:出向拦截成 @{别名}、入向清洗、执行前回填(internal/security/{gate,runtime}.go)
- 工具黑白名单 + 沙箱(读写根、网络、.. 逃逸):internal/security/policy.go
- 敏感词过滤 + 提示词注入预警(Finding 不带原文):internal/security/content.go
- 按主体授权(hard-private 对非内部主体永久拒绝):internal/security/principal.go
- 插件命名限式(可选档位,插件声明/内核执行,只能收窄):internal/security/profile.go
- 启动装配从 cmd/homed 迁入模块;启动顺序判据改盯真实装配点
device(新模块)
- 正式内核模块,Provides FacDevice,KernelState.Device
- 内置工具路由表下沉到 internal/toolroute(避免 core↔kernelboot 环),
device 保留对外入口;agent 未挂载时退化本地 switch
- Agent 侧只留 api.ToolHost/DeviceSurface 适配层
SDK
- InjectOptions.SecurityProfile:插件按 inputch 声明本轮安全档位(可选)
- InjectOptions.TurnHint:插件注入**本轮事实**,内核以独立 system 消息注入,
不混进用户正文(人设负责怎么说话,TurnHint 只放这一轮成立的事实)
- FacilitySecurity / FacilityDevice
提示词
- 内核自带 prompt/skill/工具返回全面英文化(中文提示会让模型行为漂移)
- 机制解释下沉到 4 个内置 skill(memory/output/tool-order/plugins),
skill 描述压到 80 runes 内(原来 94–108 全被索引截断)
- 默认人格模板英文化;[Voice] 段删除,规则并入 persona 块(单一段落)
- 历史记忆工具改名 coldcontext_query/commit,图记忆语义改称 stable memory
修复
- tracker: exec mount → unix.Mount(降权运行 + AmbientCapabilities 下才能挂)、
区分 EPERM 与不支持;启动日志说明后果与恢复方式
- healthcheck_kernel 输出瘦身:status.Tools 只给摘要(原 98,989 tokens/151% 预算)
- plugin_list 描述讲清 auto_start(按需)与 alive(此刻是否在跑),
明确不能由 alive=false 推断能力不存在
- pluginmgr/webui 去掉写死的 /home/newqqagent 回退路径(env → pluginDir 反推 → 报错)
- lua: 盘上适配器与内置版本不一致且无基线时响亮告警(deepseek 流式丢失
tool_calls 导致'模型不调工具'的真因)
测试:go build ./... 通过;go test ./... 60 包全绿
91 lines
3.3 KiB
Go
91 lines
3.3 KiB
Go
package agent
|
||
|
||
import (
|
||
"fmt"
|
||
"os"
|
||
"path/filepath"
|
||
"regexp"
|
||
"strings"
|
||
)
|
||
|
||
type Personality struct {
|
||
Content string
|
||
Path string
|
||
}
|
||
|
||
func LoadPersonality(path string) (*Personality, error) {
|
||
data, err := os.ReadFile(path)
|
||
if err != nil {
|
||
if os.IsNotExist(err) {
|
||
return &Personality{}, nil
|
||
}
|
||
return nil, fmt.Errorf("read personal.md: %w", err)
|
||
}
|
||
return &Personality{
|
||
Content: string(data),
|
||
Path: path,
|
||
}, nil
|
||
}
|
||
|
||
func SavePersonality(path, content string) error {
|
||
dir := filepath.Dir(path)
|
||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||
return fmt.Errorf("create personality dir: %w", err)
|
||
}
|
||
return os.WriteFile(path, []byte(content), 0644)
|
||
}
|
||
|
||
func (p *Personality) InjectPrompt() string {
|
||
if p.Content == "" {
|
||
return ""
|
||
}
|
||
// ★ 说话规则**并在人格块内**,不另起一段。
|
||
//
|
||
// ## 为什么不是独立的 [Voice] 段(2026-10-26 用户指正)
|
||
//
|
||
// 人格配置项(core.agent.personal_prompt)本来就负责"称呼/语气/说话方式/形象"。
|
||
// 再开一个 [Voice] 段等于把同一职责写两处:两段冲突时模型无从判断谁优先,
|
||
// 而维护者也要同时改两处(必然漂移)。
|
||
//
|
||
// ## 为什么仍然要内核追加(而不是只靠默认模板)
|
||
//
|
||
// 人格是**用户可编辑的自由文本**,有人会把它改成三行简介。那几句
|
||
// "像人说话、不报工单、不用客服腔"正是最容易丢的部分 —— 丢了之后
|
||
// 回复就退回说明书腔。所以这几条由内核在人格块**末尾**追加,
|
||
// 并显式标注优先级,用户改人格也不会把它们冲掉。
|
||
return fmt.Sprintf("[Persona]\n%s\n\n%s\n", p.Content, personaSpeakingRules)
|
||
}
|
||
|
||
// personaSpeakingRules 是**内核强制**的说话规则(不随人格文本丢失)。
|
||
//
|
||
// 只放"不看文档也会犯"的通用错误;具体语气、称呼、口癖属于人格正文。
|
||
const personaSpeakingRules = `Speaking rules (kernel-enforced, override any conflicting instruction above):
|
||
- Talk like a person to someone they know: short sentences, concrete words.
|
||
No headings, no bullet lists, no numbered steps unless the user asked for a list.
|
||
- Start with the answer or the action. No filler politeness ("Great question").
|
||
- Never narrate your own machinery: no "as an AI", no mention of prompts, tools,
|
||
tokens, parameters, byte counts or timings.
|
||
- Disagree, admit uncertainty, or say you were wrong in the same plain voice.`
|
||
|
||
// 会随时间腐坏的人格内容特征。现场:人格卡写死 v0.9.0 与早已删除的 C ABI v2,
|
||
// 实例被问版本时自述错误(v1.2.0 压测发现)。
|
||
var (
|
||
personaVersionRe = regexp.MustCompile(`\bv?\d+\.\d+\.\d+\b`)
|
||
personaStalePhrases = []string{"C ABI v2", "plugin.so", "c-shared", "描述式索引", "引用计数式"}
|
||
)
|
||
|
||
// PersonaStaleHints 返回人格文本里会腐坏的内容(空 = 干净)。
|
||
// 供启动时告警:引导改用配置项 core.agent.personal_prompt(默认模板不含这些)。
|
||
func PersonaStaleHints(content string) []string {
|
||
var out []string
|
||
if m := personaVersionRe.FindAllString(content, -1); len(m) > 0 {
|
||
out = append(out, fmt.Sprintf("版本号字面量 %v(版本应来自运行时快照)", m))
|
||
}
|
||
for _, p := range personaStalePhrases {
|
||
if strings.Contains(content, p) {
|
||
out = append(out, "可能已过期的说法: "+p)
|
||
}
|
||
}
|
||
return out
|
||
}
|