From 9b9e7e434f9188822ca96bc0eff7475a40996f77 Mon Sep 17 00:00:00 2001 From: bodapatisaikrishna Date: Fri, 13 Nov 2026 11:44:46 +0530 Subject: [PATCH 1/3] fix(compile): validate link paths independently of suffixes A markdown target such as references/standards.md#template names a file plus a section address. validate_link_targets passed the whole target to the filesystem check, so an existing file produced a false 'Referenced file not found' warning whenever the link carried a #fragment, ?query, or both. Split the target with UnifiedLinkResolver._split_link_target, the same interpretation the rewrite path already uses, and validate only the path component. Missing-file diagnostics now name that path without the suffix. Suffix values are not validated, and link emission, inlining, install-time rewriting and dry-run write semantics are unchanged. Fixes #3255 --- CHANGELOG.md | 1 + .../docs/producer/package-relative-links.md | 5 ++ src/apm_cli/compilation/link_resolver.py | 14 ++++-- .../test_compile_link_validation.py | 49 +++++++++++++++++++ .../compilation/test_link_resolver_phase3.py | 13 +++++ 5 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 tests/unit/compilation/test_compile_link_validation.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e263c98a0..3f73ba2190 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- `apm compile` validates the file path of a relative Markdown link independently of a `#fragment`, `?query` or combined suffix, so an existing file no longer produces a false `Referenced file not found` warning; a genuinely missing target is still reported using the path without the suffix. Heading and query values themselves are not validated, and emitted links, inlining, install-time rewriting and dry-run write semantics are unchanged. -- by @bodapatisaikrishna (#3255) - `APM_HOME` is now the single root for APM-owned user state: global `apm.yml`, `apm.lock.yaml` and `apm_modules/`, `config.json`, marketplace registrations, lifecycle trust and logs, and deployment receipts all follow it, so two `APM_HOME` values no longer share or leak into `~/.apm`. Unset keeps `~/.apm`; a relative value resolves against the starting directory; target deployment destinations are unchanged, and state-changing commands still serialize across `APM_HOME` values. -- by @WilliamK112 (#3614) - `apm install` for Codex now warns, naming the agent, when an agent's `model` is a Claude alias (`opus`, `sonnet`, `haiku`, `inherit`) or `claude-*` id; the value is still written to the Codex TOML exactly as declared. Rebuilt from #3481 by @xubenqiang. (#3650) - `apm --help` shows the `apm view` summary in full again instead of wrapping it at 80 columns. (#3651) diff --git a/docs/src/content/docs/producer/package-relative-links.md b/docs/src/content/docs/producer/package-relative-links.md index be4516b269..5726df3682 100644 --- a/docs/src/content/docs/producer/package-relative-links.md +++ b/docs/src/content/docs/producer/package-relative-links.md @@ -70,6 +70,11 @@ different -- see below. `#fragment` and `?query` suffixes are preserved through the rewrite. +Compile-time validation checks the file path independently of those +suffixes: an existing file is not reported missing just because the link +adds a section address. The suffix itself is not validated -- APM does +not check that the heading or query exists in the target document. + ## What APM does not rewrite | Link form | Why it is left alone | diff --git a/src/apm_cli/compilation/link_resolver.py b/src/apm_cli/compilation/link_resolver.py index 2414c79820..33bc8b6e38 100644 --- a/src/apm_cli/compilation/link_resolver.py +++ b/src/apm_cli/compilation/link_resolver.py @@ -616,13 +616,21 @@ def validate_link_targets(content: str, base_path: Path) -> builtins.list[str]: if path.startswith(("http://", "https://", "ftp://", "mailto:")) or path.startswith("#"): continue + # A markdown target may carry a ``#fragment``, ``?query``, or both. + # Those address part of the target; they are not part of the filename. + # Reuse the resolver's existing split so validation agrees with the + # rewriting path instead of looking for ``file.md#fragment``. + path_part, _suffix = UnifiedLinkResolver._split_link_target(path) + if not path_part: + continue + # Resolve and check path - full_path = _resolve_path(path, base_path) + full_path = _resolve_path(path_part, base_path) if not full_path or not full_path.exists(): - errors.append(f"Referenced file not found: {path} (in link '{text}')") + errors.append(f"Referenced file not found: {path_part} (in link '{text}')") elif not full_path.is_file() and not full_path.is_dir(): errors.append( - f"Referenced path is neither a file nor directory: {path} (in link '{text}')" + f"Referenced path is neither a file nor directory: {path_part} (in link '{text}')" ) return errors diff --git a/tests/unit/compilation/test_compile_link_validation.py b/tests/unit/compilation/test_compile_link_validation.py new file mode 100644 index 0000000000..fb362e116c --- /dev/null +++ b/tests/unit/compilation/test_compile_link_validation.py @@ -0,0 +1,49 @@ +"""Compile-time link validation for suffixed markdown targets. + +Issue #3255: a target such as ``references/standards.md#template`` names a +file plus a section, not a file called ``standards.md#template``. ``apm +compile`` must validate the filesystem path without the suffix and keep the +suffix out of missing-file diagnostics. +""" + +from __future__ import annotations + +from pathlib import Path + +from click.testing import CliRunner + +from apm_cli.cli import cli + + +def _write_instruction(project: Path) -> None: + instruction = project / ".apm" / "instructions" / "test.instructions.md" + instruction.parent.mkdir(parents=True) + instruction.write_text( + """--- +description: suffixed link fixture +applyTo: "**/*.py" +--- +See [template](references/standards.md#template), +[query](references/standards.md?view=full), +[combined](references/standards.md?view=full#template), and +[missing](references/missing.md#template). +""", + encoding="utf-8", + ) + + +def test_compile_dry_run_validates_suffixed_links_by_path(tmp_path: Path, monkeypatch) -> None: + monkeypatch.chdir(tmp_path) + (tmp_path / "apm.yml").write_text("name: test-project\nversion: 0.1.0\n", encoding="utf-8") + _write_instruction(tmp_path) + references = tmp_path / ".apm" / "instructions" / "references" + references.mkdir() + (references / "standards.md").write_text("# Template\n", encoding="utf-8") + + result = CliRunner().invoke(cli, ["compile", "--dry-run"]) + + assert result.exit_code == 0, result.output + output = " ".join(result.output.split()) + assert "Referenced file not found: references/standards.md" not in output + assert "Referenced file not found: references/missing.md (in link 'missing')" in output + assert not (tmp_path / "AGENTS.md").exists() diff --git a/tests/unit/compilation/test_link_resolver_phase3.py b/tests/unit/compilation/test_link_resolver_phase3.py index 5575f75da7..6eb211ae0f 100644 --- a/tests/unit/compilation/test_link_resolver_phase3.py +++ b/tests/unit/compilation/test_link_resolver_phase3.py @@ -557,6 +557,19 @@ def test_anchor_link_skipped(self, base_dir: Path) -> None: errors = validate_link_targets("[Heading](#heading)", base_dir) assert errors == [] + @pytest.mark.parametrize("suffix", ["#template", "?view=full", "?view=full#template"]) + def test_existing_file_with_suffix_no_errors(self, base_dir: Path, suffix: str) -> None: + (base_dir / "standards.md").write_text("content") + errors = validate_link_targets(f"[Standards](standards.md{suffix})", base_dir) + assert errors == [] + + @pytest.mark.parametrize("suffix", ["#template", "?view=full", "?view=full#template"]) + def test_missing_file_with_suffix_reports_path_without_suffix( + self, base_dir: Path, suffix: str + ) -> None: + errors = validate_link_targets(f"[Standards](missing.md{suffix})", base_dir) + assert errors == ["Referenced file not found: missing.md (in link 'Standards')"] + class TestResolvePath: def test_empty_string_returns_none(self, base_dir: Path) -> None: From 8a3adee98b772045adc8eef23362752dd7bbfb21 Mon Sep 17 00:00:00 2001 From: danielmeppiel Date: Sat, 21 Nov 2026 18:56:26 +0100 Subject: [PATCH 2/3] test: move #3255 validate_link_targets suffix cases to the unit module The return-value cases sat in a component module and tripped the AQ003 unobserved-outcome ratchet (72 > 70). The compile CLI case stays there. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- tests/unit/compilation/test_link_resolver.py | 13 ----------- .../test_link_resolver_resolution.py | 22 +++++++++++++++++++ 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/tests/unit/compilation/test_link_resolver.py b/tests/unit/compilation/test_link_resolver.py index 8dc3f6276e..460b0b475d 100644 --- a/tests/unit/compilation/test_link_resolver.py +++ b/tests/unit/compilation/test_link_resolver.py @@ -1262,19 +1262,6 @@ def test_anchor_link_skipped(self, base_dir: Path) -> None: errors = validate_link_targets("[Heading](#heading)", base_dir) assert errors == [] - @pytest.mark.parametrize("suffix", ["#template", "?view=full", "?view=full#template"]) - def test_existing_file_with_suffix_no_errors(self, base_dir: Path, suffix: str) -> None: - (base_dir / "standards.md").write_text("content") - errors = validate_link_targets(f"[Standards](standards.md{suffix})", base_dir) - assert errors == [] - - @pytest.mark.parametrize("suffix", ["#template", "?view=full", "?view=full#template"]) - def test_missing_file_with_suffix_reports_path_without_suffix( - self, base_dir: Path, suffix: str - ) -> None: - errors = validate_link_targets(f"[Standards](missing.md{suffix})", base_dir) - assert errors == ["Referenced file not found: missing.md (in link 'Standards')"] - class TestResolvePath: def test_whitespace_only_returns_none(self, base_dir: Path) -> None: diff --git a/tests/unit/compilation/test_link_resolver_resolution.py b/tests/unit/compilation/test_link_resolver_resolution.py index 6058743b1d..fa026099cb 100644 --- a/tests/unit/compilation/test_link_resolver_resolution.py +++ b/tests/unit/compilation/test_link_resolver_resolution.py @@ -6,6 +6,7 @@ - _is_rewritable_relative_link (empty, fragment, //, /, scheme, relative) - _split_link_target (no delimiter, fragment, query, both) - _remove_frontmatter +- validate_link_targets with #fragment / ?query suffixes (#3255) """ from __future__ import annotations @@ -17,6 +18,7 @@ from apm_cli.compilation.link_resolver import ( UnifiedLinkResolver, _remove_frontmatter, + validate_link_targets, ) pytestmark = pytest.mark.unit @@ -170,3 +172,23 @@ def test_empty_frontmatter_is_stripped(self) -> None: def test_content_without_leading_dashes_unchanged(self) -> None: content = "# No frontmatter\n\nBody text" assert _remove_frontmatter(content) == content + + +# --------------------------------------------------------------------------- +# validate_link_targets with suffixes (#3255) +# --------------------------------------------------------------------------- + + +class TestValidateLinkTargetsSuffixes: + @pytest.mark.parametrize("suffix", ["#template", "?view=full", "?view=full#template"]) + def test_existing_file_with_suffix_no_errors(self, tmp_path: Path, suffix: str) -> None: + (tmp_path / "standards.md").write_text("content") + errors = validate_link_targets(f"[Standards](standards.md{suffix})", tmp_path) + assert errors == [] + + @pytest.mark.parametrize("suffix", ["#template", "?view=full", "?view=full#template"]) + def test_missing_file_with_suffix_reports_path_without_suffix( + self, tmp_path: Path, suffix: str + ) -> None: + errors = validate_link_targets(f"[Standards](missing.md{suffix})", tmp_path) + assert errors == ["Referenced file not found: missing.md (in link 'Standards')"] From 3b798de4d9d34e968a0cd6212a44abcd7531219d Mon Sep 17 00:00:00 2001 From: danielmeppiel Date: Sun, 22 Nov 2026 14:57:57 +0100 Subject: [PATCH 3/3] test(compile): installed-CLI coverage for suffixed link validation (#3255) Drive the installed apm compile in normal and --dry-run modes over #fragment, ?query and combined suffixes: existing targets raise no missing-file warning, missing targets are named without the suffix, emitted links keep their suffix, and --dry-run writes nothing. Fails on main's link_resolver (2 failed), passes with the fix. Answers the review thread asking for the scope's installed-CLI done-when. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- ...st_compile_suffixed_link_validation_e2e.py | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 tests/integration/test_compile_suffixed_link_validation_e2e.py diff --git a/tests/integration/test_compile_suffixed_link_validation_e2e.py b/tests/integration/test_compile_suffixed_link_validation_e2e.py new file mode 100644 index 0000000000..44d920cd13 --- /dev/null +++ b/tests/integration/test_compile_suffixed_link_validation_e2e.py @@ -0,0 +1,82 @@ +"""Installed-CLI coverage for compile-time validation of suffixed links (#3255). + +A relative Markdown link may carry a ``#fragment``, ``?query`` or both. The +installed ``apm compile`` must validate the file path without the suffix: an +existing file is not reported missing, a missing file is reported by its path +alone, the emitted link keeps its suffix, and ``--dry-run`` writes nothing. +""" + +from __future__ import annotations + +import subprocess +from pathlib import Path + +import pytest + +pytestmark = [ + pytest.mark.integration, + pytest.mark.e2e, + pytest.mark.requires_apm_binary, + pytest.mark.requires_e2e_mode, + pytest.mark.promise("P-OWN-compiled-output-writes"), +] + +_SUFFIXES = ("#template", "?view=full", "?view=full#template") + + +def _create_project(project: Path) -> None: + (project / "apm.yml").write_text( + "name: suffixed-links\nversion: 0.1.0\ntargets:\n - codex\n", encoding="ascii" + ) + instructions = project / ".apm" / "instructions" + instructions.mkdir(parents=True) + links = [] + for index, suffix in enumerate(_SUFFIXES): + links.append(f"[present{index}](references/standards.md{suffix})") + links.append(f"[absent{index}](references/missing.md{suffix})") + (instructions / "links.instructions.md").write_text( + "---\ndescription: Suffixed links\napplyTo: '**'\n---\n# Links\n" + "\n".join(links) + "\n", + encoding="ascii", + ) + references = instructions / "references" + references.mkdir() + (references / "standards.md").write_text("# Template\n", encoding="ascii") + + +def _compile(apm_binary_path: Path, project: Path, *extra: str) -> str: + result = subprocess.run( + (str(apm_binary_path), "compile", *extra), + cwd=project, + capture_output=True, + text=True, + check=False, + ) + assert result.returncode == 0, result.stdout + result.stderr + return " ".join((result.stdout + result.stderr).split()) + + +@pytest.mark.parametrize("dry_run", [False, True], ids=["write", "dry-run"]) +def test_installed_compile_validates_suffixed_link_paths( + tmp_path: Path, apm_binary_path: Path, dry_run: bool +) -> None: + project = tmp_path / "project" + project.mkdir() + _create_project(project) + + output = _compile(apm_binary_path, project, *(("--dry-run",) if dry_run else ())) + + assert "Referenced file not found: references/standards.md" not in output + for index in range(len(_SUFFIXES)): + assert ( + f"Referenced file not found: references/missing.md (in link 'absent{index}')" in output + ) + assert "missing.md#" not in output + assert "missing.md?" not in output + + agents_md = project / "AGENTS.md" + if dry_run: + assert not agents_md.exists() + return + emitted = agents_md.read_text(encoding="utf-8") + for suffix in _SUFFIXES: + assert f"standards.md{suffix})" in emitted