Skip to content

fix: precheck_file matches ./ and absolute paths; get_issue accepts unpadded IDs - #26

Merged
riponcm merged 4 commits into
riponcm:mainfrom
vishalhabib99:fix/precheck-path-and-issue-id-normalization
Oct 29, 2026
Merged

riponcm merged 4 commits into
riponcm:mainfrom
vishalhabib99:fix/precheck-path-and-issue-id-normalization

Conversation

@vishalhabib99

Copy link
Copy Markdown
Contributor

Two small input-normalization fixes in the MCP tools. Each tool's field description already promises the input form that currently fails.

1. precheck_file says "Safe to modify" for ./ and absolute paths

The file_path field says "Project-relative or absolute file path to check", but _events_for_file compares raw strings. With 3 failed attempts and an open issue logged at src/auth.py:

file_path Before After
src/auth.py [BLOCK] What already failed here (3 attempts) + open issue same
./src/auth.py no warnings. Safe to modify. same warnings as src/auth.py
/abs/path/to/proj/src/auth.py no warnings. Safe to modify. same warnings as src/auth.py

It also fails the other way round: an issue logged with an absolute location was missed by precheck_file("src/db.py").

Fix: a small project_relative() helper next to is_memory_path in staleness.py. It folds ./ and .. with normpath, and turns an absolute path inside the root into a project-relative one, resolving it too for macOS /tmp and symlinked folders. _analyze_files applies it to the checked paths, and _events_for_file applies it to e.files and the location file. Paths outside the project are left as they are.

2. get_issue rejects unpadded IDs

The field says "Numeric strings without padding (e.g., '42') are also accepted", but get_issue("1") returned No issue found with ID 1., because the raw input went straight into glob(f"{issue_id}-*.md"). That also meant get_issue("*") returned whichever issue file came first.

Fix: normalize with the existing normalize_issue_id, the same helper record_fix uses. An ID that is still not numeric after that returns the usual "No issue found" message, so it never reaches the glob.

Tests

Four new stdio tests in tests/test_mcp_server.py: the three path spellings, an event logged with an absolute path, and get_issue with 0001/1/01/#1 plus * and a missing ID. All 4 fail on main (e49dd0c) and pass with this change. The full suite passes: 361 tests, Python 3.12, macOS.

precheck_file accepts a project-relative or absolute path, but events were
matched by exact string, so ./src/auth.py or the absolute path came back
"Safe to modify" past failed attempts and open issues on src/auth.py.
Normalize both the checked path and event paths to project-relative.
The field says '42' works, but get_issue('1') found nothing; it now uses
normalize_issue_id like record_fix. A non-numeric ID such as '*' returned
an arbitrary issue and now returns 'No issue found'.
@vishalhabib99

Copy link
Copy Markdown
Contributor Author

Hi @riponcm, just checking this one is still on your radar. It's still mergeable, and the new tests in tests/test_mcp_server.py still fail on main (./src/auth.py, an absolute path and an unpadded issue ID all miss) and pass here. Happy to change anything.

@riponcm

riponcm commented Oct 28, 2026

Copy link
Copy Markdown
Owner

Thanks for the fix and for the nudge, and sorry for the wait. I reproduced both bugs on main: the same file gives 2 warnings as src/auth.py and none as ./src/auth.py or as an absolute path, which is exactly the "Safe to modify" false negative you describe. I'm doing a full review this week and plan to ship it in 0.3.5.

@riponcm riponcm left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks again. Both fixes are right, and your 4 tests are exactly the reproductions I'd want: they fail on main and pass here, and the full suite is green. A second reviewer and I went through it, and four things need changing before merge:

  1. Speed. _events_for_file now calls project_relative for every path of every event, once per checked file, and absolute paths go through resolve(). On a 3,000-event log with some absolute paths, matching 5 files went from 14 ms to about 1 s, and pjm precheck from 0.7 s to 1.8 s. Precheck runs before every edit, and 0.3.3 made it flat in project size, so please normalize each event's paths once per call, with a dict cache or an index built in one pass, and resolve the root only once.
  2. legacy_XXXX issue ids. pjm init runs backfill, which creates ids like legacy_f436. The new isdecimal() check makes get_issue refuse them, while pjm fix still accepts them. Keeping the glob safe with a shape check such as re.fullmatch(r"[\w-]+", wanted) would cover both. Please add a legacy_ case to the test.
  3. Windows absolute paths. posixpath.isabs("C:/proj/src/a.py") is False, so on a Windows server a drive-letter path is never made project-relative and still comes back "Safe to modify". is_memory_path just above handles this with _WINDOWS_DRIVE; the same check works here.
  4. Stale check. staleness.location_file still returns the raw location, so a decision stored with an absolute path is flagged by pjm brief but not by precheck's staleness check for src/auth.py. Running it through project_relative keeps the two consistent.

Smaller, at your discretion: the project root's own absolute path normalizes to "." and then matches any event whose summary contains a period; two spellings of one file produce duplicate warnings, which deduping after normalization fixes; and project_relative and is_memory_path could share one anchoring helper so they can't drift apart.

Happy to help with any of it. Once these are in, I'll merge it for 0.3.5.

…ive paths, stale check

- Speed: each event's paths are normalized once per call (cached per
  distinct raw path) and the root is resolved once; find_stale_events
  judges each distinct location once too.
- get_issue accepts any ID-shaped input ([\w-]+), so backfill's
  legacy_XXXX ids work again while globs stay out.
- project_relative anchors Windows drive-letter paths.
- staleness.location_file returns the project-relative spelling, so
  precheck and brief agree on a decision logged with an absolute path.
- The project root (".") is never checked as a file, and two spellings
  of one file warn once.
- is_memory_path and project_relative share one anchoring helper.
@vishalhabib99

Copy link
Copy Markdown
Contributor Author

Thanks for the careful review. All four are in fb5542b, plus the three smaller ones.

  1. Speed. _live_event_paths normalizes the log in one pass, caching each distinct raw path, and the root is resolved once per call (root_posix_of, passed down as root_posix=). find_stale_events gets the same treatment: one root resolve, each distinct location judged once. Bench: 3,000 events, every third one with absolute files/location, matching 5 files, best of 5, macOS:

    • main: 320 ms, pjm precheck 0.42 s
    • previous head: 1,651 ms, 1.83 s
    • now: 160 ms, 0.26 s

    It comes in under main because location_file used to resolve the root once per event. Pinned by test_precheck_normalizes_each_logged_path_once, which counts calls.

  2. legacy_XXXX ids. get_issue now checks re.fullmatch(r"[\w-]+", wanted). legacy_f436 resolves, and legacy_*, legacy_f43? and [l]egacy_f436 are refused (test_mcp_get_issue_finds_backfilled_legacy_ids).

  3. Windows drive paths. project_relative treats _WINDOWS_DRIVE paths as absolute, so C:\proj\src\a.py becomes src/a.py against a C:/proj root, and a path on another drive stays unanchored. There's a unit test, plus a precheck test with the root faked as C:/proj.

  4. Stale check. location_file returns the project_relative spelling. A decision located at <abs>/src/f.py:3 is now flagged by find_stale_events(..., only_files={"src/f.py"}) (test_a_decision_logged_at_an_absolute_path_is_stale_for_precheck_too).

Smaller ones:

  • The root's own path (".") is dropped before matching.
  • Files are deduped after normalization, so src/f.py, ./src/f.py and the absolute path give one warning.
  • is_memory_path and project_relative now share _anchor, the root-relative step that includes the resolved-spelling fallback.

Tests: 7 new, and all of them fail on the previous head. Full suite 368 passed (361 before). Ruff output is unchanged on the touched files.

@riponcm riponcm left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks, this is excellent. I re-ran everything on my side:

  • Speed: at 3,000 events, precheck now takes 163 ms, against 663 ms on main.
  • Paths: every spelling of a path gives the same warnings, including ./, absolute, a symlinked folder, backslashes and src/../.
  • IDs: legacy_ IDs resolve, and glob characters are refused.
  • Tests: all 7 new tests fail on the previous head, and 368 pass on Python 3.10–3.14.

Approving. This ships in 0.3.5.

@riponcm
riponcm merged commit 626fd35 into riponcm:main Oct 29, 2026
@vishalhabib99

Copy link
Copy Markdown
Contributor Author

Thanks @riponcm, and the second reviewer, for the careful review and for re-running the speed numbers (163 ms vs 663 ms at 3,000 events). Glad it's in!

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.

2 participants