Skip to content

feat(prompts): prompt packs — one-line surface switching via compress.promptPack - #397

Merged
ranxianglei merged 3 commits into
masterfrom
2026-09-13_prompt-packs-396
Sep 12, 2026
Merged

ranxianglei merged 3 commits into
masterfrom
2026-09-13_prompt-packs-396

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Closes #396. Extends #252 (shipped in #395); lean pack adapted from the #410 investigation.

What

compress.promptPack: "<name>" selects a named bundle of surface overrides — promptSections, nudgeSections, toolPrompts, delegatePrompt, and the 4 load-bearing compression rules — as the base layer under inline acp.json overrides.

No new providers mechanism: pack selection rides the standard compress three-level cascade (models > providers > global, deepest wins, resolved per turn from the active model). Switching models switches packs automatically; mergeCompress already did the plumbing.

{ "compress": { "promptPack": "lean" } }
{ "compress": { "providers": { "zhipu": { "promptPack": "lean" } } } }

Discovery

<project>/.pi/acp/packs/N.json > ~/.pi/acp/packs/N.json > built-ins (default, lean). Name whitelist [A-Za-z0-9][A-Za-z0-9._-]* (no .., no separators); invalid names and bad JSON fall back to built-ins — never crash.

Merge semantics — pack base, inline wins

  • sections: per key, inline beats pack (including null)
  • toolPrompts: per tool → per field → per param inside paramDescriptions
  • delegatePrompt: inline wins if present (including null)
  • pack prompts (4 compression rules): gated by acknowledgePromptsRisk exactly like inline prompts; flag cannot ship inside a pack — explicit local choice only

Built-in lean pack

Token-lean surface: eight one-line acpTags rules (refs / what to compress / what to preserve verbatim / recall tools / renumbering recovery / decompress-to-file / throttle resume / summaries-are-history), one-line tool descriptions, snippets/guidelines blanked. Measured 5422 → 1312 bytes (~76% smaller) vs the default surface; compression rules stay kernel defaults, delivered by nudges on demand.

Implementation

  • src/prompt-pack.ts (new): LEAN_PROMPT, LEAN_PACK, BUILTIN_PACKS, discoverPack, resolvePackName/resolveActivePack, packSurface, mergeSurface, readToolSurfaceWithPacks
  • src/config.ts: CompressSettings.promptPack + cascade in mergeCompress
  • src/system-prompt.ts: 4 rule-slot keys with kernel defaultPrompts defaults
  • src/index.ts: tool registration uses pack-aware factory-time read; nudge + system-prompt sites resolve the pack per turn via ctx.model
  • tests/prompt-pack.test.ts: 10 cases (surface shape, lean collapse, cascade, discovery/shadowing, traversal rejection, merge precedence, risk-gate keys, sanitization, tool-surface stack)
  • CONFIGURATION.md + zh-CN: compress.promptPack reference + full Prompt Packs section

Validation

  • npm run typecheck ✅
  • npm test: 783 pass / 0 fail (3 env skips) ✅
  • npm run build ✅

….promptPack (closes #396)

Pack selection reuses the compress three-level cascade (models > providers >
global) — no new providers mechanism. Discovery: project .pi/acp/packs/N.json
shadows ~/.pi/acp/packs/N.json shadows built-ins (default, lean). Merge:
pack base layer, inline acp.json wins per field (sections per key incl null;
toolPrompts per tool/field/param; delegatePrompt inline incl null). Pack
prompts gated by acknowledgePromptsRisk like inline prompts.

Built-in lean pack: eight one-line acpTags rules + one-line tool
descriptions, snippets/guidelines blanked — 5422→1312 bytes (~76% smaller
surface), compression rules stay kernel defaults via nudges (from #410).

- src/prompt-pack.ts: LEAN_PROMPT/LEAN_PACK/BUILTIN_PACKS, discoverPack,
  resolvePackName/resolveActivePack, packSurface/mergeSurface,
  readToolSurfaceWithPacks
- src/config.ts: CompressSettings.promptPack + mergeCompress cascade
- src/system-prompt.ts: 4 rule-slot keys with kernel defaultPrompts defaults
- src/index.ts: tool registration (factory-time pack-aware read), nudge and
  system-prompt sites resolve pack per turn via ctx.model
- tests/prompt-pack.test.ts: 10 cases
- CONFIGURATION.md(+zh-CN): compress.promptPack + Prompt Packs section
@github-actions

github-actions Bot commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Extension Artifact

Branch: 2026-09-13_prompt-packs-396 (9b16cd6)

Option A — Install from npm PR tag (recommended)

pi install npm:billion-context-pi@pr-397

Each push to this PR publishes a new version under the pr-397 npm tag.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pi-pr397.tgz
pi install ./package

This comment is automatically updated on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 收到 PR #397(prompt packs)。已确认 diff 干净:单 commit e8ff15d 基于最新 master(含 #395),8 文件 +586/−9,无跑题改动。正在逐文件 review + 本地跑 typecheck/test/build,结论稍后回复。

…acks from ctx.cwd

Review fix for the prompt-packs wiring (found in #397 review):

- wireSystemPrompt resolved prompts only when merged.prompts was non-empty, so
  switching from a model whose pack ships prompt overrides to one without left
  the previous pack's rules stuck in runtime.prompts. Resolve unconditionally:
  resolvePrompts({}) returns kernel defaults, so switch-away resets correctly.
  On risk-gate failure fall back to defaultPrompts (same as session_start) and
  dedupe the warning by the gated key set instead of logging every turn.
- Pack discovery in before_agent_start / activeNudgeSections now uses
  ctx.cwd (process.cwd() fallback), matching reloadConfig(ctx.cwd): project
  acp.json and .pi/acp/packs/ resolve from the same directory.
- tests/prompt-pack.test.ts: +2 wiring-level regression tests — per-model pack
  prompt application with switch-away reset (fails on the pre-fix code,
  verified by stash), and risk gating of pack prompts without
  acknowledgePromptsRisk.
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Review complete — 已在 PR 分支直接补修复 commit 78f6f0f,可以合并。

Diff 整洁度(第一道关) ✅ 单 commit e8ff15d 基于最新 master(含 #395),8 文件 +586/−9,无跑题文件、无生成物、无无关重排。

本地验证(分支上实跑):npm run typecheck ✅ · npm test 785 pass / 0 fail / 3 env skips ✅ · npm run build ✅。逐文件审了 prompt-pack.ts / config.ts / index.ts / system-prompt.ts / 测试 / 双语文档,并核对了 acp-kernel 侧 resolvePrompts 契约与 Pi 的 ExtensionHandler 签名(before_agent_start 确实带 ctx)。

设计审核结论(均通过):发现顺序 project > home > built-in ✓;包名白名单 + ../分隔符拒绝、坏 JSON 回退不崩溃 ✓;合并语义 pack 为基座、内联逐字段胜(sections 含 null;toolPrompts 逐工具→逐字段→逐参数;delegatePrompt 含 null)✓;风险门正确——acknowledgePromptsRisk 只从本地 acp.json 读,pack 文件里写了也会被 packSurface 丢掉 ✓;lean 包形状有测试锚定(断言各 section 消失、只剩八条单行规则)✓;文档与实现一致 ✓。

发现并已修的问题(2 个,commit 78f6f0f):

  1. 切模型后压缩规则残留(真 bug):wireSystemPrompt 里 if (Object.keys(merged.prompts).length > 0) 守卫导致——从「带 prompts 的 pack 的模型」切到「无 pack 的模型」时,runtime.prompts 永远停在旧 pack 的规则上,PR 宣称的"切模型自动切包"对四条压缩规则这一项不成立。修法:resolvePrompts 无条件调用(空覆盖返回内核默认,已实测验证),风险门失败时回退 defaultPrompts(与 session_start 行为对齐),并按 gated key 集合去重告警避免每回合刷日志。
  2. cwd 一致性:逐回合的 pack 发现原来用 process.cwd(),而同一回合的配置加载走 reloadConfig(ctx.cwd)——嵌入式/RPC 宿主下两者可能不同,会拿错项目的 pack。改为 ctx?.cwd ?? process.cwd()(工具注册处工厂期无 ctx,保持原状,与既有 readToolSurfaceSync 约定一致)。

补了 2 个 wiring 级回归测试(per-model 应用 + 切走回默认;无 risk flag 时 pack prompts 被丢弃)。用 stash 在修复前代码上验证过:switch-away 测试确实 fail,修复后 11/11 全绿。

小观察(未改,供参考):nudge 渲染路径每次调用都会 readFileSync pack 文件(每回合至多 ~4 次),小 JSON 开销可忽略,暂不值得加缓存;PR 描述写"10 cases"实际是 9 个测试(+我补的 2 个 = 11),笔误而已。

关于留痕:问题 1 只存在于本 PR 未合并的代码中(从未发布),完整记录在本回复 + 修复 commit message 里,按"minor → report only"未另开 issue;若你希望单独建 issue 留档,说一声我就开。


中文摘要:审查 prompt packs(PR #397)时发现一个真 bug——从带压缩规则的 pack 切到无 pack 的模型后旧规则残留在 runtime 中不重置(另有 pack 发现目录与配置加载目录不一致的小问题),已直接在 PR 分支补了无条件重置 + cwd 对齐 + 2 个回归测试(验证过修复前必挂),typecheck/785 测试/build 全绿,可以合并。

…ource/PackResolver

- src/packs/types.ts: Pack (name+sanitized surface+provenance), PackSource
  (resolve/list), PackSurface, PromptPackFile, isValidPackName
- src/packs/lean.ts: leanPack exported as a standalone Pack module
- src/packs/builtin.ts: defaultPack + builtinSource (registry map)
- src/packs/dir.ts: createDirPackSource(id, dir) — same factory serves
  project/user/any future installer-managed dir
- src/prompt-pack.ts: createPackResolver / defaultPackSources ([project >
  user > builtin]) / listPacks; prepended sources shadow everything — the
  integration point for a future bili-pi install pack manager (write files
  or register a managed source, zero core rewrite)
- index.ts/resolveActivePack now consume Pack.surface directly
- tests: 12 cases incl. custom-source shadowing and dir list() resilience
@ranxianglei

Copy link
Copy Markdown
Owner Author

Rebased on top of 78f6f0f (review fix) — both intents merged.

Refactor: packs are now a pluggable source chain (per review feedback on the architecture)

lean is no longer a hardcoded object inside the resolver. The pack system is interface-oriented:

// src/packs/types.ts
interface Pack        { name; version?; description?; surface: PackSurface; source: string }
interface PackSource  { id: string; resolve(name): Pack | null; list?(): Pack[] }
  • src/packs/lean.ts — leanPack exported as a standalone Pack module
  • src/packs/builtin.ts — defaultPack + builtinSource (registry map; adding a built-in = one entry)
  • src/packs/dir.ts — createDirPackSource(id, dir); the same factory serves project/user/any future installer-managed dir
  • src/prompt-pack.ts — createPackResolver([...sources]) + defaultPackSources(cwd) = [project > user > builtin] + listPacks(); first match wins, so a prepended source shadows everything

Integration point for a future bili-pi install-style pack manager: write pack files into the user dir (zero code), or register a managed source ahead of the defaults — no core rewrite either way. Hosts embedding the adapter can pass their own resolver to resolveActivePack.

Review-fix commit preserved through the refactor: unconditional setPrompts (switch-away resets to kernel defaults), defaultPrompts fallback on gate failure, warning dedupe, ctx.cwd ?? process.cwd() discovery (system prompt + nudge sites).

Tests: 12 → 14 (kept the two wiring regressions; added custom-source shadowing + dir list() resilience). 787/787 pass, typecheck + build green.

@ranxianglei
ranxianglei merged commit fd5444f into master Sep 12, 2026
9 checks passed
@ranxianglei
ranxianglei deleted the 2026-09-13_prompt-packs-396 branch September 12, 2026 13:12
HangxiangMa pushed a commit to HangxiangMa/billion-context-pi that referenced this pull request Sep 21, 2026
…acks from ctx.cwd

Review fix for the prompt-packs wiring (found in ranxianglei#397 review):

- wireSystemPrompt resolved prompts only when merged.prompts was non-empty, so
  switching from a model whose pack ships prompt overrides to one without left
  the previous pack's rules stuck in runtime.prompts. Resolve unconditionally:
  resolvePrompts({}) returns kernel defaults, so switch-away resets correctly.
  On risk-gate failure fall back to defaultPrompts (same as session_start) and
  dedupe the warning by the gated key set instead of logging every turn.
- Pack discovery in before_agent_start / activeNudgeSections now uses
  ctx.cwd (process.cwd() fallback), matching reloadConfig(ctx.cwd): project
  acp.json and .pi/acp/packs/ resolve from the same directory.
- tests/prompt-pack.test.ts: +2 wiring-level regression tests — per-model pack
  prompt application with switch-away reset (fails on the pre-fix code,
  verified by stash), and risk gating of pack prompts without
  acknowledgePromptsRisk.
HangxiangMa pushed a commit to HangxiangMa/billion-context-pi that referenced this pull request Sep 21, 2026
…-packs-396

feat(prompts): prompt packs — one-line surface switching via compress.promptPack
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.

feat: prompt packs — one-line surface switching via compress.promptPack (reuses the three-level cascade)

1 participant