Skip to content

fix(runtime): preserve canonical tool argument ownership - #884

Merged
Astro-Han merged 1 commit into
apache:mainfrom
M4n5ter:fix/856-canonical-argument-ownership
Jul 14, 2026
Merged

Astro-Han merged 1 commit into
apache:mainfrom
M4n5ter:fix/856-canonical-argument-ownership

Conversation

@M4n5ter

@M4n5ter M4n5ter commented Jul 13, 2026 •

Copy link
Copy Markdown
Member
English

Follow-up to #856.

Summary

  • Give each tool invocation one private canonical argument snapshot before Runtime crosses any asynchronous ownership boundary.
  • Pass independent copies to persistence, session events, permission evaluation, the sandbox resolver, tool execution, and asynchronous artifact recording.
  • In the Pi backend, make the first accepted permission_request or tool_start frame authoritative for a toolUseId, and fail closed if a later frame changes the tool name or arguments.
  • Snapshot each delivered Pi tool_start frame before awaiting assistant persistence, so transport-owned mutation cannot rewrite or conceal frame drift.

Why

Tool arguments currently cross several independently owned and potentially asynchronous consumers. Sharing one mutable object lets an event listener, permission consumer, sandbox resolver, storage callback, or tool implementation change what another consumer observes or what eventually executes.

Pi has a related transport boundary: permission and start frames can describe the same call at different times. Without one canonical value per toolUseId, history, authorization, events, and execution can diverge even though they appear to refer to one invocation.

Key decisions

Decision Rationale
Keep one private snapshot inside Runtime and copy only when ownership is transferred. The execution fact has one source of truth without exposing a mutable shared reference or introducing a general immutability framework.
Treat Pi's first accepted frame as authoritative and deep-compare later frames. Both valid frame orders work, while transport drift fails before execution instead of silently choosing a newer value.
Snapshot a Pi tool_start frame before the first asynchronous persistence boundary. The transport still owns the delivered object; capturing it at ingress closes the aliasing window without changing durable message order.
Validate frames even when a call is already denied or policy-suppressed. Suppression must not hide an inconsistent transport sequence; existing assistant/tool-call persistence order remains unchanged.
Keep canonical identity separate from owner-specific projections. Permission, persistence, and events retain their existing shapes; ordinary Pi history/events stay redacted and Computer Use stays summarized, while no projected value can mutate the private invocation snapshot.

Scope

Included:

  • AI SDK Runtime argument ownership
  • Pi first-frame canonicalization and repeated-frame consistency
  • Ownership isolation for the sandbox resolver added on the current Runtime path
  • Behavioral coverage that mutates each external copy and verifies execution remains canonical

Not included:

  • Permission request shape or policy changes
  • TUI permission queue behavior
  • WriteStdin inspection, turn-memory, or runtime-resource contract changes
  • A deep-freeze wrapper, immutable data framework, or second payload

Verification

  • Root npm run build and npm run typecheck passed.
  • Runtime passed 1,555 tests with 7 existing platform skips.
  • git diff --check upstream/main...HEAD passed.

No visual evidence is applicable because this change has no intended UI or interaction change.

Impact

Tool providers and Runtime consumers receive ordinary mutable values, but mutations are now local to that owner. Existing redaction and approval-summary projections are unchanged; persisted history, permission facts, renderer events, sandbox decisions, and execution can no longer rewrite one another through shared references.

There is no schema migration, compatibility layer, dependency change, documentation requirement, or release-note impact.

Reviewer notes

The highest-value review boundaries are:

  1. tool-runtime.ts: where the private snapshot is created and where ownership is transferred.
  2. pi-agent-backend.ts: first-frame authority, suppressed calls, and persistence ordering.
  3. ai-sdk-flow.ts: transferring projected session-event arguments into independently owned Runtime events.

Ready for review

  • Verification is complete and the results above are current
  • Impact, migration, documentation, and release-note needs are addressed
  • Visual evidence is not applicable and the reason is stated
简体中文

#856 的后续改进。

摘要

  • 在 Runtime 跨越任何异步所有权边界前,为每次工具调用建立一份不外借的 canonical 参数快照。
  • 分别向持久化、session event、权限评估、sandbox resolver、工具执行和异步 artifact recording 交付独立副本。
  • 在 Pi backend 中,让某个 toolUseId 首个被接受的 permission_request 或 tool_start 帧成为权威值;后续帧若改变工具名或参数则 fail closed。
  • 在等待 assistant 持久化前快照每个已交付的 Pi tool_start 帧,避免 transport 持有的对象被修改后改写首帧或掩盖帧漂移。

原因

工具参数会跨越多个彼此独立、且可能异步运行的消费者。共享同一个可写对象后,event listener、权限消费者、sandbox resolver、存储回调或工具实现都可能改变其它消费者看到的值,甚至改变最终执行内容。

Pi 还有一层对应的 transport 边界:permission 与 start 帧可能在不同时间描述同一调用。如果没有按 toolUseId 唯一确定的 canonical 值,历史、授权、事件与执行可能发生漂移,尽管它们表面上都指向同一次调用。

关键决策

决策 理由
Runtime 内部只保留一份私有快照,仅在转移所有权时复制。 让执行事实只有一个来源,同时不暴露共享可写引用,也不引入通用 immutable 框架。
以 Pi 首个被接受的帧为权威,并深度比较后续帧。 两种合法帧顺序都能工作;transport 漂移会在执行前失败,而不是静默选择较新的值。
在首次异步持久化边界前快照 Pi tool_start 帧。 Transport 仍持有已交付的对象;在入口捕获它可以关闭别名窗口,同时不改变 durable message 顺序。
即使调用已被拒绝或被 policy 抑制,也必须验证后续帧。 Suppression 不能掩盖不一致的 transport 序列;现有 assistant/tool-call 持久化顺序保持不变。
将 canonical identity 与各所有者所需的 projection 分离。 权限、持久化与事件保留既有 shape;普通 Pi history/event 继续脱敏,Computer Use 继续使用摘要,同时任何 projection 都无法修改私有调用快照。

范围

包含:

  • AI SDK Runtime 参数所有权
  • Pi 首帧 canonicalization 与重复帧一致性
  • 当前 Runtime 路径新增 sandbox resolver 的参数所有权隔离
  • 主动修改每个外部副本、并验证执行仍保持 canonical 的行为测试

不包含:

  • 权限请求 shape 或 policy 变更
  • TUI permission queue 行为
  • WriteStdin 检查、turn memory 或 runtime-resource 契约变更
  • Deep-freeze wrapper、immutable data framework 或第二套 payload

验证

  • 根级 npm run build 与 npm run typecheck 通过。
  • Runtime 1,555 项测试通过,并保留 7 项既有平台 skip。
  • git diff --check upstream/main...HEAD 通过。

本次没有预期的 UI 或交互变化,因此不适用视觉证据。

影响

工具 provider 与 Runtime 消费者仍会收到普通可写值,但任何修改只影响该所有者。既有脱敏与 approval-summary projection 保持不变;持久化历史、权限事实、renderer event、sandbox 决策和执行不再能通过共享引用相互改写。

本次没有 schema migration、兼容层、依赖变更、文档要求或 release-note 影响。

审查重点

最值得审查的边界是:

  1. tool-runtime.ts:私有快照的建立位置和所有权转移位置。
  2. pi-agent-backend.ts:首帧权威、suppressed call 与持久化顺序。
  3. ai-sdk-flow.ts:把经过 projection 的 session-event 参数转移给独立所有的 Runtime event。

可供审查

  • 验证已经完成,以上结果均为当前结果
  • 影响、迁移、文档与 release-note 需求均已说明
  • 已说明不适用视觉证据的原因

@Astro-Han Astro-Han 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.

No blocking code findings on the current head.

GitHub currently reports merge conflicts. Please rebase onto the latest main and preserve the existing separation between permission, execution, persisted, and event arguments while resolving them. The resolved diff may need another look.

@M4n5ter
M4n5ter force-pushed the fix/856-canonical-argument-ownership branch from f763ec4 to 08b0a10 Compare July 14, 2026 04:21
@M4n5ter

M4n5ter commented Jul 14, 2026 •

Copy link
Copy Markdown
Member Author
English

@Astro-Han I rebased this branch onto the latest main and resolved the conflicts.

The resolved implementation preserves the separation you called out:

  • the canonical invocation snapshot remains private;
  • permission, sandbox resolution, execution, persistence, events, and asynchronous artifact recording receive independently owned values;
  • existing projections remain intact: ordinary Pi history/events are redacted, while Computer Use uses its approval summary;
  • repeated Pi frames are still checked against the first raw snapshot, including calls already suppressed by policy.

I also corrected the PR description so it no longer implies that persisted/event arguments are raw canonical data. Local build, root typecheck, and the complete Runtime suite pass on the rebased head; GitHub typecheck, test, and e2e CI also pass.

简体中文

@Astro-Han 我已将该分支 rebase 到最新 main 并解决冲突。

冲突解决后的实现保留了你指出的分层边界:

  • canonical 调用快照仍然保持私有;
  • 权限、sandbox 解析、执行、持久化、事件与异步 artifact recording 分别接收独立所有的值;
  • 既有 projection 保持不变:普通 Pi history/event 继续脱敏,Computer Use 继续使用 approval summary;
  • Pi 后续帧仍会与首个原始快照比较,包括已经被 policy 抑制的调用。

我也修正了 PR 描述,不再暗示持久化或事件参数是未经 projection 的原始 canonical 数据。Rebase 后的本地 build、根级 typecheck 与完整 Runtime 测试均已通过,GitHub typecheck、test 与 e2e CI 也已通过。

@Astro-Han
Astro-Han merged commit caa7ed6 into apache:main Jul 14, 2026
3 checks passed
@M4n5ter
M4n5ter deleted the fix/856-canonical-argument-ownership branch August 5, 2026 13:31
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