Skip to content

Fix tokens compare in subdirectory showing all files as "added"#105

Merged
chlowell merged 2 commits into
microsoft:mainfrom
chlowell:compare
Mar 12, 2026
Merged

Fix tokens compare in subdirectory showing all files as "added"#105
chlowell merged 2 commits into
microsoft:mainfrom
chlowell:compare

Conversation

@chlowell

@chlowell chlowell commented Mar 10, 2026

Copy link
Copy Markdown
Member

When running waza tokens compare from a subdirectory of a git repository, all files were incorrectly reported as added instead of modified . The "Before" column showed - for every file, even though the files existed at the base ref:

📊 Token Comparison: HEAD → WORKING

File                   Before     After      Diff  Status
-------------------------------------------------------------------
SKILL.md                    -      1095     +1095  🆕

Root Cause

A mismatch between how two git commands resolve file paths when the working directory is a subdirectory of the repository:

  • git ls-tree -r --name-only HEAD (used by GetFilesFromRef) is CWD-aware — it returns paths relative to the current directory (e.g., README.md).
  • git show HEAD:README.md (used by GetFileFromRef) treats the path as repo-root-relative — so it looks for README.md at the repository root, not at subdir/README.md.

When the file doesn't exist at the repo root, git show fails. The error is silently caught (by design, matching the TypeScript implementation), causing hasBase to remain false and every file to be classified as "added".

Fix

Added a new GetCWDFileFromRef function that uses a ./ path prefix to resolve
files relative to the working directory (matching ls-tree's behavior):

// GetCWDFileFromRef — resolves path relative to dir (CWD), not the repo root.
func GetCWDFileFromRef(dir, file, ref string) (string, error) {
    cmd := exec.Command("git", "-C", dir, "show", ref+":./"+file)
    // ...
}

compareFilesets now uses GetCWDFileFromRef for reading file content (where
paths come from CWD-relative ls-tree output), while GetFileFromRef remains
repo-root-relative for callers that intentionally read root-level files (e.g.,
skillRootsForRef reading .waza.yaml at a ref).

Changed Files

  • cmd/waza/tokens/internal/git/git.go — added GetCWDFileFromRef; updated GetFileFromRef doc to clarify repo-root path resolution
  • cmd/waza/tokens/internal/git/git_test.go — added TestGetCWDFileFromRef with 4 subtests
  • cmd/waza/tokens/compare.gocompareFilesets uses GetCWDFileFromRef for file content reads
  • cmd/waza/tokens/compare_test.go — added TestCompare_Subdirectory regression test; added --skills two-ref test

Copilot AI review requested due to automatic review settings March 10, 2026 19:39

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.

Pull request overview

This PR fixes waza tokens compare behavior when executed from a subdirectory of a git repo, where files were incorrectly classified as “added” due to inconsistent path resolution between git ls-tree and git show.

Changes:

  • Adjust GetFileFromRef to prefix paths with ./ so git show <ref>:./<file> resolves relative to the current working directory.
  • Add a regression test to ensure subdirectory execution reports modified files as modified (📈) rather than added (🆕).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
cmd/waza/tokens/internal/git/git.go Updates git show invocation to align path resolution with subdirectory execution.
cmd/waza/tokens/compare_test.go Adds a regression test covering tokens compare when run from a subdirectory.

Comment thread cmd/waza/tokens/internal/git/git.go
Comment thread cmd/waza/tokens/compare_test.go
richardpark-msft pushed a commit to richardpark-msft/waza that referenced this pull request Mar 10, 2026
Related to microsoft#105

Proactive test cases for the action_sequence grader. Written from the
spec while Linus implements.

Covers: exact_match, in_order_match, any_order_match modes, score
calculations, error cases, and factory integration.

**Test breakdown (31 test cases):**
- Constructor validation: 6 tests (missing actions, empty actions,
invalid mode, valid modes)
- exact_match mode: 6 tests (pass, extra step, missing step, wrong
order, empty actual, single tool)
- in_order_match mode: 5 tests (exact seq, extras interspersed, wrong
order, missing step, subset order)
- any_order_match mode: 5 tests (reordered, extras, missing, duplicate
expected, insufficient duplicates)
- Score calculations: 4 tests (perfect, partial, zero, details
verification + f1 check)
- Edge cases: 3 tests (nil session, nil tools_used, duration recording)
- Factory integration: 2 tests (Create success, Create invalid params
error)

**Note:** These tests depend on the implementation in the companion PR.
Merge the implementation PR first.
@chlowell chlowell marked this pull request as ready for review March 10, 2026 20:46
Copilot AI review requested due to automatic review settings March 10, 2026 20:46
@chlowell chlowell enabled auto-merge (squash) March 10, 2026 20:46

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.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

Comment thread cmd/waza/tokens/internal/git/git.go
Comment thread cmd/waza/tokens/internal/git/git_test.go
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (main@f3371ce). Learn more about missing BASE report.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #105   +/-   ##
=======================================
  Coverage        ?   72.83%           
=======================================
  Files           ?      130           
  Lines           ?    14826           
  Branches        ?        0           
=======================================
  Hits            ?    10799           
  Misses          ?     3222           
  Partials        ?      805           
Flag Coverage Δ
go-implementation 72.83% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@wbreza wbreza 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.

Code Review: PR #105 — Fix tokens compare in subdirectory showing all files as "added"

✅ What Looks Good

  • Clear root cause analysis — PR description is excellent, explains the ls-tree vs git show path resolution mismatch thoroughly
  • Surgical fix — only 2 call sites changed, preserving GetFileFromRef for callers that intentionally want repo-root paths (e.g., reading .waza.yaml)
  • Comprehensive tests — 4 unit subtests for GetCWDFileFromRef (repo root, subdirectory, repo-root file invisible from subdir, confirming old function unchanged) + regression test + two-ref skills test
  • Proper error wrappingErrFileNotFound sentinel makes it easy for callers to distinguish missing files from git errors

🟡 Medium (1 finding)

1. Inconsistent error handling patternsGetFileFromRef uses errors.AsType[*exec.ExitError] (generics-based) while GetCWDFileFromRef uses errors.As(err, &exitErr) (traditional). Same file, same purpose — consider using one pattern consistently.

Summary

Priority Count
Critical 0
High 0
Medium 1
Low 0

Overall Assessment: Approve — clean, well-documented bug fix with thorough test coverage.

Comment thread cmd/waza/tokens/internal/git/git.go
@chlowell chlowell merged commit 4b89acd into microsoft:main Mar 12, 2026
6 checks passed
@chlowell chlowell deleted the compare branch March 12, 2026 20:17
@spboyer spboyer mentioned this pull request Mar 12, 2026
4 tasks
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.

4 participants