Skip to content

feat: add first-class ZCode support - #462

Merged
jeff-r2026 merged 3 commits into
Tencent:mainfrom
hc-tec:feat/zcode-support
Sep 9, 2026
Merged

jeff-r2026 merged 3 commits into
Tencent:mainfrom
hc-tec:feat/zcode-support

Conversation

@hc-tec

@hc-tec hc-tec commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Summary

  • add ZCode as a first-class TeamAI target: skills → ~/.zcode/skills/, subagents (Claude-style Markdown) → ~/.zcode/agents/, user-scope MCP → ~/.agents/mcp.json, hooks → ~/.zcode/cli/config.json
  • add a dedicated zcode hook format: ZCode nests Claude-shaped hooks under hooks.events and gates them on hooks.enabled (config-file hooks are disabled by default, so the writer forces it on)
  • write process-type hook entries (bash -lc <dispatch> as an argv vector) and omit wildcard matchers (ZCode matchers are case-sensitive regexes; * is an invalid pattern that never matches)
  • exclude ZCode from co-author reconciliation (no documented attribution setting); rules are not synced (no user-level rules dir convention)
  • document the integration in both English and Chinese

Closes #461.

Why

ZCode was not discoverable as a tool id, so TeamAI could not deploy resources to it nor inject session hooks (no auto pull). Conventions verified against the ZCode desktop app (Windows): user skills live in ~/.zcode/skills (central ~/.agents/skills also read — covered by the existing agents entry), user subagents in ~/.zcode/agents/*.md (edited by the desktop Subagents page), user MCP in ~/.agents/mcp.json (mcpServers shape, read by the desktop MCP page), and hooks in the shared ~/.zcode/cli/config.json.

Validation

  • npx vitest run src/__tests__/zcode.test.ts (6/6 passed)
  • affected suites: agents / agent-format / coauthor-reconcile (97/97 passed)
  • npm run typecheck, npm run build
  • end-to-end teamai hooks inject against a sandbox HOME: unrelated config keys (plugin state) preserved, runner forced enabled, second run byte-identical
  • dogfooded internally on a second machine: session-start auto pull, subagent deployment, and skill sync all verified against the ZCode desktop app (subagents page lists the deployed profile; hooks fire on session start)
  • Windows note: hooks-related suites show identical pass/fail counts before and after this change (only pre-existing platform-sensitive failures, same category as noted in feat: add first-class Qoder support #420)

Boundary

  • no new runtime dependency or CLI command
  • rules are not synced (ZCode has no user-level rules dir convention)
  • project-scope MCP is not wired (ZCode stores workspace MCP under the mcp.servers key inside .zcode/config.json, which the Claude writer cannot emit)
  • ZCode is excluded from co-author reconciliation

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature causing existing behavior to change)
  • Documentation only
  • Refactor / internal cleanup

Test Plan

  • npx tsc --noEmit passes
  • npx vitest run passes (new zcode suite + affected suites green; pre-existing Windows-only failures unchanged, verified against a clean checkout)
  • Added/updated tests for the change

Related Issues

Closes #461

Notes for Reviewers

  • ~/.zcode/cli/config.json is shared with ZCode's own state (plugins etc.) — reconcileZcodeFormat merges the document and only ever touches hooks.enabled + hooks.events.<Event>.
  • Managed-entry detection follows the Codex format's strategy (command markers + manifest) because ZCode hook groups carry no description field.
  • Process entries resolve bash from PATH; on Windows a host whose PATH resolves the WSL bash.exe first would no-op — the same exposure as the existing bash -lc Claude commands, called out in the docs.

- register ZCode as a built-in target: skills deploy to ~/.zcode/skills,
  subagents (Claude-style Markdown) to ~/.zcode/agents/, and the built-in
  teamai-recall subagent rides the generic agents deployment
- add a 'zcode' hook format: ZCode nests Claude-shaped hooks under
  hooks.events and gates them on hooks.enabled (config-file hooks are
  disabled by default, so the writer forces it on)
- write process-type hook entries (bash -lc <dispatch> as an argv vector):
  a shell-string command would resolve through the host shell, which on
  Windows can land on the WSL bash.exe instead of Git Bash
- omit the matcher for wildcard groups: ZCode matchers are case-sensitive
  regexes and '*' is an invalid pattern that never matches
- user-scope MCP deploys to ~/.agents/mcp.json (Claude mcpServers shape —
  the file ZCode's own MCP settings page reads); project scope is not
  wired (ZCode stores workspace MCP under the different 'mcp.servers' key)
- exclude ZCode from co-author reconciliation: it exposes no documented
  attribution setting
- rules are not synced: ZCode has no user-level rules dir convention

Validation: 6/6 zcode tests + affected suites (agents/agent-format/coauthor)
all green, typecheck, build, and an end-to-end 'hooks inject' run against a
sandbox HOME (unrelated config keys preserved, runner forced enabled, second
run byte-identical). Hooks test files show identical pass/fail counts before
and after the change on Windows.
@jeff-r2026
jeff-r2026 self-requested a review September 9, 2026 12:27

@jeff-r2026 jeff-r2026 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Locally reproduced two issues on 8b6c870 using the built CLI and an isolated HOME. npm run build and the existing ZCode test suite (6/6) pass. Details and reproduction steps are attached inline.

Comment thread src/hooks.ts
// same strategy as the Codex format.
const isManaged = (entry: ZcodeHookMatcher): boolean => {
const cmd = zcodeEntryCommand(entry);
return TEAMAI_COMMAND_MARKERS.some((marker) => cmd.includes(marker)) || priorTeamCommands.has(cmd);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[P1] Match managed team hooks using the same command representation as the manifest

priorTeamCommands contains the original HookDef.command, but zcodeEntryCommand() returns hook.args.join(' '), which includes the added -lc prefix and >/dev/null 2>&1 || true suffix. Ordinary team commands therefore never match the manifest and are treated as user-owned entries. Every injection appends another copy, and removal leaves all those copies behind while deleting their manifest records.

Reproduced with the built CLI in an isolated HOME: configure ZCode and a hooks/hooks.yaml entry with event: SessionStart and command: sh /absolute/path/audit.sh (the script appends a line to a temporary file). Run hooks inject twice, then hooks remove. The team-entry counts are 1 → 2 → 2, despite removal reporting success. Executing the two remaining entries using their exact stored command/args appends two lines. This verifies that the retained commands remain executable; it does not depend on running the ZCode app. Repeated syncs multiply side effects, and removing a team hook does not revoke it.

Please normalize the stored command and manifest command consistently for reconciliation and managed-hook detection, and cover reinjection plus removal with a custom team command.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Additional end-to-end verification: this also affects teamai uninstall --force, not only hooks remove. In an isolated HOME with one custom SessionStart hook (sh /absolute/path/audit.sh), full uninstall reported success and removed the team skill, team agent, managed MCP server, and the entire .teamai directory. The custom hook remained in .zcode/cli/config.json; invoking its stored command/args after uninstall still appended a line to the audit file. Its ownership manifest had already been deleted. Targeted uninstall --agent zcode --force with Claude remaining also left the custom hook behind. Personal skills, agents, MCP entries, and plugin keys were preserved in the full-uninstall fixture.

Comment thread src/hooks.ts Outdated
const cfg: ZcodeHooksJson = (await readJson<ZcodeHooksJson>(expanded)) ?? {};
if (!cfg.hooks) cfg.hooks = {};
// Config-file hooks are disabled by default in ZCode; entries we write would
// never fire unless the runner is explicitly enabled.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[P2] Persist changes to hooks.enabled even when event entries are unchanged

Setting cfg.hooks.enabled = true does not set changed; the write below only happens when an event array differs. Once the entries are stable, an existing hooks.enabled: false remains false on disk after reinjection, contrary to the documented behavior.

Locally reproduced with the built CLI: inject hooks, reach a stable configuration, edit only hooks.enabled to false, then run hooks inject again. The CLI reports success, but the file still contains false, leaving the config-file hooks disabled. Please include the enabled-field change in the dirty check and add a regression test for reinjecting an otherwise up-to-date disabled configuration.

Comment thread src/hooks.ts Outdated
const cfg: ZcodeHooksJson = (await readJson<ZcodeHooksJson>(expanded)) ?? {};
if (!cfg.hooks) cfg.hooks = {};
// Config-file hooks are disabled by default in ZCode; entries we write would
// never fire unless the runner is explicitly enabled.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[P1] Preserve the hook runner state during removal

This unconditional assignment also runs with opts.removeAll: true. When removal changes the event arrays, the write persists hooks.enabled: true, turning on hooks that the user explicitly disabled. This affects retained user-owned hooks even after the managed-command matching issue is fixed.

Reproduced on 8b6c870 with the built CLI and an isolated HOME: start with a personal SessionStart hook, run hooks inject, explicitly set hooks.enabled to false, and run uninstall --force. The command reports success and deletes .teamai, but the surviving ZCode config now has hooks.enabled: true and still contains the personal hook. The same false → true transition occurs with uninstall --agent zcode --force while Claude remains installed. Combined with the separately reported team-hook ownership bug, removal also re-enables the orphaned team commands.

Please only force the runner on when installing hooks; removal should preserve its existing enabled/disabled state. Add a regression test starting with installed entries and hooks.enabled: false, then removing them.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Re-reviewed and reproduced on the latest head, 343a5d8. This P1 is still present at src/hooks.ts:548–550: the enabled-state assignment remains unconditional with respect to opts.removeAll.

Actual built-CLI reproduction in an isolated HOME:

  1. Keep a personal SessionStart hook in the ZCode config and run hooks inject.
  2. Set only hooks.enabled to false.
  3. Run uninstall --force.
  4. Inspect the remaining config: the personal hook is preserved, but hooks.enabled is true. .teamai has been deleted and the CLI reports success.

Repeated with uninstall --agent zcode --force while Claude remains installed: same false → true transition. Removal needs to preserve the existing runner state, e.g. gate the enabling branch on !opts.removeAll, with a disabled-state removal regression test.

The other fixes are verified for fresh installs: two injections now keep exactly one custom team hook, removal/full uninstall leave zero custom team hooks, and reinjection correctly persists the enabled flip. Build, TypeScript checking, and the ZCode/team-hooks/uninstall suites (52 tests) pass. The remaining removal-state issue prevents LGTM.

Review findings on Tencent#462 (both locally reproduced by jeff-r2026):

- team hooks were appended on every injection and left behind on removal:
  zcodeEntryCommand() joined the full argv while the manifest stored the raw
  HookDef.command, so managed-entry detection never matched. The shell
  payload is now stored verbatim (args[1] === def.command) and the payload
  accessor returns it as-is — one command representation across entry,
  manifest, and desired set.
- flipping hooks.enabled to true was not tracked as a change, so an
  otherwise up-to-date config with enabled: false stayed disabled after
  reinjection. The flip now participates in the dirty check.

Regression tests cover both: custom team-command reinjection/removal via the
manifest, and re-enabling a hand-disabled but otherwise stable config.
@hc-tec

hc-tec commented Sep 9, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review and the precise reproduction steps, @jeff-r2026 — both findings were real. Fixed in 343a5d8:

P1 (team-hook command mismatch). Root cause exactly as diagnosed: the manifest stored the raw HookDef.command while the rendered entry's argv payload carried the extra -lc prefix and redirection suffix, so managed-entry detection only ever worked for built-ins (whose commands contain the teamai hook-dispatch marker). The shell payload is now stored verbatim — args[1] === def.command — and zcodeEntryCommand() returns that payload, giving one command representation across the entry, the manifest, and the desired set, exactly matching the Codex-format invariant. hook-dispatch is silent and failure-tolerant on success paths, so the removed redirections cost nothing.

P2 (hooks.enabled flip not persisted). The flip now participates in the dirty check and triggers a write even when all event arrays are unchanged.

Regression tests added to zcode.test.ts (suite now 8/8):

  • custom team command (sh /tmp/audit.sh, no teamai marker): inject → exactly 1 entry; reinject → still 1; removeAll → entry gone and manifest-backed hasTeamaiHooks returns false
  • hand-disable hooks.enabled on an otherwise stable config → reinject → enabled: true restored with entries intact

Typecheck, build, and the full zcode suite pass on the rebased branch. One residual limitation worth documenting: because ZCode parses hook stdout as strict JSON, queued stdout hints (e.g. the packages-changed notice) cannot be injected into ZCode sessions until teamai learns to emit ZCode's output schema — happy to add that in a follow-up if you want it.

The enabled-state normalization introduced for the P2 finding ran on the
removal path too, so uninstall/remove flipped a user-disabled
hooks.enabled back to true while stripping the managed entries —
re-enabling surviving personal hooks against the user's explicit choice.

Gate the enabling branch on !opts.removeAll so removal preserves the
existing runner state, with a regression test: install, hand-disable
hooks.enabled, removeAll — entries stripped, enabled stays false.
@hc-tec

hc-tec commented Sep 9, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the fast re-review — agreed, and fixed in cf00e98.

reconcileZcodeFormat now gates the enabling branch on !opts.removeAll: installation forces hooks.enabled: true (the P2 behavior), while removal and uninstall preserve the runner state exactly as found — including a hand-disabled hooks.enabled — so a surviving personal hook is never switched back on.

Regression test added: install → hand-disable hooks.enabled → removeAll — entries stripped, enabled stays false, hasTeamaiHooks returns false. The pre-existing removeAll test (runner previously left enabled) covers the other direction and still passes.

Suite now 9/9; typecheck and build clean on the rebase base.

@jeff-r2026
jeff-r2026 merged commit 6ae0619 into Tencent:main Sep 9, 2026
7 checks passed
Walternater pushed a commit to Walternater/teamai-cli that referenced this pull request Nov 2, 2026
Review findings on Tencent#462 (both locally reproduced by jeff-r2026):

- team hooks were appended on every injection and left behind on removal:
  zcodeEntryCommand() joined the full argv while the manifest stored the raw
  HookDef.command, so managed-entry detection never matched. The shell
  payload is now stored verbatim (args[1] === def.command) and the payload
  accessor returns it as-is — one command representation across entry,
  manifest, and desired set.
- flipping hooks.enabled to true was not tracked as a change, so an
  otherwise up-to-date config with enabled: false stayed disabled after
  reinjection. The flip now participates in the dirty check.

Regression tests cover both: custom team-command reinjection/removal via the
manifest, and re-enabling a hand-disabled but otherwise stable config.
Walternater pushed a commit to Walternater/teamai-cli that referenced this pull request Nov 2, 2026
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.

[feat] 支持 ZCode(skills/subagents/hooks/MCP 自动分发)

2 participants