Skip to content

fix(codex): resolve package-relative links in converted agent instructions - #3611

Merged
Daniel Meppiel (danielmeppiel) merged 13 commits into
mainfrom
danielmeppiel-fix-codex-agent-link-resolution-2966
Nov 12, 2026
Merged

Daniel Meppiel (danielmeppiel) merged 13 commits into
mainfrom
danielmeppiel-fix-codex-agent-link-resolution-2966

Conversation

@danielmeppiel

Copy link
Copy Markdown
Collaborator

Description

TL;DR

Codex agent conversion now routes the Markdown body through the shared install-time link resolver (BaseIntegrator.resolve_links) before serializing developer_instructions. A valid in-package link such as ../skills/msbuild-antipatterns/SKILL.md now reaches the retained installed file from .codex/agents/, exactly as the Copilot output already did. Nothing else in the TOML changes.

Fixes #2966

Note

Thanks Shuai Zhang (@hcoona) for isolating this with the same pinned package installed for both clients.
Sergio Sisternes (@sergio-sisternes-epam) please review — the issue stays assigned to you; this PR does not change that.

Problem (WHY)

  • integrate_agents_for_target called _write_codex_agent(...) and hard-coded links_resolved = 0; the writer serialized body.strip() unchanged, while every other agent target goes through copy_agent → resolve_links.
  • Reproduced on main (2794714dca) with the reported pin dotnet/skills/plugins/dotnet-msbuild#4c72b17…: parsed Codex developer_instructions kept ../skills/msbuild-antipatterns/SKILL.md, which from .codex/agents/ points at a nonexistent .codex/skills/…. The Copilot output for the same install pointed at the retained apm_modules/…/.apm/skills/msbuild-antipatterns/SKILL.md.
  • [!] Two link-handling paths for one decision (how a relocated agent's links are rewritten) is a fork the scope record asks us to close by reusing "the canonical source-to-destination resolver".

Approach (WHAT)

# Fix
1 Pass the shared resolver into the Codex writer, bound to the real source and the generated .toml path, and resolve the body before TOML serialization.
2 Return the resolved-link count so Codex reports links_resolved like other agent targets.
3 One-line doc notes: Codex developer_instructions follows the existing package-relative link contract.

Implementation (HOW)

  • src/apm_cli/integration/agent_integrator.py — _write_codex_agent gains an optional keyword resolve_body_links callable and returns int. The Codex branch passes functools.partial(self.resolve_links, source=source_file, target=target_path). The writer stays a staticmethod; existing direct callers (tests, spec conformance) keep their behavior because the parameter defaults to None. Frontmatter parsing, native model / model_reasoning_effort fields, lossy-compilation diagnostics, symlink refusal and the target containment check are untouched.
  • tests/unit/integration/test_agent_integrator.py — three regression tests on an installed-dependency fixture (apm_modules/owner/pkg) with a retained sibling skill: link resolution from the generated location (with #fragment kept and native fields preserved), Codex-vs-Copilot parity on the same package, and reinstall + source update. The same fixture asserts the resolver's existing pass-through for URL, #anchor, missing-file and out-of-package (../../../../../outside.md) links.
  • Docs — producer/package-relative-links.md, producer/author-primitives/instructions-and-agents.md, and packages/apm-guide/.apm/skills/apm-usage/package-authoring.md: one clause each.

Diagram

Legend: the Codex branch now joins the same resolver call that copy_agent already used; the boxed step is the new behavior.

flowchart LR
    A["integrate_agents_for_target"] --> B{"format_id == codex_agent"}
    B -->|"no"| C["copy_agent"] --> R["BaseIntegrator.resolve_links"]
    B -->|"yes"| D["_write_codex_agent"]
    D --> E["parse frontmatter + diagnostics"]
    E --> F["resolve body links (NEW)"]
    F --> R
    F --> G["toml.dumps -> .codex/agents/NAME.toml"]
    style F stroke-width:3px
Loading

Trade-offs

  • Callable injection vs. making the writer an instance method. Chose an optional resolve_body_links callable so existing direct static callers stay unchanged; rejected a second Codex-specific rewrite because the resolver already owns containment and URL/anchor rules.
  • Resolve the body only, not the frontmatter. Only the body is serialized into developer_instructions; frontmatter links never reach the TOML.
  • No layout change. Links point into apm_modules/ like every other agent target; no duplication into .codex/skills, no symlinks, no new setting (all out of scope per the record).

Benefits

  1. The reported pinned package now produces a Codex link that resolves to the installed SKILL.md (verified below with a TOML parser + filesystem check).
  2. One link-rewrite owner for every agent target, guarded by three regression tests that each fail when the fix is mutated.
  3. links_resolved is now accurate for Codex installs.

Issue and approved scope

Issue: Fixes #2966

Human scope-approval comment: #2966 (comment) (review contact Daniel Meppiel (@danielmeppiel)). eligibility.cjs from a trusted origin/main copy (2794714dca) returned state: record-present, authorizes_implementation: false.

Important

The record lists "PR actions … automated implementation dispatch" and "assignment or relationship changes" as out of scope. On 2026-11-12 Daniel Meppiel (@danielmeppiel) directed delivery to proceed now (see the expedite note on the issue) — that direction supersedes the dispatch exclusion. Assignment is unchanged: the issue stays with Sergio Sisternes (@sergio-sisternes-epam), who is requested as reviewer.

Completes the issue: generated-file correctness for Codex is fixed with Copilot preserved. Native-model behavior (whether a Codex model follows the link) was not exercised and is not claimed.

Type of change

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

Testing

  • Tested locally
  • All existing tests pass — targeted files only locally; CI is the gate
  • Added tests for new functionality (if applicable)

Note

Local preflight skipped by maintainer instruction (2026-11-12); CI is the gate.

Scenario evidence

Scenario (user promise) Proof Principle
Valid in-package link in a Codex agent resolves from .codex/agents/ test_codex_relative_link_resolves_from_generated_agent_location; installed CLI on pinned dotnet-msbuild cross-target parity
Codex and Copilot links reach the same installed file test_codex_and_copilot_links_reach_same_resource; installed CLI cross-target parity
Reinstall and update rewrite links again test_codex_relative_link_survives_reinstall_and_update; apm install ×2 and apm update 8a52700→4c72b17 reproducible installs
URL / anchor / missing / out-of-package links pass through; native fields + diagnostics unchanged first test above; CLI diff vs. HEAD behavior shows only the one link line changed; 3 lossy warnings still emitted safe-by-default
Installed-CLI evidence (reported pinned package, real network install)

Checker parses each .codex/agents/*.toml with tomllib, extracts relative links from developer_instructions, and resolves them from the TOML's directory (Copilot: from the .agent.md directory).

# main @ 2794714dca, apm install --target copilot,codex
  BAD codex .codex/agents/msbuild-code-review.toml: ../skills/msbuild-antipatterns/SKILL.md
  OK  copilot .github/agents/msbuild-code-review.agent.md: ../../apm_modules/dotnet/skills/plugins/dotnet-msbuild/.apm/skills/msbuild-antipatterns/SKILL.md
TOTAL ok=1 bad=1

# this branch, reinstall over the buggy output
  OK  codex .codex/agents/msbuild-code-review.toml: ../../apm_modules/dotnet/skills/plugins/dotnet-msbuild/.apm/skills/msbuild-antipatterns/SKILL.md
  OK  copilot .github/agents/msbuild-code-review.agent.md: ../../apm_modules/dotnet/skills/plugins/dotnet-msbuild/.apm/skills/msbuild-antipatterns/SKILL.md
TOTAL ok=2 bad=0
# second reinstall: msbuild-code-review.toml byte-IDENTICAL

# fresh project pinned @8a52700 -> TOTAL ok=2 bad=0
# repin to 4c72b17, apm update -y --target copilot,codex
Updated 1 APM dependency.
  resolved_commit: 4c72b17fa2c2aaa307d8f1e337ec75cab45ac5c7
TOTAL ok=2 bad=0

# non-instruction TOML fields vs. HEAD writer: same non-instruction fields: True
# developer_instructions diff vs. HEAD writer: exactly one line (the link)
Local tests, mutation check and lint mirror
.venv/bin/python -m pytest -p no:xdist -q tests/unit/integration/test_agent_integrator.py
105 passed

.venv/bin/python -m pytest -p no:xdist -q tests/unit/primitives/test_resolve_user_invocable.py \
  tests/integration/test_integrators_validation_rules.py \
  tests/spec_conformance/test_manifest_reqs.py tests/unit/integration/test_data_driven_dispatch.py
284 passed, 1 skipped

# Before the fix: 3 new tests failed. Mutations (each run restored afterwards):
M1 drop resolve call            -> 3 failed
M2 resolve against source path  -> 3 failed
M3 discard resolved-link count  -> 3 failed

ruff check / ruff format --check (CI path set, ruff 0.15.12)  -> clean
pylint R0801 (CI path set)                                      -> 10.00/10
YAML guard, relative_to guard, file length                       -> clean
scripts/lint-auth-signals.sh                                     -> clean
scripts/lint-architecture-boundaries.sh                          -> exit 0
tests/spec_conformance/mode_b_detector.sh                        -> 14 substantive added lines (< 20); OK

How to test

  • In an empty dir, create apm.yml with dependency dotnet/skills/plugins/dotnet-msbuild#4c72b17fa2c2aaa307d8f1e337ec75cab45ac5c7.
  • apm install --target copilot,codex
  • Parse .codex/agents/msbuild-code-review.toml; the msbuild-antipatterns skill link resolved from .codex/agents/ exists.
  • Compare with .github/agents/msbuild-code-review.agent.md; both links reach the same SKILL.md.
  • uv run --extra dev pytest tests/unit/integration/test_agent_integrator.py -k "relative_link or same_resource"

Spec conformance (OpenAPM v0.1)

  • Spec edit: docs/src/content/docs/specs/openapm-v0.1.md updated
    (new/changed <a id="req-XXX"></a> anchor + prose + Appendix C
    row).
  • Manifest edit: docs/src/content/docs/specs/manifests/openapm-v0.1.requirements.yml
    updated.
  • Test edit: a @pytest.mark.req("req-XXX") test under
    tests/spec_conformance/ added or extended.
  • CONFORMANCE.{md,json} regenerated via
    uv run --extra dev python -m tests.spec_conformance.gen_statement
    and committed.
  • N/A -- this PR does not change OpenAPM-observable behaviour.

Co-authored-by: Copilot [email protected]

…tions

Codex agent conversion serialized the Markdown body into
developer_instructions without the shared install-time link resolver, so
valid in-package links (e.g. ../skills/<name>/SKILL.md) pointed at
nonexistent paths under .codex/. Route the body through the existing
BaseIntegrator.resolve_links owner for the generated agent location
before TOML serialization, and report links_resolved like other agent
targets. Native fields, diagnostics, containment and URL/anchor/
unsupported-link pass-through are unchanged.

Fixes #2966

Co-authored-by: Copilot App <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 12, 2026 13:24

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.

🟡 Changes recommended

A missing registered architecture guard for Codex link-rewriting delegation could let the shared-resolver bypass return without architecture-boundary lint detecting it.

1 open issue
What changed in this PR

Fixes #2966 by routing converted Codex agent instructions through the shared link resolver, so valid package-relative links remain usable from the generated TOML location.

Changes:

  • Resolves Markdown body links before TOML serialization and reports the resolved-link count.
  • Adds regression coverage for Codex/Copilot parity, pass-through behavior, reinstall, and source updates.
  • Documents the Codex link behavior and records the fix in the changelog.
File Description
tests/​unit/​integration/​test_agent_integrator.py Adds link-resolution and reinstall/update regression tests.
src/​apm_cli/​integration/​agent_integrator.py Delegates Codex body-link rewriting to the shared resolver.
packages/​apm-guide/​.apm/​skills/​apm-usage/​package-authoring.md Clarifies link rewriting in Codex instructions.
docs/​src/​content/​docs/​producer/​package-relative-links.md Includes Codex TOML in the shared link contract.
docs/​src/​content/​docs/​producer/​author-primitives/​instructions-and-agents.md Documents links relative to the generated agent location.
CHANGELOG.md Records the Codex link-resolution fix.

Review effort: Balanced

Register install-deployment-codex-agent-link-resolution on the
BaseIntegrator owner record. The production Codex branch must pass
_write_codex_agent a resolve_body_links callback bound to
self.resolve_links with source_file/target_path, report the returned
count as links_resolved, and the writer must apply it before TOML
serialization. Direct static writer callers stay exempt. Without this
guard, dropping the callback would silently restore the #2966 bypass
while boundary lint passed.

Addresses Copilot inline on src/apm_cli/integration/agent_integrator.py:286.
Mutation-break: source rebinding, dropped keyword, wrong callee,
discarded count, and skipped writer application each fail the rule.

Co-authored-by: Copilot App <[email protected]>
The Codex writer now takes the shared ResolveLinks hook (the shape
skill_integrator already uses) and calls it with its own source and
target, so the call site cannot bind a mismatched source/target pair.
The architecture guard now also requires the resolved body to be what
gets serialized, and the BaseIntegrator owner record names install-time
link rewriting.

Addresses panel follow-ups (python-architect FU-3, FU-5, FU-6).

Co-authored-by: Copilot App <[email protected]>
Adds a real-binary Copilot+Codex row to test_link_rewrite_e2e.py: the
converted TOML's developer_instructions link reaches the same installed
skill as the Copilot agent, and reinstall is byte-identical. Adds a unit
test proving a managed TOML written before #2966 is repaired on the next
install. Both fail when the Codex branch bypasses resolve_links.

Addresses panel follow-ups (test-coverage-expert FU-1, FU-2).

Co-authored-by: Copilot App <[email protected]>
Kiro agents do not rewrite links, so "the other agent targets" overstated
parity. Name the Markdown/verbatim targets instead.

Addresses panel follow-up (doc-writer FU-4).

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

Copy link
Copy Markdown
Collaborator Author

APM Review Panel: ship_now

Codex agent conversion now rewrites package-relative links the same way Markdown agent targets do. All six lean-pass follow-ups are folded, and the delta panel found nothing new.

panel-mode=delta; personas=python-architect,test-coverage-expert,doc-writer,apm-ceo (prior pass in this run: panel-mode=lean, same roster; doc-writer added because the PR edits user docs and CHANGELOG)

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

What changed since the lean pass at 57565410c5: all six follow-ups were folded in 21d9753003, 28b06b61ee and 4ab5288b21. The Codex writer now receives the shared resolve_links hook (ResolveLinks | None) and calls it with its own source and target, so the earlier functools.partial double binding is gone. The architecture guard now requires the resolved body to be what gets serialized before .dumps, and the BaseIntegrator owner text names install-time link rewriting.

The test evidence is the strongest part of the change. A real-binary test installs one package for Copilot and Codex and asserts both agent links reach the same installed SKILL.md, the fragment is kept, and a reinstall is byte-identical. A unit test shows a Codex TOML written before the fix is repaired on reinstall. Setting the hook to None at the call site fails the unit and e2e tests, and the guard catches 7 kinds of bypass. That covers the scope record's generated-file correctness items; native-model behavior is not claimed.

Aligned with: Portable by manifest -- the same package now produces working links on Codex and Copilot with no new author convention. Multi-harness -- Codex matches the Markdown agent targets through the shared resolver, not a per-target fork. Pragmatic as npm -- reinstall repairs stale TOML, no manual cleanup.

Panel summary

Persona B R N Takeaway
Python Architect 0 0 0 FU-3, FU-5 and FU-6 are folded correctly; the writer calls the shared owner with its own source and target and serializes the resolved body.
Test Coverage Expert 0 0 0 FU-1 and FU-2 are folded correctly; the real-binary Codex+Copilot row and the stale-TOML repair test assert the user-visible fix.
Doc Writer 0 0 0 FU-4 is folded correctly; CHANGELOG and apm-guide compare Codex only to Markdown/verbatim agent targets.

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

Recommendation

Ready to land as is. All six lean-pass follow-ups are folded and backed by tests that run the real binary, and the delta panel found nothing new. Whether the native Codex model follows these links stays unverified and out of scope, as the scope record says.

Folded in this run

  • (copilot) Registered guard install-deployment-codex-agent-link-resolution on the BaseIntegrator owner record, with MutationCase and expected rule id -- resolved in 5756541.
  • (panel FU-1) Real-binary Copilot+Codex row in tests/integration/test_link_rewrite_e2e.py; reinstall byte-identical -- resolved in 28b06b6.
  • (panel FU-2) Unit test: stale pre-fix managed Codex TOML repaired on reinstall -- resolved in 28b06b6.
  • (panel FU-3) Guard requires the resolved body to be what is serialized -- resolved in 21d9753.
  • (panel FU-4) CHANGELOG / apm-guide wording scoped to Markdown/verbatim agent targets (Kiro does not rewrite links) -- resolved in 4ab5288.
  • (panel FU-5) Writer takes the ResolveLinks hook and calls it with its own source/target -- resolved in 21d9753.
  • (panel FU-6) BaseIntegrator owner text names link rewriting via resolve_links -- resolved in 21d9753.

Copilot signals reviewed

  • src/apm_cli/integration/agent_integrator.py:286 -- LEGIT: a routed-through-owner change needs a registered static guard; same surface as this PR. Thread replied and resolved (resolved in 5756541).

Regression-trap evidence (mutation-break gate)

  • TestCodexAgentIntegration link tests (4) -- deleted resolve_links=self.resolve_links at the Codex call site; tests FAILED as expected; guard restored.
  • test_link_rewrite_e2e.py::TestMultiTargetLinkRewriting::test_codex_and_copilot_agent_links_reach_same_installed_skill -- same deletion; test FAILED (reviewer.toml link not rewritten); guard restored.
  • install-deployment-codex-agent-link-resolution -- 7 source mutations (bypass, dropped keyword, wrong callee, discarded count, skipped application, discarded body, source/target swap) each produce a rule violation.

Lint contract

ruff check and ruff format --check (CI path set) silent; pylint R0801 10.00/10; lint-auth-signals.sh and lint-architecture-boundaries.sh exit 0; file-length, YAML-IO and relative_to guards clean, all at 66cd5aa (PR merged with current main).

CI

Previous head 2c6ac25 (same PR content before the final main merge): 23 checks pass, 3 skipping, 0 failing. Final head 66cd5aa was pushed after merging current main; its CI run is in progress at posting time.

Mergeability status

PR head SHA CEO stance iters folds defers Copilot rounds CI mergeable mergeStateStatus notes
#3611 66cd5aa ship_now 2 7 0 2 yellow MERGEABLE BLOCKED pending required review; CI running
Full per-persona findings

Python Architect

No findings in the delta pass. Lean pass raised three recommended items (FU-3, FU-5, FU-6), all folded above.

Test Coverage Expert

No findings in the delta pass. Lean pass raised two recommended items (FU-1, FU-2), both folded above.

Doc Writer

No findings in the delta pass. Lean pass raised one recommended item (FU-4), folded above.

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.

Avoids landing-order conflicts with other queued PRs.

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

Copy link
Copy Markdown
Collaborator Author

Lane L: main merged for landing

  • Pre-merge head: 66cd5aacf3d303d534717ff45cfc9de98274c1b9
  • Merged origin/main c2c2e0d72c8e8c88f143099c73f40720b6529a4e (clean merge; GitHub reported DIRTY only on CHANGELOG.md, which this repo merges with merge=union).
  • New head: a021d7e7f3b6816278b0f453c9772160f7d7f95d
  • CHANGELOG: moved 1 bullet(s) in '### Fixed' after entry Add GitHub Enterprise (GHE) hostname support to dependency parsing #8 of 18, so queued PRs no longer collide at the section head while landing serially.
  • Lint: ruff check + ruff format --check silent at the new head; CI green at the new head.
  • Owner-touch report regenerated at the new head; every recorded functional test was re-run at that exact head and passed (one process at a time, no xdist).
  • Push: git push https://github.com/microsoft/apm.git HEAD:refs/heads/danielmeppiel-fix-codex-agent-link-resolution-2966 (plain push, no force).

This comment was generated by an AI agent (Lane L landing desk) on behalf of Daniel Meppiel (@danielmeppiel).

@danielmeppiel
Daniel Meppiel (danielmeppiel) merged commit 1ed1523 into main Nov 12, 2026
26 checks passed
@danielmeppiel
Daniel Meppiel (danielmeppiel) deleted the danielmeppiel-fix-codex-agent-link-resolution-2966 branch November 12, 2026 18:43
Daniel Meppiel (danielmeppiel) added a commit that referenced this pull request Nov 12, 2026
Clean merge of eight main commits (#3408, #2231, #2602, #3640, #3611,
#3645, #3602, #3646); no lockfile or autopilot package conflicts and
apm audit --ci passes with no drift.

Co-authored-by: sergio-sisternes-epam <[email protected]>
Co-authored-by: Copilot App <[email protected]>
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.

[BUG] Codex agent conversion leaves source-relative Markdown links unresolved

2 participants