Skip to content

feat(context): 接入 session 级 skills 注入并修正 runtime 降级语义#301

Closed
wynxing wants to merge 3 commits into
1024XEngineer:mainfrom
wynxing:test_campact
Closed

feat(context): 接入 session 级 skills 注入并修正 runtime 降级语义#301
wynxing wants to merge 3 commits into
1024XEngineer:mainfrom
wynxing:test_campact

Conversation

@wynxing

@wynxing wynxing commented Apr 15, 2026

Copy link
Copy Markdown
Collaborator

背景

关联 issue: #258

当前仓库里 skills 已有基础 loader/registry 能力,但还没有真正进入主链路。
本次改动将 session + runtime + context + bootstrap 串起来,让 session 级 skill 激活、运行时解析以及 prompt 注入真正生效,并补齐对应测试。

主要修改

1. Session

  • 增加 ActivatedSkills []SkillActivation 持久化字段
  • 新增 ActivateSkillDeactivateSkillActiveSkillIDs
  • 保存和加载时统一做 skill id 规范化、去重、稳定排序
  • 补齐激活/取消激活、幂等、持久化兼容相关测试

2. Runtime

  • 为 runtime 注入 skills.Registry
  • 新增 ActivateSessionSkillDeactivateSessionSkillListSessionSkills
  • prepareTurnSnapshot() 在构建上下文前解析当前 session 激活的 skills,并传入 context builder
  • 新增 runtime 事件:
    • skill_activated
    • skill_deactivated
    • skill_missing

3. Context

  • BuildInput 增加 ActiveSkills []skills.Skill
  • 新增统一的 Skills prompt section
  • 对 active skills 做稳定去重和排序
  • 固定裁剪:
    • references 最多 3 条
    • tool hints 最多 3 条
    • examples 最多 2 条
  • compact 前后都会重新注入 skills,不回写消息历史

4. Bootstrap

  • 启动时从 ~/.neocode/skills 初始化本地 skills registry
  • skills root 缺失时按空 catalog 处理,不影响应用启动
  • 初次 refresh 失败时保留 registry 实例,避免 runtime 退化为 registry unavailable

5. 降级与错误语义修正

  • 仅对真正的 skills.ErrSkillNotFoundskill_missing 软降级
  • 对上下文取消、权限失败、refresh 失败、解析失败等基础设施错误直接向上返回
  • 避免把“skill 不存在”和“registry/load 故障”混淆

验证

已通过:

  • go test ./internal/skills
  • go test ./internal/runtime
  • go test ./internal/app
  • go test ./internal/session
  • go test ./internal/context
  • go test ./internal/tui/...

影响

  • skills 现在已经进入主链路,可被 session 激活并注入 runtime/context
  • 缺失 skills root 不再导致整个 skills 子系统不可用
  • registry/load 故障不再被误报为 skill_missing

@codecov

codecov Bot commented Apr 15, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.08043% with 37 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
internal/runtime/skills.go 82.05% 12 Missing and 9 partials ⚠️
internal/context/source_skills.go 90.90% 5 Missing and 5 partials ⚠️
internal/session/skill_activation.go 91.66% 3 Missing and 3 partials ⚠️

📢 Thoughts on this report? Let us know!

fennoai[bot]

This comment was marked as outdated.

@wynxing

wynxing commented Apr 15, 2026

Copy link
Copy Markdown
Collaborator Author

/code 补充测试,解决review的问题

@fennoai

fennoai Bot commented Apr 15, 2026

Copy link
Copy Markdown
Contributor

已按本次 review 意见完成修复并补测试,代码已推送到当前 PR 分支 test_campact

本次修改

  • 修复 ActivateSessionSkill 事件 ID 不一致:skill_activated 统一发送规范化后的 skill id(与 deactivated 保持一致)。
  • 修复 skill_missing 事件噪音:同一次 run 内对同一 skill 仅上报一次缺失事件,避免 prepareTurnSnapshot 多次重建时重复发事件。
  • 修复注释语义不一致:buildSkillsRegistry 注释改为“refresh 失败仅记录日志并保留 registry 实例”。
  • 补充回归测试:
    • TestActivateSessionSkillPersistsAndEmitsEvent 覆盖 descriptor id 为下划线场景,校验事件输出为规范化 id。
    • 新增 TestPrepareTurnSnapshotDeduplicatesSkillMissingPerRun,覆盖同一 run 多次快照构建时缺失事件去重。

验证

  • go test ./internal/runtime ./internal/app
  • go test ./internal/session ./internal/context ./internal/skills
  • go test ./...

提交

  • 1332e9b fix(runtime): normalize skill events and dedupe missing notifications

View job run

Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: Yumiue <188874804+Yumiue@users.noreply.github.com>
@wynxing

wynxing commented Apr 15, 2026

Copy link
Copy Markdown
Collaborator Author

/code 添加相关测试,提高测试覆盖率!!!!

@fennoai

fennoai Bot commented Apr 15, 2026

Copy link
Copy Markdown
Contributor

已按“添加相关测试,提高测试覆盖率”完成补测并推送到当前 PR 分支 test_campact

本次补充测试

  • internal/session/skill_activation_test.go
    • 补充 nil session、空 skill id 分支
    • 补充 SkillActivation.ClonecloneSkillActivations 深拷贝分支
  • internal/runtime/skills_test.go
    • 补充 ActivateSessionSkill/DeactivateSessionSkill/ListSessionSkills 的输入校验与上下文取消分支
    • 补充 DeactivateSessionSkill 变更事件发射分支
    • 补充 ListSessionSkills 在空列表/registry 为空/已解析三种状态分支
    • 补充 mutateSessionSkillsnil mutate 与持久化失败分支
    • 补充 emitSkillMissingOncenormalizeRuntimeSkillID 分支
  • internal/runtime/runtime_internal_helpers_test.go
    • 补充 markSkillMissingReported 分支与去重行为
    • 补充 agentsessionCloneSkillActivations 深拷贝分支
  • internal/context/source_skills_test.go
    • 补充 Sections 的上下文取消分支
    • 补充 normalizeActiveSkills 空 id 过滤分支
    • 补充 truncateSkillReferences(title/summary/path/去重/limit<=0)及 min 分支
  • internal/app/bootstrap_test.go
    • 补充 buildSkillsRegistry 在 refresh 失败时仍返回可用 registry 的降级分支

验证

  • go test ./internal/session ./internal/runtime ./internal/context ./internal/app
  • go test ./...
  • go test ./internal/app ./internal/context ./internal/runtime ./internal/session -cover
    • internal/runtime 覆盖率提升到 97.2%

提交

  • a291625 test: add coverage for session/runtime/context skill branches

附:按 fork 推送流程同步创建了 fork 内 PR:wynxing#13

View job run

Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: Yumiue <188874804+Yumiue@users.noreply.github.com>
@fennoai fennoai Bot deleted the test_campact branch April 15, 2026 03:16
@wynxing

wynxing commented Apr 15, 2026

Copy link
Copy Markdown
Collaborator Author

/review

@fennoai fennoai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the implementation and test coverage expansion. I found a few doc/contract mismatches introduced by this change that should be updated to keep external/runtime contracts accurate.

EventCompactError EventType = "compact_error"
// EventTokenUsage is emitted after each provider response with token statistics.
EventTokenUsage EventType = "token_usage"
// EventSkillActivated 表示会话成功激活了一个 skill。

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

skill_activated/skill_deactivated/skill_missing are new public runtime events, but the runtime event contract doc still lists the older set only. Please update docs/runtime-provider-event-flow.md to include these events so TUI/consumer expectations stay aligned with the actual event stream.

Comment thread internal/session/store.go
UpdatedAt time.Time `json:"updated_at"`
Workdir string `json:"workdir,omitempty"`
TaskState TaskState `json:"task_state"`
ActivatedSkills []SkillActivation `json:"activated_skills,omitempty"`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Session now persists activated_skills, but docs/session-persistence-design.md does not mention this field in the persisted model. Please update the session schema documentation to reflect the new durable field and its semantics.

Comment thread internal/runtime/run.go
func (s *Service) prepareTurnSnapshot(ctx context.Context, state *runState) (turnSnapshot, bool, error) {
cfg := s.configManager.Get()
activeWorkdir := agentsession.EffectiveWorkdir(state.session.Workdir, cfg.Workdir)
activeSkills, err := s.resolveActiveSkills(ctx, state)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This run path now resolves active skills and injects them into context.BuildInput.ActiveSkills, but the context-flow doc still describes builder inputs/section ordering without a Skills section. Please update docs/runtime-provider-event-flow.md accordingly.

@wynxing

wynxing commented Apr 15, 2026

Copy link
Copy Markdown
Collaborator Author

/simplify

@fennoai

fennoai Bot commented Apr 15, 2026

Copy link
Copy Markdown
Contributor

Result

已按触发指令完成处理并推送到当前 PR 分支。

What I changed

  • 修复并关闭评论日志中的 3 条未解决 review comment(均为文档失配):
    • docs/runtime-provider-event-flow.md
      • 增补 runtime 新事件:skill_activatedskill_deactivatedskill_missing
      • 增补 context.BuildInput.ActiveSkills 输入说明
      • 增补 Skills section 以及 system prompt 注入顺序(Task State / Skills
      • 补充 active skills 去重排序与裁剪上限说明(references<=3tool_hints<=3examples<=2
    • docs/session-persistence-design.md
      • 增补持久化字段 activated_skills
      • 增补其规范化持久化语义(规范化 skill_id、统一去重和稳定排序)

Simplify pass

  • 已执行一次 code-simplifier 子代理检查,并吸收最小化措辞精简(不改语义)。

Validation

  • go test ./internal/runtime ./internal/context ./internal/session

Commit & push

  • Commit: 1efa3f1
  • 已推送到分支:fork-pr-301-1776220919

Note

  • git-push-fork 流程尝试创建/更新 fork 内 PR 时返回:No commits between test_campact and fork-pr-301-1776220919(分支间无新增差异),因此未生成新的 fork PR 链接。

View job run

@wynxing wynxing marked this pull request as draft April 15, 2026 06:13
@wynxing wynxing marked this pull request as ready for review April 15, 2026 06:13
@wynxing wynxing closed this Apr 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants