switchman-dev 0.1.0

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.
Files changed (50) hide show
  1. package/CLAUDE.md +98 -0
  2. package/README.md +243 -0
  3. package/examples/README.md +117 -0
  4. package/examples/setup.sh +102 -0
  5. package/examples/taskapi/.switchman/switchman.db +0 -0
  6. package/examples/taskapi/package-lock.json +4736 -0
  7. package/examples/taskapi/package.json +18 -0
  8. package/examples/taskapi/src/db.js +179 -0
  9. package/examples/taskapi/src/middleware/auth.js +96 -0
  10. package/examples/taskapi/src/middleware/validate.js +133 -0
  11. package/examples/taskapi/src/routes/tasks.js +65 -0
  12. package/examples/taskapi/src/routes/users.js +38 -0
  13. package/examples/taskapi/src/server.js +7 -0
  14. package/examples/taskapi/tests/api.test.js +112 -0
  15. package/examples/teardown.sh +37 -0
  16. package/examples/walkthrough.sh +172 -0
  17. package/examples/worktrees/agent-rate-limiting/package-lock.json +4736 -0
  18. package/examples/worktrees/agent-rate-limiting/package.json +18 -0
  19. package/examples/worktrees/agent-rate-limiting/src/db.js +179 -0
  20. package/examples/worktrees/agent-rate-limiting/src/middleware/auth.js +96 -0
  21. package/examples/worktrees/agent-rate-limiting/src/middleware/validate.js +133 -0
  22. package/examples/worktrees/agent-rate-limiting/src/routes/tasks.js +65 -0
  23. package/examples/worktrees/agent-rate-limiting/src/routes/users.js +38 -0
  24. package/examples/worktrees/agent-rate-limiting/src/server.js +7 -0
  25. package/examples/worktrees/agent-rate-limiting/tests/api.test.js +112 -0
  26. package/examples/worktrees/agent-tests/package-lock.json +4736 -0
  27. package/examples/worktrees/agent-tests/package.json +18 -0
  28. package/examples/worktrees/agent-tests/src/db.js +179 -0
  29. package/examples/worktrees/agent-tests/src/middleware/auth.js +96 -0
  30. package/examples/worktrees/agent-tests/src/middleware/validate.js +133 -0
  31. package/examples/worktrees/agent-tests/src/routes/tasks.js +65 -0
  32. package/examples/worktrees/agent-tests/src/routes/users.js +38 -0
  33. package/examples/worktrees/agent-tests/src/server.js +7 -0
  34. package/examples/worktrees/agent-tests/tests/api.test.js +112 -0
  35. package/examples/worktrees/agent-validation/package-lock.json +4736 -0
  36. package/examples/worktrees/agent-validation/package.json +18 -0
  37. package/examples/worktrees/agent-validation/src/db.js +179 -0
  38. package/examples/worktrees/agent-validation/src/middleware/auth.js +96 -0
  39. package/examples/worktrees/agent-validation/src/middleware/validate.js +133 -0
  40. package/examples/worktrees/agent-validation/src/routes/tasks.js +65 -0
  41. package/examples/worktrees/agent-validation/src/routes/users.js +38 -0
  42. package/examples/worktrees/agent-validation/src/server.js +7 -0
  43. package/examples/worktrees/agent-validation/tests/api.test.js +112 -0
  44. package/package.json +29 -0
  45. package/src/cli/index.js +602 -0
  46. package/src/core/db.js +240 -0
  47. package/src/core/detector.js +172 -0
  48. package/src/core/git.js +265 -0
  49. package/src/mcp/server.js +555 -0
  50. package/tests/test.js +259 -0
package/CLAUDE.md ADDED
@@ -0,0 +1,98 @@
1
+ # Switchman Agent Instructions
2
+
3
+ This repository uses **Switchman** to coordinate parallel AI coding agents.
4
+ You MUST follow these instructions every session to avoid conflicting with other agents.
5
+
6
+ ---
7
+
8
+ ## Your worktree
9
+
10
+ Find your worktree name by running:
11
+ ```bash
12
+ git worktree list
13
+ ```
14
+ The path that matches your current directory is your worktree. Use the last path segment as your worktree name (e.g. `/projects/myapp-feature-auth` → `feature-auth`). The main repo root is always named `main`.
15
+
16
+ ---
17
+
18
+ ## Required workflow — follow this every session
19
+
20
+ ### 1. Start of session — get your task
21
+
22
+ Call the `switchman_task_next` MCP tool with your worktree name:
23
+ ```
24
+ switchman_task_next({ worktree: "<your-worktree-name>", agent: "claude-code" })
25
+ ```
26
+
27
+ - If `task` is `null` — the queue is empty. Ask the user what to work on, or stop.
28
+ - If you receive a task — note the `task.id`. You'll need it in the next steps.
29
+
30
+ ### 2. Before editing any files — claim them
31
+
32
+ Call `switchman_task_claim` with every file you plan to edit **before you edit them**:
33
+ ```
34
+ switchman_task_claim({
35
+ task_id: "<task-id>",
36
+ worktree: "<your-worktree-name>",
37
+ files: ["src/auth/login.js", "tests/auth.test.js"]
38
+ })
39
+ ```
40
+
41
+ - If `safe_to_proceed` is `false` — there are conflicts. Do NOT edit those files.
42
+ Read the `conflicts` array to see which worktrees own them, then either:
43
+ - Choose different files that accomplish the same goal
44
+ - Ask the user how to proceed
45
+
46
+ - If `safe_to_proceed` is `true` — you are clear to edit.
47
+
48
+ ### 3. Do the work
49
+
50
+ Implement the task. Make commits as normal. Other agents will avoid your claimed files.
51
+
52
+ If you discover mid-task that you need to edit additional files, call `switchman_task_claim` again for those files before editing them.
53
+
54
+ ### 4. End of session — mark complete or failed
55
+
56
+ **On success:**
57
+ ```
58
+ switchman_task_done({ task_id: "<task-id>", release_files: true })
59
+ ```
60
+
61
+ **On failure (can't complete the task):**
62
+ ```
63
+ switchman_task_fail({ task_id: "<task-id>", reason: "Brief explanation of what blocked you" })
64
+ ```
65
+
66
+ Always call one of these before ending your session. Released file claims allow other agents to proceed.
67
+
68
+ ---
69
+
70
+ ## Checking for conflicts
71
+
72
+ At any time you can scan for conflicts across all worktrees:
73
+ ```
74
+ switchman_scan()
75
+ ```
76
+
77
+ Run this before merging your branch. If `safe_to_proceed` is `false`, do not merge until conflicts are resolved.
78
+
79
+ ---
80
+
81
+ ## Checking system state
82
+
83
+ To see what other agents are doing:
84
+ ```
85
+ switchman_status()
86
+ ```
87
+
88
+ This shows all pending and in-progress tasks, file claims per worktree, and worktree list.
89
+
90
+ ---
91
+
92
+ ## Rules
93
+
94
+ 1. **Always claim files before editing them** — not after.
95
+ 2. **Always call `switchman_task_done` or `switchman_task_fail` at end of session** — never leave tasks as `in_progress` when you stop.
96
+ 3. **If `safe_to_proceed` is false, do not edit the conflicting files** — coordinate first.
97
+ 4. **Do not claim files you don't need** — over-claiming blocks other agents unnecessarily.
98
+ 5. **One task per session** — complete or fail your current task before taking another.
package/README.md ADDED
@@ -0,0 +1,243 @@
1
+ # Switchman
2
+
3
+ **Stop your AI agents from overwriting each other.**
4
+
5
+ When you run multiple Claude Code instances on the same repo, they have no idea what each other is doing. One agent edits a file while another rewrites it. Hours of work disappear at merge time.
6
+
7
+ Switchman gives them a shared task queue and file locking so they stay on separate tracks — and you stay in control.
8
+
9
+ ---
10
+
11
+ ## Requirements
12
+
13
+ - Node.js 22.5+
14
+ - Git 2.5+
15
+
16
+ ---
17
+
18
+ ## Install
19
+
20
+ ```bash
21
+ npm install -g @switchman-dev
22
+ ```
23
+
24
+ ---
25
+
26
+ ## Pick your setup
27
+
28
+ ### Option A — Claude Code (recommended)
29
+
30
+ Claude Code has a native Switchman integration via MCP. Your agents coordinate automatically — no manual CLI calls, no extra prompting.
31
+
32
+ **Step 1 — Create your agent workspaces**
33
+
34
+ ```bash
35
+ cd my-project
36
+ switchman setup --agents 3
37
+ ```
38
+
39
+ That's it. Switchman creates three isolated workspaces, one per agent, and initialises the database. You'll see the folder paths printed — you'll need them in step 4.
40
+
41
+ **Step 2 — Add Switchman to Claude Code**
42
+
43
+ Add this to `~/.claude/claude_desktop_config.json`:
44
+
45
+ ```json
46
+ {
47
+ "mcpServers": {
48
+ "switchman": {
49
+ "command": "switchman-mcp",
50
+ "args": []
51
+ }
52
+ }
53
+ }
54
+ ```
55
+
56
+ Then restart Claude Code.
57
+
58
+ **Step 3 — Copy CLAUDE.md into your repo root**
59
+
60
+ ```bash
61
+ curl -O https://raw.githubusercontent.com/switchman-dev/switchman/main/CLAUDE.md
62
+ ```
63
+
64
+ This tells your agents how to use Switchman. Without it they won't know to coordinate.
65
+
66
+ **Step 4 — Add your tasks**
67
+
68
+ ```bash
69
+ switchman task add "Fix the login bug" --priority 8
70
+ switchman task add "Add rate limiting" --priority 6
71
+ switchman task add "Update README" --priority 2
72
+ ```
73
+
74
+ **Step 5 — Open Claude Code in each workspace**
75
+
76
+ Open a separate Claude Code window in each folder that `switchman setup` created. Each agent will automatically pick up a task, lock the files it needs, and release them when it's done.
77
+
78
+ **Step 6 — Check before merging**
79
+
80
+ ```bash
81
+ switchman scan
82
+ ```
83
+
84
+ ---
85
+
86
+ ### Option B — Any other agent (Cursor, Windsurf, Aider, etc.)
87
+
88
+ Switchman works as a CLI tool with any agent that can run terminal commands. The coordination isn't automatic — you'll need to prompt your agents to use Switchman commands.
89
+
90
+ **Step 1 — Create your agent workspaces**
91
+
92
+ ```bash
93
+ cd my-project
94
+ switchman setup --agents 3
95
+ ```
96
+
97
+ **Step 2 — Add your tasks**
98
+
99
+ ```bash
100
+ switchman task add "Fix the login bug" --priority 8
101
+ switchman task add "Add rate limiting" --priority 6
102
+ ```
103
+
104
+ **Step 3 — Give each agent this prompt**
105
+
106
+ Paste this into your agent at the start of each session:
107
+
108
+ ```
109
+ Before starting any work:
110
+ 1. Run `switchman task next --json` to get your assigned task
111
+ 2. Run `switchman claim <taskId> <worktreeName> <files...>` to lock the files you'll edit
112
+ - If a file is already claimed, pick a different approach or different files
113
+ 3. When finished, run `switchman task done <taskId>`
114
+
115
+ Never edit a file you haven't claimed. If a claim fails, do not use --force.
116
+ ```
117
+
118
+ **Step 4 — Check before merging**
119
+
120
+ ```bash
121
+ switchman scan
122
+ ```
123
+
124
+ ---
125
+
126
+ ## What your agents will see
127
+
128
+ Here's what a normal session looks like with Switchman running:
129
+
130
+ ```
131
+ # Agent 1 picks up a task
132
+ switchman task next
133
+ ✓ Assigned: "Add rate limiting to all routes" [task-abc-123]
134
+
135
+ # Agent 1 locks its files
136
+ switchman claim task-abc-123 agent1 src/middleware/auth.js src/server.js
137
+ ✓ 2 files locked — no conflicts
138
+
139
+ # Agent 2 picks up a different task
140
+ switchman task next
141
+ ✓ Assigned: "Add validation to POST /tasks" [task-def-456]
142
+
143
+ # Agent 2 tries to claim a file already locked by Agent 1
144
+ switchman claim task-def-456 agent2 src/middleware/auth.js
145
+ ⚠ Conflict: auth.js is locked by agent1
146
+
147
+ # Agent 2 claims different files instead
148
+ switchman claim task-def-456 agent2 src/middleware/validate.js src/routes/tasks.js
149
+ ✓ 2 files locked — no conflicts
150
+
151
+ # Both agents working, zero collisions
152
+ switchman status
153
+ agent1 → "Add rate limiting" editing auth.js, server.js
154
+ agent2 → "Add validation" editing validate.js, tasks.js
155
+ ```
156
+
157
+ ---
158
+
159
+ ## Commands
160
+
161
+ ### `switchman setup`
162
+ One-command setup — creates agent workspaces and initialises the database.
163
+ - `--agents 3` — number of workspaces to create (default: 3, max: 10)
164
+ - `--prefix switchman` — branch name prefix (default: switchman)
165
+
166
+ ### `switchman init`
167
+ Initialise in the current git repo without creating worktrees. Creates `.switchman/switchman.db` and auto-detects existing worktrees.
168
+
169
+ ### `switchman task add <title>`
170
+ Add a task to the queue.
171
+ - `--priority 1-10` (default: 5)
172
+ - `--description "..."`
173
+
174
+ ### `switchman task list`
175
+ List all tasks. Filter with `--status pending|in_progress|done|failed`.
176
+
177
+ ### `switchman task next`
178
+ Get and assign the next pending task. Use `--json` for agent automation.
179
+ - `--worktree <name>` — worktree to assign to (defaults to current folder name)
180
+ - `--agent <name>` — agent identifier for logging
181
+
182
+ ### `switchman task done <taskId>`
183
+ Mark a task complete and release all file claims.
184
+
185
+ ### `switchman task fail <taskId>`
186
+ Mark a task failed and release all file claims.
187
+
188
+ ### `switchman claim <taskId> <worktree> [files...]`
189
+ Lock files before editing. Warns immediately if any file is already claimed by another agent.
190
+
191
+ ### `switchman release <taskId>`
192
+ Release all file claims for a task.
193
+
194
+ ### `switchman scan`
195
+ Check all worktrees for conflicts — both uncommitted file overlaps and branch-level merge conflicts. Run this before merging.
196
+
197
+ ### `switchman status`
198
+ Full overview: task counts, active tasks, locked files, and a quick conflict scan.
199
+
200
+ ### `switchman worktree list`
201
+ List all git worktrees with their registered agents and status.
202
+
203
+ ### `switchman worktree sync`
204
+ Re-sync git worktrees into the Switchman database (useful if you add worktrees after init).
205
+
206
+ ---
207
+
208
+ ## MCP tools (Claude Code)
209
+
210
+ | Tool | What it does |
211
+ |------|-------------|
212
+ | `switchman_task_next` | Get + assign the next pending task |
213
+ | `switchman_task_add` | Add a new task to the queue |
214
+ | `switchman_task_claim` | Claim files before editing (conflict check) |
215
+ | `switchman_task_done` | Mark task complete, release file claims |
216
+ | `switchman_task_fail` | Mark task failed, release file claims |
217
+ | `switchman_scan` | Scan all worktrees for conflicts |
218
+ | `switchman_status` | Full system overview |
219
+
220
+ ---
221
+
222
+ ## Roadmap
223
+
224
+ - [ ] Merge queue — serialize worktree→main merges with auto-retry
225
+ - [ ] Agent health monitoring — reclaim tasks from dead/stalled agents
226
+ - [ ] Cursor and Windsurf native MCP integration
227
+ - [ ] Web dashboard
228
+ - [ ] `brew install switchman`
229
+
230
+ ---
231
+
232
+ ## Feedback & contact
233
+
234
+ Building this in public — if you're running parallel agents and hit something broken or missing, I'd love to hear about it.
235
+
236
+ - **GitHub Issues** — [github.com/switchman-dev/switchman/issues](https://github.com/switchman-dev/switchman/issues)
237
+ - **Email** — [hello@switchman.dev](mailto:hello@switchman.dev)
238
+
239
+ ---
240
+
241
+ ## License
242
+
243
+ MIT — free to use, modify, and distribute.
@@ -0,0 +1,117 @@
1
+ # Switchman Example — taskapi
2
+
3
+ A real Express REST API used to test Switchman locally.
4
+
5
+ ## What's in here
6
+
7
+ ```
8
+ examples/
9
+ ├── taskapi/ — A small task management REST API
10
+ │ ├── src/
11
+ │ │ ├── server.js — Express entry point
12
+ │ │ ├── db.js — In-memory data store
13
+ │ │ ├── middleware/
14
+ │ │ │ ├── auth.js — API key auth (shared by all routes)
15
+ │ │ │ └── validate.js — Request validation
16
+ │ │ └── routes/
17
+ │ │ ├── tasks.js — CRUD endpoints for tasks
18
+ │ │ └── users.js — User listing
19
+ │ └── tests/
20
+ │ └── api.test.js — Smoke tests (run against live server)
21
+ ├── setup.sh — Creates git repo, 3 worktrees, seeds Switchman tasks
22
+ ├── walkthrough.sh — Step-by-step demo of the full agent workflow
23
+ └── teardown.sh — Resets everything so you can start fresh
24
+ ```
25
+
26
+ ## Quick start
27
+
28
+ Make sure Switchman is installed globally first:
29
+ ```bash
30
+ npm install -g . # from the switchman repo root
31
+ ```
32
+
33
+ Then from the switchman repo root:
34
+ ```bash
35
+ bash examples/setup.sh
36
+ bash examples/walkthrough.sh
37
+ ```
38
+
39
+ ## What the walkthrough shows
40
+
41
+ 1. **3 worktrees created** — simulating 3 parallel Claude Code instances each on their own branch
42
+ 2. **Agent 1 picks up a task** — `switchman task next` returns the highest-priority item
43
+ 3. **Agent 1 claims files** — declares which files it will edit before touching them
44
+ 4. **Agent 2 picks up a task** — takes the next item from the queue
45
+ 5. **Agent 2 tries to claim a conflicting file** — Switchman blocks it
46
+ 6. **Agent 2 adapts** — claims only the safe files instead
47
+ 7. **Agent 1 finishes** — marks task done, releases file claims
48
+ 8. **Final status** — queue updated, no lingering conflicts
49
+
50
+ ## The taskapi project
51
+
52
+ A minimal but real Express API with:
53
+ - API key authentication (`Authorization: Bearer dev-key-alice-123`)
54
+ - CRUD endpoints for tasks (`GET/POST/PATCH/DELETE /tasks`)
55
+ - User listing (`GET /users`, admin only)
56
+ - Input validation middleware
57
+ - In-memory data store (no database setup required)
58
+
59
+ ### Running the API
60
+
61
+ ```bash
62
+ cd examples/taskapi
63
+ npm install
64
+ npm start
65
+ ```
66
+
67
+ ```
68
+ taskapi running on http://localhost:3000
69
+ GET /health
70
+ GET /tasks (Bearer dev-key-alice-123)
71
+ GET /users (Bearer dev-key-alice-123, admin only)
72
+ ```
73
+
74
+ ### Running the API tests
75
+
76
+ With the server running in one terminal:
77
+ ```bash
78
+ cd examples/taskapi
79
+ node tests/api.test.js
80
+ ```
81
+
82
+ ### API keys
83
+
84
+ | Key | User | Role |
85
+ |-----|------|------|
86
+ | `dev-key-alice-123` | Alice | admin |
87
+ | `dev-key-bob-456` | Bob | member |
88
+ | `test-key-789` | Test | member |
89
+
90
+ ### Example requests
91
+
92
+ ```bash
93
+ # Health check
94
+ curl http://localhost:3000/health
95
+
96
+ # List tasks
97
+ curl -H "Authorization: Bearer dev-key-alice-123" http://localhost:3000/tasks
98
+
99
+ # Create a task
100
+ curl -X POST http://localhost:3000/tasks \
101
+ -H "Authorization: Bearer dev-key-alice-123" \
102
+ -H "Content-Type: application/json" \
103
+ -d '{"title": "My new task", "priority": "high"}'
104
+
105
+ # Update task status
106
+ curl -X PATCH http://localhost:3000/tasks/1 \
107
+ -H "Authorization: Bearer dev-key-alice-123" \
108
+ -H "Content-Type: application/json" \
109
+ -d '{"status": "done"}'
110
+ ```
111
+
112
+ ## Reset
113
+
114
+ ```bash
115
+ bash examples/teardown.sh
116
+ bash examples/setup.sh
117
+ ```
@@ -0,0 +1,102 @@
1
+ #!/usr/bin/env bash
2
+ # examples/setup.sh
3
+ #
4
+ # Sets up the taskapi example project for testing Switchman locally.
5
+ #
6
+ # What this does:
7
+ # 1. Inits taskapi as a git repo with an initial commit
8
+ # 2. Creates 3 git worktrees (simulating 3 parallel Claude Code instances)
9
+ # 3. Runs switchman init
10
+ # 4. Seeds 4 realistic parallel tasks
11
+ #
12
+ # Run from the switchman repo root:
13
+ # bash examples/setup.sh
14
+
15
+ set -e
16
+
17
+ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
18
+ TASKAPI_DIR="$SCRIPT_DIR/taskapi"
19
+ WORKTREES_DIR="$SCRIPT_DIR/worktrees"
20
+
21
+ echo ""
22
+ echo "━━━ Switchman Example Setup ━━━━━━━━━━━━━━━━━━"
23
+ echo ""
24
+
25
+ # ── Step 1: Init the project as a git repo ────────────────────────────────────
26
+
27
+ echo "→ Initialising taskapi as a git repo..."
28
+ cd "$TASKAPI_DIR"
29
+
30
+ if [ -d ".git" ]; then
31
+ echo " Already a git repo — skipping init"
32
+ else
33
+ git init -q
34
+ git config user.email "demo@switchman.dev"
35
+ git config user.name "Switchman Demo"
36
+ npm install --silent
37
+ git add -A
38
+ git commit -m "Initial commit: taskapi Express REST API" -q
39
+ echo " ✓ Git repo initialised"
40
+ fi
41
+
42
+ # ── Step 2: Create 3 worktrees ────────────────────────────────────────────────
43
+
44
+ echo ""
45
+ echo "→ Creating 3 git worktrees (simulating 3 parallel agents)..."
46
+ mkdir -p "$WORKTREES_DIR"
47
+
48
+ create_worktree() {
49
+ local name=$1
50
+ local branch=$2
51
+ local path="$WORKTREES_DIR/$name"
52
+ if [ -d "$path" ]; then
53
+ echo " Worktree '$name' already exists — skipping"
54
+ else
55
+ git worktree add -b "$branch" "$path" -q
56
+ echo " ✓ $name → branch: $branch"
57
+ fi
58
+ }
59
+
60
+ create_worktree "agent-rate-limiting" "feature/rate-limiting"
61
+ create_worktree "agent-validation" "feature/input-validation"
62
+ create_worktree "agent-tests" "feature/write-tests"
63
+
64
+ echo ""
65
+ git worktree list
66
+
67
+ # ── Step 3: Switchman init ────────────────────────────────────────────────────
68
+
69
+ echo ""
70
+ echo "→ Initialising Switchman in taskapi..."
71
+ switchman init
72
+
73
+ # ── Step 4: Seed tasks ────────────────────────────────────────────────────────
74
+
75
+ echo ""
76
+ echo "→ Seeding 4 parallel tasks..."
77
+
78
+ switchman task add "Add rate limiting to all API routes" \
79
+ --priority 8 \
80
+ --description "Token bucket: 100 req/min per API key. Return 429 with Retry-After header."
81
+
82
+ switchman task add "Add input validation to POST /tasks and PATCH /tasks/:id" \
83
+ --priority 7 \
84
+ --description "Validate title length, status enum, priority enum. Return 400 with descriptive errors."
85
+
86
+ switchman task add "Write tests for the auth middleware" \
87
+ --priority 6 \
88
+ --description "Test requireAuth and requireAdmin: valid key, missing key, bad key, wrong role."
89
+
90
+ switchman task add "Add pagination to GET /tasks" \
91
+ --priority 5 \
92
+ --description "Add ?page=1&limit=20. Return { tasks, count, page, totalPages }."
93
+
94
+ echo ""
95
+ echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
96
+ echo "✓ Setup complete."
97
+ echo ""
98
+ echo "Tasks ready:"
99
+ switchman task list
100
+ echo ""
101
+ echo "→ Next: bash examples/walkthrough.sh"
102
+ echo ""