Skip to content

feat(migration): 重建 OpenCode 原生 subagent 会话 - #31

Open
yororoA wants to merge 6 commits into
mainfrom
fix/subagent-session-migration
Open

yororoA wants to merge 6 commits into
mainfrom
fix/subagent-session-migration

Conversation

@yororoA

@yororoA yororoA commented Sep 29, 2026 •

Copy link
Copy Markdown
Owner

问题

TRAE 会把 subagent 执行记录合并进主 session。记录中既包含空工具占位符和矛盾时间,也包含可恢复的 agent_run_id / parent_agent_run_ids 拓扑。此前整个会话会被拒绝;第一版修复只能扁平导入,无法在 OpenCode 中显示为真正的 subagent。

修复

  • 忽略结构为空的工具占位符,非空非法调用仍保持 fail-closed
  • 对有明确诊断证据的 partial session 归一矛盾完成时间,并保留原始时间
  • 按唯一且一致的 run、父链、agent、任务描述、状态和 timing 重建直接 subagent 子会话
  • 在子会话写入 info.parentID,在父 assistant 消息写入原生 subagent tool,并通过 state.metadata.sessionID 关联
  • 选择根会话时自动包含派生子会话;跨消息、不完整、冲突或嵌套 run 保守保持内联
  • mapping 升级到 v10,继续识别 v1-v9 所有权记录用于安全覆盖和回滚

验证

  • 真实失败会话:重建 1 个父会话和 21 个子会话,dry-run ready=22、blocked=0
  • OpenCode 2.0.18 隔离原生导入:22 个会话全部导入并回读
  • 父消息 21 个 subagent tool 的 sessionID 与 children API 返回的 21 个子会话完全一致
  • npm run check:489 项测试通过
  • npm run verify:opencode -- --json
  • npm run verify:versions
  • npm run verify:package
  • OpenCode v1 1.18.32 / 1.17.9 集成回归通过

产品版本和 CHANGELOG 继续由 release-please 管理。

Summary by Sourcery

Rebuild verifiable inline TRAE subagent activity as safely linked native OpenCode child sessions while preserving diagnostics and compatibility across supported protocols.

New Features:

  • Reconstruct verifiable TRAE subagent runs as native OpenCode parent-child sessions across v1 and v2 protocols.

Bug Fixes:

  • Ignore empty TRAE tool placeholders while continuing to reject non-empty invalid tool calls.
  • Normalize contradictory completion times for diagnostically supported partial sessions while preserving the original timing evidence.
  • Keep ambiguous, conflicting, incomplete, cross-message, or nested subagent runs inline instead of forcing unsafe reconstruction.

Enhancements:

  • Extend migration selection, manifest ownership, validation, and rollback handling to cover derived session graphs.
  • Upgrade mapping semantics to version 10 with stable subagent identities, native parent links, and dialect-specific tool references.

Documentation:

  • Document native subagent migration behavior, compatibility requirements, conservative reconstruction boundaries, and troubleshooting guidance.

Tests:

  • Add coverage for subagent parsing, reconstruction, identity linking, graph selection, timing projection, and OpenCode v1/v2 compatibility round trips.

Chores:

  • Update the migration bundle schema and intermediate representation for derived sessions, subagent-linked tools, and text presentation metadata.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Sorry @yororoA, you've used your own review budget of 250,000 diff characters for the last 7 days.

You can request another review in 20 hours and 21 minutes by commenting @sourcery-ai review. Upgrade to get a review now.

@sourcery-ai

sourcery-ai Bot commented Sep 29, 2026

Copy link
Copy Markdown

Reviewer's Guide

Mapping v9 adds targeted recovery for TRAE sessions containing merged subagent history: empty tool placeholders are ignored, proven-invalid assistant completion times are clamped only in partial sessions with full source evidence retained, and both OpenCode mapping paths expose the projection metadata while preserving v1–v8 ownership compatibility.

Sequence diagram for mapping a TRAE subagent session

sequenceDiagram
    participant Bundle as TRAE bundle
    participant Parser as parseTraeToolCalls
    participant Mapper as mapOpenCodeSession
    participant Target as OpenCode session

    Bundle->>Parser: parseTraeToolCalls
    Parser-->>Mapper: tool calls
    Note over Parser: Empty name + null/empty params/result is ignored
    Parser-->>Mapper: Non-empty invalid call fails closed
    Bundle->>Mapper: assistant event and diagnostics
    Mapper->>Mapper: projectedAssistantTime
    alt partial session with T2O_IR_EVENT_TIME_INVALID evidence
        Mapper->>Target: Clamp completed time to created time
        Mapper->>Target: Store source and target times in metadata
    else no explicit invalid-time evidence
        Mapper-->>Bundle: Reject session
    end
Loading

File-Level Changes

Change Details Files
Relax parsing for structurally empty subagent tool placeholders while preserving fail-closed validation for malformed non-empty calls.
  • Treat null and undefined parameters as empty-placeholder cases when name and result are also empty.
  • Add coverage for null placeholders and mixed malformed calls.
  • Keep invalid non-empty tool calls diagnostic and blocking.
src/source/trae/tool-calls.ts
src/source/__tests__/tool-calls.test.ts
src/source/__tests__/assemble-bundle.test.ts
Normalize contradictory assistant event times only when partial-session diagnostics prove the source timestamp is invalid, and preserve the projection evidence.
  • Detect source completion times earlier than creation times using event and source-reference diagnostics.
  • Clamp target completion to creation time only for partial sessions with T2O_IR_EVENT_TIME_INVALID.
  • Record source/target timestamps and projection reason in message or v1 provenance metadata.
  • Emit T2O_OPENCODE_EVENT_TIME_PROJECTED while continuing to reject unsupported contradictory timestamps.
src/target/opencode/mapping.ts
src/target/opencode/v1/mapping.ts
src/target/__tests__/opencode-mapping.test.ts
src/target/__tests__/opencode-v1-mapping.test.ts
Upgrade OpenCode mappings to v9 while retaining ownership compatibility with prior mapping versions.
  • Centralize the current mapping version as OPENCODE_MAPPING_VERSION = 9.
  • Apply v9 markers to sessions and continuation boundaries for both mapping paths.
  • Allow ownership checks to recognize verified mappings from v1 through v9 for overwrite and rollback.
src/target/opencode/mapping.ts
src/target/opencode/v1/mapping.ts
src/migration/ownership.ts
src/target/__tests__/opencode-mapping.test.ts
Document subagent recovery behavior, timestamp projection semantics, and the v9 migration path.
  • Explain that empty placeholders are ignored but unverifiable tool data still stops migration.
  • Update operational and troubleshooting guidance from v8 to v9.
  • Document v1 provenance storage for projected timing and update the mapping version table.
docs/operation-manual.md
docs/troubleshooting.md
docs/versioning.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@yororoA yororoA changed the title fix(mapping): 支持包含 subagent 调用的会话 feat(migration): 重建 OpenCode 原生 subagent 会话 Sep 29, 2026
本次更新实现了子会话工具在 v1 和 v2 方言下的差异化适配:
1. 文档中补充了 v1 使用 task 工具、v2 使用 subagent 工具的兼容说明
2. 新增 observedSubagentRunIds 字段跟踪观测到的子会话运行 ID
3. 实现 v1 映射时将 subagent 工具转换为 task 工具,并调整相关元数据字段名
4. 新增兼容性验证逻辑,确保不同方言下子会话链接的正确性
5. 补充对应测试用例,覆盖子会话映射的核心场景

This branch has not been deployed

No deployments
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.

1 participant