|
| 1 | +// SPDX-License-Identifier: MPL-2.0 |
| 2 | +// Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) <j.d.a.jewell@open.ac.uk> |
| 3 | +// |
| 4 | +// Advisory path-claims for local-coord-mcp. |
| 5 | +// |
| 6 | +// The Zig/Idris backend enforces a task-id mutex; this module layers an |
| 7 | +// in-bridge advisory map of `task -> {holder, paths, expires_at}` so |
| 8 | +// `coord_claim_task` can return `path_overlap` hints when two active |
| 9 | +// claims declare overlapping working-tree paths. Advisory by design: |
| 10 | +// the backend is the source of truth for ownership; this module never |
| 11 | +// rejects a claim, it only annotates the response. |
| 12 | + |
| 13 | +const _claims = new Map(); // task -> { holder, paths, expires_at_ms } |
| 14 | + |
| 15 | +function normalize(p) { |
| 16 | + if (typeof p !== "string") return null; |
| 17 | + let s = p.trim(); |
| 18 | + if (!s) return null; |
| 19 | + s = s.replace(/\\/g, "/"); |
| 20 | + while (s.includes("//")) s = s.replace(/\/\//g, "/"); |
| 21 | + if (s.endsWith("/") && s.length > 1) s = s.slice(0, -1); |
| 22 | + if (s.startsWith("./")) s = s.slice(2); |
| 23 | + return s; |
| 24 | +} |
| 25 | + |
| 26 | +function segments(p) { |
| 27 | + return p.split("/").filter((s) => s !== "" && s !== "."); |
| 28 | +} |
| 29 | + |
| 30 | +// Two paths overlap when one is a segment-prefix of the other (or equal). |
| 31 | +// `src/a` overlaps `src/a/b` and `src/a`, but NOT `src/abc`. |
| 32 | +export function pathsOverlap(a, b) { |
| 33 | + const A = segments(a); |
| 34 | + const B = segments(b); |
| 35 | + const n = Math.min(A.length, B.length); |
| 36 | + for (let i = 0; i < n; i++) if (A[i] !== B[i]) return false; |
| 37 | + return true; |
| 38 | +} |
| 39 | + |
| 40 | +function sweep(nowMs = Date.now()) { |
| 41 | + for (const [task, entry] of _claims) { |
| 42 | + if (entry.expires_at_ms && entry.expires_at_ms <= nowMs) _claims.delete(task); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Register an advisory path-claim and return overlap warnings with |
| 48 | + * other *active* claims (excluding the same task by the same holder). |
| 49 | + * |
| 50 | + * @param {object} args |
| 51 | + * @param {string} args.task — task identifier (matches backend) |
| 52 | + * @param {string} args.holder — peer-id from backend response (or "?") |
| 53 | + * @param {string[]} args.paths — working-tree paths claimed |
| 54 | + * @param {number} [args.ttl_s] — bridge-side TTL hint from backend |
| 55 | + * @returns {{paths: string[], overlaps: Array<{task,holder,paths:string[],with:string[]}>}} |
| 56 | + */ |
| 57 | +export function register({ task, holder, paths, ttl_s }) { |
| 58 | + sweep(); |
| 59 | + const norm = Array.isArray(paths) |
| 60 | + ? paths.map(normalize).filter(Boolean) |
| 61 | + : []; |
| 62 | + const overlaps = []; |
| 63 | + for (const [otherTask, other] of _claims) { |
| 64 | + if (otherTask === task && other.holder === holder) continue; |
| 65 | + const hits = []; |
| 66 | + for (const a of norm) { |
| 67 | + for (const b of other.paths) { |
| 68 | + if (pathsOverlap(a, b)) hits.push(a); |
| 69 | + } |
| 70 | + } |
| 71 | + if (hits.length) { |
| 72 | + overlaps.push({ |
| 73 | + task: otherTask, |
| 74 | + holder: other.holder, |
| 75 | + paths: other.paths.slice(), |
| 76 | + with: Array.from(new Set(hits)), |
| 77 | + }); |
| 78 | + } |
| 79 | + } |
| 80 | + const ttl = typeof ttl_s === "number" && ttl_s > 0 ? ttl_s : 300; |
| 81 | + _claims.set(task, { |
| 82 | + holder, |
| 83 | + paths: norm, |
| 84 | + expires_at_ms: Date.now() + ttl * 1000, |
| 85 | + }); |
| 86 | + return { paths: norm, overlaps }; |
| 87 | +} |
| 88 | + |
| 89 | +/** Refresh the TTL for a task's path-claim (called by coord_progress). */ |
| 90 | +export function refresh(task, ttl_s) { |
| 91 | + const entry = _claims.get(task); |
| 92 | + if (!entry) return false; |
| 93 | + const ttl = typeof ttl_s === "number" && ttl_s > 0 ? ttl_s : 300; |
| 94 | + entry.expires_at_ms = Date.now() + ttl * 1000; |
| 95 | + return true; |
| 96 | +} |
| 97 | + |
| 98 | +/** Release a task's path-claim (called by coord_report_outcome). */ |
| 99 | +export function release(task) { |
| 100 | + return _claims.delete(task); |
| 101 | +} |
| 102 | + |
| 103 | +/** List active path-claims (for tests/observability). */ |
| 104 | +export function list() { |
| 105 | + sweep(); |
| 106 | + return Array.from(_claims.entries()).map(([task, e]) => ({ |
| 107 | + task, |
| 108 | + holder: e.holder, |
| 109 | + paths: e.paths.slice(), |
| 110 | + expires_at_ms: e.expires_at_ms, |
| 111 | + })); |
| 112 | +} |
| 113 | + |
| 114 | +/** Test-only: wipe state. */ |
| 115 | +export function _reset() { |
| 116 | + _claims.clear(); |
| 117 | +} |
0 commit comments