fix(import): serialize comment marker-dedupe across concurrent imports

Merged
UN
4mo ago
+945-29

Problem

The native comment sync in pm github import --comments-mode annotations performed a read-markers-then-append sequence that is not atomic across two concurrent import processes targeting the same workspace: both can observe a GitHub comment id as absent and append it twice. Individual writes go through the pm CLI (which locks per mutation), but the check-then-act spans multiple mutations. Previously documented as a known limitation on syncGithubCommentsToAnnotations (CodeRabbit, PR #33).

Solution

A cross-process mutex serializes the marker-dedupe critical section, scoped per item so unrelated items in concurrent imports still sync in parallel:

  • acquireImportLock() — atomic fs.openSync(path, "wx") (O_EXCL) lockfile under the workspace runtime dir, JSON payload (pid, timestamp) shaped like the CLI's own locks so pm gc can sweep strays.
  • Stale-lock handling: a held lock is broken with a stderr warning when older than the TTL (default 5 min) or its recorded owner PID is dead.
  • syncGithubCommentsToAnnotations docstring updated: the limitation is lifted.

Tests

  • 10 new test cases in test/import-lock.test.ts, including a two-process parallel-import race test (via test/helpers/comment-sync-child.mjs) asserting zero duplicate markers.
  • Full suite: 122/122 green; build + changelog:check pass.

pm item

🤖 Generated with pi (K3) under Claude orchestration


Summary by cubic

Stops duplicate comments during concurrent imports by serializing the comment marker dedupe in pm github import --comments-mode annotations. Adds a per-item lockfile so the read-and-append step is atomic across processes; satisfies pm-github-503u.

  • Bug Fixes
    • Added a cross-process per-item lockfile in workspace locks/ via fs.openSync("wx"); payload mirrors the pm CLI lock format and includes a per-acquisition token. Staleness is liveness-first: break only on dead or recycled owner PIDs; TTL (5 minutes) applies only to unparseable payloads; never age-break a live owner.
    • Stale-break is now atomic via an O_EXCL <lock>.break sidecar election; the single winner re-verifies then unlinks, and crashed breaker sidecars age out (10s) and are cleaned.
    • Jittered backoff waits up to 30s; on timeout, skips that item's sync with a warning (never runs unlocked). If the lock mechanism is unavailable, degrades to unlocked with a warning. Cleans partial lockfiles on write failure and uses token-checked release to avoid unlinking a successor’s lock.
    • Lock scope is per item so unrelated items still sync in parallel; syncGithubCommentsToAnnotations now uses this lock to prevent duplicates. Tests cover live-owner-never-age-broken, token-checked release, recycled-PID handling, partial-lockfile cleanup, breaker election (in-progress and aged-out), and a two-process race test with a start barrier asserting zero duplicate markers.