{"data":{"slug":"discovered-54453cb67e4502d5b17db1e8d485a79c367c3b5d5255ef8b","title":"Multi-Instance Coordination for Claude, Kimi, and Grok Sessions","summary":"Coordinates multiple Claude, Kimi, and Grok sessions sharing a project folder by using a shared state bus under `.claude\/state\/` to prevent file-edit collisions and manage peer detection. The skill ensures atomic state writes and provides a CLI (`\/svs-peers`) for managing inter-session communication.","url":"https:\/\/skillshunter.net\/skills\/discovered-54453cb67e4502d5b17db1e8d485a79c367c3b5d5255ef8b","json_url":"https:\/\/skillshunter.net\/api\/public\/v1\/skills\/discovered-54453cb67e4502d5b17db1e8d485a79c367c3b5d5255ef8b","markdown_url":"https:\/\/skillshunter.net\/skills\/discovered-54453cb67e4502d5b17db1e8d485a79c367c3b5d5255ef8b.md","download_url":"https:\/\/skillshunter.net\/skills\/discovered-54453cb67e4502d5b17db1e8d485a79c367c3b5d5255ef8b\/download","category":{"slug":"software-development","name":"Software & Development"},"editor_note":"Ensure `compat.claude.hooks=false` is set on Grok to avoid double-firing hooks. Use `peers.ts` CLI for SVS inbox operations and never write to `.claude\/state\/inbox\/` manually. Committing `.claude\/state\/` is prohibited to avoid leaking per-host PIDs and transcript paths.","instructions":"---\nname: multi-instance-coordination\nversion: 1.4.0\ndescription: >\n  Coordination when multiple Claude \/ Kimi \/ Grok sessions share a project folder.\n  Shared bus under `.claude\/state\/`, peer detection, file-edit collision avoidance,\n  `\/svs-peers` CLI. Same cwd = one git HEAD \u2014 stay on main for ship\/deploy (memory\n  multi-instance-main-only); use worktrees for parallel feature branches. Claude Code\n  \u22652.1.224 also has native cross-session messaging (ListAgents\/SendMessage; native\n  \/peers) \u2014 use that for Claude\u2194Claude text; SVS for locks + Kimi\/Grok. Memory:\n  claude-cross-session-messaging.\n---\n\n# Multi-Instance Coordination\n\nWhen two or more agent sessions (Claude Code, Kimi, or Grok via SVS bridge) share a\nproject folder, they auto-discover each other through **one** bus \u2014 `.claude\/state\/` \u2014\nand refuse to overwrite each other's uncommitted work. There is no separate\n`.kimi\/state` or `.grok\/state`; migrate wires hooks into that shared tree.\n\n## Shared cwd = one git HEAD (critical)\n\nFile locks do **not** isolate branches. `git checkout -b` in Session A moves Session B\nonto that branch. **Default:** stay on `main`, scoped commits, push\/deploy from main\nonly (memory `multi-instance-main-only`). `scope --push` refuses non-main.\n\nNeed divergent long-lived branches? **`git worktree add`** so each session has its own cwd\/HEAD.\n\n## Claude native messaging (v2.1.224+) vs this bus\n\n| Need | Prefer |\n|------|--------|\n| Claude \u2194 Claude text mid-task | Native `ListAgents` \/ `SendMessage` (`\/list-agents` or Claude\u2019s `\/peers`) |\n| File Edit\/Write collision shield | **This skill** (PreToolUse + `file-touches.jsonl`) |\n| Notify **Kimi** or **Grok** | **`\/svs-peers`** \u2192 `peers.ts notify` |\n| List SVS heartbeats (all targets) | `peers.ts list` |\n\n**Slash name:** SVS command is **`\/svs-peers`**. Do **not** install a project command named `\/peers` \u2014 it shadows Claude\u2019s native alias.\n\nFull matrix: memory `claude-cross-session-messaging`.\n\n## Mental model\n\n```\n.claude\/state\/\n  sessions\/<id>.json        registry  (heartbeat = lastSeenAt)\n  sessions\/_archive\/        sessions ended or gone idle >30min\n  inbox\/<id>.jsonl          messages queued FOR that session (SVS notify)\n  file-touches.jsonl        append-only log of every Edit\/Write\n  file-touches\/_archive\/    rotated logs (every 1000 lines)\n```\n\nEvery hook updates `lastSeenAt` (heartbeat). Thresholds:\n\n| Age of last activity | State    | Effect                                                                 |\n| -------------------- | -------- | ---------------------------------------------------------------------- |\n| < 180s               | active   | Counts for collision detection; PreToolUse may BLOCK Edit\/Write.       |\n| 180s \u2013 30min         | idle     | Surfaced as a warning; edits NOT blocked.                              |\n| > 30min              | stale    | Auto-archived on the next sweep.                                       |\n| > 24h                | removed  | Deleted entirely.                                                      |\n\n## How a session announces itself\n\n1. `SessionStart` registers the session, extracts a `title` when `transcript_path` is present (Claude `\/resume` titles), lists peers, drains inbox.\n2. `UserPromptSubmit` heartbeats each user turn and re-drains the inbox.\n3. `PreToolUse` (edit tools) checks `file-touches.jsonl` before Edit\/Write.\n4. `PostToolUse` records the touch after a successful edit.\n5. `Stop` \/ `SessionEnd` archives the session and clears its inbox.\n\nKimi and Grok reach the same hooks through `svs-bridge.mjs` (project `.kimi-code` \/ `.grok\/hooks`). Keep user `compat.claude.hooks=false` on Grok so hooks do not double-fire.\n\n## PreToolUse decision matrix\n\n| Peer who touched the same file last | Peer's heartbeat | Touch age | Decision |\n| ----------------------------------- | ---------------- | --------- | --------------------------------------------- |\n| any                                 | active (<180s)   | < 5min    | **BLOCK** with a recovery hint                |\n| any                                 | idle (180s\u201330min)| < 5min    | APPROVE + warning in `systemMessage`          |\n| any                                 | stale or gone    | any       | APPROVE silently                              |\n| no peer touched the file            | n\/a              | n\/a       | APPROVE silently                              |\n\nOverride path: wait until the active peer's heartbeat goes idle (180s of no activity), then retry \u2014 the hook will downgrade to a warning. If the user explicitly tells you to override, ask them to run `peers.ts notify <id> \"I'm taking over <file>\"` first (or native SendMessage if the peer is Claude-only and mid-turn delivery matters).\n\n## Committing under multi-instance (`scope.ts`)\n\nEdits are shielded by PreToolUse; **commits** need `scope.ts` (see `git-workflow` \u00a70).\n\n```bash\nnpx tsx \"$CLAUDE_PROJECT_DIR\/.claude\/hooks\/scope.ts\" status\nnpx tsx \"$CLAUDE_PROJECT_DIR\/.claude\/hooks\/scope.ts\" commit \"<type>: <subject>\"\n```\n\nWith \u22652 active sessions and no `$CLAUDE_SESSION_ID`, `scope` **exits 1** \u2014 fix with\n`--session`, never `git add -A`. Prefer `commit` over `stage` (atomic path).\n\n## Verify the bridge fires (Kimi \/ Grok)\n\nHooks are **fail-open**: a broken bridge produces no block and no bus entry.\n\n```bash\nls -l .claude\/hooks\/svs-bridge.mjs\nrg -n 'svs-bridge' .grok\/hooks\/svs.json .kimi-code\/local.toml ~\/.kimi-code\/config.toml\n# must NOT contain ..\/..\/\necho '{\"session_id\":\"smoke\",\"cwd\":\"'$PWD'\",\"hook_event_name\":\"SessionStart\"}' \\\n  | node .claude\/hooks\/svs-bridge.mjs grok session-start; echo \"exit=$?\"\n```\n\nCorrect form: `node .claude\/hooks\/svs-bridge.mjs <kimi|grok> <hook>`. Grok needs\n`\/hooks-trust` and user `compat.claude.hooks=false`.\n\n## Talking to a peer (SVS bus)\n\nAlways use the `peers.ts` CLI for the SVS inbox. NEVER write to `.claude\/state\/inbox\/` by hand.\nSlash wrapper: **`\/svs-peers`**.\n\n```bash\nnpx tsx \"$CLAUDE_PROJECT_DIR\/.claude\/hooks\/peers.ts\" list\nnpx tsx \"$CLAUDE_PROJECT_DIR\/.claude\/hooks\/peers.ts\" notify a1b2c3d4 \"I just committed auth changes\"\nnpx tsx \"$CLAUDE_PROJECT_DIR\/.claude\/hooks\/peers.ts\" locks --minutes 10\nnpx tsx \"$CLAUDE_PROJECT_DIR\/.claude\/hooks\/peers.ts\" cleanup\n```\n\nClaude\u2194Claude findings: prefer native SendMessage when available (memory `claude-cross-session-messaging`).\n\n## Guardrails\n\n- The coordination layer is **single-host**. It does not replace `git pull` or PR review across machines.\n- All state writes are atomic (`.tmp` + `rename`); reads are tolerant of corruption \u2014 hooks never block Claude on a broken state file.\n- `.claude\/state\/` MUST stay gitignored. Committing it would leak per-host PIDs and transcript paths.\n- `\/svs-peers` is the SVS user-facing surface; hooks are internal plumbing. Do not invoke `_state.ts` directly.\n\n## Failure modes (and what to do)\n\n| Symptom                                                  | Cause                                | Fix                                                    |\n| -------------------------------------------------------- | ------------------------------------ | ------------------------------------------------------ |\n| `peers list` shows a phantom session                     | Crashed instance left a record       | `peers cleanup` (archives idle, removes >24h)          |\n| Edit blocked but the other instance is gone              | Stale heartbeat, peer never archived | Wait 30min, or run `peers cleanup`                     |\n| SVS inbox messages didn't arrive                         | Peer paused \/ no UserPromptSubmit    | Appear on the next prompt the peer submits             |\n| Native SendMessage didn't arrive                         | Inbound hold\/refuse \/ no socket      | Check `\/list-agents`, `crossSessionInbound`, Claude version \u22652.1.224 |\n| Project `\/peers` overrides Claude native                 | Stale `commands\/peers.md`            | `migrate --apply` removes it; use `\/svs-peers`         |\n| Stop never blocks after source edits | Bridge path wrong \/ not trusted \u2192 fail-open | Bridge smoke-test above; `migrate --apply`; Grok `\/hooks-trust` |\n| `scope` exits 1 | \u22652 sessions, no session id | `peers list` \u2192 `scope commit --session <id>` |\n| `scope --push` exits 2 on feature branch | Main-only ship gate | `git checkout main` then push; or `--allow-non-main-push` |\n| Empty `.claude\/state\/sessions\/` while a peer is working | Their bridge fail-open | Smoke-test their side; do not conclude \"no peers\" |\n| Peer deploy hit my feature branch | Shared cwd one HEAD | Memory `multi-instance-main-only`; stay on main or use worktree |\n\n## See Also\n\n- Memory: `claude-cross-session-messaging`, `multi-instance-main-only`\n- Command: `\/svs-peers`\n- `session-history` \u2014 find Claude \/ Kimi \/ Grok transcripts and resume past chats.\n- `git-workflow` \u00a70 \u2014 scoped commit; same-cwd multi-instance \u2192 prefer main; worktrees for parallel branches.\n- `svs-finalize` \u2014 documenter \u2192 domain-updater after code commits.\n- `_state.ts` \u2014 shared TypeScript helpers used by every hook.\n- `.claude\/state\/` README at `.claude\/hooks\/_state.README.md`.\n","use_cases":["Preventing file-edit collisions in shared project folders","Managing peer detection across multiple agent sessions","Using `\/svs-peers` CLI for inter-session communication","Coordinating file locks and atomic state writes"],"limitations":[],"compatibility":[],"setup_tip":null,"sample_input":null,"sample_output":null,"source_url":"https:\/\/github.com\/0x57-1\/start-vibing-stacks\/blob\/6e398b34c4dfe73ac8f809216b218856b8e02ff4\/stacks\/_shared\/skills\/multi-instance-coordination\/SKILL.md","source_commit":"6e398b34c4dfe73ac8f809216b218856b8e02ff4","license":null,"version":null,"listing_approved_at":"2026-12-03","created_at":"2026-12-02T09:11:25.000000Z","updated_at":"2026-12-03T16:20:08.000000Z","seo_title":"Multi-Instance Coordination for Claude, Kimi, and Grok","seo_description":"A skill for managing multiple Claude, Kimi, and Grok sessions in a shared project folder, preventing file-edit collisions and enabling inter-session communication through","seo_keywords":["multi-instance coordination","Claude session management","Kimi and Grok collaboration","file-edit collision avoidance","SVS inbox CLI","shared state bus","atomic state writes"]}}