Skip to content

[建议 / Feature] PreToolUse / SessionStart hook stdin 增加 parent_session_id 字段 / Expose parent_session_id in hook stdin #236

Closed as not planned

Description

@earneet

提交前确认 · Pre-submission checklist

  • 我已搜索过现有 issue,确认这不是重复提议 / I searched existing issues and confirmed this isn't a duplicate.
  • 我已阅读 CONTRIBUTING.md / I've read CONTRIBUTING.md.

问题类别 · Category

工具调用 / MCP · Tool use / MCP

涉及的 Agent 框架 · Agent framework

ZCode Agent(自研)

使用场景 · Use case

我在开发 zcode-worktree-guard 插件,强制 git worktree 工作流(PreToolUse 透明路径重写 + 主分支写保护)。该插件需要实现子代理继承父 session 的 worktree 绑定——子代理(Agent/Task 派生)应和父代理共享同一个 worktree 副本,而非各自创建。

ZCode 的 SQLite session 表已存储 parent_session_id 列,但该信息未暴露到 hook stdin。我通过源码探索确认,PreToolUse 和 SessionStart hook 的 stdin JSON 中不包含任何 parent 字段(详见下方证据)。

当前 workaround 是插件直读 ZCode 的 SQLite DB 查询 session.parent_id:

const row = db.prepare("SELECT parent_id FROM session WHERE id = ?").get(sessionId);

I am developing the zcode-worktree-guard plugin, which enforces a git worktree workflow (PreToolUse transparent path rewriting + main-branch write protection). The plugin needs to implement child-agent inheritance of the parent session's worktree binding — subagents (spawned by Agent/Task) should share the parent's worktree rather than creating their own.

ZCode's SQLite session table already stores a parent_session_id column, but this information is not exposed in the hook stdin. Through source-code exploration, I confirmed that the stdin JSON for PreToolUse and SessionStart hooks contains no parent field (see evidence below).

The current workaround is for the plugin to directly read ZCode's SQLite DB to query session.parent_id — this creates a schema coupling risk.

建议方案 · Proposal

在 PreToolUse 和 SessionStart hook 的 stdin JSON 中增加 parent_session_id 字段。

具体变更:在 hook stdin 构造器(createClaudeCompatibleHookStdin)构造的对象中,透传 parent_session_id:

let t = {
  ...e,
  session_id: e.sessionId,
  parent_session_id: e.parentSessionId ?? null,  // ← 新增
  // ...existing fields
};

同时在 PreToolUse / SessionStart 的事件对象构造处,从 session 上下文填充 parentSessionId(该信息在 session 行中已有)。

语义:

  • 顶层会话的 parent_session_id 为 null(明确语义,而非"字段缺失")
  • snake_case 命名,与现有 session_id、hook_event_name 一致
  • 向后兼容:新增字段,不读取该字段的插件不受影响

Add a parent_session_id field to the stdin JSON for PreToolUse and SessionStart hooks, populated from the session context. Top-level sessions get null. Backward compatible (new field, existing plugins unaffected).

预期价值 · Expected value

  1. 消除 DB schema 耦合:插件不再需要直读宿主 SQLite DB,ZCode 升级修改 session 表结构不会破坏插件继承机制

  2. 符合最小权限原则:插件不应访问宿主内部 DB

  3. 与生态对齐:opencode 通过 client.session.get(id).parentID API 暴露 parent,跨工具一致性更好

  4. Removes DB schema coupling — plugins no longer read the host SQLite DB; ZCode upgrades won't break inheritance.

  5. Follows least-privilege — plugins shouldn't access internal DBs.

  6. Aligns with ecosystem conventions (opencode exposes parent via client API).

你认为的优先级 · Your perceived priority

中 · Medium

你使用的 ZCode 版本 / 环境 · ZCode version / environment

ZCode Desktop App v3.7.3,Windows 11 x64 / Node.js v24.15.0

补充材料 · Additional context

源码证据(基于 ZCode v3.7.3 bundle zcode.cjs 探索):

hook stdin 构造器 createClaudeCompatibleHookStdin(约 L2942)构造的 JSON 字段:

PreToolUse(事件源 L2460):session_id, cwd, agent_type, hook_event_name, permission_mode, transcript_path, tool_name, tool_input, tool_use_id, traceId, turnId, riskLevel, sideEffectScope, timestamp, toolCallId — 无 parent

SessionStart(事件源 L3094):session_id, cwd, agent_type, hook_event_name, permission_mode, model, source, timestamp, traceId, turnId — 无 parent

parentSessionId 在 bundle 中仅出现在:SQLite session 表 Zod schema(L43, L57)、DB 行映射(L1019)、OpenTelemetry span 属性(L2958 等)、子 session spawn 上下文(L3062 等)——从未进入 hook stdin。

相关插件实现:

Activity

  1. github-actions commented on Aug 11, 2026

    @github-actions

    👋 感谢你的反馈,我们已经收到。

    • 维护者会在 3 个工作日内 给出首次响应。
    • 状态保持为 status: 待评估,你可以随时补充信息。
    • 信息不全时我们会打上 needs: 更多信息 标签并 @ 你。

    👋 Thanks — we've received your issue.

    • A maintainer will respond within 3 business days.
    • Status stays at status: 待评估 (Triage); feel free to add context.
    • If we need more details, we'll add needs: 更多信息 and ping you.
  2. panda920 commented on Aug 17, 2026

    @panda920
    Collaborator

    这条和 #44 是同一个问题,已合并到 #44 统一跟进。

    共同根因:hook 的输入/输出契约不够用:PreToolUse 只能观察、追加上下文和放行/拦截,无法改写即将执行的工具输入(挡住了 rtk 这类命令代理);hook stdin 也没有 parent_session_id,子会话无法归属到父会话。

    合并是为了让研发看到完整上下文,而不是同一件事被拆成 2 条各自跟进。你的反馈没有丢——我已经在 #44 里把这几条一起列了出来,后续进展都会更新在那边,建议去 #44 点一下 Subscribe。

    如果你认为这其实是另一个问题,直接回复本条,我们会重新拆开。


    This is the same problem as #44, and is now tracked there.

    Shared root cause: The hook input/output contract is too narrow: PreToolUse can only observe, append context and allow/deny — it cannot rewrite the tool input about to be executed (which blocks command proxies like rtk) — and hook stdin carries no parent_session_id, so child sessions can't be attributed to their parent.

    We're consolidating so the team sees one complete picture instead of the same thing split across 2 issues. Nothing is lost — your report is listed in #44, all updates will go there, and you're welcome to subscribe to it.

    If you think this is actually a different problem, reply here and we'll split it back out.

  3. added
    duplicate
    on Aug 17, 2026
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

    duplicatepriority: P2

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions