Skip to content

fix(telemetry): a devops command run from a deleted working directory gives no FileNotFoundError traceback from the git directory lookup #2531

Closed

Description

@dan-petty

Problem Description

A devops gh api call printed a FileNotFoundError traceback when its working directory no longer existed. The post-merge worktree cleanup had deleted the directory. The telemetry setup calls _resolve_git_dir (src/devops_cli/telemetry/tracer.py), which starts from Path.cwd(), and Path.cwd() raises FileNotFoundError for a deleted directory. The command did not report a clean error.

Reproduction Steps

  1. mkdir /tmp/gone && cd /tmp/gone && rmdir /tmp/gone
  2. uv run --project /workspaces/devops-cli devops gh api rate_limit

Expected vs Actual Behavior

Expected: telemetry finds no git directory and the command runs, or the command stops with one error line that says the working directory does not exist.

Actual: a FileNotFoundError traceback from _resolve_git_dir.

Environment Diagnostics

main at 2026-10-28, devcontainer.

Logs, Traces, or Terminal Output

telemetry/tracer.py in _resolve_git_dir -> Path.cwd()
FileNotFoundError: [Errno 2] No such file or directory

Proposed design

Problem statement

A devops command run from a deleted working directory crashes with an unhandled FileNotFoundError traceback. The telemetry setup calls _resolve_git_dir, which calls Path.cwd(). Path.cwd() raises FileNotFoundError when the directory no longer exists. The command does not report a clean error and does not complete.

Acceptance criteria

Requirement: Deleted CWD returns no git directory

_resolve_git_dir SHALL return None when Path.cwd() raises FileNotFoundError because the working directory no longer exists.

Scenario: CWD deleted before call

  • WHEN Path.cwd() raises FileNotFoundError because the process working directory was removed
  • THEN _resolve_git_dir returns None and no exception propagates to the caller

Requirement: Command completes without traceback

A devops command run from a deleted working directory SHALL complete without an unhandled FileNotFoundError traceback from the telemetry setup.

Scenario: gh api from deleted directory

  • WHEN a devops gh api command runs after its working directory has been deleted
  • THEN the command exits with a clean result or a single error line, not a traceback

Requirement: Normal resolution unaffected

_resolve_git_dir SHALL return the correct git directory when the working directory exists and contains a .git entry.

Scenario: CWD inside a git repository

  • WHEN the process working directory exists and is inside a git repository
  • THEN _resolve_git_dir returns the path to the .git directory as before

Existing Tool Considered

No external tool or library covers this need. The fix is a try/except OSError around the Path.cwd() call in _resolve_git_dir (src/devops_cli/telemetry/tracer.py:628). The written cost is one try/except block of about 4 lines, one test case, and the maintenance of that guard for the lifetime of the function.

Key questions

  • Where in the call chain should the FileNotFoundError from Path.cwd() be caught?: In _resolve_git_dir itself, at the Path.cwd() call site. This is the origin of the exception, and the function already returns None when no git directory is found, so returning None on a deleted CWD is consistent with its contract. Catching it in _detect_vcs_metadata would leave _resolve_git_dir vulnerable for any other caller. (best practice) [sources: src/devops_cli/telemetry/tracer.py:628, src/devops_cli/telemetry/tracer.py:646]
    • Chosen: Catch in _resolve_git_dir at the Path.cwd() call. Most targeted fix. Protects all callers. Matches the function's existing None-return contract for the not-found case.
    • Rejected: Catch in _detect_vcs_metadata. Leaves _resolve_git_dir vulnerable for other callers. The exception would still propagate if any other code calls _resolve_git_dir directly.
    • Rejected: Catch in the CLI entry point. Too broad. Hides the specific cause and makes the telemetry path less testable in isolation.
  • Which exception type should the handler catch?: OSError. FileNotFoundError is a subclass of OSError. Path.cwd() can raise OSError for reasons other than a missing directory (for example, a permission error on the underlying getcwd syscall). The existing code in this file already catches OSError in _resolve_git_dir_from_file and _read_packed_refs, so OSError is the established pattern. (best practice) [sources: src/devops_cli/telemetry/tracer.py:618, src/devops_cli/telemetry/tracer.py:596]
    • Chosen: Catch OSError. Covers FileNotFoundError and any other OS-level failure from getcwd. Matches the existing pattern in this file.
    • Rejected: Catch FileNotFoundError only. Narrower than needed. A permission error on getcwd would still crash the command.
  • What value should _resolve_git_dir return when the CWD is unavailable?: None. The function's signature is Path | None, and it already returns None when no .git entry is found in the directory tree. A deleted CWD is another case of 'no git directory found', so None is the correct and consistent return. (best practice) [sources: src/devops_cli/telemetry/tracer.py:628]
    • Chosen: Return None. Matches the existing not-found contract. The caller _detect_vcs_metadata already handles None by returning an empty dict.
    • Rejected: Raise a custom exception. Adds a new exception type for a case that is semantically identical to 'no git directory found'. Unnecessary complexity.
  • Does _normalize_code_filepath need the same guard?: No. It already wraps its body in try/except Exception and returns the original filepath on any failure. A FileNotFoundError from Path.cwd() inside that function is already caught. [sources: src/devops_cli/telemetry/tracer.py:700]

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    source/agent
    type/bug
    status/new
    value/low
    effort/low
    on Oct 28, 2026
  2. dan-petty commented on Oct 28, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones.

    • Type: type/bug. The command crashes with an unhandled FileNotFoundError traceback instead of reporting a clean error.
    • Priority: P2-Medium. The crash is real but only occurs in the rare case of a deleted working directory.
    • Value: Low. Most users will not run a command from a directory that no longer exists.
    • Effort: Low. The fix is to catch the FileNotFoundError from Path.cwd() in one function and return a clean result.
    • Related: fix(github): gh project sync and reconcile resolve their template and task paths against the cwd and fail outside the repo root #1010, which asks for a different change. Both items handle cwd-related failures in devops commands, but #1010 fixes relative path resolution in project commands while the candidate fixes an unhandled Path.cwd() exception in telemetry/tracer.py.
  3. added
    priority/p2-medium
    on Oct 28, 2026
  4. added
    status/ready
    and removed
    status/new
    on Oct 29, 2026
  5. devops-cli-service commented on Oct 29, 2026

    @devops-cli-service
    Contributor

    Status set to Ready at 0f7bc26 (see proposed design in issue body).

  6. added
    scope/telemetry
    on Oct 30, 2026
  7. devops-cli-service commented on Nov 1, 2026

    @devops-cli-service
    Contributor

    Staged: branch fix/2531-a-devops-command-run-from-a, draft pull request #2995. The pull request plans the item and closes no issue.

    The task groups, with the tokens of the inputs of each step, for a budget of 40000 tokens:

    1. Guard Path.cwd in _resolve_git_dir: 3110 tokens

    These paths of the plan are no file on the default branch, and no group creates them: tests/test_tracer.py. A person checks that the plan names them right.

  8. added
    owner/service
    status/in-progress
    and removed
    status/ready
    on Nov 1, 2026
  9. 27 remaining items

  10. added
    status/blocked
    and removed
    status/in-progress
    on Nov 1, 2026
  11. devops-cli-service commented on Nov 1, 2026

    @devops-cli-service
    Contributor

    The runner Job devops-cli-runner-20261101-230849-2b22 ended: succeeded.
    Image digest: sha256:b949bd8406a0bfcfb30e4f62b707fbdba2709666e80c3d6b6f57d6dd9a11e53f

    The last lines of its log:

    Tool call: shell, started: uv run devops ci test tests/telemetry/test_telemetry.py::test_span_no_vcs_attributes_when_working_directory_deleted ...
    Tool call: shell, program uv, ok.
    Tool call: shell, started: uv run pytest tests/telemetry/test_telemetry.py -k test_span_no_vcs_attributes_when_working_directory_deleted ...
    Tool call: shell, program uv, ok.
    Tool call: shell, started: uv run pytest tests/commands/test_command_error_exit.py -k test_command_help_works_when_working_directory_deleted ...
    Tool call: shell, program uv, ok.
    Tool call: shell, started: git diff
    Tool call: shell, program git, ok.
    Tool call: shell, started: git diff HEAD~1
    Tool call: shell, program git, ok.
    Tool call: shell, started: git diff HEAD~1 src/devops_cli/telemetry/tracer.py src/devops_cli/core/cli.py
    Tool call: shell, program git, ok.
    Tool call: shell, started: git diff HEAD~1 src/devops_cli/telemetry/tracer.py src/devops_cli/core/cli.py | ...
    Tool call: shell, program git, refused.
    Tool call: shell, started: git diff HEAD~1 src/devops_cli/telemetry/tracer.py | head ...
    Tool call: shell, program git, refused.
    Tool call: shell, started: git status
    Tool call: shell, program git, ok.
    Tool call: shell, started: git diff --name-only
    Tool call: shell, program git, ok.
    Tool call: shell, started: git diff --stat
    Tool call: shell, program git, ok.
    Tool call: shell, started: uv run devops ci docs generate ...
    Tool call: shell, program uv, ok.
    Tool call: shell, started: uv run pytest tests/telemetry/test_telemetry.py -k <masked> ...
    Tool call: shell, program uv, ok.
    Tool call: shell, started: uv run pytest tests/commands/test_command_error_exit.py -k <masked> ...
    Tool call: shell, program uv, ok.
    Tool call: shell, started: cd /home/devops/.data/agent/worktrees/w-2531 && echo <masked>
    Tool call: shell, program cd, refused.
    Tool call: shell, started: other
    Tool call: shell, program other, refused.
    Tool call: shell, started: other
    Tool call: shell, program other, refused.
    Tool call: shell, started: git diff --name-only
    Tool call: shell, program git, ok.
    Tool call: shell, started: git diff --stat
    Tool call: shell, program git, ok.
    ✓ Took 0 item(s) to a ready pull request, stopped 1.
    Elapsed: 31m 27s
    
  12. added
    status/planned
    and removed
    owner/service
    status/blocked
    on Nov 1, 2026
  13. devops-cli-service commented on Nov 1, 2026

    @devops-cli-service
    Contributor

    Cleared the stop at fix by the rule digest: the runner image changed since the stop, from sha256:b949bd8406a0bfcfb30e4f62b707fbdba2709666e80c3d6b6f57d6dd9a11e53f to sha256:f206af606614022263cb3e3e36711137492f1954c806edd078e8193eb82a254c. The item is Planned again, and the Service takes it in the next round. The Service clears a stop one time for each runner image. A new stop on this image waits for a comment implement: retry.

  14. removed
    status/planned
    on Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/lowpriority/p2-mediumscope/telemetrysource/agenttype/bugvalue/low

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions