fix(import): serialize comment marker-dedupe across concurrent imports
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()— atomicfs.openSync(path, "wx")(O_EXCL) lockfile under the workspace runtime dir, JSON payload (pid, timestamp) shaped like the CLI's own locks sopm gccan 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.
syncGithubCommentsToAnnotationsdocstring updated: the limitation is lifted.
Tests
- 10 new test cases in
test/import-lock.test.ts, including a two-process parallel-import race test (viatest/helpers/comment-sync-child.mjs) asserting zero duplicate markers. - Full suite: 122/122 green; build +
changelog:checkpass.
pm item
- pm-github-503u — Serialize marker-dedupe read-and-append across concurrent imports (closed in this PR)
🤖 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/viafs.openSync("wx"); payload mirrors thepmCLI 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>.breaksidecar 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;
syncGithubCommentsToAnnotationsnow 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.
- Added a cross-process per-item lockfile in workspace