Skip to content

fix(hooks): type untyped flat command handlers for Codex - #3385

Merged
Daniel Meppiel (danielmeppiel) merged 3 commits into
microsoft:mainfrom
Parth-Vasave:fix/3249-codex-hook-handler-type
Nov 12, 2026
Merged

Daniel Meppiel (danielmeppiel) merged 3 commits into
microsoft:mainfrom
Parth-Vasave:fix/3249-codex-hook-handler-type

Conversation

@Parth-Vasave

Copy link
Copy Markdown
Contributor

Description

A flat hook entry may omit type (Cursor defaults it to command). APM already wraps such entries in Codex's { "matcher", "hooks": [...] } groups, but the generated handler had no type. In the pinned Codex source (hook_config.rs at 36512a34), HookHandlerConfig is #[serde(tag = "type")] with no default, so an untyped handler matches no variant.

This is the Codex counterpart of #3130 / #3153. It reuses the renderer and ownership code merged there; there is no second implementation.

  • Rendering: the flat-command type default from fix(hooks): type untyped flat command handlers for Claude #3153 (now _with_default_command_type) also runs in _to_codex_hook_entries. Untyped flat command entries render as "type": "command" in .codex/hooks.json and its sidecar, including when they join groups from a Codex-shaped hook file in the same package.
  • Unchanged: explicit types (command, prompt, mcp_tool), untyped non-command entries, handlers already nested in the source, matchers, timeouts, and other targets (Cursor and Gemini are checked by a guard test).
  • Upgrade path: _legacy_hook_entries for Codex now returns both earlier shapes APM wrote: the original flat entry (before fix(hooks): render Codex hooks in native shape #3060) and the nested untyped group. Proven APM-owned output, including stale root-package sources healed by content match, therefore converges to one typed entry on reinstall and apm update. Hooks with no ownership record are never rewritten or removed, even when they are byte-identical to old APM output.

Before (from the issue):

{ "matcher": "Write", "hooks": [ { "command": "node \".codex/hooks/demo-pkg/.apm/hooks/scripts/gate.mjs\"", "timeout": 10 } ] }

After:

{ "matcher": "Write", "hooks": [ { "type": "command", "command": "node \".codex/hooks/demo-pkg/.apm/hooks/scripts/gate.mjs\"", "timeout": 10 } ] }

Evidence and its limits: this establishes that the generated output matches Codex's source schema. I have not observed a Codex runtime rejecting the old output or accepting the new output.

Issue and approved scope

Issue: #3249

Human scope-approval comment: #3249 (comment) (clarified in #3249 (comment))

This PR completes the approved scope. Out of scope, as agreed: defaults inside already-nested source handlers, camelCase event-name mapping, a shared default across all targets, guessing non-command types, and #2111.

Fixes #3249

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Maintenance / refactor

Testing

  • Tested locally
  • All existing tests pass
  • Added tests for new functionality (if applicable)

New and updated tests:

  • tests/unit/integration/test_hook_integrator_issue3249.py (14 cases):

    • standalone and merged-with-Codex-shaped rendering;
    • command-only defaulting, with explicit types preserved;
    • nested source handlers unchanged;
    • reinstall over owned flat and nested-untyped output;
    • a manual untyped hook neither claimed nor removed;
    • stale root-source healing for both legacy shapes.

    9 of these fail without the fix. If only the new nested-untyped legacy matching is removed, the stale-root case fails.

  • test_required_codex_untyped_hook_upgrade_preserves_ownership_and_converges runs the installed CLI with {standalone, merged} × {nested-untyped, flat} legacy output. It covers first install, two reinstalls and apm update to a new revision, and checks that:

    • a single typed entry results and the state converges;
    • the user and manual hooks are kept;
    • the .apm sources are unchanged;
    • an unrelated target's config is untouched;
    • a sentinel shows the hook never ran during install.

    All 4 variants fail without the fix.

  • test_codex_native_edge_types_untyped_flat_commands covers the renderer and the legacy shapes.

  • In the [BUG] Flat hook entries without type reach .claude/settings.json without the required type: command #3130 suite, the guard asserting Codex did not get the default now targets Cursor and Gemini.

Run locally, mirroring ci.yml:

  • Lint: ruff check and format on CI's paths, the YAML/file-length/relative_to checks, pylint R0801, and the auth-signal and architecture-boundary linters.
  • Architecture ratchet contracts (188) and the scaling gate.
  • Unit, console and red-team suites: 25,771 passed, 89.33% coverage.
  • Lifecycle smoke, both shards: 412 passed.
  • Audit workspace identity (6 passed) and apm audit --ci.

Not run locally: the Windows jobs and the PyInstaller-built binary (I used the dev install's apm).

Docs: hooks-and-commands.md and the apm-guide package-authoring.md now describe the Claude and Codex input/output contract. The CHANGELOG has an entry under Unreleased.

Spec conformance (OpenAPM v0.1)

  • N/A -- this PR does not change OpenAPM-observable behaviour.

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@sergio-sisternes-epam

Copy link
Copy Markdown
Collaborator

Thank you for contributing this Codex hook correction. Issue #3249 has a current maintainer-approved scope record for flat command handlers and owned legacy-output repair. The requested reviewers include Daniel Meppiel (@danielmeppiel) and Sergio Sisternes (@sergio-sisternes-epam). This is a recommendation for human review only, not merge approval.


Generated by autopilot-pr-triage-worker. This comment is AI-generated and may contain errors.

@sergio-sisternes-epam Sergio Sisternes (sergio-sisternes-epam) added area/multi-target Multi-target deploy spec, target directory creation, agent surface routing. triage/recommended Automated advice completed; not human scope approval. type/bug Something does not work as documented. labels Nov 3, 2026
@danielmeppiel

Copy link
Copy Markdown
Collaborator

APM Review Panel: ship_with_followups

Community fix types Codex flat command handlers, closing a gap where untyped entries rendered without the type discriminator Codex requires -- clean architecture, strong lifecycle tests, three foldable follow-ups.

panel-mode=lean; personas=python-architect,test-coverage-expert,doc-writer (adhoc: PR edits hook docs, apm-guide guidance and CHANGELOG),apm-ceo

cc parth (@Parth-Vasave) Sergio Sisternes (@sergio-sisternes-epam) -- a fresh advisory pass is ready for your review.

All three panelists converge: the code change is narrow, correct, and aligned with the scoped decision on #3249 that limits this to input-shape defaulting only. The python-architect found zero correctness or abstraction risks -- the shared _with_default_command_type helper is minimal, respects explicit types, and the legacy-shape ownership matching reuses existing content-key discipline without introducing new ownership semantics. The 14 unit tests plus the contract-guard and installed-CLI lifecycle test form a strong regression net.

The one finding worth strategic attention is the missing denied-consent assertion for untyped flat Codex input. The consent gate (install/exec_gate.py) is upstream of rendering and untouched by this PR, and the existing hook-path lifecycle test already exercises the same gate with typed Codex input, so the regression risk is low. But the approved scope names "preserve denied-consent behavior", and without a test on the new input shape that promise holds only by coincidence. Fold it before merge.

The doc-writer findings are housekeeping: the CHANGELOG entry references the issue instead of the PR per repo convention (and the main merge left a duplicate Fixed heading), and the apm-guide omits the "supply type in nested groups" instruction that the hooks doc page already states.

Aligned with: Secure by default (consent gate upstream and untouched; the untyped-flat denial test is the one gap to fold); multi-harness (Codex now gets the same flat-command default Claude already has, without leaking target semantics into the neutral IR); community-driven (clean, well-tested fix on a maintainer-scoped issue); pragmatic as npm (authors no longer need type: command on flat Codex hook entries).

Panel summary

Persona B R N Takeaway
Python Architect 0 0 0 Shared default-type helper and legacy-shape ownership matching are correct, minimal, and respect single-canonical-owner discipline.
Test Coverage Expert 0 1 0 Unit + contract-guard + lifecycle coverage is strong; denied consent is not exercised with untyped flat Codex hooks.
Doc Writer 0 2 0 Hook doc matches the code; fix the CHANGELOG entry format and tighten the apm-guide wording.

B = blocking-severity findings, R = recommended, N = nits.
Counts are signal strength, not gates. The maintainer ships.

Top 3 follow-ups

  1. [Test Coverage Expert] Add a denied-consent assertion for untyped flat Codex hook input in installed-CLI lifecycle coverage. -- The approved scope names "preserve denied-consent behavior"; no test proves it for the new input shape.
  2. [Doc Writer] Fix the CHANGELOG entry: merge into the existing Fixed heading and end with -- by @Parth-Vasave (#3385). -- Repo convention requires the PR number; the main merge also left a duplicate Fixed section under [Unreleased].
  3. [Doc Writer] Align the apm-guide package-authoring section with the hooks doc: state the shared flat-command default and that nested source groups must supply type. -- The guide is the agent-facing authoring surface and currently omits the nested-group instruction.

Recommendation

Ship after the three recommended items are folded -- the denied-consent test gap, the CHANGELOG format fix, and the apm-guide alignment. All three are in-scope and bounded; the merge worker is folding them into this PR. The code itself is clean per architecture review and well covered by unit, contract and lifecycle tests. No blocking findings.


Full per-persona findings

Python Architect

No findings.

Test Coverage Expert

  • [recommended] Denied-consent path not tested with untyped flat Codex hook input at tests/integration/test_hook_path_package_lifecycle.py:315
    The existing denied-consent block exercises allowExecutables={} for Codex with a typed nested descriptor only. The gate is upstream of rendering, so risk is low, but the approved scope lists it explicitly.
    Suggested: Consumer with allowExecutables={}, install the untyped flat source, assert no .codex/hooks.json, no hook-executed sentinel, exec_status gated_pending_approval.
    Proof (missing at): tests/integration/test_hook_path_package_lifecycle.py::test_hook_path_install_restore_update_remove -- proves: denied consent prevents hook deployment for untyped flat Codex hooks [secure-by-default]

Doc Writer

  • [recommended] CHANGELOG entry ends with (closes #3249) instead of (#3385) and adds a second ### Fixed heading under [Unreleased] at CHANGELOG.md:10
    changelog.instructions.md requires -- by @username (#PR_NUMBER); one heading per type.
    Suggested: Move the bullet into the existing Fixed list and end it with -- by @Parth-Vasave (#3385).
  • [recommended] apm-guide repeats the type-default rule per target, names the nested scope inconsistently, and omits "supply type yourself in nested groups" at packages/apm-guide/.apm/skills/apm-usage/package-authoring.md:235
    The code checks nesting in the source entry for both targets; the docs page already says so.
    Suggested: One paragraph stating the shared flat-command default, per-target wrapping, and that nested source groups must supply type.

This panel is advisory. It does not block merge. Re-apply the
panel-review label after addressing feedback to re-run.


Generated by autopilot-pr-review-worker. This comment is AI-generated and may contain errors.

Codex's HookHandlerConfig is an internally tagged enum
(#[serde(tag = "type")]) with no default, so the nested handler APM
generated from a flat command entry without `type` matched no variant.

Share the flat-command type default introduced for Claude in microsoft#3153 with
the Codex renderer: untyped flat command entries now render as
`"type": "command"` handlers in .codex/hooks.json, including when they
join existing groups. Explicit types, non-command entries and handlers
already nested in the source are unchanged, as are other targets.

Ownership matching on reinstall and update now recognises both earlier
Codex shapes APM wrote (the original flat entry and the nested untyped
group), so proven APM-owned output converges to one typed entry.
Manual and unowned hooks are not rewritten or removed.

Fixes microsoft#3249
Address review panel follow-ups on microsoft#3385:

- Add an installed-CLI lifecycle test proving that with
  allowExecutables={} an untyped flat Codex hook source deploys no
  .codex/hooks.json, runs nothing, and records exec_status
  gated_pending_approval; after approval it renders a typed handler.
- Merge the CHANGELOG bullet into the existing [Unreleased] Fixed list
  and credit the PR number.
- Collapse the apm-guide Claude/Codex paragraphs into one shared
  flat-command default and state that nested source groups must supply
  type.
@Parth-Vasave
parth (Parth-Vasave) force-pushed the fix/3249-codex-hook-handler-type branch from 22ed02d to 9797ba1 Compare November 12, 2026 11:54
@Parth-Vasave

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I've addressed all three follow-ups in 9797ba1 and rebased onto main to fix the merge conflict:

  1. Denied consent for untyped flat Codex input: new test_untyped_flat_codex_hook_denied_consent_blocks_deployment in tests/integration/test_hook_path_package_lifecycle.py. With allowExecutables: {}, installing an untyped flat Codex hook writes no .codex/hooks.json, runs nothing, and records exec_status: gated_pending_approval. After apm approve, the reinstall renders one "type": "command" handler.
  2. CHANGELOG: the bullet is merged into the existing [Unreleased] ### Fixed list and ends with -- by @Parth-Vasave (#3385).
  3. apm-guide: one paragraph now states the shared flat-command type default, how each target wraps it, and that nested source groups must set type. This matches the hooks doc page.

@danielmeppiel

Copy link
Copy Markdown
Collaborator

APM Review Panel: ship_now

All three prior follow-ups folded in 9797ba1; delta panelists confirm zero remaining in-scope findings -- community fix is ready to ship.

panel-mode=delta; personas=test-coverage-expert,doc-writer,apm-ceo

cc parth (@Parth-Vasave) Sergio Sisternes (@sergio-sisternes-epam) -- a fresh advisory pass is ready for your review.

This delta pass closes the loop on the lean advisory posted against 22ed02d. Thanks parth (@Parth-Vasave) -- every follow-up was addressed in a single commit (9797ba1) with a clean rebase onto main:

  1. test_untyped_flat_codex_hook_denied_consent_blocks_deployment now exercises the executable consent gate on the exact untyped flat Codex input this PR introduces: no .codex/hooks.json, nothing executed, exec_status: gated_pending_approval, then one typed handler after approval. The test-coverage-expert confirms the [BUG] Flat hook entries without type reach .codex/hooks.json without the required type: command #3249 done-when criteria have coverage and returns zero findings.
  2. The CHANGELOG bullet now sits in the existing Unreleased Fixed list with the (#3385) trailer, and the apm-guide states the shared flat-command default once, including that nested source groups must supply type. The doc-writer confirms both match _with_default_command_type and returns zero findings.
  3. The python-architect was not re-spawned: the src/ delta is byte-identical to the previously reviewed head, where it had zero findings.

Exact-head evidence on 9797ba1: CI fully green (Lint, Build & Test shards, Lifecycle Smoke shards, Windows gates, Spec conformance, CodeQL, license/cla); locally 30 hook unit/contract tests and 5 installed-CLI lifecycle tests (4 Codex upgrade variants plus the new denied-consent test) passed.

Separate from this PR: while validating, the merge worker saw that for a github-hosted aliased dependency apm approve <owner/repo> (the command the gate hint prints) wrote owner/repo#<version> while install looked up github.com/owner/repo#<ref>@<alias>, so the hint did not unlock hooks in that topology. That is pre-existing executable-consent identity behavior, untouched here and outside the #3249 scope; it deserves its own maintainer-triaged issue.

Aligned with: Secure by default (denial now proven on the new input shape; gate untouched); multi-harness (Codex gets the same flat-command default Claude has, without leaking target semantics into the neutral IR); community-driven (contributor iterated on every follow-up in one cycle); pragmatic as npm (no type: command boilerplate on flat Codex hooks).

Panel summary

Persona B R N Takeaway
Test Coverage Expert 0 0 0 Prior denied-consent follow-up closed by the new lifecycle test; #3249 done-when criteria covered.
Doc Writer 0 0 0 Both prior doc follow-ups resolved and accurate against the renderer.

B = blocking-severity findings, R = recommended, N = nits.
Counts are signal strength, not gates. The maintainer ships.

Recommendation

All three follow-ups from the lean advisory are folded, both re-spawned panelists return zero findings, and CI is green. Ship. The pre-existing approve-key identity mismatch for aliased github-hosted dependencies is worth a separate issue but is outside #3249 scope and does not affect this PR.


Full per-persona findings

Test Coverage Expert

No findings.

Doc Writer

No findings.

This panel is advisory. It does not block merge. Re-apply the
panel-review label after addressing feedback to re-run.


Generated by autopilot-pr-review-worker. This comment is AI-generated and may contain errors.

Bring PR microsoft#3385 up to date with main. Clean merge; main's CHANGELOG
entries preserved and the microsoft#3385 entry stays in Unreleased Fixed.

Co-authored-by: Copilot App <[email protected]>
@danielmeppiel

Copy link
Copy Markdown
Collaborator

Merged current main into this branch at 2b14652 -> eda49ca (merge-of-main fallback, since the branch history already contains merge-from-main commits; no history rewrite).

Conflicting paths: none. git merge origin/main completed cleanly with merge-ort. Main's CHANGELOG entries are kept, and the #3385 entry stays in ## [Unreleased] / Fixed. None of the PR's regression-trap tests or guarded source files (hook_native_formats.py, exec_gate.py) changed in the merge, so the mutation-break gate did not need to run again.

Lint contract: ruff check and ruff format --check (src/ tests/ plus the architecture-linter scripts), pylint R0801, lint-auth-signals.sh and lint-architecture-boundaries.sh all exited 0 after the merge, and no src file is over 2100 lines.

Exact-head evidence at eda49ca:

  • Re-ran the targeted Codex hook tests: 30 passed (unit + contract guards) and 5 passed (installed-CLI upgrade convergence + denied-consent gating).
  • The owner-touch detection matches the earlier run (the same single hook-contract owner decision).
  • All PR checks are green: CI, Merge Gate, Spec conformance, CodeQL, NOTICE Drift, Docs build and license/cla.

Post-push mergeability: gh pr view --json mergeStateStatus,mergeable reports BLOCKED / MERGEABLE. BLOCKED here comes only from the code-owner review and merge-queue ruleset, not from a conflict. Push used git push https://github.com/Parth-Vasave/apm.git HEAD:refs/heads/fix/3249-codex-hook-handler-type (plain fast-forward push, no force).

Ready for maintainer review.


Generated by autopilot-pr-merge-worker. This comment is AI-generated and may contain errors.

@danielmeppiel
Daniel Meppiel (danielmeppiel) merged commit 29f323b into microsoft:main Nov 12, 2026
27 checks passed
@Parth-Vasave
parth (Parth-Vasave) deleted the fix/3249-codex-hook-handler-type branch November 13, 2026 06:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/multi-target Multi-target deploy spec, target directory creation, agent surface routing. triage/recommended Automated advice completed; not human scope approval. type/bug Something does not work as documented.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Flat hook entries without type reach .codex/hooks.json without the required type: command

4 participants