Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,9 @@ session cwd 执行、通知诚实标注 best-effort、工具契约对未知输
- **`output_matches` 的正则现在以 `m` 编译**(REV-10)。`^` / `$` 从「整段锚点」变成
「行锚点」,多行输出下匹配结果可能变化。
- **per-task activity id 形状变更**:`pi-async/<taskId>` → `plugin/pi-async/<taskId>`
(REV-8)。OSC 7501 / legion viz 侧可见。
(REV-8)。这是 **id 形状约束,不是可见性承诺**:写入唯一走宿主的 `ui.setActivity`
(feature-detect;官方 pi 1.2–1.5 无该 API 时是 no-op),本仓库不自行发 OSC 转义,
legion 侧要等上游 #156 的转发通道。
- **`async_logs` 输出文本清洗范围扩大**:从 `stripAnsi` 改为同时剥离裸 C0 控制字符,
并用 `truncateUtf8Bytes` 替代按字节 `subarray` 截断 —— 含 BEL/BS/FF 或多字节字符时
返回文本与 0.1.0 不同。
Expand Down
25 changes: 13 additions & 12 deletions docs/CONTRACT.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,31 +281,32 @@ async_wait({ taskIds: string[], timeoutMs: number }) // timeoutMs 必填(sch
`agent_before_settle`,Esc 中断场景需要单独覆盖。
- 开关:`PI_ASYNC_SETTLE_GUARD=0` → 整个守卫不注册任何 handler。`dispose()` 幂等,退订所有 handler。
- **无心跳**:不建 `setInterval`、不写 `async-task-heartbeat` entry、不读 `PI_ASYNC_HEARTBEAT_MS`。
- **OSC busy 上报只是 host-API shim,不是已实现能力**(官方 pi 1.2–1.5 均无 `ui.setActivity`;四处
`typeof ui?.setActivity !== "function"` 特性检测在生产上是 no-op:`src/core/bus.ts:15,26,34` 与
`src/guard/settle-guard.ts:222`)。保留它只为上游将来加 API 时自动生效;README/CHANGELOG 不得把它
写成已实现能力。
- **OSC busy 上报只是 host-API shim,不是已实现能力**(官方 pi 1.2–1.5 均无 `ui.setActivity`;
`src/core/bus.ts` 的三个 shim 函数都做 `typeof ui?.setActivity !== "function"` 特性检测,生产上是
no-op;settle 守卫也走同一个 shim,不自带副本)。保留它只为上游将来加 API 时自动生效;
README/CHANGELOG 不得把它写成已实现能力。
- **它能表达什么**:一次 write-only 的 `(id, { message: string } | undefined)` 调用
(`src/core/bus.ts:14,25,33`)。两个 id:聚合 `"pi-async"`(`bus.ts:4,17`)与每任务
`plugin/pi-async/<task-id>`(`bus.ts:6-9`)。清除 = 传 `undefined`(没有 idle/working 枚举,
(`src/core/bus.ts:31,45,53`)。两个 id:聚合 `"pi-async"`(`bus.ts:10,31`)与每任务
`plugin/pi-async/<task-id>`(`bus.ts:13-15`)。清除 = 传 `undefined`(没有 idle/working 枚举,
也没有结构化字段)。
- **它不能表达什么**:不能读回(`src/` / `extensions/` 无 getActivity,activity 不进模型上下文)、
不能表达每任务进度/耗时/优先级、**不能证明「真的在干活」或「没有工具在飞」**。legion 跨机场景
今天没有转发通道(见下一条 RPC 段:依赖上游 #156 的 `extension_activity`),该侧 activity 字段
恒缺/恒错;**任何消费者不得把 activity 的有无当作工作证据**。外部唯一诚实的 busy 信号是 settle
hold 不结束 run(§7.1 / README「Settle hold」,OSC 7501 保持 `working`)。
- **shim 形状(上游落地后才生效的形状约束,不是能力承诺)**:
- 聚合文案 `{ message: "N async tasks" }`(`bus.ts:17`,恒复数);每任务 raise/clear 与任务状态
同步(`extensions/index.ts:411-413`)。
- 聚合文案 `{ message: "N async tasks" }`(`src/core/bus.ts` 的 `reportActivity`,恒复数);每任务
raise/clear 与任务状态同步(`extensions/index.ts:411-413`)。
- 每任务 id 必须带 `plugin/` 前缀 —— **REV-8 裁决(#26)**:对齐 legion
`docs/contracts/osc-busy-plugin.md` 的 `plugin/<name>/…`;聚合源仍为 `pi-async`(§7 冻结)。
此前实现漏前缀会导致宿主按 `plugin/` 路由时 per-task activity 静默无效。
- **禁止 printf OSC**;尊重 `PI_PROGRAM_STATUS`
- 上报时机:任务启动、终态、以及 `agent_before_settle`(闭合 retire 竞态窗口的关键,见 #156 补充评论)
- **已知实现分叉(不在本次文档变更内改实现)**:`src/guard/settle-guard.ts:217-230` 有一份不 import
`src/core/bus.ts` 的私有 `reportActivity()`(第 4 处 shim;带 try/catch + stale-ctx `dispose()`,
文案会单数化为 `1 async task`,与 bus 的 `1 async tasks` 不同)。同一聚合 id 因此有两个写入者;
收敛实现另开 issue(见 `docs/reports/contract-cleanup.md`)。
- **实现收敛(#107,替换此前的「已知实现分叉」)**:`src/guard/settle-guard.ts` 不再自带私有
`reportActivity()`;`agent_before_settle` 与 `register()` 都调 `src/core/bus.ts` 的
`reportActivity`(一处实现、一个文案 `N async tasks`,恒复数)。该 shim 在宿主拒写(stale ctx)时
吞掉异常并返回 `false`;守卫据此在答完当前 settle 边界后 `dispose()` 死代——异常不参与、也不改变
settled 判定(0.3.2 stale-ctx 自愈保留)。
- RPC(legion child)内:setActivity 依赖 legion `extension_activity` 转发(#156 落地后生效),
未落地时静默降级,不影响工具面;**因此 RPC/跨机侧没有可用的 activity 证据**。

Expand Down
175 changes: 175 additions & 0 deletions docs/reports/pi-async-guard-gaps.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
# 守卫缺口收口(#105 的 3 处实现/文档不一致 + 1 处守卫盲区)— 交付报告

- Issue:zzjcool/pi-async#107(**Fixes #107**;条目原样来自 PR #105 报告 `docs/reports/contract-cleanup.md` §2)
- PR:zzjcool/pi-async#108
- 分支:`fix/guard-gaps`(worktree 分支 `pi-subagent/pi-async-inconsistencies-6ab404ba`,同一 commit)
- 基线:`origin/main` @ `d0407d1`(`git fetch` 确认相对开工点 `985a083` 前进:#105 / #104 / #101 / #102 已合入;worktree `reset --hard` 到该 commit 后开工)
- 范围:**只做任务卡四件事**。未改工具 schema、spawn/TUI 行为、版本号;`src/` 的行为改动只有 activity 上报路径(下一节)。

## 1) settle-guard 的私有 activity 副本 → 单一 bus 实现

### 改了什么

- `src/guard/settle-guard.ts`
- 删除私有 `reportActivity()`(原 `:217-230`),改从 `src/core/bus.ts` import `reportActivity`。
- `beforeSettle` 拆成纯判定 `decideBeforeSettle(active, event)` + 包装:先上报、答完当前 settle
边界,再决定是否 `dispose()`。这样 `setActivity` 抛错**不会改变**提醒/advisory 的产生。
- `register()` 末尾的上报同样走 bus;宿主拒写时 `dispose()`。
- `src/core/bus.ts`
- `reportActivity` 加 `try/catch`,返回 `boolean`:`true` = 已上报或宿主无此 API(无 API 不算失败),
`false` = 宿主拒写(stale ctx)。异常不再逃逸;TUI/扩展调用点忽略返回值,行为不变。
- `docs/CONTRACT.md` §7:原文写「四处特性检测 + 已知实现分叉(settle-guard 私有副本)」;实现收敛后
这段话会变成**新的**文档不一致,故同步改为「三处 shim + 守卫走同一实现」(属于任务 1,不是额外重构)。

### 文案选了哪个、为什么

选 **bus 的字面量 `N async tasks`(恒复数)**,删掉 guard 的单数化分支(`1 async task`)。

理由:bus 是共享实现,调用点有 3 处(`src/tui/status.ts:122`、`extensions/index.ts:331,411`)+ 守卫;
选 bus 字面量只改守卫 `count == 1` 这一条路径(对应 2 个测试),选守卫的单数文案则要改 bus 的
formatter,波及 TUI/扩展的 3 个调用点——同样的「一处实现」目标,blast radius 更大。代价是
`1 async tasks` 的语法瑕疵(#16 此前的单数化被统一掉);它不承载任何语义,记录为已知 cosmetic。

### stale-ctx 自愈没有丢

0.3.2 的修复(「`reportActivity` 读到 stale ctx 异常即 `dispose()` 自身」)保留:bus 把异常转成
`false`,守卫**先答完边界**再 `dispose()`。比原实现更强:旧实现异常时直接 return,连当次提醒都
不发;新实现让 settled 判定与 activity 彻底解耦。

### 单测

- ① `tests/a-settle-guard.test.ts` — `settle guard reports through the bus wording: one implementation, one literal`:
fake `ui.setActivity`,断言 `register()` 上报 1 次、`agent_before_settle` 再上报 1 次,文案分别是
`1 async tasks` / `2 async tasks`。
- ①(结构)`settle-guard keeps no private activity copy; the bus shim is the only writer (§7 / #105)`:
源码不含 `.setActivity(`,且 import 的是 `../core/bus.ts` 的 `reportActivity`。
- ② `a throwing setActivity cannot change the settle decision; the stale generation detaches after answering`:
宿主拒写后提醒仍带 `continue: true`,然后 4 个 handler 全部退订(0.3.2 自愈)。
- `tests/a-bus.test.ts`:返回值三态(上报 `true` / 无 API `true` / 拒写且不抛、返回 `false`)。
- `tests/w2-safety.test.ts` 的 `#16` 用例改名为「uses the shared bus wording」,断言从 `1 async task`
改为 `1 async tasks`(这正是 #105 §2.2 记录的「两份测试分别钉死两种文案」,收敛必须一起改)。

### 变异(修前红)

```
git checkout HEAD -- src/guard/settle-guard.ts # 恢复私有副本
node --experimental-strip-types --test tests/a-settle-guard.test.ts
→ not ok 3 settle guard reports through the bus wording: one implementation, one literal
→ not ok 4 settle-guard keeps no private activity copy; the bus shim is the only writer (§7 / #105)
→ not ok 5 a throwing setActivity cannot change the settle decision; the stale generation detaches after answering
# pass 4 / # fail 3
```

恢复收敛实现后同文件 **7/7 绿**。

## 2) 漂移门扩到 §18(含 §18.11)+ 下限断言

### 改了什么

`tests/f-reminder.test.ts` 把原来的「只 `slice("## 17.", "## 18.")`」守卫换成
`scanToolSurfaceContract(contract)`:

- 扫 **§17 + §18** 两段(§18 slice 到 `## 19.`),返回 `{ sections, checks, findings }`。
- §17:3 项检查(不得出现 `6 个工具`;必须有 `5 个工具`;必须写 `PI_ASYNC_EXPOSE_WAIT`)。
- §18:必须保留 `### 18.11`,且 §18.11(slice 到 `### 18.12`)必须含
`暴露 async_wait 时 6 个`(REV-29 第 6 席)。
- **下限断言**:主测试断言 `sections` 恰为 `["§17","§18"]`,且 `checks >= MIN_TOOL_SURFACE_CHECKS (5)`。
段丢失/切片失效会让 findings 非空或 checks 掉到 5 以下,**不会**因为「文本里出现过 18」而空转通过。
- 自测 4 条:
- §18.11 的 `时 6 个` → `时 5 个` → 红(finding 精确点名 §18.11);
- §17 的 `默认 5 个工具` → `默认 6 个工具` → 红(证明旧覆盖没被扩围时弄丢);
- 删掉 §18 的文本 → findings 报缺段、`sections` 不含 §18、`checks < 5`(反空转下限);
- 三条自测都先 `assert.notEqual(mutated, contract)`,防止「没改成也算红」。

### 修前红 / 修后绿

- **旧门(HEAD 版本)对同一漂移文本 13/13 通过** = 盲区复现:把 CONTRACT §18.11 的 `时 6 个` 做单点
替换为 `时 5 个`(`git diff` 只有这一行),旧门 `# Subtest: §18.11 docs…` → `ok 10`、`# fail 0`。
- **新门对同一文本红**:`not ok 10 … §18.11 must keep the sixth seat (暴露 async_wait 时 6 个)`。
- 恢复 CONTRACT 后 `tests/f-reminder.test.ts` **16/16 绿**。

## 3) `CHANGELOG.md` 历史可见性措辞

- 位置:`CHANGELOG.md:709-710`(任务卡写 `673-674` 是 #102/#106 合入前的行号,行号漂移已在 #105 报告
出现过同类问题)。
- 旧:`` (REV-8)。OSC 7501 / legion viz 侧可见。``
- 新:

```
(REV-8)。这是 **id 形状约束,不是可见性承诺**:写入唯一走宿主的 `ui.setActivity`
(feature-detect;官方 pi 1.2–1.5 无该 API 时是 no-op),本仓库不自行发 OSC 转义,
legion 侧要等上游 #156 的转发通道。
```

### 等价证据(不是「改到能过」)

旧断言的三个子句逐个可证伪,且今天都不成立:

1. **「OSC 7501 可见」没有生产者**:`grep -rn "7501\|OSC" src extensions` 只命中 `src/core/output.ts`
的转义剥离说明、`src/core/bus.ts` 的「never emit terminal OSC/printf escapes」、`src/guard/hold.ts`
里对宿主 OSC 状态的**描述**;没有任何代码发 OSC 7501。
2. **「legion viz 侧可见」没有传输通道**:`src/core/bus.ts:12` 自己标注 per-task id 是
「legion #156 / osc-busy-plugin」的**形状**目标;CONTRACT §7 的 RPC 段写明依赖上游 #156 的
`extension_activity` 转发,未落地前 activity 字段恒缺。
3. **`ui.setActivity` 在官方 pi 1.2–1.5 不存在**:`src/core/bus.ts` 三个 shim 都 feature-detect。

因此把「可见」降级为「形状约束、不是可见性承诺」;这是 #105 §2.4 留下的判断(引不到具体行为就降级),
不是新增承诺。prose-only,无测试可红——用上面的 grep/代码证据作为等价证据。

## 验证

基线(`origin/main` @ `d0407d1`,本机 macOS):

```
npm run typecheck # pass
npm test # tests 474 / pass 445 / fail 16 / skipped 13
```

改动后:

```
npm run typecheck # pass
npm test # tests 480 / pass 451 / fail 16 / skipped 13
```

- 新增 6 条测试(a-bus +1、a-settle-guard +2、f-reminder +3),**没有删除/跳过任何既有测试**。
- **失败集合逐条相同**(16 条,均为本机 macOS 的既存环境失败;CI 是 Ubuntu):
`/var/folders` vs `/private/var` realpath 导致 `assertInsideDataDir` 拒绝 **5** 条;`kill EPERM` 1 条;
hold/rail 时序 **9** 条;`tests/g-revive-after-suspend.test.ts` 文件级 1 条(tmp 文件 ENOENT 竞态)。
- 13 条 skip 全部是既有的 `# SKIP needs /proc process-group probing (CI is ubuntu)`,未新增 skip,
无 `SKIPPED:` 行。
- 失败数 16 → 16、skip 13 → 13、pass +6 = 新增测试数;**无回归**。

## 变异矩阵

| # | 变异 | 期望 | 实测 |
|---|---|---|---|
| 1 | 恢复 settle-guard 私有 `reportActivity` 副本 | a-settle-guard 红 | 3 fail / 4 pass(文案、结构、抛错三条) |
| 2a | CONTRACT §18.11 `时 6 个` → `时 5 个`,跑旧门 | **旧门漏检(应为绿)** | 旧门 13/13 pass(盲区复现) |
| 2b | 同一文本,跑新门 | 红 | 1 fail,finding 点名 §18.11 |
| 3 | CHANGELOG 旧措辞 | prose-only,无测试 | 用「无 OSC 生产者 / 依赖 #156 / feature-detect」三条代码证据替代 |

## 变更文件

```
CHANGELOG.md | 4 +-
docs/CONTRACT.md | 21 +++--- # §7:实现收敛 + 三处 shim
docs/reports/pi-async-guard-gaps.md # 本文件
src/core/bus.ts | 30 +++++---
src/guard/settle-guard.ts | 39 ++++----
tests/a-bus.test.ts | 19 ++++-
tests/a-settle-guard.test.ts | 55 +++++++++++-
tests/f-reminder.test.ts | 101 ++++++++++++++++++++-
tests/w2-safety.test.ts | 9 +-
```

没有 `git add -A`;`node_modules/`(typecheck 需要)被 `.gitignore` 覆盖,未入库。

## 未做 / 后续

- `raiseTaskActivity` / `clearTaskActivity` 仍未加 `try/catch`:不在四件事内。stale ctx 下它们仍可能从
扩展回调逃逸(与本次收敛的聚合路径不同源),建议另开 issue。
- `1 async tasks` 的语法瑕疵:若想修,应在 bus 一处改成单复数 helper;那会改 3 个共享调用点在
`count == 1` 时的文案,属于独立变更。
- 0.5.2 的 `[Unreleased]` 段未加条目:该段叙事是 #102 存储生命周期;本次用户可见变化只有单任务
activity 文案,已在报告记录。
30 changes: 21 additions & 9 deletions src/core/bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,27 @@ export function taskActivityId(taskId: string): string {
return `plugin/pi-async/${taskId}`;
}

/** Report aggregate async work through Pi's activity API when the host exposes it. */
export function reportActivity(pi: PiCtx, active: { running: number; pending: number }): void {
// SAFETY: PiCtx is the installed extension API; ui.setActivity is feature-detected for versions without it.
const ui = (pi as unknown as { ui?: { setActivity?: (id: string, activity: { message: string } | undefined) => void } }).ui;
if (typeof ui?.setActivity !== "function") return;
const count = active.running + active.pending;
ui.setActivity(AGGREGATE_ACTIVITY_ID, count === 0 ? undefined : { message: `${count} async tasks` });
// Activity uses the host API; never emit terminal OSC/printf escapes. In particular,
// leave PI_PROGRAM_STATUS untouched for hosts which own that status channel.
/**
* Report aggregate async work through Pi's activity API when the host exposes it.
*
* Returns `false` only when the host rejected the call (a stale ctx throws on `pi.ui` or
* `setActivity`); callers may use that signal to detach a dead generation. A host without the API
* returns `true` — nothing failed, there is just nothing to report. The throw is swallowed here so
* a best-effort activity write can never decide a settle boundary (0.3.2 stale-ctx fix).
*/
export function reportActivity(pi: PiCtx, active: { running: number; pending: number }): boolean {
try {
// SAFETY: PiCtx is the installed extension API; ui.setActivity is feature-detected for versions without it.
const ui = (pi as unknown as { ui?: { setActivity?: (id: string, activity: { message: string } | undefined) => void } }).ui;
if (typeof ui?.setActivity !== "function") return true;
const count = active.running + active.pending;
ui.setActivity(AGGREGATE_ACTIVITY_ID, count === 0 ? undefined : { message: `${count} async tasks` });
// Activity uses the host API; never emit terminal OSC/printf escapes. In particular,
// leave PI_PROGRAM_STATUS untouched for hosts which own that status channel.
return true;
} catch {
return false;
}
}

/** Raise task-scoped activity when the host supports Pi's optional activity API. */
Expand Down
Loading