fd-workspace-mcp 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fd_workspace_mcp-0.1.0/LICENSE +21 -0
- fd_workspace_mcp-0.1.0/PKG-INFO +51 -0
- fd_workspace_mcp-0.1.0/README.md +38 -0
- fd_workspace_mcp-0.1.0/fd_workspace_mcp/__init__.py +6 -0
- fd_workspace_mcp-0.1.0/fd_workspace_mcp/cli.py +74 -0
- fd_workspace_mcp-0.1.0/fd_workspace_mcp/core.py +153 -0
- fd_workspace_mcp-0.1.0/fd_workspace_mcp/server.py +66 -0
- fd_workspace_mcp-0.1.0/fd_workspace_mcp.egg-info/PKG-INFO +51 -0
- fd_workspace_mcp-0.1.0/fd_workspace_mcp.egg-info/SOURCES.txt +14 -0
- fd_workspace_mcp-0.1.0/fd_workspace_mcp.egg-info/dependency_links.txt +1 -0
- fd_workspace_mcp-0.1.0/fd_workspace_mcp.egg-info/entry_points.txt +3 -0
- fd_workspace_mcp-0.1.0/fd_workspace_mcp.egg-info/requires.txt +3 -0
- fd_workspace_mcp-0.1.0/fd_workspace_mcp.egg-info/top_level.txt +1 -0
- fd_workspace_mcp-0.1.0/pyproject.toml +26 -0
- fd_workspace_mcp-0.1.0/setup.cfg +4 -0
- fd_workspace_mcp-0.1.0/tests/test_claims.py +99 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 FindDataTechnology
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fd-workspace-mcp
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Collaborative dev workspace: POSIX-atomic claim/lease + shared fact tree, exposed as CLI and MCP tools.
|
|
5
|
+
Author: FindDataTechnology
|
|
6
|
+
License: MIT
|
|
7
|
+
Requires-Python: >=3.10
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Provides-Extra: mcp
|
|
11
|
+
Requires-Dist: mcp>=1.0; extra == "mcp"
|
|
12
|
+
Dynamic: license-file
|
|
13
|
+
|
|
14
|
+
# fd-workspace-mcp
|
|
15
|
+
|
|
16
|
+
Collaborative dev workspace primitives: a **shared fact tree** on one authoritative disk plus **POSIX-atomic claim/lease** so multiple devices, agents, and sessions can work on one project without stepping on each other.
|
|
17
|
+
|
|
18
|
+
Stdlib-only core. CLI + optional MCP surface. See `openspec/changes/collab-dev-workspace/` for the spec this implements.
|
|
19
|
+
|
|
20
|
+
## Why
|
|
21
|
+
|
|
22
|
+
Storing a claim in a shared DB is heavy; syncing copies across devices loses updates. The reliable primitive is a **local filesystem `mkdir`** on one authoritative host: whoever's `mkdir` wins, period. The claim always executes on the host (locally, or via `ssh <host> fd-workspace ...`), so it stays correct even when participants are remote and network-FS locks can't be trusted.
|
|
23
|
+
|
|
24
|
+
## CLI
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
fd-workspace --root /srv/workspace --owner me claim feature-x # exit 0 acquired / 1 denied
|
|
28
|
+
fd-workspace --root /srv/workspace --owner me renew feature-x # heartbeat
|
|
29
|
+
fd-workspace --root /srv/workspace --owner me release feature-x
|
|
30
|
+
fd-workspace --root /srv/workspace status feature-x # running | stalled | dead | released
|
|
31
|
+
fd-workspace --root /srv/workspace sweep # reclaim expired
|
|
32
|
+
fd-workspace --root /srv/workspace list
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Defaults come from `FD_WORKSPACE_ROOT` / `FD_WORKSPACE_OWNER`. The same logic runs over `ssh`, so a remote agent claims on the host's own disk.
|
|
36
|
+
|
|
37
|
+
## Lease model
|
|
38
|
+
|
|
39
|
+
- Claim = atomic `mkdir <root>/.claims/<unit>/`; heartbeat = a `touch`ed file; TTL lets a dead holder be taken over.
|
|
40
|
+
- Tunables (`FD_WORKSPACE_HEARTBEAT` 30s, `FD_WORKSPACE_LEASE_TTL` 300s, `FD_WORKSPACE_STALL_AFTER` 120s); TTL must exceed heartbeat.
|
|
41
|
+
- States: `running` / `stalled` (no renew, still leased) / `dead` (expired, takeable) / `released`.
|
|
42
|
+
|
|
43
|
+
## MCP
|
|
44
|
+
|
|
45
|
+
`pip install 'fd-workspace-mcp[mcp]'` then `fd-workspace-mcp` (env: `FD_WORKSPACE_ROOT`, `FD_WORKSPACE_OWNER`). Tools: `claim`, `renew`, `release`, `status`, `sweep`. Without the SDK, use the CLI.
|
|
46
|
+
|
|
47
|
+
## Test
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
python3 tests/test_claims.py # 6 self-checks, no pytest needed
|
|
51
|
+
```
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# fd-workspace-mcp
|
|
2
|
+
|
|
3
|
+
Collaborative dev workspace primitives: a **shared fact tree** on one authoritative disk plus **POSIX-atomic claim/lease** so multiple devices, agents, and sessions can work on one project without stepping on each other.
|
|
4
|
+
|
|
5
|
+
Stdlib-only core. CLI + optional MCP surface. See `openspec/changes/collab-dev-workspace/` for the spec this implements.
|
|
6
|
+
|
|
7
|
+
## Why
|
|
8
|
+
|
|
9
|
+
Storing a claim in a shared DB is heavy; syncing copies across devices loses updates. The reliable primitive is a **local filesystem `mkdir`** on one authoritative host: whoever's `mkdir` wins, period. The claim always executes on the host (locally, or via `ssh <host> fd-workspace ...`), so it stays correct even when participants are remote and network-FS locks can't be trusted.
|
|
10
|
+
|
|
11
|
+
## CLI
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
fd-workspace --root /srv/workspace --owner me claim feature-x # exit 0 acquired / 1 denied
|
|
15
|
+
fd-workspace --root /srv/workspace --owner me renew feature-x # heartbeat
|
|
16
|
+
fd-workspace --root /srv/workspace --owner me release feature-x
|
|
17
|
+
fd-workspace --root /srv/workspace status feature-x # running | stalled | dead | released
|
|
18
|
+
fd-workspace --root /srv/workspace sweep # reclaim expired
|
|
19
|
+
fd-workspace --root /srv/workspace list
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Defaults come from `FD_WORKSPACE_ROOT` / `FD_WORKSPACE_OWNER`. The same logic runs over `ssh`, so a remote agent claims on the host's own disk.
|
|
23
|
+
|
|
24
|
+
## Lease model
|
|
25
|
+
|
|
26
|
+
- Claim = atomic `mkdir <root>/.claims/<unit>/`; heartbeat = a `touch`ed file; TTL lets a dead holder be taken over.
|
|
27
|
+
- Tunables (`FD_WORKSPACE_HEARTBEAT` 30s, `FD_WORKSPACE_LEASE_TTL` 300s, `FD_WORKSPACE_STALL_AFTER` 120s); TTL must exceed heartbeat.
|
|
28
|
+
- States: `running` / `stalled` (no renew, still leased) / `dead` (expired, takeable) / `released`.
|
|
29
|
+
|
|
30
|
+
## MCP
|
|
31
|
+
|
|
32
|
+
`pip install 'fd-workspace-mcp[mcp]'` then `fd-workspace-mcp` (env: `FD_WORKSPACE_ROOT`, `FD_WORKSPACE_OWNER`). Tools: `claim`, `renew`, `release`, `status`, `sweep`. Without the SDK, use the CLI.
|
|
33
|
+
|
|
34
|
+
## Test
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
python3 tests/test_claims.py # 6 self-checks, no pytest needed
|
|
38
|
+
```
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"""fd-workspace CLI — the agent-facing surface.
|
|
2
|
+
|
|
3
|
+
Usage (runs locally or via ``ssh <host> fd-workspace ...`` so the claim
|
|
4
|
+
always lands on the host's own filesystem):
|
|
5
|
+
|
|
6
|
+
fd-workspace [--root DIR] [--owner NAME] claim <unit>
|
|
7
|
+
fd-workspace ... renew <unit>
|
|
8
|
+
fd-workspace ... release <unit>
|
|
9
|
+
fd-workspace ... status <unit>
|
|
10
|
+
fd-workspace ... sweep
|
|
11
|
+
fd-workspace ... list
|
|
12
|
+
|
|
13
|
+
Exit codes for ``claim``: 0 = acquired, 1 = denied (already claimed).
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import argparse
|
|
19
|
+
import json
|
|
20
|
+
import os
|
|
21
|
+
import sys
|
|
22
|
+
|
|
23
|
+
from .core import Workspace
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _default_root() -> str:
|
|
27
|
+
return os.environ.get("FD_WORKSPACE_ROOT") or os.getcwd()
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _default_owner() -> str:
|
|
31
|
+
return os.environ.get("FD_WORKSPACE_OWNER") or os.environ.get("USER") or "unknown"
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def main(argv: list[str] | None = None) -> int:
|
|
35
|
+
p = argparse.ArgumentParser(prog="fd-workspace", description="collaborative dev workspace claims")
|
|
36
|
+
p.add_argument("--root", default=_default_root(), help="workspace root (default: $FD_WORKSPACE_ROOT or cwd)")
|
|
37
|
+
p.add_argument("--owner", default=_default_owner(), help="claim holder id (default: $FD_WORKSPACE_OWNER or $USER)")
|
|
38
|
+
sub = p.add_subparsers(dest="cmd", required=True)
|
|
39
|
+
|
|
40
|
+
for name in ("claim", "renew", "release", "status"):
|
|
41
|
+
sp = sub.add_parser(name)
|
|
42
|
+
sp.add_argument("unit")
|
|
43
|
+
sub.add_parser("sweep")
|
|
44
|
+
sub.add_parser("list")
|
|
45
|
+
|
|
46
|
+
args = p.parse_args(argv)
|
|
47
|
+
ws = Workspace(args.root)
|
|
48
|
+
|
|
49
|
+
if args.cmd == "claim":
|
|
50
|
+
r = ws.claim(args.unit, args.owner)
|
|
51
|
+
print(json.dumps({"ok": r.ok, "reason": r.reason, "unit": args.unit, "holder": r.holder}))
|
|
52
|
+
return 0 if r.ok else 1
|
|
53
|
+
if args.cmd == "renew":
|
|
54
|
+
ok = ws.renew(args.unit, args.owner)
|
|
55
|
+
print(json.dumps({"ok": ok, "unit": args.unit}))
|
|
56
|
+
return 0 if ok else 1
|
|
57
|
+
if args.cmd == "release":
|
|
58
|
+
ok = ws.release(args.unit, args.owner)
|
|
59
|
+
print(json.dumps({"ok": ok, "unit": args.unit}))
|
|
60
|
+
return 0 if ok else 1
|
|
61
|
+
if args.cmd == "status":
|
|
62
|
+
print(json.dumps(ws.status(args.unit)))
|
|
63
|
+
return 0
|
|
64
|
+
if args.cmd == "sweep":
|
|
65
|
+
print(json.dumps({"reclaimed": ws.sweep()}))
|
|
66
|
+
return 0
|
|
67
|
+
if args.cmd == "list":
|
|
68
|
+
print(json.dumps(ws.list_claims()))
|
|
69
|
+
return 0
|
|
70
|
+
return 2
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
if __name__ == "__main__":
|
|
74
|
+
sys.exit(main())
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
"""fd-workspace-mcp — collaborative dev workspace claim/lease engine.
|
|
2
|
+
|
|
3
|
+
POSIX-atomic claims on a shared workspace root. The claim is an atomic
|
|
4
|
+
``mkdir`` under ``<root>/.claims/<unit>/``; the lease is a heartbeat file
|
|
5
|
+
refreshed by ``renew``; expiry lets another participant take over.
|
|
6
|
+
|
|
7
|
+
Stdlib only. No network, no DB. Works locally, and the same file logic runs
|
|
8
|
+
when invoked on the host over ``ssh`` (claim always lands on the host's own
|
|
9
|
+
filesystem — see design D1).
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import json
|
|
15
|
+
import os
|
|
16
|
+
import shutil
|
|
17
|
+
import time
|
|
18
|
+
from dataclasses import dataclass
|
|
19
|
+
from pathlib import Path
|
|
20
|
+
|
|
21
|
+
# Lease constants — declared here (not scattered), overridable via env.
|
|
22
|
+
HEARTBEAT_INTERVAL = float(os.environ.get("FD_WORKSPACE_HEARTBEAT", 30))
|
|
23
|
+
LEASE_TTL = float(os.environ.get("FD_WORKSPACE_LEASE_TTL", 300))
|
|
24
|
+
STALL_AFTER = float(os.environ.get("FD_WORKSPACE_STALL_AFTER", 120))
|
|
25
|
+
assert LEASE_TTL > HEARTBEAT_INTERVAL, "lease TTL must exceed heartbeat interval"
|
|
26
|
+
|
|
27
|
+
OWNER_FILE = "owner.json"
|
|
28
|
+
HEARTBEAT_FILE = "heartbeat"
|
|
29
|
+
GRAVEYARD = ".graveyard"
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _safe_unit(unit: str) -> str:
|
|
33
|
+
u = unit.strip().strip("/")
|
|
34
|
+
if not u or "/" in u or u in (".", "..") or u.startswith("."):
|
|
35
|
+
raise ValueError(f"invalid unit: {unit!r}")
|
|
36
|
+
return u
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@dataclass
|
|
40
|
+
class ClaimResult:
|
|
41
|
+
ok: bool
|
|
42
|
+
reason: str
|
|
43
|
+
holder: dict | None = None
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class Workspace:
|
|
47
|
+
def __init__(self, root: str | Path):
|
|
48
|
+
self.root = Path(root).resolve()
|
|
49
|
+
self.claims = self.root / ".claims"
|
|
50
|
+
|
|
51
|
+
# --- internal helpers -------------------------------------------------
|
|
52
|
+
def _unit_dir(self, unit: str) -> Path:
|
|
53
|
+
return self.claims / _safe_unit(unit)
|
|
54
|
+
|
|
55
|
+
def _age(self, unit: str) -> float:
|
|
56
|
+
hb = self._unit_dir(unit) / HEARTBEAT_FILE
|
|
57
|
+
try:
|
|
58
|
+
return time.time() - hb.stat().st_mtime
|
|
59
|
+
except FileNotFoundError:
|
|
60
|
+
return float("inf")
|
|
61
|
+
|
|
62
|
+
def _read_owner(self, unit: str) -> dict | None:
|
|
63
|
+
try:
|
|
64
|
+
return json.loads((self._unit_dir(unit) / OWNER_FILE).read_text())
|
|
65
|
+
except (FileNotFoundError, json.JSONDecodeError):
|
|
66
|
+
return None
|
|
67
|
+
|
|
68
|
+
def _write_owner(self, unit: str, owner: str) -> None:
|
|
69
|
+
d = self._unit_dir(unit)
|
|
70
|
+
(d / OWNER_FILE).write_text(
|
|
71
|
+
json.dumps({"owner": owner, "since": time.time()}), encoding="utf-8"
|
|
72
|
+
)
|
|
73
|
+
(d / HEARTBEAT_FILE).write_text("", encoding="utf-8")
|
|
74
|
+
|
|
75
|
+
def _steal(self, unit: str) -> None:
|
|
76
|
+
"""Atomically move an expired claim aside so we can mkdir fresh.
|
|
77
|
+
|
|
78
|
+
``rename`` is atomic; losers of the race fall through to ``mkdir``,
|
|
79
|
+
which is the single authoritative arbitration point.
|
|
80
|
+
"""
|
|
81
|
+
grave = self.claims / GRAVEYARD
|
|
82
|
+
grave.mkdir(parents=True, exist_ok=True)
|
|
83
|
+
try:
|
|
84
|
+
os.rename(self._unit_dir(unit), grave / f"{unit}.{time.time_ns()}")
|
|
85
|
+
except FileNotFoundError:
|
|
86
|
+
pass # someone else already stole it; mkdir will settle it
|
|
87
|
+
|
|
88
|
+
# --- public API -------------------------------------------------------
|
|
89
|
+
def claim(self, unit: str, owner: str) -> ClaimResult:
|
|
90
|
+
self.claims.mkdir(parents=True, exist_ok=True)
|
|
91
|
+
d = self._unit_dir(unit)
|
|
92
|
+
try:
|
|
93
|
+
d.mkdir() # atomic: exactly one creator wins
|
|
94
|
+
except FileExistsError:
|
|
95
|
+
if self._age(unit) > LEASE_TTL:
|
|
96
|
+
self._steal(unit)
|
|
97
|
+
try:
|
|
98
|
+
d.mkdir()
|
|
99
|
+
except FileExistsError:
|
|
100
|
+
return ClaimResult(False, "claimed", self._read_owner(unit))
|
|
101
|
+
else:
|
|
102
|
+
return ClaimResult(False, "claimed", self._read_owner(unit))
|
|
103
|
+
self._write_owner(unit, owner)
|
|
104
|
+
return ClaimResult(True, "acquired", {"owner": owner})
|
|
105
|
+
|
|
106
|
+
def renew(self, unit: str, owner: str) -> bool:
|
|
107
|
+
cur = self._read_owner(unit)
|
|
108
|
+
if not cur or cur.get("owner") != owner:
|
|
109
|
+
return False
|
|
110
|
+
(self._unit_dir(unit) / HEARTBEAT_FILE).write_text("", encoding="utf-8")
|
|
111
|
+
return True
|
|
112
|
+
|
|
113
|
+
def release(self, unit: str, owner: str) -> bool:
|
|
114
|
+
cur = self._read_owner(unit)
|
|
115
|
+
if not cur or cur.get("owner") != owner:
|
|
116
|
+
return False
|
|
117
|
+
shutil.rmtree(self._unit_dir(unit), ignore_errors=True)
|
|
118
|
+
return True
|
|
119
|
+
|
|
120
|
+
def status(self, unit: str) -> dict:
|
|
121
|
+
d = self._unit_dir(unit)
|
|
122
|
+
if not d.exists():
|
|
123
|
+
return {"unit": unit, "state": "released"}
|
|
124
|
+
age = self._age(unit)
|
|
125
|
+
if age > LEASE_TTL:
|
|
126
|
+
state = "dead"
|
|
127
|
+
elif age > STALL_AFTER:
|
|
128
|
+
state = "stalled"
|
|
129
|
+
else:
|
|
130
|
+
state = "running"
|
|
131
|
+
return {"unit": unit, "state": state, "age": round(age, 1), "holder": self._read_owner(unit)}
|
|
132
|
+
|
|
133
|
+
def sweep(self) -> list[str]:
|
|
134
|
+
if not self.claims.exists():
|
|
135
|
+
return []
|
|
136
|
+
reclaimed = []
|
|
137
|
+
for d in self.claims.iterdir():
|
|
138
|
+
if not d.is_dir() or d.name == GRAVEYARD:
|
|
139
|
+
continue
|
|
140
|
+
unit = d.name
|
|
141
|
+
if self._age(unit) > LEASE_TTL:
|
|
142
|
+
self._steal(unit)
|
|
143
|
+
reclaimed.append(unit)
|
|
144
|
+
return reclaimed
|
|
145
|
+
|
|
146
|
+
def list_claims(self) -> list[dict]:
|
|
147
|
+
if not self.claims.exists():
|
|
148
|
+
return []
|
|
149
|
+
return [
|
|
150
|
+
self.status(d.name)
|
|
151
|
+
for d in sorted(self.claims.iterdir())
|
|
152
|
+
if d.is_dir() and d.name != GRAVEYARD
|
|
153
|
+
]
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"""Optional MCP surface for fd-workspace.
|
|
2
|
+
|
|
3
|
+
Exposes the same claim/lease operations as MCP tools for harnesses that
|
|
4
|
+
speak MCP. Import-guarded: the stdlib core and CLI work without the MCP SDK
|
|
5
|
+
installed. Configure via env: FD_WORKSPACE_ROOT, FD_WORKSPACE_OWNER.
|
|
6
|
+
|
|
7
|
+
Run: fd-workspace-mcp (or: python -m fd_workspace_mcp.server)
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import os
|
|
13
|
+
|
|
14
|
+
from .core import Workspace
|
|
15
|
+
|
|
16
|
+
ROOT = os.environ.get("FD_WORKSPACE_ROOT") or os.getcwd()
|
|
17
|
+
OWNER = os.environ.get("FD_WORKSPACE_OWNER") or os.environ.get("USER") or "unknown"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _ws() -> Workspace:
|
|
21
|
+
return Workspace(ROOT)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def claim(unit: str) -> dict:
|
|
25
|
+
r = _ws().claim(unit, OWNER)
|
|
26
|
+
return {"ok": r.ok, "reason": r.reason, "unit": unit, "holder": r.holder}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def renew(unit: str) -> dict:
|
|
30
|
+
return {"ok": _ws().renew(unit, OWNER), "unit": unit}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def release(unit: str) -> dict:
|
|
34
|
+
return {"ok": _ws().release(unit, OWNER), "unit": unit}
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def status(unit: str) -> dict:
|
|
38
|
+
return _ws().status(unit)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def sweep() -> dict:
|
|
42
|
+
return {"reclaimed": _ws().sweep()}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def build():
|
|
46
|
+
"""Return a FastMCP server with the workspace tools registered."""
|
|
47
|
+
try:
|
|
48
|
+
from mcp.server.fastmcp import FastMCP
|
|
49
|
+
except ImportError as e: # pragma: no cover - depends on env
|
|
50
|
+
raise SystemExit(
|
|
51
|
+
"MCP SDK not installed. Use the CLI (`fd-workspace ...`) instead, "
|
|
52
|
+
"or `pip install mcp`."
|
|
53
|
+
) from e
|
|
54
|
+
|
|
55
|
+
mcp = FastMCP("fd-workspace")
|
|
56
|
+
for fn in (claim, renew, release, status, sweep):
|
|
57
|
+
mcp.tool()(fn)
|
|
58
|
+
return mcp
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def main() -> None: # pragma: no cover
|
|
62
|
+
build().run()
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
if __name__ == "__main__": # pragma: no cover
|
|
66
|
+
main()
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fd-workspace-mcp
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Collaborative dev workspace: POSIX-atomic claim/lease + shared fact tree, exposed as CLI and MCP tools.
|
|
5
|
+
Author: FindDataTechnology
|
|
6
|
+
License: MIT
|
|
7
|
+
Requires-Python: >=3.10
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Provides-Extra: mcp
|
|
11
|
+
Requires-Dist: mcp>=1.0; extra == "mcp"
|
|
12
|
+
Dynamic: license-file
|
|
13
|
+
|
|
14
|
+
# fd-workspace-mcp
|
|
15
|
+
|
|
16
|
+
Collaborative dev workspace primitives: a **shared fact tree** on one authoritative disk plus **POSIX-atomic claim/lease** so multiple devices, agents, and sessions can work on one project without stepping on each other.
|
|
17
|
+
|
|
18
|
+
Stdlib-only core. CLI + optional MCP surface. See `openspec/changes/collab-dev-workspace/` for the spec this implements.
|
|
19
|
+
|
|
20
|
+
## Why
|
|
21
|
+
|
|
22
|
+
Storing a claim in a shared DB is heavy; syncing copies across devices loses updates. The reliable primitive is a **local filesystem `mkdir`** on one authoritative host: whoever's `mkdir` wins, period. The claim always executes on the host (locally, or via `ssh <host> fd-workspace ...`), so it stays correct even when participants are remote and network-FS locks can't be trusted.
|
|
23
|
+
|
|
24
|
+
## CLI
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
fd-workspace --root /srv/workspace --owner me claim feature-x # exit 0 acquired / 1 denied
|
|
28
|
+
fd-workspace --root /srv/workspace --owner me renew feature-x # heartbeat
|
|
29
|
+
fd-workspace --root /srv/workspace --owner me release feature-x
|
|
30
|
+
fd-workspace --root /srv/workspace status feature-x # running | stalled | dead | released
|
|
31
|
+
fd-workspace --root /srv/workspace sweep # reclaim expired
|
|
32
|
+
fd-workspace --root /srv/workspace list
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Defaults come from `FD_WORKSPACE_ROOT` / `FD_WORKSPACE_OWNER`. The same logic runs over `ssh`, so a remote agent claims on the host's own disk.
|
|
36
|
+
|
|
37
|
+
## Lease model
|
|
38
|
+
|
|
39
|
+
- Claim = atomic `mkdir <root>/.claims/<unit>/`; heartbeat = a `touch`ed file; TTL lets a dead holder be taken over.
|
|
40
|
+
- Tunables (`FD_WORKSPACE_HEARTBEAT` 30s, `FD_WORKSPACE_LEASE_TTL` 300s, `FD_WORKSPACE_STALL_AFTER` 120s); TTL must exceed heartbeat.
|
|
41
|
+
- States: `running` / `stalled` (no renew, still leased) / `dead` (expired, takeable) / `released`.
|
|
42
|
+
|
|
43
|
+
## MCP
|
|
44
|
+
|
|
45
|
+
`pip install 'fd-workspace-mcp[mcp]'` then `fd-workspace-mcp` (env: `FD_WORKSPACE_ROOT`, `FD_WORKSPACE_OWNER`). Tools: `claim`, `renew`, `release`, `status`, `sweep`. Without the SDK, use the CLI.
|
|
46
|
+
|
|
47
|
+
## Test
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
python3 tests/test_claims.py # 6 self-checks, no pytest needed
|
|
51
|
+
```
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
fd_workspace_mcp/__init__.py
|
|
5
|
+
fd_workspace_mcp/cli.py
|
|
6
|
+
fd_workspace_mcp/core.py
|
|
7
|
+
fd_workspace_mcp/server.py
|
|
8
|
+
fd_workspace_mcp.egg-info/PKG-INFO
|
|
9
|
+
fd_workspace_mcp.egg-info/SOURCES.txt
|
|
10
|
+
fd_workspace_mcp.egg-info/dependency_links.txt
|
|
11
|
+
fd_workspace_mcp.egg-info/entry_points.txt
|
|
12
|
+
fd_workspace_mcp.egg-info/requires.txt
|
|
13
|
+
fd_workspace_mcp.egg-info/top_level.txt
|
|
14
|
+
tests/test_claims.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
fd_workspace_mcp
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "fd-workspace-mcp"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = "Collaborative dev workspace: POSIX-atomic claim/lease + shared fact tree, exposed as CLI and MCP tools."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [{ name = "FindDataTechnology" }]
|
|
13
|
+
dependencies = [] # stdlib-only core; MCP SDK optional (`pip install mcp`)
|
|
14
|
+
|
|
15
|
+
[project.optional-dependencies]
|
|
16
|
+
mcp = ["mcp>=1.0"]
|
|
17
|
+
|
|
18
|
+
[project.scripts]
|
|
19
|
+
fd-workspace = "fd_workspace_mcp.cli:main"
|
|
20
|
+
fd-workspace-mcp = "fd_workspace_mcp.server:main"
|
|
21
|
+
|
|
22
|
+
[tool.setuptools]
|
|
23
|
+
packages = ["fd_workspace_mcp"]
|
|
24
|
+
|
|
25
|
+
[tool.setuptools.dynamic]
|
|
26
|
+
version = { attr = "fd_workspace_mcp.__version__" }
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"""Self-check for the claim/lease engine. No pytest needed:
|
|
2
|
+
|
|
3
|
+
python3 tests/test_claims.py
|
|
4
|
+
|
|
5
|
+
Covers the spec scenarios: concurrent claim (one wins), release frees,
|
|
6
|
+
expiry allows takeover, isolation by root, and tri-state status. Fails loudly
|
|
7
|
+
with a non-zero exit if any assertion breaks.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
import os
|
|
11
|
+
import sys
|
|
12
|
+
import time
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
import tempfile
|
|
15
|
+
|
|
16
|
+
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
|
17
|
+
|
|
18
|
+
from fd_workspace_mcp.core import LEASE_TTL, STALL_AFTER, Workspace # noqa: E402
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _age_out(ws: Workspace, unit: str, seconds_old: float) -> None:
|
|
22
|
+
p = ws.claims / unit / "heartbeat"
|
|
23
|
+
t = time.time() - seconds_old
|
|
24
|
+
os.utime(p, (t, t))
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def test_concurrent_claim_one_wins():
|
|
28
|
+
with tempfile.TemporaryDirectory() as root:
|
|
29
|
+
ws = Workspace(root)
|
|
30
|
+
a = ws.claim("change-1", "agentA")
|
|
31
|
+
b = ws.claim("change-1", "agentB")
|
|
32
|
+
assert a.ok is True, a
|
|
33
|
+
assert b.ok is False and b.reason == "claimed", b
|
|
34
|
+
assert b.holder["owner"] == "agentA"
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def test_release_frees():
|
|
38
|
+
with tempfile.TemporaryDirectory() as root:
|
|
39
|
+
ws = Workspace(root)
|
|
40
|
+
assert ws.claim("u", "A").ok
|
|
41
|
+
assert ws.release("u", "A") is True
|
|
42
|
+
assert ws.status("u")["state"] == "released"
|
|
43
|
+
assert ws.claim("u", "B").ok is True
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def test_expiry_allows_takeover():
|
|
47
|
+
with tempfile.TemporaryDirectory() as root:
|
|
48
|
+
ws = Workspace(root)
|
|
49
|
+
assert ws.claim("u", "A").ok
|
|
50
|
+
_age_out(ws, "u", LEASE_TTL + 1)
|
|
51
|
+
assert ws.status("u")["state"] == "dead"
|
|
52
|
+
r = ws.claim("u", "B")
|
|
53
|
+
assert r.ok is True, r
|
|
54
|
+
assert ws.status("u")["holder"]["owner"] == "B"
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def test_stalled_state():
|
|
58
|
+
with tempfile.TemporaryDirectory() as root:
|
|
59
|
+
ws = Workspace(root)
|
|
60
|
+
assert ws.claim("u", "A").ok
|
|
61
|
+
_age_out(ws, "u", STALL_AFTER + 1)
|
|
62
|
+
assert ws.status("u")["state"] == "stalled"
|
|
63
|
+
# still within TTL: another owner must be denied
|
|
64
|
+
assert ws.claim("u", "B").ok is False
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def test_isolation_by_root():
|
|
68
|
+
with tempfile.TemporaryDirectory() as r1, tempfile.TemporaryDirectory() as r2:
|
|
69
|
+
w1, w2 = Workspace(r1), Workspace(r2)
|
|
70
|
+
assert w1.claim("u", "A").ok
|
|
71
|
+
assert w2.status("u")["state"] == "released" # cannot see other root's claim
|
|
72
|
+
assert w2.claim("u", "B").ok is True
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def test_sweep_reclaims_expired():
|
|
76
|
+
with tempfile.TemporaryDirectory() as root:
|
|
77
|
+
ws = Workspace(root)
|
|
78
|
+
assert ws.claim("u", "A").ok
|
|
79
|
+
_age_out(ws, "u", LEASE_TTL + 5)
|
|
80
|
+
assert ws.sweep() == ["u"]
|
|
81
|
+
assert ws.status("u")["state"] == "released"
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def main() -> int:
|
|
85
|
+
tests = [v for k, v in sorted(globals().items()) if k.startswith("test_")]
|
|
86
|
+
failed = 0
|
|
87
|
+
for t in tests:
|
|
88
|
+
try:
|
|
89
|
+
t()
|
|
90
|
+
print(f"PASS {t.__name__}")
|
|
91
|
+
except AssertionError as e:
|
|
92
|
+
failed += 1
|
|
93
|
+
print(f"FAIL {t.__name__}: {e}")
|
|
94
|
+
print(f"\n{len(tests) - failed}/{len(tests)} passed")
|
|
95
|
+
return 1 if failed else 0
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
if __name__ == "__main__":
|
|
99
|
+
sys.exit(main())
|