Skip to content

delivery: 统一后台完成事件的 owner 路由、epoch 与消费收据合同 #160

Closed

Description

@tt-a1i

一句话结论

OpenPI 的 Direct Subagent、Background Terminal、Workflow 都已实现“完成后投递给父会话”,但分别维护三套 pending map、消费、重试、持久化与 wake 语义。

建议抽取一个薄的 completion inbox contract:生产者仍拥有终态和 artifact,inbox 只负责 owner 路由、generation/epoch、防重复消费、投递收据和 wake policy。不要把三套执行状态机合并。

固定证据

对比固定在:

  • OpenPI main@2a69d3f32994da4123f1312b7fa84ef3d6119be1
  • OMP main@151dc754e8cd9314721aa6c78a168a205a4b0e4c

OMP 当前:

  • 每个 AgentSession 用自己的 agent id 注册 delivery sink,避免所有 completion 被硬路由到第一个 top-level Session:owner-routed contract
  • delivery entry 带 Session generation;/new、switch、handoff 后旧 epoch 会被丢弃,即使 job id 被复用:epoch contract, format/enqueue fence
  • owner sink late cleanup不会删除 revival 后的新 sink;没有 live owner 时 dead-letter,不会误投给其他 Session,结果仍暂存于 job row:sink replacement, dead-letter boundary
  • sink 失败采用有界退避重试,成功后才消费结果:delivery receipt path

OpenPI 当前:

  • Direct Subagent 使用内存 pending map;auto-delivery 与 explicit wait 谁先 drain 谁消费,delivery throw 会恢复批次:subagent delivery
  • Background Terminal 使用另一套 pending map + idle batcher,并在 idle 时 followUp/triggerTurn、busy backlog 时 nextTurn:terminal pending, wake policy
  • Workflow 有第三套 durable delivery plane:稳定 delivery id、per-run receipt、retry、restart restore;transport 成功但 receipt 落盘失败时采用 at-least-once recovery:workflow delivery contract, receipt/retry

为什么值得讨论

三条实现分别解决了真实竞态,但合同逐渐漂移:

  • 只有 Workflow completion 可跨 process restart 恢复;
  • Direct Subagent 会主动重新唤醒 parent,Terminal 对 busy backlog 选择 nextTurn;
  • stable identity、attempt、receipt、dead-letter、owner-lost 的表达不同;
  • Session switch / branch / resume 后 stale completion 的判定没有统一 epoch vocabulary;
  • 修过的一类竞态需要在三条路径重新证明。

共享的应是 transport/inbox 不变量,不是 producer lifecycle。

OpenPI 应保留的优势

  • WorkflowDetails、SubagentSnapshot、TerminalSnapshot 仍是各自 canonical execution facts;
  • inbox 不从 UI label 推断完成;
  • producer 自己决定何时 terminal、artifact 在哪里、取消如何结算;
  • wake policy 可按 producer 和 client capability 配置,不统一成“一完成就强制起 turn”;
  • fail closed:不确定 owner/epoch 时保留可检查状态,不误投。

建议的最小 envelope

示意:

type CompletionEnvelope = {
  deliveryId: string;
  ownerSessionId: string;
  ownerEpoch: number;
  producer: "subagent" | "workflow" | "background";
  producerId: string;
  terminalRef: unknown;
  wake: "follow-up" | "next-turn" | "producer-policy";
}

具体 API 需先确认 Pi 是否已有 completion/yield queue seam;若有,应扩展 Pi seam,而不是 OpenPI 自建第二个 Session mailbox。

必须明确的语义

  • exactly-once 是“模型可见消费”还是 transport acceptance?崩溃窗口下能否真正保证?
  • transport 成功、receipt 失败时是 at-least-once,并如何让模型识别 replay?
  • explicit wait/status 与 automatic delivery 如何原子争抢消费权?
  • owner Session switch、fork、resume、dispose、revive 时 epoch 如何变化?
  • 无 live owner 是 dead-letter、durable pending,还是迁移到新 owner?
  • client 不支持 agent-initiated turn 时如何降级为 next-turn/card?
  • 一批 completion 部分成功时如何只重试未确认 sibling?

非目标

  • 不统一三类执行状态机;
  • 不把 producer result 内联进 inbox storage;
  • 不声称分布式 exactly-once;
  • 不让 completion 自动决定 Goal/Task 完成;
  • 不强制所有 producer 采用相同 wake 行为;
  • 不把 delivery success 当 execution success。

完成条件

  • 明确 canonical execution、delivery envelope、model context、UI projection 四层边界
  • envelope 有稳定 delivery id、owner identity、epoch、producer identity 和 terminal reference
  • wait/status 与 auto-delivery 并发只产生一次模型可见消费
  • transport/receipt 失败、process crash、session switch/resume、owner dispose/revive 全部有测试矩阵
  • stale completion 不会进入新 transcript,结果仍可从 producer canonical state 找回
  • partial batch receipt 只重试未确认 envelope
  • wake policy 尊重 interactive/print/RPC/ACP client 能力
  • 迁移后删除重复机制,不能在共享层外继续保留三套消费 gate

与现有 Issue 的关系

Activity

  1. added
    enhancement
    on Aug 25, 2026
  2. 627150795 commented on Aug 28, 2026

    @627150795
    Contributor

    Hi! I would like to work on the documentation/design part of this issue.

    I will define a thin completion-inbox contract across Direct Subagent, Background Terminal, and Workflow: producer ownership, destination Session, epoch, delivery identity, consumption receipts, wake policy, duplicate/crash handling, fork/resume boundaries, and at-least-once limits. I will not merge the three execution state machines or add a global task store in this contribution.

  3. 627150795 commented on Aug 28, 2026

    @627150795
    Contributor

    Update: the completion inbox contract is implemented in PR #254: #254

    Validation completed: git diff --cached --check, bun run lint, and bun run typecheck passed. This is documentation-only; the three producer state machines and their owners remain unchanged, with no global task store or exactly-once claim. The PR uses the repository template and will close this issue when merged.

  4. testikun commented on Sep 4, 2026

    @testikun
    Collaborator

    我来认领 #160 尚未落地的运行时代码阶段,不重复此前的 completion-inbox 设计文档。

    技术方案:抽取只负责 transport/inbox 不变量的共享 primitive,并迁移 Direct Subagent、Workflow、Background Terminal 三条完成投递;producer 仍拥有终态、artifact、取消和恢复。envelope 具有稳定 delivery id、owner Session identity/epoch、producer identity、terminal reference 与 wake policy;wait/status 与 auto-delivery 原子争抢消费权,transport/receipt failure 可重试,stale epoch 不进入新 transcript,partial batch 只重试未确认项。不会合并三套执行状态机,也不宣称崩溃窗口下 exactly-once。

    已完成:

    验证:

    • bun run check:通过
    • bun run test:1,253 passed / 0 failed / 1 skipped;Vitest 30 passed
    • Node 22 全量:1,253 passed / 0 failed / 1 skipped;Vitest 30 passed
    • shared inbox + Workflow delivery/lifecycle e2e 聚焦:40 passed / 0 failed

    该 PR 不修改 UI,因此真实测试截图不适用。

  5. tt-a1i commented on Sep 8, 2026

    @tt-a1i
    CollaboratorAuthor

    补充本轮 Pi 社区源码证据,作为 #160 投递合同的回归参考,不主张重新建立一个统一后台运行时。

    固定来源:nicobailon/pi-subagents@56f247ac86169cfffa1af9a4c07997c33804bf9a(MIT,package 0.66.0)。本轮仅阅读源码,没有安装或运行该插件。

    它有三个值得保留在我们的交付测试矩阵中的明确边界:

    1. 事件广播不是交付回执。 notify.ts 的模块合同 明确事件总线只负责观察;发送函数 仅在 pi.sendMessage 正常返回后报告 accepted,抛错返回 false。accepted 仍不等于模型已经消费结果。
    2. 延迟批次发出前重新核验 owner。 入队和发出时的 owner 检查 防止入队后 Session 切换导致结果误投;predecessor ownership 对可接管的旧 Session 单独记账,不能因为 cwd 相同就归到当前 Session。
    3. 重入调用共享 pending promise,发送成功后才进入去重窗口。 settle 与 deliver、pending/seen 处理 避免把“开始尝试交付”当作“已交付”。这里的 seen 是进程内 10 分钟 TTL,不能当作跨重启 exactly-once 或持久消费证据。

    与之相邻但必须分开的,是结果文件本身的提交。ismailsaleekh/pi-background-tasks@14aa4ef382952f073bd4d540f57d6e8e3c2789a2(ISC)使用 单个 result package,验证 task/launch/seed/model 身份和结果哈希,并从同一验证 buffer 返回答案;durable-fs 区分 rename 前失败与 rename 已完成后 directory fsync 失败。这个模式能帮助 #196 明确 artifact committed 与 completion delivered 的区别,不能把本地同用户可修改的哈希文件称作远程安全证明。

    建议后续维护时保留这组精确场景:批次等待期间切 Session、同一结果并发投递、send 抛错后重试、发送成功但收据落盘前崩溃、artifact 提交成功但通知失败。各 producer 继续保有自己的取消、持久化和恢复语义;无需导入这些插件或新增一套 job manager。pi-background-tasks 当前声明的 Pi peer 范围截至 ^0.84.0,不覆盖我们正在使用的 0.85.1,运行兼容性也未验证。

    研究记录:Pi 社区能力研究与候选清单(2026-09-08)。源码证据、运行验证与采用决策分别记录。

  6. tt-a1i commented on Sep 9, 2026

    @tt-a1i
    CollaboratorAuthor

    SHA-pinned checkout note (2026-09-09). Not a reopen and not a rebase request.

    What closed this Issue is squash #382 9d197995d3d3de32b694224ac7924a312c5f3fc2 (2026-09-05T15:11:39Z; parent is same-day #373 3e056a48). That commit is an ancestor of origin/main 0d17f4577fe31315fe6c95370d251bdb4e2413cf and tag v0.8.1 (a3edee28). It is not an ancestor of checkout a9b40f0044ee59c360a6077c1c0bdbdbd30da10b (package 0.4.0). On that tree the three producer-local pending maps remain; extensions/shared/completion-inbox.ts is absent.

    #254 (0d9f76095d73, docs-only COMPLETION_INBOX_CONTRACT.md) is a #277 source record: CLOSED, mergedAt: null. Do not rebase it. #382 does not cite #254 or #277. The shipped record is a different file, docs/design/COMPLETION_INBOX.md blob f8392d19. Shared primitive extensions/shared/completion-inbox.ts blob 3c939474 is byte-identical from the squash through v0.8.1 to 0d17f45.

    That primitive is a per-adapter consumption gate plus a process-local SessionManager epoch (completionOwnerFor), not a second Session mailbox and not a third runner. Canonical facts stay on SubagentSnapshot / TerminalSnapshot / WorkflowDetails. Pi followUp / nextTurn are the model-visible layer. TUI Footer / /ps / Web chips are projections and do not read the inbox.

    #196 stays the background-ownership document ticket. This close is owner / epoch / receipt, not that umbrella. Issue body checkboxes were not rewritten when GitHub marked this COMPLETED.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions